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 | /* |
| 18 | ONOS GUI -- Topology Layout Module. |
| 19 | Module that contains the d3.force.layout logic |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 25 | var randomService, ps, sus, is, ts, t2mcs; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | var fn; |
| 27 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 28 | // Internal state; |
| 29 | var nearDist = 15; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 30 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 31 | var devIconDim = 36, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame^] | 32 | devIconDimMin = 20, |
| 33 | devIconDimMax = 40, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 34 | labelPad = 5, |
| 35 | textPad = 5, |
| 36 | halfDevIcon = devIconDim / 2; |
| 37 | |
| 38 | // note: these are the device icon colors without affinity (no master) |
| 39 | var dColTheme = { |
| 40 | light: { |
| 41 | online: '#444444', |
| 42 | offline: '#cccccc' |
| 43 | }, |
| 44 | dark: { |
| 45 | // TODO: theme |
| 46 | online: '#444444', |
| 47 | offline: '#cccccc' |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | function devGlyphColor(d) { |
| 52 | var o = this.get('online'), |
| 53 | id = this.get('master'), |
| 54 | otag = o ? 'online' : 'offline'; |
| 55 | return o ? sus.cat7().getColor(id, 0, ts.theme()) : |
| 56 | dColTheme[ts.theme()][otag]; |
| 57 | } |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 58 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 59 | function positionNode(node, forUpdate) { |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 60 | var meta = node.get('metaUi'), |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 61 | x = meta && meta.x, |
| 62 | y = meta && meta.y, |
| 63 | dim = [800, 600], |
| 64 | xy; |
| 65 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 66 | // If the device contains explicit LONG/LAT data, use that to position |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 67 | if (setLongLat(node)) { |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 68 | // Indicate we want to update cached meta data... |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
| 72 | // else if we have [x,y] cached in meta data, use that... |
| 73 | if (x !== undefined && y !== undefined) { |
| 74 | node.fixed = true; |
| 75 | node.px = node.x = x; |
| 76 | node.py = node.y = y; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // if this is a node update (not a node add).. skip randomizer |
| 81 | if (forUpdate) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 86 | // (center of the view) causes them to explode outwards when |
| 87 | // the force layout kicks in. So, we spread them out a bit |
| 88 | // initially, to provide a more serene layout convergence. |
| 89 | // Additionally, if the node is a host, we place it near |
| 90 | // the device it is connected to. |
| 91 | |
| 92 | function rand() { |
| 93 | return { |
| 94 | x: randomService.randDim(dim[0]), |
| 95 | y: randomService.randDim(dim[1]) |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | function near(node) { |
| 100 | return { |
| 101 | x: node.x + nearDist + randomService.spread(nearDist), |
| 102 | y: node.y + nearDist + randomService.spread(nearDist) |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | function getDevice(cp) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 107 | return rand(); |
| 108 | } |
| 109 | |
| 110 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 111 | |
| 112 | if (node.class === 'sub-region') { |
| 113 | xy = rand(); |
| 114 | node.x = node.px = xy.x; |
| 115 | node.y = node.py = xy.y; |
| 116 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 117 | angular.extend(node, xy); |
| 118 | } |
| 119 | |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 120 | function setLongLat(el) { |
| 121 | var loc = el.get('location'), |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 122 | coord; |
| 123 | |
| 124 | if (loc && loc.type === 'lnglat') { |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 125 | |
| 126 | if (loc.lat === 0 && loc.lng === 0) { |
| 127 | return false; |
| 128 | } |
| 129 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 130 | coord = coordFromLngLat(loc); |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 131 | el.fixed = true; |
| 132 | el.x = el.px = coord[0]; |
| 133 | el.y = el.py = coord[1]; |
| 134 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 139 | function coordFromLngLat(loc) { |
| 140 | var p = t2mcs.projection(); |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 141 | return p ? p([loc.lng, loc.lat]) : [0, 0]; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 144 | angular.module('ovTopo2') |
| 145 | .factory('Topo2NodeModel', |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 146 | ['Topo2Model', 'FnService', 'RandomService', 'Topo2PrefsService', |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 147 | 'SvgUtilService', 'IconService', 'ThemeService', |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame^] | 148 | 'Topo2MapConfigService', 'Topo2ZoomService', |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 149 | function (Model, _fn_, _RandomService_, _ps_, _sus_, _is_, _ts_, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame^] | 150 | _t2mcs_, zoomService) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 151 | |
| 152 | randomService = _RandomService_; |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 153 | ts = _ts_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 154 | fn = _fn_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 155 | ps = _ps_; |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 156 | sus = _sus_; |
| 157 | is = _is_; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 158 | t2mcs = _t2mcs_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 159 | |
| 160 | return Model.extend({ |
| 161 | initialize: function () { |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 162 | this.set('class', this.nodeType); |
| 163 | this.set('svgClass', this.svgClassName()); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 164 | this.node = this.createNode(); |
| 165 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 166 | createNode: function () { |
| 167 | this.set('class', this.nodeType); |
| 168 | this.set('svgClass', this.svgClassName()); |
| 169 | positionNode(this); |
| 170 | return this; |
| 171 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 172 | setUpEvents: function () { |
| 173 | var _this = this; |
| 174 | angular.forEach(this.events, function (handler, key) { |
| 175 | _this.el.on(key, _this[handler].bind(_this)); |
| 176 | }); |
| 177 | }, |
| 178 | icon: function () { |
| 179 | return 'unknown'; |
| 180 | }, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 181 | label: function () { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 182 | var props = this.get('props'), |
| 183 | id = this.get('id'), |
| 184 | friendlyName = props ? props.name : id, |
| 185 | labels = ['', friendlyName, id], |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 186 | nli = ps.get('dlbls'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 187 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 188 | |
| 189 | return labels[idx]; |
| 190 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 191 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 192 | return (label && label.trim()) || ''; |
| 193 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 194 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 195 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 196 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 197 | return box.width + labelPad * 2; |
| 198 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 199 | addLabelElements: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 200 | var rect = this.el.append('rect'); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 201 | var glythRect = this.el.append('rect') |
| 202 | .attr('y', -halfDevIcon) |
| 203 | .attr('x', -halfDevIcon) |
| 204 | .attr('width', devIconDim) |
| 205 | .attr('height', devIconDim) |
| 206 | .style('fill', devGlyphColor.bind(this)); |
| 207 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 208 | var text = this.el.append('text').text(label) |
| 209 | .attr('text-anchor', 'left') |
| 210 | .attr('y', '0.3em') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 211 | .attr('x', halfDevIcon + labelPad + textPad); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 212 | |
| 213 | return { |
| 214 | rect: rect, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 215 | glythRect: glythRect, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 216 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 217 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 218 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 219 | labelBox: function (dim, labelWidth) { |
| 220 | var _textPad = (textPad * 2) - labelPad; |
| 221 | |
| 222 | if (labelWidth === 0) { |
| 223 | _textPad = 0; |
| 224 | } |
| 225 | |
| 226 | return { |
| 227 | x: -dim / 2 - labelPad, |
| 228 | y: -dim / 2 - labelPad, |
| 229 | width: dim + labelWidth + (labelPad * 2) + _textPad, |
| 230 | height: dim + (labelPad * 2) |
| 231 | }; |
| 232 | }, |
| 233 | iconBox: function (dim, labelWidth) { |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 234 | return { |
| 235 | x: -dim / 2, |
| 236 | y: -dim / 2, |
| 237 | width: dim + labelWidth, |
| 238 | height: dim |
| 239 | }; |
| 240 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 241 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 242 | return fn.classNames('node', |
| 243 | this.nodeType, |
| 244 | this.get('type'), |
| 245 | { |
| 246 | online: this.get('online') |
| 247 | } |
| 248 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 249 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 250 | lngLatFromCoord: function (coord) { |
| 251 | var p = t2mcs.projection(); |
| 252 | return p ? p.invert(coord) : [0, 0]; |
| 253 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 254 | update: function () { |
| 255 | this.updateLabel(); |
| 256 | }, |
| 257 | updateLabel: function () { |
| 258 | var node = this.el, |
| 259 | label = this.trimLabel(this.label()), |
| 260 | labelWidth; |
| 261 | |
| 262 | node.select('text').text(label); |
| 263 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 264 | |
| 265 | node.select('rect') |
| 266 | .transition() |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 267 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 268 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 269 | onEnter: function (el) { |
| 270 | this.el = d3.select(el); |
| 271 | this.render(); |
| 272 | }, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame^] | 273 | setScale: function () { |
| 274 | |
| 275 | var dim = devIconDim, |
| 276 | multipler = 1; |
| 277 | |
| 278 | if (dim * zoomService.scale() < devIconDimMin) { |
| 279 | multipler = devIconDimMin / (dim * zoomService.scale()); |
| 280 | } else if (dim * zoomService.scale() > devIconDimMax) { |
| 281 | multipler = devIconDimMax / (dim * zoomService.scale()); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | this.el.selectAll('*').style('transform', 'scale(' + multipler + ')'); |
| 286 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 287 | render: function () { |
| 288 | var node = this.el, |
| 289 | glyphId = this.icon(this.get('type')), |
| 290 | label = this.trimLabel(this.label()), |
| 291 | glyph, labelWidth; |
| 292 | |
| 293 | // Label |
| 294 | var labelElements = this.addLabelElements(label); |
| 295 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 296 | labelElements.rect.attr(this.labelBox(devIconDim, labelWidth)); |
| 297 | |
| 298 | // Icon |
| 299 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
| 300 | glyph.attr(this.iconBox(devIconDim, 0)); |
| 301 | glyph.style('fill', 'white'); |
| 302 | |
| 303 | node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon)); |
| 304 | |
| 305 | if (this.events) { |
| 306 | this.setUpEvents(); |
| 307 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame^] | 308 | |
| 309 | this.setScale(); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 310 | } |
| 311 | }); |
| 312 | }] |
| 313 | ); |
| 314 | })(); |