-
Notifications
You must be signed in to change notification settings - Fork 1
/
d3.lasso.js
228 lines (190 loc) · 6.32 KB
/
d3.lasso.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* A plugin for d3.js that implements a lasso tool
*
* @author Pavlos Polianidis
*/
d3.lasso = function (opts) {
/**
* Returns the initial offset of the target element
*/
var getInitialOffset = function () {
var defaultOffset = { top: 0, left: 0 },
domEl = $(el),
position = domEl.css('position');
return position === 'relative' || position === 'absolute'
? domEl.offset()
: defaultOffset;
};
var el = opts.target,
$el = $(el),
appendTo = 'html>body',
clickX, clickY,
moveX, moveY,
newX, newY,
width, height,
$selection = $('<div>').addClass('selection-box'),
elLocation = getInitialOffset(),
leftBtnClicked = false,
dragOn = false;
//#region event callbacks
var
/**
* mousedown callback
*/
dragStarted = function (event) {
//var event = d3.event;
event.preventDefault();
if (!isLeftBtnClicked(event)) {
return;
}
leftBtnClicked = true;
// consider the current offset of this element
clickX = event.pageX; // + elLocation.left;
clickY = event.pageY; // + elLocation.top;
updateCss(0, 0, clickY, clickX, '0');
$selection.appendTo(appendTo);
},
/**
* mousemove callback
*/
drag = function (event) {
//var event = d3.event;
event.preventDefault();
if (!leftBtnClicked) {
return;
}
dragOn = true;
// consider the current offset of this element
moveX = event.pageX; // + elLocation.left;
moveY = event.pageY; // + elLocation.top;
width = Math.abs(moveX - clickX);
height = Math.abs(moveY - clickY);
// consider the drag direction
newX = (moveX < clickX) ? (clickX - width) : clickX;
newY = (moveY < clickY) ? (clickY - height) : clickY;
var padding = getPadding();
updateCss(width, height, newY + padding.y, newX + padding.x, '1px');
},
/**
* mousemove mouseup
*/
dragend = function (event) {
// var event = d3.event;
event.preventDefault();
event.stopPropagation();
if (!isLeftBtnClicked(event)) {
return;
}
$selection.remove();
// did we have a mousemove event earlier?
if (!dragOn) {
return;
}
leftBtnClicked = false;
dragOn = false;
opts.callback({
start: getLassoRelativeStartPoint(),
points: getLassoCornerPoints(),
width: moveX - clickX,
height: moveY - clickY,
ctrlPressed: event.ctrlKey
});
};
//#endregion
//#region private methods
var
/**
* Updates the css of the selection box
*
* @param {number} w - The new width of the seleciton box
* @param {number} h - The new height of the seleciton box
* @param {number} top - The new top position of the seleciton box
* @param {number} left - The new left position of the seleciton box
*/
updateCss = function (w, h, top, left, borderWidth) {
$selection.css({
'width': w,
'height': h,
'top': top,
'left': left,
'border-width': borderWidth
});
},
/**
* Examines if the left mouse btn was clicked
*/
isLeftBtnClicked = function (event) {
var btnClicked = event.which,
NO_BTN = 0,
MIDDLE_BTN = 2,
RIGHT_BTN = 3;
if (btnClicked === MIDDLE_BTN
|| btnClicked === RIGHT_BTN
|| btnClicked === NO_BTN) {
return false;
}
return true;
},
/**
* Returns the relative to the parent node start point of the rectangle
*/
getLassoRelativeStartPoint = function() {
return {
x: clickX - elLocation.left,
y: clickY - elLocation.top
};
},
/**
* Returns the relative to the parent node end point of the rectangle
*/
getLassoRelativeEndPoint = function() {
return {
x: moveX-elLocation.left,
y: moveY-elLocation.top
};
},
/**
* Returns the width of the rectangle
*/
getLassoWidth = function() {
return moveX - clickX;
},
/**
* Returns the width of the rectangle
*/
getLassoHeight = function() {
return moveY - clickY;
},
/**
* Returns the four corner points of the rectangle
*/
getLassoCornerPoints = function() {
var w = getLassoWidth(),
h = getLassoHeight(),
startPoint = getLassoRelativeStartPoint();
return [
{ x: startPoint.x, y: startPoint.y }, ,
{ x: startPoint.x + w, y: startPoint.y },
{ x: startPoint.x, y: startPoint.y + h },
{ x: startPoint.x + w, y: startPoint.y + h }
];
},
/**
*returns a small space so that the mouseup event is fired on the target element
*/
getPadding = function () {
var padding = {};
padding.x = moveX < clickX
? 2
: -2;
padding.y = moveY < clickY
? 2
: -2;
return padding;
};
//#endregion
// attach the events to the target element
$el.on('dragstart.lasso', function(e, eventInfo) { return dragStarted(eventInfo); })
.on('dragmove.lasso', function (e, eventInfo) { return drag(eventInfo); })
.on('dragend.lasso', function(e, eventInfo) { return dragend(eventInfo); });
};