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