Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1 | /* |
| 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 18 | ONOS GUI -- Topology Node Module. |
| 19 | Module that contains model for nodes within the topology |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 25 | var ps, sus, is, ts, t2mcs, t2nps, fn; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 27 | var devIconDim = 36, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 28 | devIconDimMin = 20, |
| 29 | devIconDimMax = 40, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 30 | 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 Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 36 | light: { |
| 37 | online: '#444444', |
| 38 | offline: '#cccccc' |
| 39 | }, |
| 40 | dark: { |
| 41 | // TODO: theme |
| 42 | online: '#444444', |
| 43 | offline: '#cccccc' |
| 44 | } |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 45 | }, |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 46 | // and here are the stroke colors of the glyph, per theme |
| 47 | dUseTheme = { |
| 48 | light: 'white', |
| 49 | dark: 'black' |
| 50 | }; |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 51 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 52 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 53 | .factory('Topo2NodeModel', [ |
| 54 | 'Topo2Model', 'FnService', 'Topo2PrefsService', |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 55 | 'SvgUtilService', 'IconService', 'ThemeService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 56 | 'Topo2MapConfigService', 'Topo2ZoomService', 'Topo2NodePositionService', |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 57 | 'Topo2SelectService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 58 | function (Model, _fn_, _ps_, _sus_, _is_, _ts_, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 59 | _t2mcs_, zoomService, _t2nps_, t2ss) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 60 | |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 61 | ts = _ts_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 62 | fn = _fn_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 63 | ps = _ps_; |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 64 | sus = _sus_; |
| 65 | is = _is_; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 66 | t2mcs = _t2mcs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 67 | t2nps = _t2nps_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 68 | |
| 69 | return Model.extend({ |
| 70 | initialize: function () { |
| 71 | this.node = this.createNode(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 72 | this._events = { |
| 73 | 'mouseover': 'mouseoverHandler', |
| 74 | 'mouseout': 'mouseoutHandler' |
| 75 | }; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 76 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 77 | select: function () { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 78 | this.set('selected', true); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 79 | }, |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 80 | index: function () { |
| 81 | |
| 82 | var models = this.collection.models, |
| 83 | id = this.get('id'); |
| 84 | |
| 85 | var index = _.find(models, function (model, i) { |
| 86 | return model.get('id') === id; |
| 87 | }); |
| 88 | |
| 89 | return index || models.length; |
| 90 | }, |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 91 | deselect: function () { |
| 92 | this.set('selected', false); |
| 93 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 94 | createNode: function () { |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 95 | this.set('svgClass', this.svgClassName()); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 96 | t2nps.positionNode(this); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 97 | return this; |
| 98 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 99 | setUpEvents: function () { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 100 | var _this = this, |
| 101 | events = angular.extend({}, this._events, this.events); |
| 102 | angular.forEach(events, function (handler, key) { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 103 | _this.el.on(key, _this[handler].bind(_this)); |
| 104 | }); |
| 105 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 106 | mouseoverHandler: function () { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 107 | this.set('hovered', true); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 108 | }, |
| 109 | mouseoutHandler: function () { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 110 | this.set('hovered', false); |
| 111 | }, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 112 | onClick: function () { |
| 113 | if (d3.event.defaultPrevented) return; |
| 114 | |
| 115 | d3.event.preventDefault(); |
| 116 | t2ss.selectObject(this, this.multiSelectEnabled); |
| 117 | }, |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 118 | fix: function (fixed) { |
| 119 | this.set({ fixed: fixed }); |
| 120 | this.fixed = fixed; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 121 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 122 | icon: function () { |
| 123 | return 'unknown'; |
| 124 | }, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 125 | labelIndex: function () { |
| 126 | return ps.get('dlbls'); |
| 127 | }, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 128 | label: function () { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 129 | var props = this.get('props'), |
| 130 | id = this.get('id'), |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 131 | friendlyName = props && props.name ? props.name : id, |
| 132 | labels = ['', friendlyName || id, id], |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 133 | nli = this.labelIndex(), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 134 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 135 | |
| 136 | return labels[idx]; |
| 137 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 138 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 139 | return (label && label.trim()) || ''; |
| 140 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 141 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 142 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 143 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 144 | return box.width + labelPad * 2; |
| 145 | }, |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 146 | devGlyphColor: function () { |
| 147 | var o = this.get('online'), |
| 148 | id = this.get('master'), |
| 149 | otag = o ? 'online' : 'offline'; |
| 150 | return o ? sus.cat7().getColor(id, 0, ts.theme()) : |
| 151 | dColTheme[ts.theme()][otag]; |
| 152 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 153 | addLabelElements: function (label) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 154 | var rect = this.el.append('rect') |
| 155 | .attr('class', 'node-container'); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 156 | var glythRect = this.el.append('rect') |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 157 | .attr('class', 'icon-rect') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 158 | .attr('y', -halfDevIcon) |
| 159 | .attr('x', -halfDevIcon) |
| 160 | .attr('width', devIconDim) |
| 161 | .attr('height', devIconDim) |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 162 | .style('fill', this.devGlyphColor.bind(this)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 163 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 164 | var text = this.el.append('text').text(label) |
| 165 | .attr('text-anchor', 'left') |
| 166 | .attr('y', '0.3em') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 167 | .attr('x', halfDevIcon + labelPad + textPad); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 168 | |
| 169 | return { |
| 170 | rect: rect, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 171 | glythRect: glythRect, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 172 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 173 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 174 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 175 | labelBox: function (dim, labelWidth) { |
| 176 | var _textPad = (textPad * 2) - labelPad; |
| 177 | |
| 178 | if (labelWidth === 0) { |
| 179 | _textPad = 0; |
| 180 | } |
| 181 | |
| 182 | return { |
| 183 | x: -dim / 2 - labelPad, |
| 184 | y: -dim / 2 - labelPad, |
| 185 | width: dim + labelWidth + (labelPad * 2) + _textPad, |
| 186 | height: dim + (labelPad * 2) |
| 187 | }; |
| 188 | }, |
| 189 | iconBox: function (dim, labelWidth) { |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 190 | return { |
| 191 | x: -dim / 2, |
| 192 | y: -dim / 2, |
| 193 | width: dim + labelWidth, |
| 194 | height: dim |
| 195 | }; |
| 196 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 197 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 198 | return fn.classNames('node', |
| 199 | this.nodeType, |
| 200 | this.get('type'), |
| 201 | { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 202 | online: this.get('online'), |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 203 | selected: this.get('selected'), |
| 204 | hovered: this.get('hovered'), |
| 205 | fixed: this.get('fixed') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 206 | } |
| 207 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 208 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 209 | lngLatFromCoord: function (coord) { |
| 210 | var p = t2mcs.projection(); |
| 211 | return p ? p.invert(coord) : [0, 0]; |
| 212 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 213 | resetPosition: function () { |
| 214 | t2nps.setLongLat(this); |
| 215 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 216 | update: function () { |
| 217 | this.updateLabel(); |
| 218 | }, |
| 219 | updateLabel: function () { |
| 220 | var node = this.el, |
| 221 | label = this.trimLabel(this.label()), |
| 222 | labelWidth; |
| 223 | |
| 224 | node.select('text').text(label); |
| 225 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 226 | |
| 227 | node.select('rect') |
| 228 | .transition() |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 229 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 230 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 231 | onEnter: function (el) { |
| 232 | this.el = d3.select(el); |
| 233 | this.render(); |
| 234 | }, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 235 | setScale: function () { |
| 236 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 237 | if (!this.el) return; |
| 238 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 239 | var dim = devIconDim, |
| 240 | multipler = 1; |
| 241 | |
| 242 | if (dim * zoomService.scale() < devIconDimMin) { |
| 243 | multipler = devIconDimMin / (dim * zoomService.scale()); |
| 244 | } else if (dim * zoomService.scale() > devIconDimMax) { |
| 245 | multipler = devIconDimMax / (dim * zoomService.scale()); |
| 246 | } |
| 247 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 248 | this.el.selectAll('*') |
| 249 | .style('transform', 'scale(' + multipler + ')'); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 250 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 251 | render: function () { |
| 252 | var node = this.el, |
| 253 | glyphId = this.icon(this.get('type')), |
| 254 | label = this.trimLabel(this.label()), |
| 255 | glyph, labelWidth; |
| 256 | |
| 257 | // Label |
| 258 | var labelElements = this.addLabelElements(label); |
| 259 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 260 | labelElements.rect |
| 261 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 262 | |
| 263 | // Icon |
| 264 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
| 265 | glyph.attr(this.iconBox(devIconDim, 0)); |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame^] | 266 | glyph.style('fill', dUseTheme[ts.theme()]); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 267 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 268 | node.attr('transform', |
| 269 | sus.translate(-halfDevIcon, -halfDevIcon)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 270 | |
| 271 | if (this.events) { |
| 272 | this.setUpEvents(); |
| 273 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 274 | |
| 275 | this.setScale(); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 276 | } |
| 277 | }); |
| 278 | }] |
| 279 | ); |
| 280 | })(); |