blob: 1f26220ddb96b81a231dd0ce9fbef3863e475a91 [file] [log] [blame]
Simon Hunt6cc86452017-04-27 17:46:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Hunt6cc86452017-04-27 17:46:22 -07003 *
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 Burrows1c2a9682017-07-14 16:52:46 +010025 var $log, fs, t2kcs, t2rs, t2lc, LinkLabel;
Simon Hunt6cc86452017-04-27 17:46:22 -070026
27 // internal state
28 var overlays = {},
Steven Burrows58bdbdd2018-02-13 12:56:43 +000029 current = null,
30 reset = true;
Simon Hunt6cc86452017-04-27 17:46:22 -070031
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 Burrows58bdbdd2018-02-13 12:56:43 +000040 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 Hunt6cc86452017-04-27 17:46:22 -070073 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 Burrows58bdbdd2018-02-13 12:56:43 +000086 handleGlyphs(overlay);
Simon Hunt6cc86452017-04-27 17:46:22 -070087
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 Burrows58bdbdd2018-02-13 12:56:43 +0000103 // 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 Hunt6cc86452017-04-27 17:46:22 -0700145 // 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 Hunte6f64612017-04-28 00:01:48 -0700197 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 Hunt6cc86452017-04-27 17:46:22 -0700207 function showHighlights(data) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700208 $log.info('+++ TOPO 2 +++ show highlights', data);
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400209 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 Burrows2b29ca42017-05-12 10:58:05 -0400214 nodeLink = t2rs.getLink(id);
215 if (nodeLink) {
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400216 t2lc.addLabel(LinkLabel, link, linkLabelsDOM, {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100217 link: nodeLink,
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400218 });
219 }
220 });
Simon Hunt6cc86452017-04-27 17:46:22 -0700221 }
222
223 // ========================================================================
224
225 angular.module('ovTopo2')
226 .factory('Topo2OverlayService', [
Steven Burrows1c2a9682017-07-14 16:52:46 +0100227 '$log', 'FnService', 'Topo2KeyCommandService',
228 'Topo2RegionService', 'Topo2LabelCollection', 'Topo2LinkLabel',
Simon Hunt6cc86452017-04-27 17:46:22 -0700229
Steven Burrows1c2a9682017-07-14 16:52:46 +0100230 function (_$log_, _fs_, _t2kcs_, _t2rs_,
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400231 _t2lc_, _t2ll_) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700232 $log = _$log_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700233 fs = _fs_;
Simon Hunte6f64612017-04-28 00:01:48 -0700234 t2kcs = _t2kcs_;
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400235 t2rs = _t2rs_;
236 t2lc = _t2lc_;
237 LinkLabel = _t2ll_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700238
239 return {
240 register: register,
Simon Hunte6f64612017-04-28 00:01:48 -0700241 setOverlay: setOverlay,
Steven Burrows58bdbdd2018-02-13 12:56:43 +0000242 augmentRbset: augmentRbset,
243 tbSelection: tbSelection,
244 mkGlyphId: mkGlyphId,
Simon Hunt6cc86452017-04-27 17:46:22 -0700245
246 hooks: {
247 escape: escapeHook,
248 emptySelect: emptySelectHook,
249 singleSelect: singleSelectHook,
250 multiSelect: multiSelectHook,
251 mouseOver: mouseOverHook,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100252 mouseOut: mouseOutHook,
Simon Hunt6cc86452017-04-27 17:46:22 -0700253 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100254 showHighlights: showHighlights,
255 };
256 },
Simon Hunt6cc86452017-04-27 17:46:22 -0700257 ]);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100258}());