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 | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 25 | var randomService, ps; |
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, |
| 32 | labelPad = 10, |
| 33 | halfDevIcon = devIconDim / 2, |
| 34 | nodeLabelIndex = 1; |
| 35 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 36 | function positionNode(node, forUpdate) { |
| 37 | |
| 38 | var meta = node.metaUi, |
| 39 | x = meta && meta.x, |
| 40 | y = meta && meta.y, |
| 41 | dim = [800, 600], |
| 42 | xy; |
| 43 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 44 | // If the device contains explicit LONG/LAT data, use that to position |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 45 | if (setLongLat(node)) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 46 | // Indicate we want to update cached meta data... |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 47 | return true; |
| 48 | } |
| 49 | |
| 50 | // else if we have [x,y] cached in meta data, use that... |
| 51 | if (x !== undefined && y !== undefined) { |
| 52 | node.fixed = true; |
| 53 | node.px = node.x = x; |
| 54 | node.py = node.y = y; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // if this is a node update (not a node add).. skip randomizer |
| 59 | if (forUpdate) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 64 | // (center of the view) causes them to explode outwards when |
| 65 | // the force layout kicks in. So, we spread them out a bit |
| 66 | // initially, to provide a more serene layout convergence. |
| 67 | // Additionally, if the node is a host, we place it near |
| 68 | // the device it is connected to. |
| 69 | |
| 70 | function rand() { |
| 71 | return { |
| 72 | x: randomService.randDim(dim[0]), |
| 73 | y: randomService.randDim(dim[1]) |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | function near(node) { |
| 78 | return { |
| 79 | x: node.x + nearDist + randomService.spread(nearDist), |
| 80 | y: node.y + nearDist + randomService.spread(nearDist) |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | function getDevice(cp) { |
| 85 | // console.log(cp); |
| 86 | // var d = lu[cp.device]; |
| 87 | // return d || rand(); |
| 88 | return rand(); |
| 89 | } |
| 90 | |
| 91 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 92 | angular.extend(node, xy); |
| 93 | } |
| 94 | |
| 95 | function setLongLat(node) { |
| 96 | var loc = node.location, |
| 97 | coord; |
| 98 | |
| 99 | if (loc && loc.type === 'lnglat') { |
| 100 | coord = [0, 0]; |
| 101 | node.fixed = true; |
| 102 | node.px = node.x = coord[0]; |
| 103 | node.py = node.y = coord[1]; |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | angular.module('ovTopo2') |
| 109 | .factory('Topo2NodeModel', |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 110 | ['Topo2Model', 'FnService', 'RandomService', 'Topo2PrefsService', |
| 111 | function (Model, _fn_, _RandomService_, _ps_) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 112 | |
| 113 | randomService = _RandomService_; |
| 114 | fn = _fn_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 115 | ps = _ps_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 116 | |
| 117 | return Model.extend({ |
| 118 | initialize: function () { |
| 119 | this.node = this.createNode(); |
| 120 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 121 | onEnter: function () {}, // To be overridden by sub-class |
| 122 | onExit: function () {}, // To be overridden by sub-class |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 123 | label: function () { |
| 124 | |
| 125 | var props = this.get('props'), |
| 126 | id = this.get('id'), |
| 127 | friendlyName = props ? props.name : id, |
| 128 | labels = ['', friendlyName, id], |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 129 | nli = ps.get('dlbls'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 130 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 131 | |
| 132 | return labels[idx]; |
| 133 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 134 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 135 | return (label && label.trim()) || ''; |
| 136 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 137 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 138 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 139 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 140 | return box.width + labelPad * 2; |
| 141 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 142 | addLabelElements: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 143 | var rect = this.el.append('rect'); |
| 144 | var text = this.el.append('text').text(label) |
| 145 | .attr('text-anchor', 'left') |
| 146 | .attr('y', '0.3em') |
| 147 | .attr('x', halfDevIcon + labelPad); |
| 148 | |
| 149 | return { |
| 150 | rect: rect, |
| 151 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 152 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 153 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 154 | iconBox: function(dim, labelWidth) { |
| 155 | return { |
| 156 | x: -dim / 2, |
| 157 | y: -dim / 2, |
| 158 | width: dim + labelWidth, |
| 159 | height: dim |
| 160 | }; |
| 161 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 162 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 163 | return fn.classNames('node', |
| 164 | this.nodeType, |
| 165 | this.get('type'), |
| 166 | { |
| 167 | online: this.get('online') |
| 168 | } |
| 169 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 170 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame^] | 171 | update: function () { |
| 172 | this.updateLabel(); |
| 173 | }, |
| 174 | updateLabel: function () { |
| 175 | var node = this.el, |
| 176 | label = this.trimLabel(this.label()), |
| 177 | labelWidth; |
| 178 | |
| 179 | node.select('text').text(label); |
| 180 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 181 | |
| 182 | node.select('rect') |
| 183 | .transition() |
| 184 | .attr(this.iconBox(devIconDim, labelWidth)); |
| 185 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 186 | createNode: function () { |
| 187 | |
| 188 | var node = angular.extend({}, this.attributes); |
| 189 | |
| 190 | // Augment as needed... |
| 191 | node.class = this.nodeType; |
| 192 | node.svgClass = this.svgClassName(); |
| 193 | positionNode(node); |
| 194 | return node; |
| 195 | } |
| 196 | }); |
| 197 | }] |
| 198 | ); |
| 199 | })(); |