forked from karolyi/ui-selectableScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectableScroll.js
396 lines (376 loc) · 15.8 KB
/
selectableScroll.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* This module is an extension to jQuery ui selectable, which scrolls when you try to select
* outside the container, and the scrollables are not fitting it originally.
*
* Created by László Károlyi <http://linkedin.com/in/karolyi>
* with the GPLv3 License: http://opensource.org/licenses/GPL-3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function ($) {
$.widget('ui.selectableScroll', $.ui.selectable, {
options: {
scrollSnapX: 5, // When the selection is that pixels near to the top/bottom edges, start to scroll
scrollSnapY: 5, // When the selection is that pixels near to the side edges, start to scroll
scrollAmount: 25, // In pixels
scrollIntervalTime: 100 // In milliseconds
},
/**
* This is a slightly modified version of the original _create function, we just add the element's relative positions too
* @return {null} no return value
*/
_create: function () {
var selectees,
that = this;
this.element.addClass("ui-selectable");
this.dragged = false;
this.helperClasses = ['no-top', 'no-right', 'no-bottom', 'no-left'];
// cache selectee children based on filter
this.refresh = function() {
var elementOffset = this.element.offset();
var scrollLeft = this.element.prop('scrollLeft');
var scrollTop = this.element.prop('scrollTop');
selectees = $(that.options.filter, that.element[0]);
selectees.addClass("ui-selectee");
selectees.each(function() {
var $this = $(this),
pos = $this.offset();
$.data(this, "selectable-item", {
element: this,
$element: $this,
left: pos.left,
top: pos.top,
right: pos.left + $this.outerWidth(),
bottom: pos.top + $this.outerHeight(),
relative: { // Relative positions according to the element's 0.0
left: pos.left - elementOffset.left + scrollLeft,
top: pos.top - elementOffset.top + scrollTop,
right: pos.left - elementOffset.left + scrollLeft + $this.outerWidth(),
bottom: pos.top - elementOffset.top + scrollTop + $this.outerHeight()
},
startselected: false,
selected: $this.hasClass("ui-selected"),
selecting: $this.hasClass("ui-selecting"),
unselecting: $this.hasClass("ui-unselecting")
});
});
};
this.refresh();
this.selectees = selectees.addClass("ui-selectee");
this._mouseInit();
this.helper = $("<div class='ui-selectable-helper'></div>");
},
/**
* By starting dragging, calculate the element's current scroll states, relative to the elements 0.0 offset
* @param {object} event the mousedown event
* @return {boolean} The parent's _mouseStart return value
*/
_mouseStart: function (event) {
var retValue = this._super(event);
this.lastDragEvent = null;
this.scrollInfo = {
elementOffset: this.element.offset(), // The element's 0.0 offset related to the document element
scrollHeight: this.element.prop('scrollHeight'), // The maximum scrollable height (visible height + scrollTop)
scrollWidth: this.element.prop('scrollWidth'), // The maximum scrollable height (visible width + scrollLeft)
elementHeight: this.element.height(), // The visible height
elementWidth: this.element.width() // The visible width
};
this.scrollInfo.dragStartXPos = event.pageX - this.scrollInfo.elementOffset.left + this.element.prop('scrollLeft'); // Relative to element's 0
this.scrollInfo.dragStartYPos = event.pageY - this.scrollInfo.elementOffset.top + this.element.prop('scrollTop'); // Relative to element's 0
this.scrollIntervalId = null;
return retValue;
},
/**
* Calculate/redraw the helper lasso, keep the helper within the scrolled element
* @param {object} options The object containing the relative positions of the selected rectangle
* @return {null} no return value
*/
_updateHelper: function (options) {
var that = this;
var x1, y1, x2, y2; // Absolute positions for the lasso helper
var lassoClassesArray = [];
if (options.x1 - options.scrollLeft < 0) {
lassoClassesArray.push('no-left');
x1 = this.scrollInfo.elementOffset.left;
} else {
x1 = this.scrollInfo.elementOffset.left + options.x1 - options.scrollLeft;
}
if (options.y1 - options.scrollTop < 0) {
lassoClassesArray.push('no-top');
y1 = this.scrollInfo.elementOffset.top;
} else {
y1 = this.scrollInfo.elementOffset.top + options.y1 - options.scrollTop;
}
if (options.x2 - options.scrollLeft > this.scrollInfo.elementWidth) {
lassoClassesArray.push('no-right');
x2 = this.scrollInfo.elementOffset.left + this.scrollInfo.elementWidth;
} else {
x2 = this.scrollInfo.elementOffset.left + options.x2 - options.scrollLeft;
}
if (options.y2 - options.scrollTop > this.scrollInfo.elementHeight) {
lassoClassesArray.push('no-bottom');
y2 = this.scrollInfo.elementOffset.top + this.scrollInfo.elementHeight;
} else {
y2 = this.scrollInfo.elementOffset.top + options.y2 - options.scrollTop;
}
for (var counter = 0; counter < this.helperClasses.length; counter++) {
var className = this.helperClasses[counter];
if ($.inArray(className, lassoClassesArray) !== -1) {
this.helper.addClass(className);
} else {
this.helper.removeClass(className);
}
}
this.helper.css({
left: x1,
top: y1,
width: x2 - x1,
height: y2 - y1
});
},
/**
* If the mouse is near to the edges of the element's area, scroll
* @return {object} The new scrollLeft and scrollTop values, and a boolean if the element should keep scrolling
*/
_scrollIfNeeded: function (options) {
var scrollLeft = this.element.prop('scrollLeft');
var scrollTop = this.element.prop('scrollTop');
var keepScrolling = false;
// Scroll if close to edges or over them
if (this.lastDragEvent.pageX - this.scrollInfo.elementOffset.left < this.options.scrollSnapX && scrollLeft > 0) {
scrollLeft = scrollLeft < this.options.scrollAmount ? 0 : scrollLeft - this.options.scrollAmount;
this.element.prop('scrollLeft', scrollLeft);
keepScrolling = true;
}
if (this.lastDragEvent.pageY - this.scrollInfo.elementOffset.top < this.options.scrollSnapY && scrollTop > 0) {
scrollTop = scrollTop < this.options.scrollAmount ? 0 : scrollTop - this.options.scrollAmount;
this.element.prop('scrollTop', scrollTop);
keepScrolling = true;
}
if (this.lastDragEvent.pageX - this.scrollInfo.elementOffset.left > this.scrollInfo.elementWidth - this.options.scrollSnapX && this.scrollInfo.scrollWidth > scrollLeft + this.scrollInfo.elementWidth) {
scrollLeft = scrollLeft + this.options.scrollAmount > this.scrollInfo.scrollWidth - this.scrollInfo.elementWidth ? this.scrollInfo.scrollWidth - this.scrollInfo.elementWidth : scrollLeft + this.options.scrollAmount;
this.element.prop('scrollLeft', scrollLeft);
keepScrolling = true;
}
if (this.lastDragEvent.pageY - this.scrollInfo.elementOffset.top > this.scrollInfo.elementHeight - this.options.scrollSnapY && this.scrollInfo.scrollHeight > scrollLeft + this.scrollInfo.elementHeight) {
scrollTop = scrollTop + this.options.scrollAmount > this.scrollInfo.scrollHeight - this.scrollInfo.elementHeight ? this.scrollInfo.scrollHeight - this.scrollInfo.elementHeight : scrollTop + this.options.scrollAmount;
this.element.prop('scrollTop', scrollTop);
keepScrolling = true;
}
return {
scrollLeft: scrollLeft,
scrollTop: scrollTop,
keepScrolling: keepScrolling
};
},
/**
* Calculate a relative position to the element's 0.0 offset, from the original drag event's coordinates
* @param {object} options The absolute X and Y positions
* @return {object} The relative X and Y positions
*/
_calcRelativePosition: function (options) {
var relXPos = options.x - this.scrollInfo.elementOffset.left + options.scrollLeft;
var relYPos = options.y - this.scrollInfo.elementOffset.top + options.scrollTop;
return {
x: relXPos,
y: relYPos
};
},
/**
* Calculate relative area positions to offset element
* @param {object} options The relative X and Y positions to the 0.0 of the element
* @return {object} An object containing the relative area coordinates
*/
_calcRelativeArea: function (options) {
var relX1 = options.xPos < this.scrollInfo.dragStartXPos ? options.xPos : this.scrollInfo.dragStartXPos;
var relY1 = options.yPos < this.scrollInfo.dragStartYPos ? options.yPos : this.scrollInfo.dragStartYPos;
var relX2 = options.xPos > this.scrollInfo.dragStartXPos ? options.xPos : this.scrollInfo.dragStartXPos;
var relY2 = options.yPos > this.scrollInfo.dragStartYPos ? options.yPos : this.scrollInfo.dragStartYPos;
return {
x1: relX1,
y1: relY1,
x2: relX2,
y2: relY2
};
},
/**
* Update the selected elements
* @param {object} options The return value of _calcRelativeArea()
* @return {null} No return value
*/
_updateSelectees: function (options) {
var that = this;
this.selectees.each(function() {
var selectee = $.data(this, "selectable-item"),
hit = false;
//prevent helper from being selected if appendTo: selectable
if (!selectee || selectee.element === that.element[0]) {
return;
}
if (that.options.tolerance === "touch") {
hit = ( !(selectee.relative.left > options.x2 || selectee.relative.right < options.x1 || selectee.relative.top > options.y2 || selectee.relative.bottom < options.y1) );
} else if (that.options.tolerance === "fit") {
hit = (selectee.relative.left > options.x1 && selectee.relative.right < options.x2 && selectee.relative.top > options.y1 && selectee.relative.bottom < options.y2);
}
if (hit) {
// SELECT
if (selectee.selected) {
selectee.$element.removeClass("ui-selected");
selectee.selected = false;
}
if (selectee.unselecting) {
selectee.$element.removeClass("ui-unselecting");
selectee.unselecting = false;
}
if (!selectee.selecting) {
selectee.$element.addClass("ui-selecting");
selectee.selecting = true;
// selectable SELECTING callback
that._trigger("selecting", that.lastDragEvent, {
selecting: selectee.element
});
}
} else {
// UNSELECT
if (selectee.selecting) {
if ((that.lastDragEvent.metaKey || that.lastDragEvent.ctrlKey) && selectee.startselected) {
selectee.$element.removeClass("ui-selecting");
selectee.selecting = false;
selectee.$element.addClass("ui-selected");
selectee.selected = true;
} else {
selectee.$element.removeClass("ui-selecting");
selectee.selecting = false;
if (selectee.startselected) {
selectee.$element.addClass("ui-unselecting");
selectee.unselecting = true;
}
// selectable UNSELECTING callback
that._trigger("unselecting", that.lastDragEvent, {
unselecting: selectee.element
});
}
}
if (selectee.selected) {
if (!that.lastDragEvent.metaKey && !that.lastDragEvent.ctrlKey && !selectee.startselected) {
selectee.$element.removeClass("ui-selected");
selectee.selected = false;
selectee.$element.addClass("ui-unselecting");
selectee.unselecting = true;
// selectable UNSELECTING callback
that._trigger("unselecting", that.lastDragEvent, {
unselecting: selectee.element
});
}
}
}
});
},
/**
* The original _mouseDrag function overvritten by our one
* @param {object} event The original mousemove event
* @return {boolean} Returning false, as the parent returns too
*/
_mouseDrag: function (event) {
this.dragged = true;
if (this.options.disabled) {
return;
}
this.lastDragEvent = event;
var scrollObj = this._scrollIfNeeded();
var that = this;
this._updateIntervals({
keepScrolling: scrollObj.keepScrolling
});
this._updateUi({
doUpdateHelper: true
});
return false;
},
/**
* Do the actual calculating/updating of the positions/elements
* @param {object} options Options containing if the function should update the helper lasso
* @return {null} No return value
*/
_updateUi: function (options) {
var scrollObj = this._scrollIfNeeded({
pageX: this.lastDragEvent.pageX,
pageY: this.lastDragEvent.pageY
});
var relativePos = this._calcRelativePosition({
x: this.lastDragEvent.pageX,
y: this.lastDragEvent.pageY,
scrollLeft: scrollObj.scrollLeft,
scrollTop: scrollObj.scrollTop
});
var relativeArea = this._calcRelativeArea({
xPos: relativePos.x,
yPos: relativePos.y
});
if (options.doUpdateHelper) {
this._updateHelper({
scrollLeft: scrollObj.scrollLeft,
scrollTop: scrollObj.scrollTop,
x1: relativeArea.x1,
y1: relativeArea.y1,
x2: relativeArea.x2,
y2: relativeArea.y2
});
}
this._updateSelectees({
x1: relativeArea.x1,
y1: relativeArea.y1,
x2: relativeArea.x2,
y2: relativeArea.y2
});
},
/**
* Start the automatic scrolling if needed
* @param {object} options Options containing if the function should start the interval
* @return {null} No return value
*/
_updateIntervals: function (options) {
var that = this;
if (options.keepScrolling && !this.scrollIntervalId) {
this.scrollIntervalId = setInterval(function () {
that._updateUi({
doUpdateHelper: false
});
}, this.options.scrollIntervalTime);
}
if (!options.keepScrolling && this.scrollIntervalId)
this._clearIntervals();
},
/**
* Clear the autoscroll interval
* @return {null} No return value
*/
_clearIntervals: function () {
// Stop scrolling
if (this.scrollIntervalId)
clearInterval(this.scrollIntervalId);
this.scrollIntervalId = null;
},
/**
* The original _mouseStop event extended with the interval clearer
* @param {object} event The original mousestop event
* @return {boolean} The parent's return value
*/
_mouseStop: function (event) {
this._clearIntervals();
var retValue = this._super(event);
return retValue;
}
});
})(jQuery);