blob: 39dd26c28b8cfe07327772197234c243c116cc90 [file] [log] [blame]
Steven Burrowsf50a1772017-09-14 11:58:15 +01001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17(function () {
18
19 var ps, fs, mast, wss, is, EditableTextComponent;
20
21 var panel,
22 pStartY,
23 wSize,
24 wssHandlers = {},
25 options;
26
27 // Constants
28 var topPdg = 28,
29 defaultLabelWidth = 110,
30 defaultValueWidth = 80;
31
32 // Elements
33 var container,
34 top,
35 bottom;
36
37 function createDetailsPanel(name, _options) {
38 options = _options;
39 scope = options.scope;
40
41 panel = ps.createPanel(name, options);
42
43 calculatePositions();
44
45 panel.el().style({
46 position: 'absolute',
47 top: pStartY + 'px',
48 });
49
50 hide();
51
52 return panel;
53 }
54
55 function calculatePositions() {
56 pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height')
57 + mast.mastHeight() + topPdg;
58 wSize = fs.windowSize(pStartY);
59 pHeight = wSize.height;
60 }
61
62 function hide() {
63 panel.hide();
64 }
65
66 function setResponse(name, callback) {
67 var additionalHandler = {};
68 additionalHandler[name] = callback;
69
70 wss.bindHandlers(additionalHandler);
71 wssHandlers = _.extend({}, wssHandlers, additionalHandler);
72 }
73
74 function addContainers() {
75 container = panel.append('div').classed('container', true);
76 top = container.append('div').classed('top', true);
77 bottom = container.append('div').classed('bottom', true);
78 }
79
80 function addCloseButton(onClose) {
81 var closeBtn = top.append('div').classed('close-btn', true);
82
83 is.loadEmbeddedIcon(closeBtn, 'close', 20);
84 closeBtn.on('click', onClose || function () {});
85 }
86
Simon Huntc9e83212017-10-09 16:52:07 -070087 function addHeading(icon, makeEditable) {
Steven Burrowsf50a1772017-09-14 11:58:15 +010088 top.append('div').classed('iconDiv ' + icon, true);
Simon Huntc9e83212017-10-09 16:52:07 -070089
90 if (makeEditable) {
91 new EditableTextComponent(top.append('h2'), {
92 scope: options.scope,
93 nameChangeRequest: options.nameChangeRequest,
94 keyBindings: options.keyBindings,
95 });
96 } else {
97 top.append('h2'); // note: title is inserted later
98 }
Steven Burrowsf50a1772017-09-14 11:58:15 +010099 }
100
101 function addTable(parent, className) {
102 return parent.append('div').classed(className, true).append('table');
103 }
104
105 function addProp(tbody, key, value) {
106 console.log(tbody);
107 var tr = tbody.append('tr');
108
109 function addCell(cls, txt, width) {
110 tr.append('td').attr('class', cls).attr('width', width).text(txt);
111 }
112
113 addCell('label', key + ' :', defaultLabelWidth);
114 addCell('value', value, defaultValueWidth);
115 }
116
117 function addPropsList(el, props) {
118 var tblDiv = el.append('div').classed('top-tables', true);
119 var left = addTable(tblDiv, 'left').append('tbody');
120 var right = addTable(tblDiv, 'right').append('tbody');
121
122 var keys = _.keys(props);
123
124 _.each(props, function (value, key) {
125 var index = keys.indexOf(key);
126
127 if (index < keys.length / 2) {
128 addProp(left, key, value);
129 } else {
130 addProp(right, key, value);
131 }
132 });
133 }
134
135 function empty() {
136 panel.empty();
137 }
138
139 function select(id) {
140 return panel.el().select(id);
141 }
142
143 function destroy() {
144 wss.unbindHandlers(wssHandlers);
145 }
146
147 angular.module('onosLayer')
148 .factory('DetailsPanelService', [
149
150 'PanelService', 'FnService', 'MastService', 'WebSocketService',
151 'IconService', 'EditableTextComponent',
152
153 function (_ps_, _fs_, _mast_, _wss_, _is_, _etc_) {
154
155 ps = _ps_;
156 fs = _fs_;
157 mast = _mast_;
158 wss = _wss_;
159 is = _is_;
160 EditableTextComponent = _etc_;
161
162 return {
163 create: createDetailsPanel,
164 setResponse: setResponse,
165
166 addContainers: addContainers,
167 addCloseButton: addCloseButton,
168 addHeading: addHeading,
169 addPropsList: addPropsList,
170
171 // Elements
172 container: function () { return container; },
173 top: function () { return top; },
174 bottom: function () { return bottom; },
175 select: select,
176
177 empty: empty,
178 hide: hide,
179 destroy: destroy,
180 };
181 },
182 ]);
183})();