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