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 | |
| 25 | var randomService; |
| 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 | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 110 | ['Topo2Model', 'FnService', 'RandomService', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 111 | function (Model, _fn_, _RandomService_) { |
| 112 | |
| 113 | randomService = _RandomService_; |
| 114 | fn = _fn_; |
| 115 | |
| 116 | return Model.extend({ |
| 117 | initialize: function () { |
| 118 | this.node = this.createNode(); |
| 119 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 120 | onEnter: function () {}, // To be overridden by sub-class |
| 121 | onExit: function () {}, // To be overridden by sub-class |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 122 | label: function () { |
| 123 | |
| 124 | var props = this.get('props'), |
| 125 | id = this.get('id'), |
| 126 | friendlyName = props ? props.name : id, |
| 127 | labels = ['', friendlyName, id], |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 128 | nli = nodeLabelIndex, |
| 129 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 130 | |
| 131 | return labels[idx]; |
| 132 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 133 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 134 | return (label && label.trim()) || ''; |
| 135 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 136 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 137 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 138 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 139 | return box.width + labelPad * 2; |
| 140 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 141 | addLabelElements: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 142 | var rect = this.el.append('rect'); |
| 143 | var text = this.el.append('text').text(label) |
| 144 | .attr('text-anchor', 'left') |
| 145 | .attr('y', '0.3em') |
| 146 | .attr('x', halfDevIcon + labelPad); |
| 147 | |
| 148 | return { |
| 149 | rect: rect, |
| 150 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 151 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 152 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 153 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 154 | return fn.classNames('node', |
| 155 | this.nodeType, |
| 156 | this.get('type'), |
| 157 | { |
| 158 | online: this.get('online') |
| 159 | } |
| 160 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 161 | }, |
| 162 | createNode: function () { |
| 163 | |
| 164 | var node = angular.extend({}, this.attributes); |
| 165 | |
| 166 | // Augment as needed... |
| 167 | node.class = this.nodeType; |
| 168 | node.svgClass = this.svgClassName(); |
| 169 | positionNode(node); |
| 170 | return node; |
| 171 | } |
| 172 | }); |
| 173 | }] |
| 174 | ); |
| 175 | })(); |