Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 26 | var $log, fs, ps, gs, flash, wss; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 27 | |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 28 | // constants |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 29 | var pCls = 'topo-p', |
| 30 | idSum = 'topo-p-summary', |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 31 | idDet = 'topo-p-detail', |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 32 | panelOpts = { |
| 33 | width: 260 |
| 34 | }; |
| 35 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 36 | // panels |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 37 | var summaryPanel, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 38 | detailPanel; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 39 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 40 | // internal state |
| 41 | var useDetails = true, // should we show details if we have 'em? |
| 42 | haveDetails = false; // do we have details that we could show? |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 43 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 44 | // === ----------------------------------------------------- |
| 45 | // Utility functions |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 46 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 47 | function addSep(tbody) { |
| 48 | tbody.append('tr').append('td').attr('colspan', 2).append('hr'); |
| 49 | } |
| 50 | |
| 51 | function addProp(tbody, label, value) { |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 52 | var tr = tbody.append('tr'), |
| 53 | lab = label.replace(/_/g, ' '); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 54 | |
| 55 | function addCell(cls, txt) { |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 56 | tr.append('td').attr('class', cls).html(txt); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 57 | } |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 58 | addCell('label', lab + ' :'); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 59 | addCell('value', value); |
| 60 | } |
| 61 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 62 | function listProps(tbody, data) { |
| 63 | data.propOrder.forEach(function(p) { |
| 64 | if (p === '-') { |
| 65 | addSep(tbody); |
| 66 | } else { |
| 67 | addProp(tbody, p, data.props[p]); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | function dpa(x) { |
| 73 | return detailPanel.append(x); |
| 74 | } |
| 75 | |
| 76 | function spa(x) { |
| 77 | return summaryPanel.append(x); |
| 78 | } |
| 79 | |
| 80 | // === ----------------------------------------------------- |
| 81 | // Functions for populating the summary panel |
| 82 | |
| 83 | function populateSummary(data) { |
| 84 | summaryPanel.empty(); |
| 85 | |
| 86 | var svg = spa('svg'), |
| 87 | title = spa('h2'), |
| 88 | table = spa('table'), |
| 89 | tbody = table.append('tbody'); |
| 90 | |
| 91 | gs.addGlyph(svg, 'node', 40); |
| 92 | gs.addGlyph(svg, 'bird', 24, true, [8,12]); |
| 93 | |
| 94 | title.text(data.id); |
| 95 | listProps(tbody, data); |
| 96 | } |
| 97 | |
| 98 | // === ----------------------------------------------------- |
| 99 | // Functions for populating the detail panel |
| 100 | |
| 101 | function displaySingle(data) { |
| 102 | detailPanel.empty(); |
| 103 | |
| 104 | var svg = dpa('svg'), |
| 105 | title = dpa('h2'), |
| 106 | table = dpa('table'), |
| 107 | tbody = table.append('tbody'); |
| 108 | |
| 109 | gs.addGlyph(svg, (data.type || 'unknown'), 40); |
| 110 | title.text(data.id); |
| 111 | listProps(tbody, data); |
| 112 | dpa('hr'); |
| 113 | } |
| 114 | |
| 115 | function displayMulti(ids) { |
| 116 | detailPanel.empty(); |
| 117 | |
| 118 | var title = dpa('h3'), |
| 119 | table = dpa('table'), |
| 120 | tbody = table.append('tbody'); |
| 121 | |
| 122 | title.text('Selected Nodes'); |
| 123 | ids.forEach(function (d, i) { |
| 124 | addProp(tbody, i+1, d); |
| 125 | }); |
| 126 | dpa('hr'); |
| 127 | } |
| 128 | |
| 129 | function addAction(text, cb) { |
| 130 | dpa('div') |
| 131 | .classed('actionBtn', true) |
| 132 | .text(text) |
| 133 | .on('click', cb); |
| 134 | } |
| 135 | |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 136 | var friendlyIndex = { |
| 137 | device: 1, |
| 138 | host: 0 |
| 139 | }; |
| 140 | |
| 141 | function friendly(d) { |
| 142 | var i = friendlyIndex[d.class] || 0; |
| 143 | return (d.labels && d.labels[i]) || ''; |
| 144 | } |
| 145 | |
| 146 | function linkSummary(d) { |
| 147 | var o = d && d.online ? 'online' : 'offline'; |
| 148 | return d ? d.type + ' / ' + o : '-'; |
| 149 | } |
| 150 | |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 151 | // provided to change presentation of internal type name |
| 152 | var linkTypePres = { |
| 153 | hostLink: 'edge link' |
| 154 | }; |
| 155 | |
| 156 | function linkType(d) { |
| 157 | return linkTypePres[d.type()] || d.type(); |
| 158 | } |
| 159 | |
| 160 | var coreOrder = [ |
| 161 | 'Type', '-', |
| 162 | 'A_type', 'A_id', 'A_label', 'A_port', '-', |
| 163 | 'B_type', 'B_id', 'B_label', 'B_port', '-' |
| 164 | ], |
| 165 | edgeOrder = [ |
| 166 | 'Type', '-', |
| 167 | 'A_type', 'A_id', 'A_label', '-', |
| 168 | 'B_type', 'B_id', 'B_label', 'B_port' |
| 169 | ]; |
| 170 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 171 | function displayLink(data) { |
| 172 | detailPanel.empty(); |
| 173 | |
| 174 | var svg = dpa('svg'), |
| 175 | title = dpa('h2'), |
| 176 | table = dpa('table'), |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 177 | tbody = table.append('tbody'), |
| 178 | edgeLink = data.type() === 'hostLink', |
| 179 | order = edgeLink ? edgeOrder : coreOrder; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 180 | |
| 181 | gs.addGlyph(svg, 'ports', 40); |
| 182 | title.text('Link'); |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 183 | |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 184 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 185 | listProps(tbody, { |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 186 | propOrder: order, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 187 | props: { |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 188 | Type: linkType(data), |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 189 | |
| 190 | A_type: data.source.class, |
| 191 | A_id: data.source.id, |
| 192 | A_label: friendly(data.source), |
| 193 | A_port: data.srcPort, |
| 194 | |
| 195 | B_type: data.target.class, |
| 196 | B_id: data.target.id, |
| 197 | B_label: friendly(data.target), |
| 198 | B_port: data.tgtPort |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 199 | } |
| 200 | }); |
Simon Hunt | a36f03b | 2015-04-01 15:22:49 -0700 | [diff] [blame] | 201 | |
Simon Hunt | e25c5a2 | 2015-04-02 14:37:12 -0700 | [diff] [blame] | 202 | if (!edgeLink) { |
| 203 | addProp(tbody, 'A → B', linkSummary(data.fromSource)); |
| 204 | addProp(tbody, 'B → A', linkSummary(data.fromTarget)); |
| 205 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | function displayNothing() { |
| 209 | haveDetails = false; |
| 210 | hideDetailPanel(); |
| 211 | } |
| 212 | |
| 213 | function displaySomething() { |
| 214 | haveDetails = true; |
| 215 | if (useDetails) { |
| 216 | showDetailPanel(); |
| 217 | } |
| 218 | } |
| 219 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 220 | // === ----------------------------------------------------- |
| 221 | // Event Handlers |
| 222 | |
| 223 | function showSummary(data) { |
| 224 | populateSummary(data); |
| 225 | showSummaryPanel(); |
| 226 | } |
| 227 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame^] | 228 | function toggleSummary(x) { |
| 229 | var on = (x === 'keyev') ? !summaryPanel.isVisible() : !!x; |
| 230 | |
| 231 | if (on) { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 232 | // ask server to start sending summary data. |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 233 | wss.sendEvent('requestSummary'); |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 234 | // note: the summary panel will appear, once data arrives |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame^] | 235 | } else { |
| 236 | hideSummaryPanel(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 239 | |
| 240 | // === ----------------------------------------------------- |
| 241 | // === LOGIC For showing/hiding summary and detail panels... |
| 242 | |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 243 | function showSummaryPanel() { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 244 | if (detailPanel.isVisible()) { |
| 245 | detailPanel.down(summaryPanel.show); |
| 246 | } else { |
| 247 | summaryPanel.show(); |
| 248 | } |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | function hideSummaryPanel() { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 252 | // instruct server to stop sending summary data |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 253 | wss.sendEvent("cancelSummary"); |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 254 | summaryPanel.hide(detailPanel.up); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 255 | } |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 256 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 257 | function showDetailPanel() { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 258 | if (summaryPanel.isVisible()) { |
| 259 | detailPanel.down(detailPanel.show); |
| 260 | } else { |
| 261 | detailPanel.up(detailPanel.show); |
| 262 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | function hideDetailPanel() { |
| 266 | detailPanel.hide(); |
| 267 | } |
| 268 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 269 | // ========================== |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 270 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 271 | function noop () {} |
| 272 | |
| 273 | function augmentDetailPanel() { |
| 274 | var dp = detailPanel; |
| 275 | dp.ypos = { up: 64, down: 320, current: 320}; |
| 276 | |
| 277 | dp._move = function (y, cb) { |
| 278 | var endCb = fs.isF(cb) || noop, |
| 279 | yp = dp.ypos; |
| 280 | if (yp.current !== y) { |
| 281 | yp.current = y; |
| 282 | dp.el().transition().duration(300) |
| 283 | .each('end', endCb) |
| 284 | .style('top', yp.current + 'px'); |
| 285 | } else { |
| 286 | endCb(); |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | dp.down = function (cb) { dp._move(dp.ypos.down, cb); }; |
| 291 | dp.up = function (cb) { dp._move(dp.ypos.up, cb); }; |
| 292 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 293 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame^] | 294 | function toggleDetails(x) { |
| 295 | useDetails = (x === 'keyev') ? !useDetails : !!x; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 296 | if (useDetails) { |
| 297 | flash.flash('Enable details panel'); |
| 298 | if (haveDetails) { |
| 299 | showDetailPanel(); |
| 300 | } |
| 301 | } else { |
| 302 | flash.flash('Disable details panel'); |
| 303 | hideDetailPanel(); |
| 304 | } |
| 305 | } |
| 306 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 307 | // ========================== |
| 308 | |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 309 | function initPanels() { |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 310 | summaryPanel = ps.createPanel(idSum, panelOpts); |
| 311 | detailPanel = ps.createPanel(idDet, panelOpts); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 312 | |
| 313 | summaryPanel.classed(pCls, true); |
| 314 | detailPanel.classed(pCls, true); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 315 | |
| 316 | augmentDetailPanel(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | function destroyPanels() { |
| 320 | ps.destroyPanel(idSum); |
| 321 | ps.destroyPanel(idDet); |
| 322 | summaryPanel = detailPanel = null; |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // ========================== |
| 326 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 327 | angular.module('ovTopo') |
| 328 | .factory('TopoPanelService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 329 | ['$log', 'FnService', 'PanelService', 'GlyphService', |
| 330 | 'FlashService', 'WebSocketService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 331 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 332 | function (_$log_, _fs_, _ps_, _gs_, _flash_, _wss_) { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 333 | $log = _$log_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 334 | fs = _fs_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 335 | ps = _ps_; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 336 | gs = _gs_; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 337 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 338 | wss = _wss_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 339 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 340 | return { |
| 341 | initPanels: initPanels, |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 342 | destroyPanels: destroyPanels, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 343 | |
| 344 | showSummary: showSummary, |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 345 | toggleSummary: toggleSummary, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 346 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 347 | toggleDetails: toggleDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 348 | displaySingle: displaySingle, |
| 349 | displayMulti: displayMulti, |
| 350 | addAction: addAction, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 351 | displayLink: displayLink, |
| 352 | displayNothing: displayNothing, |
| 353 | displaySomething: displaySomething, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 354 | |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 355 | hideSummaryPanel: hideSummaryPanel, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 356 | |
| 357 | detailVisible: function () { return detailPanel.isVisible(); }, |
| 358 | summaryVisible: function () { return summaryPanel.isVisible(); } |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 359 | }; |
| 360 | }]); |
| 361 | }()); |