blob: 485bfc8cf585a271376e9a7570c643dceab83778 [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 Hunt237676b52015-03-10 19:04:26 -070026 var $log, fs, ps, gs, wss;
Simon Huntb0ec1e52015-01-28 18:13:49 -080027
Simon Hunt626d2102015-01-29 11:54:50 -080028 // constants
Simon Hunt08f841d02015-02-10 14:39:20 -080029 var pCls = 'topo-p',
30 idSum = 'topo-p-summary',
Simon Hunt626d2102015-01-29 11:54:50 -080031 idDet = 'topo-p-detail',
Simon Hunt626d2102015-01-29 11:54:50 -080032 panelOpts = {
33 width: 260
34 };
35
Simon Hunt4b668592015-01-29 17:33:53 -080036 // panels
Simon Huntb0ec1e52015-01-28 18:13:49 -080037 var summaryPanel,
Simon Hunt4b668592015-01-29 17:33:53 -080038 detailPanel;
Simon Huntb0ec1e52015-01-28 18:13:49 -080039
Simon Huntb0ec1e52015-01-28 18:13:49 -080040
Simon Hunt08f841d02015-02-10 14:39:20 -080041 // === -----------------------------------------------------
42 // Utility functions
Simon Hunt626d2102015-01-29 11:54:50 -080043
Simon Hunt4b668592015-01-29 17:33:53 -080044 function addSep(tbody) {
45 tbody.append('tr').append('td').attr('colspan', 2).append('hr');
46 }
47
48 function addProp(tbody, label, value) {
49 var tr = tbody.append('tr');
50
51 function addCell(cls, txt) {
52 tr.append('td').attr('class', cls).text(txt);
53 }
54 addCell('label', label + ' :');
55 addCell('value', value);
56 }
57
Simon Hunt08f841d02015-02-10 14:39:20 -080058 function listProps(tbody, data) {
59 data.propOrder.forEach(function(p) {
60 if (p === '-') {
61 addSep(tbody);
62 } else {
63 addProp(tbody, p, data.props[p]);
64 }
65 });
66 }
67
68 function dpa(x) {
69 return detailPanel.append(x);
70 }
71
72 function spa(x) {
73 return summaryPanel.append(x);
74 }
75
76 // === -----------------------------------------------------
77 // Functions for populating the summary panel
78
79 function populateSummary(data) {
80 summaryPanel.empty();
81
82 var svg = spa('svg'),
83 title = spa('h2'),
84 table = spa('table'),
85 tbody = table.append('tbody');
86
87 gs.addGlyph(svg, 'node', 40);
88 gs.addGlyph(svg, 'bird', 24, true, [8,12]);
89
90 title.text(data.id);
91 listProps(tbody, data);
92 }
93
94 // === -----------------------------------------------------
95 // Functions for populating the detail panel
96
97 function displaySingle(data) {
98 detailPanel.empty();
99
100 var svg = dpa('svg'),
101 title = dpa('h2'),
102 table = dpa('table'),
103 tbody = table.append('tbody');
104
105 gs.addGlyph(svg, (data.type || 'unknown'), 40);
106 title.text(data.id);
107 listProps(tbody, data);
108 dpa('hr');
109 }
110
111 function displayMulti(ids) {
112 detailPanel.empty();
113
114 var title = dpa('h3'),
115 table = dpa('table'),
116 tbody = table.append('tbody');
117
118 title.text('Selected Nodes');
119 ids.forEach(function (d, i) {
120 addProp(tbody, i+1, d);
121 });
122 dpa('hr');
123 }
124
125 function addAction(text, cb) {
126 dpa('div')
127 .classed('actionBtn', true)
128 .text(text)
129 .on('click', cb);
130 }
131
132 // === -----------------------------------------------------
133 // Event Handlers
134
135 function showSummary(data) {
136 populateSummary(data);
137 showSummaryPanel();
138 }
139
Simon Hunt6036b192015-02-11 11:20:26 -0800140 function toggleSummary() {
141 if (summaryPanel.isVisible()) {
Simon Hunt6036b192015-02-11 11:20:26 -0800142 hideSummaryPanel();
143 } else {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800144 // ask server to start sending summary data.
Simon Hunt237676b52015-03-10 19:04:26 -0700145 wss.sendEvent('requestSummary');
Simon Hunta0eb0a82015-02-11 12:30:06 -0800146 // note: the summary panel will appear, once data arrives
Simon Hunt6036b192015-02-11 11:20:26 -0800147 }
148 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800149
150 // === -----------------------------------------------------
151 // === LOGIC For showing/hiding summary and detail panels...
152
Simon Hunt626d2102015-01-29 11:54:50 -0800153 function showSummaryPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800154 if (detailPanel.isVisible()) {
155 detailPanel.down(summaryPanel.show);
156 } else {
157 summaryPanel.show();
158 }
Simon Huntc252aa62015-02-10 16:45:39 -0800159 }
160
161 function hideSummaryPanel() {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800162 // instruct server to stop sending summary data
Simon Hunt237676b52015-03-10 19:04:26 -0700163 wss.sendEvent("cancelSummary");
Simon Hunta0eb0a82015-02-11 12:30:06 -0800164 summaryPanel.hide(detailPanel.up);
Simon Hunt4b668592015-01-29 17:33:53 -0800165 }
Simon Hunt626d2102015-01-29 11:54:50 -0800166
Simon Hunt08f841d02015-02-10 14:39:20 -0800167 function showDetailPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800168 if (summaryPanel.isVisible()) {
169 detailPanel.down(detailPanel.show);
170 } else {
171 detailPanel.up(detailPanel.show);
172 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800173 }
174
175 function hideDetailPanel() {
176 detailPanel.hide();
177 }
178
Simon Hunt6036b192015-02-11 11:20:26 -0800179 // ==========================
Simon Hunt08f841d02015-02-10 14:39:20 -0800180
Simon Hunt6036b192015-02-11 11:20:26 -0800181 function noop () {}
182
183 function augmentDetailPanel() {
184 var dp = detailPanel;
185 dp.ypos = { up: 64, down: 320, current: 320};
186
187 dp._move = function (y, cb) {
188 var endCb = fs.isF(cb) || noop,
189 yp = dp.ypos;
190 if (yp.current !== y) {
191 yp.current = y;
192 dp.el().transition().duration(300)
193 .each('end', endCb)
194 .style('top', yp.current + 'px');
195 } else {
196 endCb();
197 }
198 };
199
200 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
201 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
202 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800203
Simon Hunt4b668592015-01-29 17:33:53 -0800204 // ==========================
205
Simon Hunt237676b52015-03-10 19:04:26 -0700206 function initPanels() {
Simon Hunt4b668592015-01-29 17:33:53 -0800207 summaryPanel = ps.createPanel(idSum, panelOpts);
208 detailPanel = ps.createPanel(idDet, panelOpts);
Simon Hunt08f841d02015-02-10 14:39:20 -0800209
210 summaryPanel.classed(pCls, true);
211 detailPanel.classed(pCls, true);
Simon Hunt6036b192015-02-11 11:20:26 -0800212
213 augmentDetailPanel();
Simon Hunt4b668592015-01-29 17:33:53 -0800214 }
215
216 function destroyPanels() {
217 ps.destroyPanel(idSum);
218 ps.destroyPanel(idDet);
219 summaryPanel = detailPanel = null;
Simon Hunt626d2102015-01-29 11:54:50 -0800220 }
221
222 // ==========================
223
Simon Huntb0ec1e52015-01-28 18:13:49 -0800224 angular.module('ovTopo')
225 .factory('TopoPanelService',
Simon Hunt237676b52015-03-10 19:04:26 -0700226 ['$log', 'FnService', 'PanelService', 'GlyphService', 'WebSocketService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800227
Simon Hunt237676b52015-03-10 19:04:26 -0700228 function (_$log_, _fs_, _ps_, _gs_, _wss_) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800229 $log = _$log_;
Simon Hunt6036b192015-02-11 11:20:26 -0800230 fs = _fs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800231 ps = _ps_;
Simon Huntc9b73162015-01-29 14:02:15 -0800232 gs = _gs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700233 wss = _wss_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800234
Simon Huntb0ec1e52015-01-28 18:13:49 -0800235 return {
236 initPanels: initPanels,
Simon Hunt626d2102015-01-29 11:54:50 -0800237 destroyPanels: destroyPanels,
Simon Hunt08f841d02015-02-10 14:39:20 -0800238
239 showSummary: showSummary,
Simon Hunt6036b192015-02-11 11:20:26 -0800240 toggleSummary: toggleSummary,
Simon Hunt08f841d02015-02-10 14:39:20 -0800241
242 displaySingle: displaySingle,
243 displayMulti: displayMulti,
244 addAction: addAction,
245
Simon Huntc252aa62015-02-10 16:45:39 -0800246 hideSummaryPanel: hideSummaryPanel,
Simon Hunt08f841d02015-02-10 14:39:20 -0800247 showDetailPanel: showDetailPanel,
248 hideDetailPanel: hideDetailPanel,
249
250 detailVisible: function () { return detailPanel.isVisible(); },
251 summaryVisible: function () { return summaryPanel.isVisible(); }
Simon Huntb0ec1e52015-01-28 18:13:49 -0800252 };
253 }]);
254}());