blob: 51651fa8000b906c6d29c9f7efc9d592d8b62347 [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
26 function createDragBehavior(force, selectCb, atDragEnd) {
27 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) {
54 d.oldX = d.x;
55 d.oldY = d.y;
56 d.dragged = false;
57 d.fixed |= 2;
58 })
59 .on('drag', function(d) {
60 d.px = d3.event.x;
61 d.py = d3.event.y;
62 if (dragged(d)) {
63 if (!force.alpha()) {
64 force.alpha(.025);
65 }
66 }
67 })
68 .on('dragend', function(d) {
69 if (!dragged(d)) {
70 // consider this the same as a 'click' (selection of node)
71 selectCb(d, this); // TODO: set 'this' context instead of param
72 }
73 d.fixed &= ~6;
74
75 // hook at the end of a drag gesture
76 atDragEnd(d, this); // TODO: set 'this' context instead of param
77 });
78
79 return drag;
80 }
81
82 function appendGlow(svg) {
83 // TODO: parameterize color
84
85 var glow = svg.append('filter')
86 .attr('x', '-50%')
87 .attr('y', '-50%')
88 .attr('width', '200%')
89 .attr('height', '200%')
90 .attr('id', 'blue-glow');
91
92 glow.append('feColorMatrix')
93 .attr('type', 'matrix')
94 .attr('values', '0 0 0 0 0 ' +
95 '0 0 0 0 0 ' +
96 '0 0 0 0 .7 ' +
97 '0 0 0 1 0 ');
98
99 glow.append('feGaussianBlur')
100 .attr('stdDeviation', 3)
101 .attr('result', 'coloredBlur');
102
103 glow.append('feMerge').selectAll('feMergeNode')
104 .data(['coloredBlur', 'SourceGraphic'])
105 .enter().append('feMergeNode')
106 .attr('in', String);
107 }
108
109 // === register the functions as a library
110 onos.ui.addLib('d3util', {
111 createDragBehavior: createDragBehavior,
112 appendGlow: appendGlow
113 });
114
115}(ONOS));