blob: ce662fb7f0016ab5fcd6c38b67b0995c362572db [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
25 var $log, $timeout, fs, gs, wss, api;
26
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
126 function showHighlights(data) {
127 function doHighlight() {
128 _showHighlights(data);
129 }
130
131 // note: this allows the server-side event to add a manual delay
132 // before invoking the highlight... this was (originally) to
133 // allow for the re-creation of the DOM model, before trying
134 // to reference elements. For Topo2, there may be a better
135 // solution, making this piece of code redundant. Steven??
136
137 if (data.delay) {
138 $timeout(doHighlight, data.delay);
139 } else {
140 doHighlight();
141 }
142
143 }
144
145 function _showHighlights(data) {
146 // TODO: implement the highlighting .. see topoOverlay.js for example
147 $log.info('+++ TOPO 2 +++ show highlights', data);
148 }
149
150 // ========================================================================
151
152 angular.module('ovTopo2')
153 .factory('Topo2OverlayService', [
154 '$log', '$timeout', 'FnService', 'GlyphService', 'WebSocketService',
155
156 function (_$log_, _$timeout_, _fs_, _gs_, _wss_) {
157 $log = _$log_;
158 $timeout = _$timeout_;
159 fs = _fs_;
160 gs = _gs_;
161 wss = _wss_;
162
163 return {
164 register: register,
165 setApi: setApi,
166
167 hooks: {
168 escape: escapeHook,
169 emptySelect: emptySelectHook,
170 singleSelect: singleSelectHook,
171 multiSelect: multiSelectHook,
172 mouseOver: mouseOverHook,
173 mouseOut: mouseOutHook
174 },
175 showHighlights: showHighlights
176 }
177 }
178 ]);
179
180}());