blob: 05dc4b992127ddfa532fbe4ce0e5fd16cfc9d978 [file] [log] [blame]
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001/*
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 Burrowsdfa52b02016-09-02 13:50:43 +010028 // Internal state;
29 var nearDist = 15;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010030
Steven Burrows6deb4ce2016-08-26 16:06:23 +010031 var devIconDim = 36,
32 labelPad = 10,
33 halfDevIcon = devIconDim / 2,
34 nodeLabelIndex = 1;
35
Steven Burrowsec1f45c2016-08-08 16:14:41 +010036 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 Burrowsdfa52b02016-09-02 13:50:43 +010044 // If the device contains explicit LONG/LAT data, use that to position
Steven Burrowsec1f45c2016-08-08 16:14:41 +010045 if (setLongLat(node)) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010046 // Indicate we want to update cached meta data...
Steven Burrowsec1f45c2016-08-08 16:14:41 +010047 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 Burrowsdfa52b02016-09-02 13:50:43 +0100110 ['Topo2Model', 'FnService', 'RandomService',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111 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 Burrowsdfa52b02016-09-02 13:50:43 +0100120 onEnter: function () {}, // To be overridden by sub-class
121 onExit: function () {}, // To be overridden by sub-class
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100122 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 Burrowsdfa52b02016-09-02 13:50:43 +0100128 nli = nodeLabelIndex,
129 idx = (nli < labels.length) ? nli : 0;
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100130
131 return labels[idx];
132 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100133 trimLabel: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100134 return (label && label.trim()) || '';
135 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100136 computeLabelWidth: function (el) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100137 var text = el.select('text'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100138 box = text.node().getBBox();
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100139 return box.width + labelPad * 2;
140 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100141 addLabelElements: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100142 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 Burrowsdfa52b02016-09-02 13:50:43 +0100151 };
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100152 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100153 svgClassName: function () {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100154 return fn.classNames('node',
155 this.nodeType,
156 this.get('type'),
157 {
158 online: this.get('online')
159 }
160 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100161 },
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})();