blob: d1747a31e4dd83dc2a2a331e15410f9040e78126 [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 Burrows42eb9e22017-02-06 14:20:24 +000025 var instance,
26 updateTimer;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010027
Steven Burrowsec1f45c2016-08-08 16:14:41 +010028 // default settings for force layout
29 var defaultSettings = {
30 gravity: 0.4,
31 friction: 0.7,
32 charge: {
33 // note: key is node.class
34 device: -8000,
Steven Burrows583f4be2016-11-04 14:06:50 +010035 host: -20000,
Steven Burrowsb11a8b82017-03-10 16:00:31 +000036 region: -8000,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010037 _def_: -12000
38 },
39 linkDistance: {
40 // note: key is link.type
41 direct: 100,
42 optical: 120,
Steven Burrowsb11a8b82017-03-10 16:00:31 +000043 UiEdgeLink: 100,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010044 _def_: 50
45 },
46 linkStrength: {
47 // note: key is link.type
48 // range: {0.0 ... 1.0}
Steven Burrowsec1f45c2016-08-08 16:14:41 +010049 _def_: 1.0
50 }
51 };
52
53 // configuration
54 var linkConfig = {
55 light: {
56 baseColor: '#939598',
57 inColor: '#66f',
58 outColor: '#f00'
59 },
60 dark: {
61 // TODO : theme
62 baseColor: '#939598',
63 inColor: '#66f',
64 outColor: '#f00'
65 },
66 inWidth: 12,
67 outWidth: 10
68 };
69
70 // internal state
Steven Burrowsaf96a212016-12-28 12:57:02 +000071 var nodeLock = false; // whether nodes can be dragged or not (locked)
Steven Burrows9edc7e02016-08-29 11:52:07 +010072
Steven Burrowsa3fca812016-10-14 15:11:04 -050073 // predicate that indicates when clicking is active
74 function clickEnabled() {
75 return true;
76 }
77
Steven Burrowsec1f45c2016-08-08 16:14:41 +010078 angular.module('ovTopo2')
79 .factory('Topo2LayoutService',
80 [
Steven Burrows42eb9e22017-02-06 14:20:24 +000081 '$log', '$timeout', 'WebSocketService', 'SvgUtilService', 'Topo2RegionService',
Steven Burrowsaf96a212016-12-28 12:57:02 +000082 'Topo2D3Service', 'Topo2ViewService', 'Topo2SelectService', 'Topo2ZoomService',
83 'Topo2ViewController',
Steven Burrows42eb9e22017-02-06 14:20:24 +000084 function ($log, $timeout, wss, sus, t2rs, t2d3, t2vs, t2ss, t2zs,
Steven Burrowsaf96a212016-12-28 12:57:02 +000085 ViewController) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010086
Steven Burrowsaf96a212016-12-28 12:57:02 +000087 var Layout = ViewController.extend({
Steven Burrowsbd402842017-03-08 21:30:38 +000088 init: function (svg, forceG, uplink, dim, zoomer, opts) {
Steven Burrowsaf96a212016-12-28 12:57:02 +000089 $log.debug('initialize Layout');
90 instance = this;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010091
Steven Burrowsaf96a212016-12-28 12:57:02 +000092 this.svg = svg;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010093
Steven Burrowsaf96a212016-12-28 12:57:02 +000094 // Append all the SVG Group elements to the forceG object
95 this.createForceElements();
96
97 this.uplink = uplink;
98 this.dim = dim;
99 this.zoomer = zoomer;
100
101 this.settings = angular.extend({}, defaultSettings, opts);
102
103 this.link = this.elements.linkG.selectAll('.link');
104 this.elements.linkLabelG.selectAll('.linkLabel');
105 this.node = this.elements.nodeG.selectAll('.node');
106 },
107 createForceElements: function () {
108
109 this.prevForce = this.forceG;
110
Simon Hunt95f4b422017-03-03 13:49:05 -0800111 this.forceG = d3.select('#topo2-zoomlayer')
112 .append('g').attr('class', 'topo2-force');
Steven Burrowsaf96a212016-12-28 12:57:02 +0000113
114 this.elements = {
Simon Hunt95f4b422017-03-03 13:49:05 -0800115 linkG: this.addElement(this.forceG, 'topo2-links'),
116 linkLabelG: this.addElement(this.forceG, 'topo2-linkLabels'),
117 numLinksLabels: this.addElement(this.forceG, 'topo2-numLinkLabels'),
118 nodeG: this.addElement(this.forceG, 'topo2-nodes'),
119 portLabels: this.addElement(this.forceG, 'topo2-portLabels')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000120 };
121 },
122 addElement: function (parent, className) {
123 return parent.append('g').attr('class', className);
124 },
125 settingOrDefault: function (settingName, node) {
126 var nodeType = node.get('nodeType');
127 return this.settings[settingName][nodeType] || this.settings[settingName]._def_;
128 },
129 createForceLayout: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000130
131 this.force = d3.layout.force()
132 .size(t2vs.getDimensions())
133 .gravity(this.settings.gravity)
134 .friction(this.settings.friction)
135 .charge(this.settingOrDefault.bind(this, 'charge'))
136 .linkDistance(this.settingOrDefault.bind(this, 'linkDistance'))
137 .linkStrength(this.settingOrDefault.bind(this, 'linkStrength'))
Steven Burrows6de27f42017-03-30 16:21:27 +0100138 .nodes([])
139 .links([])
Steven Burrowsaf96a212016-12-28 12:57:02 +0000140 .on("tick", this.tick.bind(this))
Steven Burrowsaf96a212016-12-28 12:57:02 +0000141 .start();
142
Steven Burrowsaf96a212016-12-28 12:57:02 +0000143 this.drag = sus.createDragBehavior(this.force,
Steven Burrows5fa057e2017-03-15 17:07:56 +0000144 function () {}, // click event is no longer handled in the drag service
Steven Burrowsaf96a212016-12-28 12:57:02 +0000145 this.atDragEnd,
146 this.dragEnabled.bind(this),
147 clickEnabled
148 );
149
150 this.update();
151 },
152 centerLayout: function () {
Simon Hunt95f4b422017-03-03 13:49:05 -0800153 d3.select('#topo2-zoomlayer').attr('data-layout', t2rs.model.get('id'));
Steven Burrowsaf96a212016-12-28 12:57:02 +0000154
Simon Hunt95f4b422017-03-03 13:49:05 -0800155 var zoomer = d3.select('#topo2-zoomlayer').node().getBBox(),
Steven Burrowsaf96a212016-12-28 12:57:02 +0000156 layoutBBox = this.forceG.node().getBBox(),
157 scale = (zoomer.height - 150) / layoutBBox.height,
158 x = (zoomer.width / 2) - ((layoutBBox.x + layoutBBox.width / 2) * scale),
159 y = (zoomer.height / 2) - ((layoutBBox.y + layoutBBox.height / 2) * scale);
160
161 t2zs.panAndZoom([x, y], scale, 1000);
162 },
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700163 setLinkPosition: function (link) {
164 link.setPosition.bind(link)();
165 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000166 tick: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000167
Steven Burrowsaf96a212016-12-28 12:57:02 +0000168 this.node
169 .attr({
170 transform: function (d) {
171 var dx = isNaN(d.x) ? 0 : d.x,
172 dy = isNaN(d.y) ? 0 : d.y;
173 return sus.translate(dx, dy);
174 }
175 });
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700176
177 this.link
178 .each(this.setLinkPosition)
179 .attr("x1", function (d) { return d.get('position').x1; })
180 .attr("y1", function (d) { return d.get('position').y1; })
181 .attr("x2", function (d) { return d.get('position').x2; })
182 .attr("y2", function (d) { return d.get('position').y2; });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000183 },
184
185 start: function () {
186 this.force.start();
187 },
188 update: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000189
190 if (updateTimer) {
191 $timeout.cancel(updateTimer);
192 }
193 updateTimer = $timeout(this._update.bind(this), 150);
194 },
195 _update: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000196 this.updateNodes();
197 this.updateLinks();
Steven Burrows3d8d9332017-03-30 15:05:52 +0100198 this.force.start();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000199 },
200 updateNodes: function () {
201 var regionNodes = t2rs.regionNodes();
202
203 // select all the nodes in the layout:
204 this.node = this.elements.nodeG.selectAll('.node')
205 .data(regionNodes, function (d) { return d.get('id'); });
206
207 var entering = this.node.enter()
208 .append('g')
209 .attr({
210 id: function (d) { return sus.safeId(d.get('id')); },
211 class: function (d) { return d.svgClassName(); },
212 transform: function (d) {
213 // Need to guard against NaN here ??
214 return sus.translate(d.node.x, d.node.y);
215 },
216 opacity: 0
217 })
218 .call(this.drag)
219 .transition()
220 .attr('opacity', 1);
221
222 entering.filter('.device').each(t2d3.nodeEnter);
223 entering.filter('.sub-region').each(t2d3.nodeEnter);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000224 entering.filter('.host').each(t2d3.nodeEnter);
225 entering.filter('.peer-region').each(t2d3.nodeEnter);
Steven Burrows3d8d9332017-03-30 15:05:52 +0100226
227 this.force.nodes(regionNodes);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000228 },
229 updateLinks: function () {
230
231 var regionLinks = t2rs.regionLinks();
232
233 this.link = this.elements.linkG.selectAll('.link')
Steven Burrows42eb9e22017-02-06 14:20:24 +0000234 .data(regionLinks, function (d) { return d.get('key'); });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000235
236 // operate on entering links:
237 var entering = this.link.enter()
238 .append('line')
239 .call(this.calcPosition)
240 .attr({
241 x1: function (d) { return d.get('position').x1; },
242 y1: function (d) { return d.get('position').y1; },
243 x2: function (d) { return d.get('position').x2; },
244 y2: function (d) { return d.get('position').y2; },
245 stroke: linkConfig.light.inColor,
246 'stroke-width': linkConfig.inWidth
247 });
248
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000249 entering.each(t2d3.nodeEnter);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000250
251 // operate on exiting links:
252 this.link.exit()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000253 .attr('stroke-dasharray', '3 3')
254 .style('opacity', 0.5)
Steven Burrowsaf96a212016-12-28 12:57:02 +0000255 .transition()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000256 .duration(1500)
257 .attr({
258 'stroke-dasharray': '3 12',
259 })
Steven Burrowsaf96a212016-12-28 12:57:02 +0000260 .style('opacity', 0.0)
261 .remove();
Steven Burrows3d8d9332017-03-30 15:05:52 +0100262
263 this.force.links(regionLinks);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000264 },
265 calcPosition: function () {
266 var lines = this;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000267 lines.each(function (d) {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700268 d.setPosition.bind(d)();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000269 });
270 },
271 sendUpdateMeta: function (d, clearPos) {
272 var metaUi = {},
273 ll;
274
275 // if we are not clearing the position data (unpinning),
276 // attach the x, y, (and equivalent longitude, latitude)...
277 if (!clearPos) {
278 ll = d.lngLatFromCoord([d.x, d.y]);
279 metaUi = {
280 x: d.x,
281 y: d.y,
282 equivLoc: {
283 lng: ll[0],
284 lat: ll[1]
285 }
286 };
287 }
288 d.metaUi = metaUi;
289 wss.sendEvent('updateMeta2', {
290 id: d.get('id'),
291 class: d.get('class'),
292 memento: metaUi
293 });
294 },
295 setDimensions: function () {
296 if (this.force) {
297 this.force.size(t2vs.getDimensions());
298 }
299 },
300 dragEnabled: function () {
301 var ev = d3.event.sourceEvent;
302 // nodeLock means we aren't allowing nodes to be dragged...
303 return !nodeLock && !this.zoomingOrPanning(ev);
304 },
305 zoomingOrPanning: function (ev) {
306 return ev.metaKey || ev.altKey;
307 },
308 atDragEnd: function (d) {
309 // once we've finished moving, pin the node in position
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000310 d.fix(true);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000311 instance.sendUpdateMeta(d);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000312 t2ss.clickConsumed(true);
313 },
314 transitionDownRegion: function () {
315
316 this.prevForce.transition()
317 .duration(1500)
318 .style('opacity', 0)
319 .remove();
320
321 this.forceG
322 .style('opacity', 0)
323 .transition()
324 .delay(500)
325 .duration(500)
326 .style('opacity', 1);
327 },
328 transitionUpRegion: function () {
329 this.prevForce.transition()
330 .duration(1000)
331 .style('opacity', 0)
332 .remove();
333
334 this.forceG
335 .style('opacity', 0)
336 .transition()
337 .delay(500)
338 .duration(500)
339 .style('opacity', 1);
340 }
341 });
342
343 function getInstance(svg, forceG, uplink, dim, zoomer, opts) {
344 return instance || new Layout(svg, forceG, uplink, dim, zoomer, opts);
345 }
346
Steven Burrowsbd402842017-03-08 21:30:38 +0000347 return getInstance();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100348 }
349 ]
350 );
351})();