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 | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 25 | var t2ps, 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 | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 57 | 'Topo2SelectService', 'Topo2MastershipService', |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 58 | function (Model, _fn_, _t2ps_, _sus_, _is_, _ts_, |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 59 | _t2mcs_, zoomService, _t2nps_, t2ss, t2mss) { |
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 | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 63 | t2ps = _t2ps_; |
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 | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 72 | this.mastershipService = t2mss; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 73 | this._events = { |
| 74 | 'mouseover': 'mouseoverHandler', |
| 75 | 'mouseout': 'mouseoutHandler' |
| 76 | }; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 77 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 78 | select: function () { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 79 | this.set('selected', true); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 80 | }, |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 81 | 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 Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 92 | deselect: function () { |
| 93 | this.set('selected', false); |
| 94 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 95 | createNode: function () { |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 96 | this.set('svgClass', this.svgClassName()); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 97 | t2nps.positionNode(this); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 98 | return this; |
| 99 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 100 | setUpEvents: function () { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 101 | var _this = this, |
| 102 | events = angular.extend({}, this._events, this.events); |
| 103 | angular.forEach(events, function (handler, key) { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 104 | _this.el.on(key, _this[handler].bind(_this)); |
| 105 | }); |
| 106 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 107 | mouseoverHandler: function () { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 108 | this.set('hovered', true); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 109 | }, |
| 110 | mouseoutHandler: function () { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 111 | this.set('hovered', false); |
| 112 | }, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 113 | onClick: function () { |
| 114 | if (d3.event.defaultPrevented) return; |
| 115 | |
| 116 | d3.event.preventDefault(); |
| 117 | t2ss.selectObject(this, this.multiSelectEnabled); |
| 118 | }, |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 119 | fix: function (fixed) { |
| 120 | this.set({ fixed: fixed }); |
| 121 | this.fixed = fixed; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 122 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 123 | icon: function () { |
| 124 | return 'unknown'; |
| 125 | }, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 126 | labelIndex: function () { |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 127 | return t2ps.get('dlbls'); |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 128 | }, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 129 | label: function () { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 130 | var props = this.get('props'), |
| 131 | id = this.get('id'), |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 132 | friendlyName = props && props.name ? props.name : id, |
| 133 | labels = ['', friendlyName || id, id], |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 134 | nli = this.labelIndex(), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 135 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 136 | |
| 137 | return labels[idx]; |
| 138 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 139 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 140 | return (label && label.trim()) || ''; |
| 141 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 142 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 143 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 144 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 145 | return box.width + labelPad * 2; |
| 146 | }, |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 147 | 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 154 | addLabelElements: function (label) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 155 | var rect = this.el.append('rect') |
| 156 | .attr('class', 'node-container'); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 157 | var glythRect = this.el.append('rect') |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 158 | .attr('class', 'icon-rect') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 159 | .attr('y', -halfDevIcon) |
| 160 | .attr('x', -halfDevIcon) |
| 161 | .attr('width', devIconDim) |
| 162 | .attr('height', devIconDim) |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 163 | .style('fill', this.devGlyphColor.bind(this)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 164 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 165 | var text = this.el.append('text').text(label) |
| 166 | .attr('text-anchor', 'left') |
| 167 | .attr('y', '0.3em') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 168 | .attr('x', halfDevIcon + labelPad + textPad); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 169 | |
| 170 | return { |
| 171 | rect: rect, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 172 | glythRect: glythRect, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 173 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 174 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 175 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 176 | 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 Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 191 | return { |
| 192 | x: -dim / 2, |
| 193 | y: -dim / 2, |
| 194 | width: dim + labelWidth, |
| 195 | height: dim |
| 196 | }; |
| 197 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 198 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 199 | return fn.classNames('node', |
| 200 | this.nodeType, |
| 201 | this.get('type'), |
| 202 | { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 203 | online: this.get('online'), |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 204 | selected: this.get('selected'), |
| 205 | hovered: this.get('hovered'), |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 206 | fixed: this.get('fixed'), |
| 207 | suppressedmax: this.get('mastership') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 208 | } |
| 209 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 210 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 211 | lngLatFromCoord: function (coord) { |
| 212 | var p = t2mcs.projection(); |
| 213 | return p ? p.invert(coord) : [0, 0]; |
| 214 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 215 | resetPosition: function () { |
| 216 | t2nps.setLongLat(this); |
| 217 | }, |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 218 | displayMastership: function () { |
| 219 | this.set({ mastership: t2mss.mastership() !== null}); |
| 220 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 221 | 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 Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 234 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 235 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 236 | onEnter: function (el) { |
| 237 | this.el = d3.select(el); |
| 238 | this.render(); |
| 239 | }, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 240 | setScale: function () { |
| 241 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 242 | if (!this.el) return; |
| 243 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 244 | 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 253 | this.el.selectAll('*') |
| 254 | .style('transform', 'scale(' + multipler + ')'); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 255 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 256 | 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 265 | labelElements.rect |
| 266 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 267 | |
| 268 | // Icon |
| 269 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
| 270 | glyph.attr(this.iconBox(devIconDim, 0)); |
Simon Hunt | b3656d4 | 2017-04-07 18:15:35 -0700 | [diff] [blame] | 271 | glyph.style('fill', dUseTheme[ts.theme()]); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 272 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 273 | node.attr('transform', |
| 274 | sus.translate(-halfDevIcon, -halfDevIcon)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 275 | |
| 276 | if (this.events) { |
| 277 | this.setUpEvents(); |
| 278 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 279 | |
| 280 | this.setScale(); |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 281 | }, |
| 282 | |
| 283 | // Override Methods |
| 284 | setOfflineVisibility: function () {}, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 285 | }); |
| 286 | }] |
| 287 | ); |
| 288 | })(); |