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 |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [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 = {}, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +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 | |
| 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 | |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 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('-', '~'), |
Steven Burrows | 2b29ca4 | 2017-05-12 10:58:05 -0400 | [diff] [blame] | 143 | nodeLink = t2rs.getLink(id); |
| 144 | if (nodeLink) { |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 145 | t2lc.addLabel(LinkLabel, link, linkLabelsDOM, { |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 146 | link: nodeLink |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 147 | }); |
| 148 | } |
| 149 | }); |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // ======================================================================== |
| 153 | |
| 154 | angular.module('ovTopo2') |
| 155 | .factory('Topo2OverlayService', [ |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 156 | '$log', '$timeout', 'FnService', 'GlyphService', 'WebSocketService', |
| 157 | 'Topo2KeyCommandService', 'Topo2RegionService', 'Topo2LabelCollection', |
| 158 | 'Topo2LinkLabel', |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 159 | |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 160 | function (_$log_, _$timeout_, _fs_, _gs_, _wss_, _t2kcs_, _t2rs_, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 161 | _t2lc_, _t2ll_) { |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 162 | $log = _$log_; |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 163 | $timeout = _$timeout_; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 164 | fs = _fs_; |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 165 | gs = _gs_; |
| 166 | wss = _wss_; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 167 | t2kcs = _t2kcs_; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 168 | t2rs = _t2rs_; |
| 169 | t2lc = _t2lc_; |
| 170 | LinkLabel = _t2ll_; |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 171 | |
| 172 | return { |
| 173 | register: register, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 174 | setApi: setApi, |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 175 | setOverlay: setOverlay, |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 176 | |
| 177 | hooks: { |
| 178 | escape: escapeHook, |
| 179 | emptySelect: emptySelectHook, |
| 180 | singleSelect: singleSelectHook, |
| 181 | multiSelect: multiSelectHook, |
| 182 | mouseOver: mouseOverHook, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 183 | mouseOut: mouseOutHook |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 184 | }, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 185 | showHighlights: showHighlights |
| 186 | } |
| 187 | } |
Simon Hunt | 6cc8645 | 2017-04-27 17:46:22 -0700 | [diff] [blame] | 188 | ]); |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 189 | |
| 190 | }()); |