blob: e647a37a316a409e367e2fa10800cd43ec3c6ff5 [file] [log] [blame]
Simon Hunt1a9eff92014-11-07 11:06:34 -08001/*
2 * Copyright 2014 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 Utility functions for D3 visualizations.
19
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Paul Greysonfcba0e82014-11-13 10:21:16 -080026 function createDragBehavior(force, selectCb, atDragEnd, requireMeta) {
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 var draggedThreshold = d3.scale.linear()
28 .domain([0, 0.1])
29 .range([5, 20])
30 .clamp(true),
31 drag;
32
33 // TODO: better validation of parameters
34 if (!$.isFunction(selectCb)) {
35 alert('d3util.createDragBehavior(): selectCb is not a function')
36 }
37 if (!$.isFunction(atDragEnd)) {
38 alert('d3util.createDragBehavior(): atDragEnd is not a function')
39 }
40
41 function dragged(d) {
42 var threshold = draggedThreshold(force.alpha()),
43 dx = d.oldX - d.px,
44 dy = d.oldY - d.py;
45 if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) {
46 d.dragged = true;
47 }
48 return d.dragged;
49 }
50
51 drag = d3.behavior.drag()
52 .origin(function(d) { return d; })
53 .on('dragstart', function(d) {
Paul Greysonfcba0e82014-11-13 10:21:16 -080054 if (requireMeta ^ !d3.event.sourceEvent.metaKey) {
55 d3.event.sourceEvent.stopPropagation();
56
57 d.oldX = d.x;
58 d.oldY = d.y;
59 d.dragged = false;
60 d.fixed |= 2;
61 d.dragStarted = true;
62 }
Simon Hunt1a9eff92014-11-07 11:06:34 -080063 })
64 .on('drag', function(d) {
Paul Greysonfcba0e82014-11-13 10:21:16 -080065 if (requireMeta ^ !d3.event.sourceEvent.metaKey) {
66 d.px = d3.event.x;
67 d.py = d3.event.y;
68 if (dragged(d)) {
69 if (!force.alpha()) {
70 force.alpha(.025);
71 }
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 }
73 }
74 })
75 .on('dragend', function(d) {
Paul Greysonfcba0e82014-11-13 10:21:16 -080076 if (d.dragStarted) {
77 d.dragStarted = false;
78 if (!dragged(d)) {
79 // consider this the same as a 'click' (selection of node)
80 selectCb(d, this); // TODO: set 'this' context instead of param
81 }
82 d.fixed &= ~6;
Simon Hunt1a9eff92014-11-07 11:06:34 -080083
Paul Greysonfcba0e82014-11-13 10:21:16 -080084 // hook at the end of a drag gesture
85 atDragEnd(d, this); // TODO: set 'this' context instead of param
86 }
Simon Hunt1a9eff92014-11-07 11:06:34 -080087 });
88
89 return drag;
90 }
91
92 function appendGlow(svg) {
93 // TODO: parameterize color
94
95 var glow = svg.append('filter')
96 .attr('x', '-50%')
97 .attr('y', '-50%')
98 .attr('width', '200%')
99 .attr('height', '200%')
100 .attr('id', 'blue-glow');
101
102 glow.append('feColorMatrix')
103 .attr('type', 'matrix')
104 .attr('values', '0 0 0 0 0 ' +
105 '0 0 0 0 0 ' +
106 '0 0 0 0 .7 ' +
107 '0 0 0 1 0 ');
108
109 glow.append('feGaussianBlur')
110 .attr('stdDeviation', 3)
111 .attr('result', 'coloredBlur');
112
113 glow.append('feMerge').selectAll('feMergeNode')
114 .data(['coloredBlur', 'SourceGraphic'])
115 .enter().append('feMergeNode')
116 .attr('in', String);
117 }
118
119 // === register the functions as a library
120 onos.ui.addLib('d3util', {
121 createDragBehavior: createDragBehavior,
122 appendGlow: appendGlow
123 });
124
125}(ONOS));