blob: 808ee69a3b86bb9152221dae8b28c39f960536b8 [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
Simon Hunte6f64612017-04-28 00:01:48 -070025 var $log, $timeout, fs, gs, wss, t2kcs, api;
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) {
137 function doHighlight() {
138 _showHighlights(data);
139 }
140
141 // note: this allows the server-side event to add a manual delay
142 // before invoking the highlight... this was (originally) to
143 // allow for the re-creation of the DOM model, before trying
144 // to reference elements. For Topo2, there may be a better
145 // solution, making this piece of code redundant. Steven??
146
147 if (data.delay) {
148 $timeout(doHighlight, data.delay);
149 } else {
150 doHighlight();
151 }
152
153 }
154
155 function _showHighlights(data) {
156 // TODO: implement the highlighting .. see topoOverlay.js for example
157 $log.info('+++ TOPO 2 +++ show highlights', data);
158 }
159
160 // ========================================================================
161
162 angular.module('ovTopo2')
163 .factory('Topo2OverlayService', [
164 '$log', '$timeout', 'FnService', 'GlyphService', 'WebSocketService',
Simon Hunte6f64612017-04-28 00:01:48 -0700165 'Topo2KeyCommandService',
Simon Hunt6cc86452017-04-27 17:46:22 -0700166
Simon Hunte6f64612017-04-28 00:01:48 -0700167 function (_$log_, _$timeout_, _fs_, _gs_, _wss_, _t2kcs_) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700168 $log = _$log_;
169 $timeout = _$timeout_;
170 fs = _fs_;
171 gs = _gs_;
172 wss = _wss_;
Simon Hunte6f64612017-04-28 00:01:48 -0700173 t2kcs = _t2kcs_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700174
175 return {
176 register: register,
177 setApi: setApi,
Simon Hunte6f64612017-04-28 00:01:48 -0700178 setOverlay: setOverlay,
Simon Hunt6cc86452017-04-27 17:46:22 -0700179
180 hooks: {
181 escape: escapeHook,
182 emptySelect: emptySelectHook,
183 singleSelect: singleSelectHook,
184 multiSelect: multiSelectHook,
185 mouseOver: mouseOverHook,
186 mouseOut: mouseOutHook
187 },
188 showHighlights: showHighlights
189 }
190 }
191 ]);
192
193}());