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