9
9
10
10
'use strict' ;
11
11
var polygon = require ( '../../lib/polygon' ) ;
12
+ var axes = require ( './axes' ) ;
12
13
var filteredPolygon = polygon . filter ;
14
+ var polygonTester = polygon . tester ;
13
15
var BENDPX = 1.5 ; // max pixels off straight before a line counts as bent
14
16
17
+ function getAxId ( ax ) { return ax . _id ; }
18
+
15
19
module . exports = function prepSelect ( e , startX , startY , dragOptions , mode ) {
16
20
var plot = dragOptions . plotinfo . plot ,
17
21
dragBBox = dragOptions . element . getBoundingClientRect ( ) ,
@@ -21,7 +25,10 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
21
25
y1 = y0 ,
22
26
path0 = 'M' + x0 + ',' + y0 ,
23
27
pw = dragOptions . xaxes [ 0 ] . _length ,
24
- ph = dragOptions . yaxes [ 0 ] . _length ;
28
+ ph = dragOptions . yaxes [ 0 ] . _length ,
29
+ xAxisIds = dragOptions . xaxes . map ( getAxId ) ,
30
+ yAxisIds = dragOptions . yaxes . map ( getAxId ) ;
31
+
25
32
if ( mode === 'lasso' ) {
26
33
var pts = filteredPolygon ( [ [ x0 , y0 ] ] , BENDPX ) ;
27
34
}
@@ -33,28 +40,63 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
33
40
. attr ( 'class' , function ( d ) { return 'select-outline select-outline-' + d ; } )
34
41
. attr ( 'd' , path0 + 'Z' ) ;
35
42
43
+ // find the traces to search for selection points
44
+ var searchTraces = [ ] ,
45
+ gd = dragOptions . gd ,
46
+ i ,
47
+ cd ,
48
+ trace ,
49
+ searchInfo ,
50
+ selection = [ ] ;
51
+ for ( i = 0 ; i < gd . calcdata . length ; i ++ ) {
52
+ cd = gd . calcdata [ i ] ;
53
+ trace = cd [ 0 ] . trace ;
54
+ if ( ! trace . _module || ! trace . _module . selectPoints ) continue ;
55
+
56
+ if ( xAxisIds . indexOf ( trace . xaxis ) === - 1 ) continue ;
57
+ if ( yAxisIds . indexOf ( trace . yaxis ) === - 1 ) continue ;
58
+
59
+ searchTraces . push ( {
60
+ selectPoints : trace . _module . selectPoints ,
61
+ cd : cd ,
62
+ xaxis : axes . getFromId ( gd , trace . xaxis ) ,
63
+ yaxis : axes . getFromId ( gd , trace . yaxis )
64
+ } ) ;
65
+ }
66
+
36
67
dragOptions . moveFn = function ( dx0 , dy0 ) {
68
+ var poly ;
37
69
x1 = Math . max ( 0 , Math . min ( pw , dx0 + x0 ) ) ;
38
70
y1 = Math . max ( 0 , Math . min ( ph , dy0 + y0 ) ) ;
39
71
40
72
if ( mode === 'select' ) {
73
+ poly = polygonTester ( [ [ x0 , y0 ] , [ x0 , y1 ] , [ x1 , y1 ] , [ x1 , y0 ] ] ) ;
41
74
outlines . attr ( 'd' , path0 + 'H' + x1 + 'V' + y1 + 'H' + x0 + 'Z' ) ;
42
75
}
43
76
else if ( mode === 'lasso' ) {
44
77
pts . addPt ( [ x1 , y1 ] ) ;
45
- outlines . attr ( 'd' , 'M' + pts . filtered . join ( 'L' ) ) ;
78
+ poly = polygonTester ( pts . filtered ) ;
79
+ outlines . attr ( 'd' , 'M' + pts . filtered . join ( 'L' ) + 'Z' ) ;
46
80
}
47
81
48
- // TODO - actual selection and dimming!
82
+ selection = [ ] ;
83
+ for ( i = 0 ; i < searchTraces . length ; i ++ ) {
84
+ searchInfo = searchTraces [ i ] ;
85
+ [ ] . push . apply ( selection , searchInfo . selectPoints ( searchInfo , poly ) ) ;
86
+ }
87
+ dragOptions . gd . emit ( 'plotly_selecting' , { points : selection } ) ;
49
88
} ;
50
89
51
90
dragOptions . doneFn = function ( dragged , numclicks ) {
52
91
if ( ! dragged && numclicks === 2 ) dragOptions . doubleclick ( ) ;
53
92
else {
54
- // TODO - select event
93
+ dragOptions . gd . emit ( 'plotly_selected' , { points : selection } ) ;
55
94
}
56
95
outlines . remove ( ) ;
57
- // TODO - remove dimming
96
+ for ( i = 0 ; i < searchTraces . length ; i ++ ) {
97
+ searchInfo = searchTraces [ i ] ;
98
+ searchInfo . selectPoints ( searchInfo , false ) ;
99
+ }
58
100
} ;
59
101
} ;
60
102
0 commit comments