blob: 49f76eac7b842ff8a31a5c336c614c0adbc70975 [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 Burrows1c2a9682017-07-14 16:52:46 +010029 current = null;
Simon Hunt6cc86452017-04-27 17:46:22 -070030
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 Hunte6f64612017-04-28 00:01:48 -0700121 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 Hunt6cc86452017-04-27 17:46:22 -0700131 function showHighlights(data) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700132 $log.info('+++ TOPO 2 +++ show highlights', data);
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400133 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 Burrows2b29ca42017-05-12 10:58:05 -0400138 nodeLink = t2rs.getLink(id);
139 if (nodeLink) {
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400140 t2lc.addLabel(LinkLabel, link, linkLabelsDOM, {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100141 link: nodeLink,
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400142 });
143 }
144 });
Simon Hunt6cc86452017-04-27 17:46:22 -0700145 }
146
147 // ========================================================================
148
149 angular.module('ovTopo2')
150 .factory('Topo2OverlayService', [
Steven Burrows1c2a9682017-07-14 16:52:46 +0100151 '$log', 'FnService', 'Topo2KeyCommandService',
152 'Topo2RegionService', 'Topo2LabelCollection', 'Topo2LinkLabel',
Simon Hunt6cc86452017-04-27 17:46:22 -0700153
Steven Burrows1c2a9682017-07-14 16:52:46 +0100154 function (_$log_, _fs_, _t2kcs_, _t2rs_,
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400155 _t2lc_, _t2ll_) {
Simon Hunt6cc86452017-04-27 17:46:22 -0700156 $log = _$log_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700157 fs = _fs_;
Simon Hunte6f64612017-04-28 00:01:48 -0700158 t2kcs = _t2kcs_;
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400159 t2rs = _t2rs_;
160 t2lc = _t2lc_;
161 LinkLabel = _t2ll_;
Simon Hunt6cc86452017-04-27 17:46:22 -0700162
163 return {
164 register: register,
Simon Hunte6f64612017-04-28 00:01:48 -0700165 setOverlay: setOverlay,
Simon Hunt6cc86452017-04-27 17:46:22 -0700166
167 hooks: {
168 escape: escapeHook,
169 emptySelect: emptySelectHook,
170 singleSelect: singleSelectHook,
171 multiSelect: multiSelectHook,
172 mouseOver: mouseOverHook,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100173 mouseOut: mouseOutHook,
Simon Hunt6cc86452017-04-27 17:46:22 -0700174 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100175 showHighlights: showHighlights,
176 };
177 },
Simon Hunt6cc86452017-04-27 17:46:22 -0700178 ]);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100179}());