GUI -- [ONOS-282] - clean up drag behavior API.
Change-Id: I562eedd6f075afdd8d35e109fda9c6cd1d594d82
Showing
2 changed files
with
47 additions
and
20 deletions
... | @@ -23,24 +23,41 @@ | ... | @@ -23,24 +23,41 @@ |
23 | (function (onos) { | 23 | (function (onos) { |
24 | 'use strict'; | 24 | 'use strict'; |
25 | 25 | ||
26 | - function createDragBehavior(force, selectCb, atDragEnd, requireMeta) { | 26 | + function isF(f) { |
27 | + return $.isFunction(f) ? f : null; | ||
28 | + } | ||
29 | + | ||
30 | + function createDragBehavior(force, selectCb, atDragEnd, enabled) { | ||
27 | var draggedThreshold = d3.scale.linear() | 31 | var draggedThreshold = d3.scale.linear() |
28 | .domain([0, 0.1]) | 32 | .domain([0, 0.1]) |
29 | .range([5, 20]) | 33 | .range([5, 20]) |
30 | .clamp(true), | 34 | .clamp(true), |
31 | - drag; | 35 | + drag, |
36 | + fSel = isF(selectCb), | ||
37 | + fEnd = isF(atDragEnd), | ||
38 | + fEnb = isF(enabled), | ||
39 | + bad = []; | ||
40 | + | ||
41 | + function naf(what) { | ||
42 | + return 'd3util.createDragBehavior(): '+ what + ' is not a function'; | ||
43 | + } | ||
32 | 44 | ||
33 | - // TODO: better validation of parameters | 45 | + if (!fSel) { |
34 | - if (!$.isFunction(selectCb)) { | 46 | + bad.push(naf('selectCb')); |
35 | - alert('d3util.createDragBehavior(): selectCb is not a function') | ||
36 | } | 47 | } |
37 | - if (!$.isFunction(atDragEnd)) { | 48 | + if (!fEnd) { |
38 | - alert('d3util.createDragBehavior(): atDragEnd is not a function') | 49 | + bad.push(naf('atDragEnd')); |
39 | } | 50 | } |
40 | - if (!$.isFunction(requireMeta)) { | 51 | + if (!fEnb) { |
41 | - alert('d3util.createDragBehavior(): requireMeta is not a function') | 52 | + bad.push(naf('enabled')); |
42 | } | 53 | } |
43 | 54 | ||
55 | + if (bad.length) { | ||
56 | + alert(bad.join('\n')); | ||
57 | + return null; | ||
58 | + } | ||
59 | + | ||
60 | + | ||
44 | function dragged(d) { | 61 | function dragged(d) { |
45 | var threshold = draggedThreshold(force.alpha()), | 62 | var threshold = draggedThreshold(force.alpha()), |
46 | dx = d.oldX - d.px, | 63 | dx = d.oldX - d.px, |
... | @@ -54,7 +71,7 @@ | ... | @@ -54,7 +71,7 @@ |
54 | drag = d3.behavior.drag() | 71 | drag = d3.behavior.drag() |
55 | .origin(function(d) { return d; }) | 72 | .origin(function(d) { return d; }) |
56 | .on('dragstart', function(d) { | 73 | .on('dragstart', function(d) { |
57 | - if (requireMeta() ^ !d3.event.sourceEvent.metaKey) { | 74 | + if (enabled()) { |
58 | d3.event.sourceEvent.stopPropagation(); | 75 | d3.event.sourceEvent.stopPropagation(); |
59 | 76 | ||
60 | d.oldX = d.x; | 77 | d.oldX = d.x; |
... | @@ -65,7 +82,7 @@ | ... | @@ -65,7 +82,7 @@ |
65 | } | 82 | } |
66 | }) | 83 | }) |
67 | .on('drag', function(d) { | 84 | .on('drag', function(d) { |
68 | - if (requireMeta() ^ !d3.event.sourceEvent.metaKey) { | 85 | + if (enabled()) { |
69 | d.px = d3.event.x; | 86 | d.px = d3.event.x; |
70 | d.py = d3.event.y; | 87 | d.py = d3.event.y; |
71 | if (dragged(d)) { | 88 | if (dragged(d)) { | ... | ... |
... | @@ -206,7 +206,8 @@ | ... | @@ -206,7 +206,8 @@ |
206 | showHosts = false, | 206 | showHosts = false, |
207 | showOffline = true, | 207 | showOffline = true, |
208 | useDetails = true, | 208 | useDetails = true, |
209 | - haveDetails = false; | 209 | + haveDetails = false, |
210 | + dragAllowed = true; | ||
210 | 211 | ||
211 | // constants | 212 | // constants |
212 | var hoverModeAll = 1, | 213 | var hoverModeAll = 1, |
... | @@ -2642,14 +2643,13 @@ | ... | @@ -2642,14 +2643,13 @@ |
2642 | svg = view.$div.append('svg').attr('viewBox', viewBox); | 2643 | svg = view.$div.append('svg').attr('viewBox', viewBox); |
2643 | setSize(svg, view); | 2644 | setSize(svg, view); |
2644 | 2645 | ||
2646 | + // load glyphs and filters... | ||
2645 | loadGlyphs(svg); | 2647 | loadGlyphs(svg); |
2648 | + d3u.appendGlow(svg); | ||
2646 | 2649 | ||
2647 | zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer'); | 2650 | zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer'); |
2648 | setupZoomPan(); | 2651 | setupZoomPan(); |
2649 | 2652 | ||
2650 | - // add blue glow filter to svg layer | ||
2651 | - d3u.appendGlow(zoomPanContainer); | ||
2652 | - | ||
2653 | // group for the topology | 2653 | // group for the topology |
2654 | topoG = zoomPanContainer.append('g') | 2654 | topoG = zoomPanContainer.append('g') |
2655 | .attr('id', 'topo-G') | 2655 | .attr('id', 'topo-G') |
... | @@ -2691,6 +2691,14 @@ | ... | @@ -2691,6 +2691,14 @@ |
2691 | } | 2691 | } |
2692 | } | 2692 | } |
2693 | 2693 | ||
2694 | + // predicate that indicates when dragging is active | ||
2695 | + function dragEnabled() { | ||
2696 | + // meta key pressed means we are zooming/panning (so disable drag) | ||
2697 | + return dragAllowed && !d3.event.sourceEvent.metaKey; | ||
2698 | + // dragAllowed will be set false when we are in oblique view | ||
2699 | + // or when we 'lock' node positions | ||
2700 | + } | ||
2701 | + | ||
2694 | // set up the force layout | 2702 | // set up the force layout |
2695 | network.force = d3.layout.force() | 2703 | network.force = d3.layout.force() |
2696 | .size(forceDim) | 2704 | .size(forceDim) |
... | @@ -2704,10 +2712,16 @@ | ... | @@ -2704,10 +2712,16 @@ |
2704 | .on('tick', tick); | 2712 | .on('tick', tick); |
2705 | 2713 | ||
2706 | network.drag = d3u.createDragBehavior(network.force, | 2714 | network.drag = d3u.createDragBehavior(network.force, |
2707 | - selectCb, atDragEnd, panZoom); | 2715 | + selectCb, atDragEnd, dragEnabled); |
2716 | + | ||
2708 | 2717 | ||
2709 | // create mask layer for when we lose connection to server. | 2718 | // create mask layer for when we lose connection to server. |
2710 | // TODO: this should be part of the framework | 2719 | // TODO: this should be part of the framework |
2720 | + | ||
2721 | + function para(sel, text) { | ||
2722 | + sel.append('p').text(text); | ||
2723 | + } | ||
2724 | + | ||
2711 | mask = view.$div.append('div').attr('id','topo-mask'); | 2725 | mask = view.$div.append('div').attr('id','topo-mask'); |
2712 | para(mask, 'Oops!'); | 2726 | para(mask, 'Oops!'); |
2713 | para(mask, 'Web-socket connection to server closed...'); | 2727 | para(mask, 'Web-socket connection to server closed...'); |
... | @@ -2731,10 +2745,6 @@ | ... | @@ -2731,10 +2745,6 @@ |
2731 | }) | 2745 | }) |
2732 | } | 2746 | } |
2733 | 2747 | ||
2734 | - function para(sel, text) { | ||
2735 | - sel.append('p').text(text); | ||
2736 | - } | ||
2737 | - | ||
2738 | 2748 | ||
2739 | function load(view, ctx, flags) { | 2749 | function load(view, ctx, flags) { |
2740 | // resize, in case the window was resized while we were not loaded | 2750 | // resize, in case the window was resized while we were not loaded | ... | ... |
-
Please register or login to post a comment