blob: d3b50bd280b39e56ca6f7ae2ff927da4cda310b3 [file] [log] [blame]
Simon Huntb0ec1e52015-01-28 18:13:49 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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/*
18 ONOS GUI -- Topology Panel Module.
19 Defines functions for manipulating the summary, detail, and instance panels.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Hunt6036b192015-02-11 11:20:26 -080026 var $log, fs, ps, gs;
27
28 var api;
29 /*
30 sendEvent( event, {payload} )
31 */
Simon Huntb0ec1e52015-01-28 18:13:49 -080032
Simon Hunt626d2102015-01-29 11:54:50 -080033 // constants
Simon Hunt08f841d02015-02-10 14:39:20 -080034 var pCls = 'topo-p',
35 idSum = 'topo-p-summary',
Simon Hunt626d2102015-01-29 11:54:50 -080036 idDet = 'topo-p-detail',
Simon Hunt626d2102015-01-29 11:54:50 -080037 panelOpts = {
38 width: 260
39 };
40
Simon Hunt4b668592015-01-29 17:33:53 -080041 // panels
Simon Huntb0ec1e52015-01-28 18:13:49 -080042 var summaryPanel,
Simon Hunt4b668592015-01-29 17:33:53 -080043 detailPanel;
Simon Huntb0ec1e52015-01-28 18:13:49 -080044
Simon Huntb0ec1e52015-01-28 18:13:49 -080045
Simon Hunt08f841d02015-02-10 14:39:20 -080046 // === -----------------------------------------------------
47 // Utility functions
Simon Hunt626d2102015-01-29 11:54:50 -080048
Simon Hunt4b668592015-01-29 17:33:53 -080049 function addSep(tbody) {
50 tbody.append('tr').append('td').attr('colspan', 2).append('hr');
51 }
52
53 function addProp(tbody, label, value) {
54 var tr = tbody.append('tr');
55
56 function addCell(cls, txt) {
57 tr.append('td').attr('class', cls).text(txt);
58 }
59 addCell('label', label + ' :');
60 addCell('value', value);
61 }
62
Simon Hunt08f841d02015-02-10 14:39:20 -080063 function listProps(tbody, data) {
64 data.propOrder.forEach(function(p) {
65 if (p === '-') {
66 addSep(tbody);
67 } else {
68 addProp(tbody, p, data.props[p]);
69 }
70 });
71 }
72
73 function dpa(x) {
74 return detailPanel.append(x);
75 }
76
77 function spa(x) {
78 return summaryPanel.append(x);
79 }
80
81 // === -----------------------------------------------------
82 // Functions for populating the summary panel
83
84 function populateSummary(data) {
85 summaryPanel.empty();
86
87 var svg = spa('svg'),
88 title = spa('h2'),
89 table = spa('table'),
90 tbody = table.append('tbody');
91
92 gs.addGlyph(svg, 'node', 40);
93 gs.addGlyph(svg, 'bird', 24, true, [8,12]);
94
95 title.text(data.id);
96 listProps(tbody, data);
97 }
98
99 // === -----------------------------------------------------
100 // Functions for populating the detail panel
101
102 function displaySingle(data) {
103 detailPanel.empty();
104
105 var svg = dpa('svg'),
106 title = dpa('h2'),
107 table = dpa('table'),
108 tbody = table.append('tbody');
109
110 gs.addGlyph(svg, (data.type || 'unknown'), 40);
111 title.text(data.id);
112 listProps(tbody, data);
113 dpa('hr');
114 }
115
116 function displayMulti(ids) {
117 detailPanel.empty();
118
119 var title = dpa('h3'),
120 table = dpa('table'),
121 tbody = table.append('tbody');
122
123 title.text('Selected Nodes');
124 ids.forEach(function (d, i) {
125 addProp(tbody, i+1, d);
126 });
127 dpa('hr');
128 }
129
130 function addAction(text, cb) {
131 dpa('div')
132 .classed('actionBtn', true)
133 .text(text)
134 .on('click', cb);
135 }
136
137 // === -----------------------------------------------------
138 // Event Handlers
139
140 function showSummary(data) {
141 populateSummary(data);
142 showSummaryPanel();
143 }
144
Simon Hunt6036b192015-02-11 11:20:26 -0800145 function toggleSummary() {
146 if (summaryPanel.isVisible()) {
147 api.sendEvent("cancelSummary");
148 hideSummaryPanel();
149 } else {
150 api.sendEvent('requestSummary');
151 }
152 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800153
154 // === -----------------------------------------------------
155 // === LOGIC For showing/hiding summary and detail panels...
156
Simon Hunt626d2102015-01-29 11:54:50 -0800157 function showSummaryPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800158 if (detailPanel.isVisible()) {
159 detailPanel.down(summaryPanel.show);
160 } else {
161 summaryPanel.show();
162 }
Simon Huntc252aa62015-02-10 16:45:39 -0800163 }
164
165 function hideSummaryPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800166 summaryPanel.hide(function () {
167 if (detailPanel.isVisible()) {
168 detailPanel.up();
169 }
170 });
Simon Hunt4b668592015-01-29 17:33:53 -0800171 }
Simon Hunt626d2102015-01-29 11:54:50 -0800172
Simon Hunt08f841d02015-02-10 14:39:20 -0800173 function showDetailPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800174 if (summaryPanel.isVisible()) {
175 detailPanel.down(detailPanel.show);
176 } else {
177 detailPanel.up(detailPanel.show);
178 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800179 }
180
181 function hideDetailPanel() {
182 detailPanel.hide();
183 }
184
Simon Hunt6036b192015-02-11 11:20:26 -0800185 // ==========================
Simon Hunt08f841d02015-02-10 14:39:20 -0800186
Simon Hunt6036b192015-02-11 11:20:26 -0800187 function noop () {}
188
189 function augmentDetailPanel() {
190 var dp = detailPanel;
191 dp.ypos = { up: 64, down: 320, current: 320};
192
193 dp._move = function (y, cb) {
194 var endCb = fs.isF(cb) || noop,
195 yp = dp.ypos;
196 if (yp.current !== y) {
197 yp.current = y;
198 dp.el().transition().duration(300)
199 .each('end', endCb)
200 .style('top', yp.current + 'px');
201 } else {
202 endCb();
203 }
204 };
205
206 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
207 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
208 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800209
Simon Hunt4b668592015-01-29 17:33:53 -0800210 // ==========================
211
Simon Hunt6036b192015-02-11 11:20:26 -0800212 function initPanels(_api_) {
213 api = _api_;
214
Simon Hunt4b668592015-01-29 17:33:53 -0800215 summaryPanel = ps.createPanel(idSum, panelOpts);
216 detailPanel = ps.createPanel(idDet, panelOpts);
Simon Hunt08f841d02015-02-10 14:39:20 -0800217
218 summaryPanel.classed(pCls, true);
219 detailPanel.classed(pCls, true);
Simon Hunt6036b192015-02-11 11:20:26 -0800220
221 augmentDetailPanel();
Simon Hunt4b668592015-01-29 17:33:53 -0800222 }
223
224 function destroyPanels() {
225 ps.destroyPanel(idSum);
226 ps.destroyPanel(idDet);
227 summaryPanel = detailPanel = null;
Simon Hunt626d2102015-01-29 11:54:50 -0800228 }
229
230 // ==========================
231
Simon Huntb0ec1e52015-01-28 18:13:49 -0800232 angular.module('ovTopo')
233 .factory('TopoPanelService',
Simon Hunt6036b192015-02-11 11:20:26 -0800234 ['$log', 'FnService', 'PanelService', 'GlyphService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800235
Simon Hunt6036b192015-02-11 11:20:26 -0800236 function (_$log_, _fs_, _ps_, _gs_) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800237 $log = _$log_;
Simon Hunt6036b192015-02-11 11:20:26 -0800238 fs = _fs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800239 ps = _ps_;
Simon Huntc9b73162015-01-29 14:02:15 -0800240 gs = _gs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800241
Simon Huntb0ec1e52015-01-28 18:13:49 -0800242 return {
243 initPanels: initPanels,
Simon Hunt626d2102015-01-29 11:54:50 -0800244 destroyPanels: destroyPanels,
Simon Hunt08f841d02015-02-10 14:39:20 -0800245
246 showSummary: showSummary,
Simon Hunt6036b192015-02-11 11:20:26 -0800247 toggleSummary: toggleSummary,
Simon Hunt08f841d02015-02-10 14:39:20 -0800248
249 displaySingle: displaySingle,
250 displayMulti: displayMulti,
251 addAction: addAction,
252
Simon Huntc252aa62015-02-10 16:45:39 -0800253 hideSummaryPanel: hideSummaryPanel,
Simon Hunt08f841d02015-02-10 14:39:20 -0800254 showDetailPanel: showDetailPanel,
255 hideDetailPanel: hideDetailPanel,
256
257 detailVisible: function () { return detailPanel.isVisible(); },
258 summaryVisible: function () { return summaryPanel.isVisible(); }
Simon Huntb0ec1e52015-01-28 18:13:49 -0800259 };
260 }]);
261}());