Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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 | (function () { |
| 19 | 'use strict'; |
| 20 | |
| 21 | // constants |
| 22 | var t2os = 'Topo2OverlayService: '; |
| 23 | |
| 24 | // injected refs |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 25 | var $log, $timeout, fs, gs, wss, t2kcs, t2rs, t2lc, api, LinkLabel; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 26 | |
| 27 | // internal state |
| 28 | var overlays = {}, |
| 29 | current = null, |
| 30 | reset = true; |
| 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 | |
| 40 | function register(overlay) { |
| 41 | var r = 'register', |
| 42 | over = fs.isO(overlay), |
| 43 | kb = over ? fs.isO(overlay.keyBindings) : null, |
| 44 | id = over ? over.overlayId : ''; |
| 45 | |
| 46 | if (!id) { |
| 47 | return error(r, 'not a recognized overlay'); |
| 48 | } |
| 49 | if (overlays[id]) { |
| 50 | return warn(r, 'already registered: "' + id + '"'); |
| 51 | } |
| 52 | overlays[id] = overlay; |
| 53 | // TODO handleGlyphs(overlay) ... see topoOverlay.js |
| 54 | |
| 55 | if (kb) { |
| 56 | if (!fs.isA(kb._keyOrder)) { |
| 57 | warn(r, 'no _keyOrder array defined on keyBindings'); |
| 58 | } else { |
| 59 | kb._keyOrder.forEach(function (k) { |
| 60 | if (k !== '-' && !kb[k]) { |
| 61 | warn(r, 'no "' + k + '" property defined on keyBindings'); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $log.debug(t2os + 'registered overlay: ' + id, overlay); |
| 68 | } |
| 69 | |
| 70 | // TODO: check topoOverlay.js for more code |
| 71 | // TODO: medium term -- factor out common code |
| 72 | // TODO: longer term -- deprecate classic topology view |
| 73 | |
| 74 | // === ----------------------------------------------------- |
| 75 | // Hooks for overlays |
| 76 | |
| 77 | function _hook(x) { |
| 78 | var h = current && current.hooks; |
| 79 | return h && fs.isF(h[x]); |
| 80 | } |
| 81 | |
| 82 | function escapeHook() { |
| 83 | var eh = _hook('escape'); |
| 84 | return eh ? eh() : false; |
| 85 | } |
| 86 | |
| 87 | function emptySelectHook() { |
| 88 | var cb = _hook('empty'); |
| 89 | cb && cb(); |
| 90 | } |
| 91 | |
| 92 | function singleSelectHook(data) { |
| 93 | var cb = _hook('single'); |
| 94 | cb && cb(data); |
| 95 | } |
| 96 | |
| 97 | function multiSelectHook(selectOrder) { |
| 98 | var cb = _hook('multi'); |
| 99 | cb && cb(selectOrder); |
| 100 | } |
| 101 | |
| 102 | function mouseOverHook(what) { |
| 103 | var cb = _hook('mouseover'); |
| 104 | cb && cb(what); |
| 105 | } |
| 106 | |
| 107 | function mouseOutHook() { |
| 108 | var cb = _hook('mouseout'); |
| 109 | cb && cb(); |
| 110 | } |
| 111 | |
| 112 | // NOTE: modifyLinkData (on classic topo) should not be necessary, as |
| 113 | // we should have a way of doing that server side |
| 114 | |
| 115 | // NOTE: while classic topology view persists, it should be the one to |
| 116 | // handle "visualization of intents" from intent view |
| 117 | |
| 118 | |
| 119 | // === ----------------------------------------------------- |
| 120 | // Event Handlers (events from server) |
| 121 | |
| 122 | function setApi(_api_) { |
| 123 | api = _api_; |
| 124 | } |
| 125 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 126 | function setOverlay(ovid) { |
| 127 | var ov = overlays[ovid]; |
| 128 | if (!ov) { |
| 129 | $log.error('setOverlay: no such overlay ID: ' + ovid); |
| 130 | } else { |
| 131 | current = ov; |
| 132 | t2kcs.bindCommands(current.keyBindings); |
| 133 | } |
| 134 | } |
| 135 | |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 136 | function showHighlights(data) { |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 137 | $log.info('+++ TOPO 2 +++ show highlights', data); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 138 | t2lc.empty(); |
| 139 | var linkLabelsDOM = d3.select('.topo2-linkLabels'); |
| 140 | _.each(data.links, function (link) { |
| 141 | // TODO: Inconsistent host id's (currentRegion and LinkLabel) |
| 142 | var id = link.id.replace('/None/0', '/None').replace('-', '~'), |
| 143 | lab = t2rs.getLink(id); |
Simon Hunt | 0ee20bf | 2017-05-10 19:59:17 -0700 | [diff] [blame] | 144 | // DONE: There's a bug in backend where link id is in reverse |
| 145 | // This is fixed -- SDH |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 146 | if (lab) { |
| 147 | t2lc.addLabel(LinkLabel, link, linkLabelsDOM, { |
| 148 | link: lab |
| 149 | }); |
| 150 | } |
| 151 | }); |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // ======================================================================== |
| 155 | |
| 156 | angular.module('ovTopo2') |
| 157 | .factory('Topo2OverlayService', [ |
| 158 | '$log', '$timeout', 'FnService', 'GlyphService', 'WebSocketService', |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 159 | 'Topo2KeyCommandService', 'Topo2RegionService', 'Topo2LabelCollection', |
| 160 | 'Topo2LinkLabel', |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 161 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 162 | function (_$log_, _$timeout_, _fs_, _gs_, _wss_, _t2kcs_, _t2rs_, |
| 163 | _t2lc_, _t2ll_) { |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 164 | $log = _$log_; |
| 165 | $timeout = _$timeout_; |
| 166 | fs = _fs_; |
| 167 | gs = _gs_; |
| 168 | wss = _wss_; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 169 | t2kcs = _t2kcs_; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 170 | t2rs = _t2rs_; |
| 171 | t2lc = _t2lc_; |
| 172 | LinkLabel = _t2ll_; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 173 | |
| 174 | return { |
| 175 | register: register, |
| 176 | setApi: setApi, |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 177 | setOverlay: setOverlay, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 178 | |
| 179 | hooks: { |
| 180 | escape: escapeHook, |
| 181 | emptySelect: emptySelectHook, |
| 182 | singleSelect: singleSelectHook, |
| 183 | multiSelect: multiSelectHook, |
| 184 | mouseOver: mouseOverHook, |
| 185 | mouseOut: mouseOutHook |
| 186 | }, |
| 187 | showHighlights: showHighlights |
| 188 | } |
| 189 | } |
| 190 | ]); |
| 191 | |
| 192 | }()); |