blob: 1227c307e39692ebe59f4c0be648fd9949d44e1b [file] [log] [blame]
Simon Hunt6cc86452017-04-27 17:46:22 -07001/*
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 Burrowsca1a39c2017-05-08 17:31:08 -040025 var $log, $timeout, fs, gs, wss, t2kcs, t2rs, t2lc, api, LinkLabel;
Simon Hunt6cc86452017-04-27 17:46:22 -070026
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 Hunte6f64612017-04-28 00:01:48 -0700126 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 Hunt6cc86452017-04-27 17:46:22 -0700136 function showHighlights(data) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700137 $log.info('+++ TOPO 2 +++ show highlights', data);
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400138 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);
144 // TODO: There's a bug in backend where link id is in reverse
145 if (lab) {
146 t2lc.addLabel(LinkLabel, link, linkLabelsDOM, {
147 link: lab
148 });
149 }
150 });
Simon Hunt6cc86452017-04-27 17:46:22 -0700151 }
152
153 // ========================================================================
154
155 angular.module('ovTopo2')
156 .factory('Topo2OverlayService', [
157 '$log', '$timeout', 'FnService', 'GlyphService', 'WebSocketService',
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400158 'Topo2KeyCommandService', 'Topo2RegionService', 'Topo2LabelCollection',
159 'Topo2LinkLabel',
Simon Hunt6cc86452017-04-27 17:46:22 -0700160
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400161 function (_$log_, _$timeout_, _fs_, _gs_, _wss_, _t2kcs_, _t2rs_,
162 _t2lc_, _t2ll_) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700163 $log = _$log_;
164 $timeout = _$timeout_;
165 fs = _fs_;
166 gs = _gs_;
167 wss = _wss_;
Simon Hunte6f64612017-04-28 00:01:48 -0700168 t2kcs = _t2kcs_;
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400169 t2rs = _t2rs_;
170 t2lc = _t2lc_;
171 LinkLabel = _t2ll_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700172
173 return {
174 register: register,
175 setApi: setApi,
Simon Hunte6f64612017-04-28 00:01:48 -0700176 setOverlay: setOverlay,
Simon Hunt6cc86452017-04-27 17:46:22 -0700177
178 hooks: {
179 escape: escapeHook,
180 emptySelect: emptySelectHook,
181 singleSelect: singleSelectHook,
182 multiSelect: multiSelectHook,
183 mouseOver: mouseOverHook,
184 mouseOut: mouseOutHook
185 },
186 showHighlights: showHighlights
187 }
188 }
189 ]);
190
191}());