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 | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 26 | var $log, fs, ps, gs; |
| 27 | |
| 28 | var api; |
| 29 | /* |
| 30 | sendEvent( event, {payload} ) |
| 31 | */ |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 32 | |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 33 | // constants |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 34 | var pCls = 'topo-p', |
| 35 | idSum = 'topo-p-summary', |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 36 | idDet = 'topo-p-detail', |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 37 | panelOpts = { |
| 38 | width: 260 |
| 39 | }; |
| 40 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 41 | // panels |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 42 | var summaryPanel, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 43 | detailPanel; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 44 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 45 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 46 | // === ----------------------------------------------------- |
| 47 | // Utility functions |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 49 | 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 Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 63 | 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 Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 145 | function toggleSummary() { |
| 146 | if (summaryPanel.isVisible()) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 147 | hideSummaryPanel(); |
| 148 | } else { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 149 | // ask server to start sending summary data. |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 150 | api.sendEvent('requestSummary'); |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 151 | // note: the summary panel will appear, once data arrives |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 154 | |
| 155 | // === ----------------------------------------------------- |
| 156 | // === LOGIC For showing/hiding summary and detail panels... |
| 157 | |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 158 | function showSummaryPanel() { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 159 | if (detailPanel.isVisible()) { |
| 160 | detailPanel.down(summaryPanel.show); |
| 161 | } else { |
| 162 | summaryPanel.show(); |
| 163 | } |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | function hideSummaryPanel() { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 167 | // instruct server to stop sending summary data |
| 168 | api.sendEvent("cancelSummary"); |
| 169 | summaryPanel.hide(detailPanel.up); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 170 | } |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 171 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 172 | function showDetailPanel() { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 173 | if (summaryPanel.isVisible()) { |
| 174 | detailPanel.down(detailPanel.show); |
| 175 | } else { |
| 176 | detailPanel.up(detailPanel.show); |
| 177 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | function hideDetailPanel() { |
| 181 | detailPanel.hide(); |
| 182 | } |
| 183 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 184 | // ========================== |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 185 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 186 | 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 Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 208 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 209 | // ========================== |
| 210 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 211 | function initPanels(_api_) { |
| 212 | api = _api_; |
| 213 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 214 | summaryPanel = ps.createPanel(idSum, panelOpts); |
| 215 | detailPanel = ps.createPanel(idDet, panelOpts); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 216 | |
| 217 | summaryPanel.classed(pCls, true); |
| 218 | detailPanel.classed(pCls, true); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 219 | |
| 220 | augmentDetailPanel(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | function destroyPanels() { |
| 224 | ps.destroyPanel(idSum); |
| 225 | ps.destroyPanel(idDet); |
| 226 | summaryPanel = detailPanel = null; |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // ========================== |
| 230 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 231 | angular.module('ovTopo') |
| 232 | .factory('TopoPanelService', |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 233 | ['$log', 'FnService', 'PanelService', 'GlyphService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 234 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 235 | function (_$log_, _fs_, _ps_, _gs_) { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 236 | $log = _$log_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 237 | fs = _fs_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 238 | ps = _ps_; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 239 | gs = _gs_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 240 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 241 | return { |
| 242 | initPanels: initPanels, |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 243 | destroyPanels: destroyPanels, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 244 | |
| 245 | showSummary: showSummary, |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 246 | toggleSummary: toggleSummary, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 247 | |
| 248 | displaySingle: displaySingle, |
| 249 | displayMulti: displayMulti, |
| 250 | addAction: addAction, |
| 251 | |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 252 | hideSummaryPanel: hideSummaryPanel, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 253 | showDetailPanel: showDetailPanel, |
| 254 | hideDetailPanel: hideDetailPanel, |
| 255 | |
| 256 | detailVisible: function () { return detailPanel.isVisible(); }, |
| 257 | summaryVisible: function () { return summaryPanel.isVisible(); } |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 258 | }; |
| 259 | }]); |
| 260 | }()); |