blob: c1a8400c970cc5ade8412e9229c888042ee66143 [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()) {
Simon Hunt6036b192015-02-11 11:20:26 -0800147 hideSummaryPanel();
148 } else {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800149 // ask server to start sending summary data.
Simon Hunt6036b192015-02-11 11:20:26 -0800150 api.sendEvent('requestSummary');
Simon Hunta0eb0a82015-02-11 12:30:06 -0800151 // note: the summary panel will appear, once data arrives
Simon Hunt6036b192015-02-11 11:20:26 -0800152 }
153 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800154
155 // === -----------------------------------------------------
156 // === LOGIC For showing/hiding summary and detail panels...
157
Simon Hunt626d2102015-01-29 11:54:50 -0800158 function showSummaryPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800159 if (detailPanel.isVisible()) {
160 detailPanel.down(summaryPanel.show);
161 } else {
162 summaryPanel.show();
163 }
Simon Huntc252aa62015-02-10 16:45:39 -0800164 }
165
166 function hideSummaryPanel() {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800167 // instruct server to stop sending summary data
168 api.sendEvent("cancelSummary");
169 summaryPanel.hide(detailPanel.up);
Simon Hunt4b668592015-01-29 17:33:53 -0800170 }
Simon Hunt626d2102015-01-29 11:54:50 -0800171
Simon Hunt08f841d02015-02-10 14:39:20 -0800172 function showDetailPanel() {
Simon Hunt6036b192015-02-11 11:20:26 -0800173 if (summaryPanel.isVisible()) {
174 detailPanel.down(detailPanel.show);
175 } else {
176 detailPanel.up(detailPanel.show);
177 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800178 }
179
180 function hideDetailPanel() {
181 detailPanel.hide();
182 }
183
Simon Hunt6036b192015-02-11 11:20:26 -0800184 // ==========================
Simon Hunt08f841d02015-02-10 14:39:20 -0800185
Simon Hunt6036b192015-02-11 11:20:26 -0800186 function noop () {}
187
188 function augmentDetailPanel() {
189 var dp = detailPanel;
190 dp.ypos = { up: 64, down: 320, current: 320};
191
192 dp._move = function (y, cb) {
193 var endCb = fs.isF(cb) || noop,
194 yp = dp.ypos;
195 if (yp.current !== y) {
196 yp.current = y;
197 dp.el().transition().duration(300)
198 .each('end', endCb)
199 .style('top', yp.current + 'px');
200 } else {
201 endCb();
202 }
203 };
204
205 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
206 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
207 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800208
Simon Hunt4b668592015-01-29 17:33:53 -0800209 // ==========================
210
Simon Hunt6036b192015-02-11 11:20:26 -0800211 function initPanels(_api_) {
212 api = _api_;
213
Simon Hunt4b668592015-01-29 17:33:53 -0800214 summaryPanel = ps.createPanel(idSum, panelOpts);
215 detailPanel = ps.createPanel(idDet, panelOpts);
Simon Hunt08f841d02015-02-10 14:39:20 -0800216
217 summaryPanel.classed(pCls, true);
218 detailPanel.classed(pCls, true);
Simon Hunt6036b192015-02-11 11:20:26 -0800219
220 augmentDetailPanel();
Simon Hunt4b668592015-01-29 17:33:53 -0800221 }
222
223 function destroyPanels() {
224 ps.destroyPanel(idSum);
225 ps.destroyPanel(idDet);
226 summaryPanel = detailPanel = null;
Simon Hunt626d2102015-01-29 11:54:50 -0800227 }
228
229 // ==========================
230
Simon Huntb0ec1e52015-01-28 18:13:49 -0800231 angular.module('ovTopo')
232 .factory('TopoPanelService',
Simon Hunt6036b192015-02-11 11:20:26 -0800233 ['$log', 'FnService', 'PanelService', 'GlyphService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800234
Simon Hunt6036b192015-02-11 11:20:26 -0800235 function (_$log_, _fs_, _ps_, _gs_) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800236 $log = _$log_;
Simon Hunt6036b192015-02-11 11:20:26 -0800237 fs = _fs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800238 ps = _ps_;
Simon Huntc9b73162015-01-29 14:02:15 -0800239 gs = _gs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800240
Simon Huntb0ec1e52015-01-28 18:13:49 -0800241 return {
242 initPanels: initPanels,
Simon Hunt626d2102015-01-29 11:54:50 -0800243 destroyPanels: destroyPanels,
Simon Hunt08f841d02015-02-10 14:39:20 -0800244
245 showSummary: showSummary,
Simon Hunt6036b192015-02-11 11:20:26 -0800246 toggleSummary: toggleSummary,
Simon Hunt08f841d02015-02-10 14:39:20 -0800247
248 displaySingle: displaySingle,
249 displayMulti: displayMulti,
250 addAction: addAction,
251
Simon Huntc252aa62015-02-10 16:45:39 -0800252 hideSummaryPanel: hideSummaryPanel,
Simon Hunt08f841d02015-02-10 14:39:20 -0800253 showDetailPanel: showDetailPanel,
254 hideDetailPanel: hideDetailPanel,
255
256 detailVisible: function () { return detailPanel.isVisible(); },
257 summaryVisible: function () { return summaryPanel.isVisible(); }
Simon Huntb0ec1e52015-01-28 18:13:49 -0800258 };
259 }]);
260}());