blob: 42275bf1a2ee7bd9c1bc45126a6b74059c54f179 [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 = {
36 light: {
37 online: '#444444',
38 offline: '#cccccc'
39 },
40 dark: {
41 // TODO: theme
42 online: '#444444',
43 offline: '#cccccc'
44 }
45 };
46
47 function devGlyphColor(d) {
48 var o = this.get('online'),
49 id = this.get('master'),
50 otag = o ? 'online' : 'offline';
51 return o ? sus.cat7().getColor(id, 0, ts.theme()) :
52 dColTheme[ts.theme()][otag];
53 }
Steven Burrows6deb4ce2016-08-26 16:06:23 +010054
Steven Burrowsec1f45c2016-08-08 16:14:41 +010055 angular.module('ovTopo2')
56 .factory('Topo2NodeModel',
Steven Burrows1c5c8612016-10-05 13:45:13 -050057 ['Topo2Model', 'FnService', 'Topo2PrefsService',
Steven Burrows482d9502016-09-27 11:24:58 -070058 'SvgUtilService', 'IconService', 'ThemeService',
Steven Burrows1c5c8612016-10-05 13:45:13 -050059 'Topo2MapConfigService', 'Topo2ZoomService', 'Topo2NodePositionService',
60 function (Model, _fn_, _ps_, _sus_, _is_, _ts_,
61 _t2mcs_, zoomService, _t2nps_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010062
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070063 ts = _ts_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010064 fn = _fn_;
Steven Burrows37549ee2016-09-21 14:41:39 +010065 ps = _ps_;
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070066 sus = _sus_;
67 is = _is_;
Steven Burrowse7cc3082016-09-27 11:24:58 -070068 t2mcs = _t2mcs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -050069 t2nps = _t2nps_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010070
71 return Model.extend({
72 initialize: function () {
73 this.node = this.createNode();
Steven Burrows1c5c8612016-10-05 13:45:13 -050074 this._events = {
75 'mouseover': 'mouseoverHandler',
76 'mouseout': 'mouseoutHandler'
77 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010078 },
Steven Burrowse7cc3082016-09-27 11:24:58 -070079 createNode: function () {
Steven Burrowse7cc3082016-09-27 11:24:58 -070080 this.set('svgClass', this.svgClassName());
Steven Burrows1c5c8612016-10-05 13:45:13 -050081 t2nps.positionNode(this);
Steven Burrowse7cc3082016-09-27 11:24:58 -070082 return this;
83 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070084 setUpEvents: function () {
Steven Burrows1c5c8612016-10-05 13:45:13 -050085 var _this = this,
86 events = angular.extend({}, this._events, this.events);
87 angular.forEach(events, function (handler, key) {
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070088 _this.el.on(key, _this[handler].bind(_this));
89 });
90 },
Steven Burrows1c5c8612016-10-05 13:45:13 -050091 mouseoverHandler: function () {
92 this.set('hovered', true);
93 },
94 mouseoutHandler: function () {
95 this.set('hovered', false);
96 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -070097 icon: function () {
98 return 'unknown';
99 },
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100100 label: function () {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100101 var props = this.get('props'),
102 id = this.get('id'),
Steven Burrows1c5c8612016-10-05 13:45:13 -0500103 friendlyName = props && props.name ? props.name : id,
104 labels = ['', friendlyName || id, id],
Steven Burrows37549ee2016-09-21 14:41:39 +0100105 nli = ps.get('dlbls'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100106 idx = (nli < labels.length) ? nli : 0;
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100107
108 return labels[idx];
109 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100110 trimLabel: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100111 return (label && label.trim()) || '';
112 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100113 computeLabelWidth: function (el) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100114 var text = el.select('text'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100115 box = text.node().getBBox();
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100116 return box.width + labelPad * 2;
117 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100118 addLabelElements: function (label) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500119 var rect = this.el.append('rect')
120 .attr('class', 'node-container');
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700121 var glythRect = this.el.append('rect')
122 .attr('y', -halfDevIcon)
123 .attr('x', -halfDevIcon)
124 .attr('width', devIconDim)
125 .attr('height', devIconDim)
126 .style('fill', devGlyphColor.bind(this));
127
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100128 var text = this.el.append('text').text(label)
129 .attr('text-anchor', 'left')
130 .attr('y', '0.3em')
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700131 .attr('x', halfDevIcon + labelPad + textPad);
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100132
133 return {
134 rect: rect,
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700135 glythRect: glythRect,
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100136 text: text
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100137 };
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100138 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700139 labelBox: function (dim, labelWidth) {
140 var _textPad = (textPad * 2) - labelPad;
141
142 if (labelWidth === 0) {
143 _textPad = 0;
144 }
145
146 return {
147 x: -dim / 2 - labelPad,
148 y: -dim / 2 - labelPad,
149 width: dim + labelWidth + (labelPad * 2) + _textPad,
150 height: dim + (labelPad * 2)
151 };
152 },
153 iconBox: function (dim, labelWidth) {
Steven Burrows37549ee2016-09-21 14:41:39 +0100154 return {
155 x: -dim / 2,
156 y: -dim / 2,
157 width: dim + labelWidth,
158 height: dim
159 };
160 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100161 svgClassName: function () {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100162 return fn.classNames('node',
163 this.nodeType,
164 this.get('type'),
165 {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500166 online: this.get('online'),
167 selected: this.get('selected')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100168 }
169 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100170 },
Steven Burrowse7cc3082016-09-27 11:24:58 -0700171 lngLatFromCoord: function (coord) {
172 var p = t2mcs.projection();
173 return p ? p.invert(coord) : [0, 0];
174 },
Steven Burrows1c5c8612016-10-05 13:45:13 -0500175 resetPosition: function () {
176 t2nps.setLongLat(this);
177 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100178 update: function () {
179 this.updateLabel();
180 },
181 updateLabel: function () {
182 var node = this.el,
183 label = this.trimLabel(this.label()),
184 labelWidth;
185
186 node.select('text').text(label);
187 labelWidth = label ? this.computeLabelWidth(node) : 0;
188
189 node.select('rect')
190 .transition()
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700191 .attr(this.labelBox(devIconDim, labelWidth));
Steven Burrows37549ee2016-09-21 14:41:39 +0100192 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700193 onEnter: function (el) {
194 this.el = d3.select(el);
195 this.render();
196 },
Steven Burrows0616e802016-10-06 21:45:07 -0500197 setScale: function () {
198
199 var dim = devIconDim,
200 multipler = 1;
201
202 if (dim * zoomService.scale() < devIconDimMin) {
203 multipler = devIconDimMin / (dim * zoomService.scale());
204 } else if (dim * zoomService.scale() > devIconDimMax) {
205 multipler = devIconDimMax / (dim * zoomService.scale());
206 }
207
Steven Burrows1c5c8612016-10-05 13:45:13 -0500208 this.el.selectAll('*')
209 .style('transform', 'scale(' + multipler + ')');
Steven Burrows0616e802016-10-06 21:45:07 -0500210 },
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700211 render: function () {
212 var node = this.el,
213 glyphId = this.icon(this.get('type')),
214 label = this.trimLabel(this.label()),
215 glyph, labelWidth;
216
217 // Label
218 var labelElements = this.addLabelElements(label);
219 labelWidth = label ? this.computeLabelWidth(node) : 0;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500220 labelElements.rect
221 .attr(this.labelBox(devIconDim, labelWidth));
Steven Burrowsbbe3dda2016-09-26 14:41:59 -0700222
223 // Icon
224 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
225 glyph.attr(this.iconBox(devIconDim, 0));
226 glyph.style('fill', 'white');
227
228 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
229
230 if (this.events) {
231 this.setUpEvents();
232 }
Steven Burrows0616e802016-10-06 21:45:07 -0500233
234 this.setScale();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100235 }
236 });
237 }]
238 );
239})();