blob: fb383d2a08f1a7072857301876d84584b0590c30 [file] [log] [blame]
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001/*
2 * Copyright 2016-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/*
Steven Burrows1c5c8612016-10-05 13:45:13 -050018 ONOS GUI -- Topology Node Module.
19 Module that contains model for nodes within the topology
Steven Burrowsec1f45c2016-08-08 16:14:41 +010020 */
21
22(function () {
23 'use strict';
24
Steven Burrows1c5c8612016-10-05 13:45:13 -050025 var ps, sus, is, ts, t2mcs, t2nps, fn;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010026
Steven Burrows6deb4ce2016-08-26 16:06:23 +010027 var devIconDim = 36,
Steven Burrows0616e802016-10-06 21:45:07 -050028 devIconDimMin = 20,
29 devIconDimMax = 40,
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070030 labelPad = 5,
31 textPad = 5,
32 halfDevIcon = devIconDim / 2;
33
34 // note: these are the device icon colors without affinity (no master)
35 var dColTheme = {
Simon Huntb3656d42017-04-07 18:15:35 -070036 light: {
37 online: '#444444',
38 offline: '#cccccc'
39 },
40 dark: {
41 // TODO: theme
42 online: '#444444',
43 offline: '#cccccc'
44 }
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070045 },
Simon Huntb3656d42017-04-07 18:15:35 -070046 // and here are the stroke colors of the glyph, per theme
47 dUseTheme = {
48 light: 'white',
49 dark: 'black'
50 };
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070051
Steven Burrowsec1f45c2016-08-08 16:14:41 +010052 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +000053 .factory('Topo2NodeModel', [
54 'Topo2Model', 'FnService', 'Topo2PrefsService',
Steven Burrows482d9502016-09-27 11:24:58 -070055 'SvgUtilService', 'IconService', 'ThemeService',
Steven Burrows1c5c8612016-10-05 13:45:13 -050056 'Topo2MapConfigService', 'Topo2ZoomService', 'Topo2NodePositionService',
Steven Burrowsc515e602017-04-13 11:17:40 -070057 'Topo2SelectService', 'Topo2MastershipService',
Steven Burrows1c5c8612016-10-05 13:45:13 -050058 function (Model, _fn_, _ps_, _sus_, _is_, _ts_,
Steven Burrowsc515e602017-04-13 11:17:40 -070059 _t2mcs_, zoomService, _t2nps_, t2ss, t2mss) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010060
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070061 ts = _ts_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010062 fn = _fn_;
Steven Burrows37549ee2016-09-21 14:41:39 +010063 ps = _ps_;
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070064 sus = _sus_;
65 is = _is_;
Steven Burrowse7cc3082016-09-27 11:24:58 -070066 t2mcs = _t2mcs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -050067 t2nps = _t2nps_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010068
69 return Model.extend({
70 initialize: function () {
71 this.node = this.createNode();
Steven Burrowsc515e602017-04-13 11:17:40 -070072 this.mastershipService = t2mss;
Steven Burrows1c5c8612016-10-05 13:45:13 -050073 this._events = {
74 'mouseover': 'mouseoverHandler',
75 'mouseout': 'mouseoutHandler'
76 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010077 },
Steven Burrows86af4352016-11-16 18:19:12 -060078 select: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +000079 this.set('selected', true);
Steven Burrows86af4352016-11-16 18:19:12 -060080 },
Steven Burrows68d6f952017-03-10 13:53:35 +000081 index: function () {
82
83 var models = this.collection.models,
84 id = this.get('id');
85
86 var index = _.find(models, function (model, i) {
87 return model.get('id') === id;
88 });
89
90 return index || models.length;
91 },
Steven Burrows1aa4f582016-12-13 15:05:41 -050092 deselect: function () {
93 this.set('selected', false);
94 },
Steven Burrowse7cc3082016-09-27 11:24:58 -070095 createNode: function () {
Steven Burrowse7cc3082016-09-27 11:24:58 -070096 this.set('svgClass', this.svgClassName());
Steven Burrows1c5c8612016-10-05 13:45:13 -050097 t2nps.positionNode(this);
Steven Burrowse7cc3082016-09-27 11:24:58 -070098 return this;
99 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700100 setUpEvents: function () {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500101 var _this = this,
102 events = angular.extend({}, this._events, this.events);
103 angular.forEach(events, function (handler, key) {
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700104 _this.el.on(key, _this[handler].bind(_this));
105 });
106 },
Steven Burrows1c5c8612016-10-05 13:45:13 -0500107 mouseoverHandler: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000108 this.set('hovered', true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500109 },
110 mouseoutHandler: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000111 this.set('hovered', false);
112 },
Steven Burrows5fa057e2017-03-15 17:07:56 +0000113 onClick: function () {
114 if (d3.event.defaultPrevented) return;
115
116 d3.event.preventDefault();
117 t2ss.selectObject(this, this.multiSelectEnabled);
118 },
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000119 fix: function (fixed) {
120 this.set({ fixed: fixed });
121 this.fixed = fixed;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500122 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700123 icon: function () {
124 return 'unknown';
125 },
Steven Burrows583f4be2016-11-04 14:06:50 +0100126 labelIndex: function () {
127 return ps.get('dlbls');
128 },
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100129 label: function () {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100130 var props = this.get('props'),
131 id = this.get('id'),
Steven Burrows1c5c8612016-10-05 13:45:13 -0500132 friendlyName = props && props.name ? props.name : id,
133 labels = ['', friendlyName || id, id],
Steven Burrows583f4be2016-11-04 14:06:50 +0100134 nli = this.labelIndex(),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100135 idx = (nli < labels.length) ? nli : 0;
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100136
137 return labels[idx];
138 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100139 trimLabel: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100140 return (label && label.trim()) || '';
141 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100142 computeLabelWidth: function (el) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100143 var text = el.select('text'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100144 box = text.node().getBBox();
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100145 return box.width + labelPad * 2;
146 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000147 devGlyphColor: function () {
148 var o = this.get('online'),
149 id = this.get('master'),
150 otag = o ? 'online' : 'offline';
151 return o ? sus.cat7().getColor(id, 0, ts.theme()) :
152 dColTheme[ts.theme()][otag];
153 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100154 addLabelElements: function (label) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500155 var rect = this.el.append('rect')
156 .attr('class', 'node-container');
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700157 var glythRect = this.el.append('rect')
Steven Burrows42eb9e22017-02-06 14:20:24 +0000158 .attr('class', 'icon-rect')
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700159 .attr('y', -halfDevIcon)
160 .attr('x', -halfDevIcon)
161 .attr('width', devIconDim)
162 .attr('height', devIconDim)
Steven Burrows42eb9e22017-02-06 14:20:24 +0000163 .style('fill', this.devGlyphColor.bind(this));
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700164
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100165 var text = this.el.append('text').text(label)
166 .attr('text-anchor', 'left')
167 .attr('y', '0.3em')
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700168 .attr('x', halfDevIcon + labelPad + textPad);
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100169
170 return {
171 rect: rect,
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700172 glythRect: glythRect,
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100173 text: text
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100174 };
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100175 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700176 labelBox: function (dim, labelWidth) {
177 var _textPad = (textPad * 2) - labelPad;
178
179 if (labelWidth === 0) {
180 _textPad = 0;
181 }
182
183 return {
184 x: -dim / 2 - labelPad,
185 y: -dim / 2 - labelPad,
186 width: dim + labelWidth + (labelPad * 2) + _textPad,
187 height: dim + (labelPad * 2)
188 };
189 },
190 iconBox: function (dim, labelWidth) {
Steven Burrows37549ee2016-09-21 14:41:39 +0100191 return {
192 x: -dim / 2,
193 y: -dim / 2,
194 width: dim + labelWidth,
195 height: dim
196 };
197 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100198 svgClassName: function () {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100199 return fn.classNames('node',
200 this.nodeType,
201 this.get('type'),
202 {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500203 online: this.get('online'),
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000204 selected: this.get('selected'),
205 hovered: this.get('hovered'),
Steven Burrowsc515e602017-04-13 11:17:40 -0700206 fixed: this.get('fixed'),
207 suppressedmax: this.get('mastership')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100208 }
209 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100210 },
Steven Burrowse7cc3082016-09-27 11:24:58 -0700211 lngLatFromCoord: function (coord) {
212 var p = t2mcs.projection();
213 return p ? p.invert(coord) : [0, 0];
214 },
Steven Burrows1c5c8612016-10-05 13:45:13 -0500215 resetPosition: function () {
216 t2nps.setLongLat(this);
217 },
Steven Burrowsc515e602017-04-13 11:17:40 -0700218 displayMastership: function () {
219 this.set({ mastership: t2mss.mastership() !== null});
220 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100221 update: function () {
222 this.updateLabel();
223 },
224 updateLabel: function () {
225 var node = this.el,
226 label = this.trimLabel(this.label()),
227 labelWidth;
228
229 node.select('text').text(label);
230 labelWidth = label ? this.computeLabelWidth(node) : 0;
231
232 node.select('rect')
233 .transition()
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700234 .attr(this.labelBox(devIconDim, labelWidth));
Steven Burrows37549ee2016-09-21 14:41:39 +0100235 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700236 onEnter: function (el) {
237 this.el = d3.select(el);
238 this.render();
239 },
Steven Burrows0616e802016-10-06 21:45:07 -0500240 setScale: function () {
241
Steven Burrowse8a455a2017-03-16 16:58:59 +0000242 if (!this.el) return;
243
Steven Burrows0616e802016-10-06 21:45:07 -0500244 var dim = devIconDim,
245 multipler = 1;
246
247 if (dim * zoomService.scale() < devIconDimMin) {
248 multipler = devIconDimMin / (dim * zoomService.scale());
249 } else if (dim * zoomService.scale() > devIconDimMax) {
250 multipler = devIconDimMax / (dim * zoomService.scale());
251 }
252
Steven Burrows1c5c8612016-10-05 13:45:13 -0500253 this.el.selectAll('*')
254 .style('transform', 'scale(' + multipler + ')');
Steven Burrows0616e802016-10-06 21:45:07 -0500255 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700256 render: function () {
257 var node = this.el,
258 glyphId = this.icon(this.get('type')),
259 label = this.trimLabel(this.label()),
260 glyph, labelWidth;
261
262 // Label
263 var labelElements = this.addLabelElements(label);
264 labelWidth = label ? this.computeLabelWidth(node) : 0;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500265 labelElements.rect
266 .attr(this.labelBox(devIconDim, labelWidth));
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700267
268 // Icon
269 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
270 glyph.attr(this.iconBox(devIconDim, 0));
Simon Huntb3656d42017-04-07 18:15:35 -0700271 glyph.style('fill', dUseTheme[ts.theme()]);
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700272
Steven Burrows583f4be2016-11-04 14:06:50 +0100273 node.attr('transform',
274 sus.translate(-halfDevIcon, -halfDevIcon));
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700275
276 if (this.events) {
277 this.setUpEvents();
278 }
Steven Burrows0616e802016-10-06 21:45:07 -0500279
280 this.setScale();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100281 }
282 });
283 }]
284 );
285})();