Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 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 | (function () { |
| 19 | 'use strict'; |
| 20 | |
| 21 | // constants |
| 22 | var t2os = 'Topo2OverlayService: '; |
| 23 | |
| 24 | // injected refs |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 25 | var $log, fs, t2kcs, t2rs, t2lc, LinkLabel; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 26 | |
| 27 | // internal state |
| 28 | var overlays = {}, |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 29 | current = null, |
| 30 | reset = true; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 31 | |
| 32 | function error(fn, msg) { |
| 33 | $log.error(t2os + fn + '(): ' + msg); |
| 34 | } |
| 35 | |
| 36 | function warn(fn, msg) { |
| 37 | $log.warn(t2os + fn + '(): ' + msg); |
| 38 | } |
| 39 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 40 | function mkGlyphId(oid, gid) { |
| 41 | return (gid[0] === '*') ? oid + '-' + gid.slice(1) : gid; |
| 42 | } |
| 43 | |
| 44 | function handleGlyphs(o) { |
| 45 | var gdata = fs.isO(o.glyphs), |
| 46 | oid = o.overlayId, |
| 47 | gid = o.glyphId || 'unknown', |
| 48 | data = {}, |
| 49 | note = []; |
| 50 | |
| 51 | o._glyphId = mkGlyphId(oid, gid); |
| 52 | |
| 53 | o.mkGid = function (g) { |
| 54 | return mkGlyphId(oid, g); |
| 55 | }; |
| 56 | o.mkId = function (s) { |
| 57 | return oid + '-' + s; |
| 58 | }; |
| 59 | |
| 60 | // process glyphs if defined |
| 61 | if (gdata) { |
| 62 | angular.forEach(gdata, function (value, key) { |
| 63 | var fullkey = oid + '-' + key; |
| 64 | data['_' + fullkey] = value.vb; |
| 65 | data[fullkey] = value.d; |
| 66 | note.push('*' + key); |
| 67 | }); |
| 68 | gs.registerGlyphs(data); |
| 69 | $log.debug('registered overlay glyphs:', oid, note); |
| 70 | } |
| 71 | } |
| 72 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 73 | function register(overlay) { |
| 74 | var r = 'register', |
| 75 | over = fs.isO(overlay), |
| 76 | kb = over ? fs.isO(overlay.keyBindings) : null, |
| 77 | id = over ? over.overlayId : ''; |
| 78 | |
| 79 | if (!id) { |
| 80 | return error(r, 'not a recognized overlay'); |
| 81 | } |
| 82 | if (overlays[id]) { |
| 83 | return warn(r, 'already registered: "' + id + '"'); |
| 84 | } |
| 85 | overlays[id] = overlay; |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 86 | handleGlyphs(overlay); |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 87 | |
| 88 | if (kb) { |
| 89 | if (!fs.isA(kb._keyOrder)) { |
| 90 | warn(r, 'no _keyOrder array defined on keyBindings'); |
| 91 | } else { |
| 92 | kb._keyOrder.forEach(function (k) { |
| 93 | if (k !== '-' && !kb[k]) { |
| 94 | warn(r, 'no "' + k + '" property defined on keyBindings'); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $log.debug(t2os + 'registered overlay: ' + id, overlay); |
| 101 | } |
| 102 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 103 | // add a radio button for each registered overlay |
| 104 | // return an overlay id to index map |
| 105 | function augmentRbset(rset, switchFn) { |
| 106 | var map = {}, |
| 107 | idx = 1; |
| 108 | |
| 109 | angular.forEach(overlays, function (ov) { |
| 110 | rset.push({ |
| 111 | gid: ov._glyphId, |
| 112 | tooltip: (ov.tooltip || ''), |
| 113 | cb: function () { |
| 114 | tbSelection(ov.overlayId, switchFn); |
| 115 | }, |
| 116 | }); |
| 117 | map[ov.overlayId] = idx++; |
| 118 | }); |
| 119 | return map; |
| 120 | } |
| 121 | |
| 122 | // an overlay was selected via toolbar radio button press from user |
| 123 | function tbSelection(id, switchFn) { |
| 124 | var same = current && current.overlayId === id, |
| 125 | payload = {}, |
| 126 | actions; |
| 127 | |
| 128 | function doop(op) { |
| 129 | var oid = current.overlayId; |
| 130 | $log.debug('Overlay:', op, oid); |
| 131 | current[op](); |
| 132 | payload[op] = oid; |
| 133 | } |
| 134 | |
| 135 | if (reset || !same) { |
| 136 | current && doop('deactivate'); |
| 137 | current = overlays[id]; |
| 138 | current && doop('activate'); |
| 139 | actions = current && fs.isO(current.keyBindings); |
| 140 | switchFn(id, actions); |
| 141 | // TODO: Update Summary Panel |
| 142 | } |
| 143 | } |
| 144 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 145 | // TODO: check topoOverlay.js for more code |
| 146 | // TODO: medium term -- factor out common code |
| 147 | // TODO: longer term -- deprecate classic topology view |
| 148 | |
| 149 | // === ----------------------------------------------------- |
| 150 | // Hooks for overlays |
| 151 | |
| 152 | function _hook(x) { |
| 153 | var h = current && current.hooks; |
| 154 | return h && fs.isF(h[x]); |
| 155 | } |
| 156 | |
| 157 | function escapeHook() { |
| 158 | var eh = _hook('escape'); |
| 159 | return eh ? eh() : false; |
| 160 | } |
| 161 | |
| 162 | function emptySelectHook() { |
| 163 | var cb = _hook('empty'); |
| 164 | cb && cb(); |
| 165 | } |
| 166 | |
| 167 | function singleSelectHook(data) { |
| 168 | var cb = _hook('single'); |
| 169 | cb && cb(data); |
| 170 | } |
| 171 | |
| 172 | function multiSelectHook(selectOrder) { |
| 173 | var cb = _hook('multi'); |
| 174 | cb && cb(selectOrder); |
| 175 | } |
| 176 | |
| 177 | function mouseOverHook(what) { |
| 178 | var cb = _hook('mouseover'); |
| 179 | cb && cb(what); |
| 180 | } |
| 181 | |
| 182 | function mouseOutHook() { |
| 183 | var cb = _hook('mouseout'); |
| 184 | cb && cb(); |
| 185 | } |
| 186 | |
| 187 | // NOTE: modifyLinkData (on classic topo) should not be necessary, as |
| 188 | // we should have a way of doing that server side |
| 189 | |
| 190 | // NOTE: while classic topology view persists, it should be the one to |
| 191 | // handle "visualization of intents" from intent view |
| 192 | |
| 193 | |
| 194 | // === ----------------------------------------------------- |
| 195 | // Event Handlers (events from server) |
| 196 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 197 | function setOverlay(ovid) { |
| 198 | var ov = overlays[ovid]; |
| 199 | if (!ov) { |
| 200 | $log.error('setOverlay: no such overlay ID: ' + ovid); |
| 201 | } else { |
| 202 | current = ov; |
| 203 | t2kcs.bindCommands(current.keyBindings); |
| 204 | } |
| 205 | } |
| 206 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 207 | function showHighlights(data) { |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 208 | $log.info('+++ TOPO 2 +++ show highlights', data); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 209 | t2lc.empty(); |
| 210 | var linkLabelsDOM = d3.select('.topo2-linkLabels'); |
| 211 | _.each(data.links, function (link) { |
| 212 | // TODO: Inconsistent host id's (currentRegion and LinkLabel) |
| 213 | var id = link.id.replace('/None/0', '/None').replace('-', '~'), |
Steven Burrows | 2b29ca4 | 2017-05-12 10:58:05 -0400 | [diff] [blame] | 214 | nodeLink = t2rs.getLink(id); |
| 215 | if (nodeLink) { |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 216 | t2lc.addLabel(LinkLabel, link, linkLabelsDOM, { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 217 | link: nodeLink, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 218 | }); |
| 219 | } |
| 220 | }); |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // ======================================================================== |
| 224 | |
| 225 | angular.module('ovTopo2') |
| 226 | .factory('Topo2OverlayService', [ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 227 | '$log', 'FnService', 'Topo2KeyCommandService', |
| 228 | 'Topo2RegionService', 'Topo2LabelCollection', 'Topo2LinkLabel', |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 229 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 230 | function (_$log_, _fs_, _t2kcs_, _t2rs_, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 231 | _t2lc_, _t2ll_) { |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 232 | $log = _$log_; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 233 | fs = _fs_; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 234 | t2kcs = _t2kcs_; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 235 | t2rs = _t2rs_; |
| 236 | t2lc = _t2lc_; |
| 237 | LinkLabel = _t2ll_; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 238 | |
| 239 | return { |
| 240 | register: register, |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 241 | setOverlay: setOverlay, |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 242 | augmentRbset: augmentRbset, |
| 243 | tbSelection: tbSelection, |
| 244 | mkGlyphId: mkGlyphId, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 245 | |
| 246 | hooks: { |
| 247 | escape: escapeHook, |
| 248 | emptySelect: emptySelectHook, |
| 249 | singleSelect: singleSelectHook, |
| 250 | multiSelect: multiSelectHook, |
| 251 | mouseOver: mouseOverHook, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 252 | mouseOut: mouseOutHook, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 253 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 254 | showHighlights: showHighlights, |
| 255 | }; |
| 256 | }, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 257 | ]); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 258 | }()); |