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