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