blob: 2b91781df0b25bda778cf85d4a8bbaf53a370af5 [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
Steven Burrows37549ee2016-09-21 14:41:39 +010025 var randomService, ps;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010026 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 Burrows37549ee2016-09-21 14:41:39 +0100110 ['Topo2Model', 'FnService', 'RandomService', 'Topo2PrefsService',
111 function (Model, _fn_, _RandomService_, _ps_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100112
113 randomService = _RandomService_;
114 fn = _fn_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100115 ps = _ps_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100116
117 return Model.extend({
118 initialize: function () {
119 this.node = this.createNode();
120 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100121 onEnter: function () {}, // To be overridden by sub-class
122 onExit: function () {}, // To be overridden by sub-class
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100123 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 Burrows37549ee2016-09-21 14:41:39 +0100129 nli = ps.get('dlbls'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100130 idx = (nli < labels.length) ? nli : 0;
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100131
132 return labels[idx];
133 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100134 trimLabel: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100135 return (label && label.trim()) || '';
136 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100137 computeLabelWidth: function (el) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100138 var text = el.select('text'),
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100139 box = text.node().getBBox();
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100140 return box.width + labelPad * 2;
141 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100142 addLabelElements: function (label) {
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100143 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 Burrowsdfa52b02016-09-02 13:50:43 +0100152 };
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100153 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100154 iconBox: function(dim, labelWidth) {
155 return {
156 x: -dim / 2,
157 y: -dim / 2,
158 width: dim + labelWidth,
159 height: dim
160 };
161 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100162 svgClassName: function () {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100163 return fn.classNames('node',
164 this.nodeType,
165 this.get('type'),
166 {
167 online: this.get('online')
168 }
169 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100170 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100171 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 Burrowsec1f45c2016-08-08 16:14:41 +0100186 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})();