-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathdraggabilly.js
403 lines (324 loc) · 10.4 KB
/
draggabilly.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
397
398
399
400
401
402
403
/*!
* Draggabilly v3.0.0
* Make that shiz draggable
* https://draggabilly.desandro.com
* MIT license
*/
( function( window, factory ) {
// universal module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('get-size'),
require('unidragger'),
);
} else {
// browser global
window.Draggabilly = factory(
window,
window.getSize,
window.Unidragger,
);
}
}( typeof window != 'undefined' ? window : this,
function factory( window, getSize, Unidragger ) {
// -------------------------- helpers & variables -------------------------- //
function noop() {}
let jQuery = window.jQuery;
// -------------------------- Draggabilly -------------------------- //
function Draggabilly( element, options ) {
// querySelector if string
this.element = typeof element == 'string' ?
document.querySelector( element ) : element;
if ( jQuery ) {
this.$element = jQuery( this.element );
}
// options
this.options = {};
this.option( options );
this._create();
}
// inherit Unidragger methods
let proto = Draggabilly.prototype = Object.create( Unidragger.prototype );
/**
* set options
* @param {Object} opts
*/
proto.option = function( opts ) {
this.options = {
...this.options,
...opts,
};
};
// css position values that don't need to be set
const positionValues = [ 'relative', 'absolute', 'fixed' ];
proto._create = function() {
// properties
this.position = {};
this._getPosition();
this.startPoint = { x: 0, y: 0 };
this.dragPoint = { x: 0, y: 0 };
this.startPosition = { ...this.position };
// set relative positioning
let style = getComputedStyle( this.element );
if ( !positionValues.includes( style.position ) ) {
this.element.style.position = 'relative';
}
// events
this.on( 'pointerDown', this.handlePointerDown );
this.on( 'pointerUp', this.handlePointerUp );
this.on( 'dragStart', this.handleDragStart );
this.on( 'dragMove', this.handleDragMove );
this.on( 'dragEnd', this.handleDragEnd );
this.setHandles();
this.enable();
};
// set this.handles and bind start events to 'em
proto.setHandles = function() {
let { handle } = this.options;
if ( typeof handle == 'string' ) {
this.handles = this.element.querySelectorAll( handle );
} else if ( typeof handle == 'object' && handle.length ) {
this.handles = handle;
} else if ( handle instanceof HTMLElement ) {
this.handles = [ handle ];
} else {
this.handles = [ this.element ];
}
};
const cancelableEvents = [ 'dragStart', 'dragMove', 'dragEnd' ];
// duck-punch emitEvent to dispatch jQuery events as well
let emitEvent = proto.emitEvent;
proto.emitEvent = function( eventName, args ) {
// do not emit cancelable events if dragging is disabled
let isCanceled = !this.isEnabled && cancelableEvents.includes( eventName );
if ( isCanceled ) return;
emitEvent.call( this, eventName, args );
// trigger jQuery event
let jquery = window.jQuery;
if ( !jquery || !this.$element ) return;
// create jQuery event
let event;
let jqArgs = args;
let isFirstArgEvent = args && args[0] instanceof Event;
if ( isFirstArgEvent ) [ event, ...jqArgs ] = args;
/* eslint-disable-next-line new-cap */
let $event = jquery.Event( event );
$event.type = eventName;
this.$element.trigger( $event, jqArgs );
};
// -------------------------- position -------------------------- //
// get x/y position from style
proto._getPosition = function() {
let style = getComputedStyle( this.element );
let x = this._getPositionCoord( style.left, 'width' );
let y = this._getPositionCoord( style.top, 'height' );
// clean up 'auto' or other non-integer values
this.position.x = isNaN( x ) ? 0 : x;
this.position.y = isNaN( y ) ? 0 : y;
this._addTransformPosition( style );
};
proto._getPositionCoord = function( styleSide, measure ) {
if ( styleSide.includes('%') ) {
// convert percent into pixel for Safari, #75
let parentSize = getSize( this.element.parentNode );
// prevent not-in-DOM element throwing bug, #131
return !parentSize ? 0 :
( parseFloat( styleSide ) / 100 ) * parentSize[ measure ];
}
return parseInt( styleSide, 10 );
};
// add transform: translate( x, y ) to position
proto._addTransformPosition = function( style ) {
let transform = style.transform;
// bail out if value is 'none'
if ( !transform.startsWith('matrix') ) return;
// split matrix(1, 0, 0, 1, x, y)
let matrixValues = transform.split(',');
// translate X value is in 12th or 4th position
let xIndex = transform.startsWith('matrix3d') ? 12 : 4;
let translateX = parseInt( matrixValues[ xIndex ], 10 );
// translate Y value is in 13th or 5th position
let translateY = parseInt( matrixValues[ xIndex + 1 ], 10 );
this.position.x += translateX;
this.position.y += translateY;
};
// -------------------------- events -------------------------- //
proto.handlePointerDown = function( event, pointer ) {
if ( !this.isEnabled ) return;
// track start event position
// Safari 9 overrides pageX and pageY. These values needs to be copied. flickity#842
this.pointerDownPointer = {
pageX: pointer.pageX,
pageY: pointer.pageY,
};
event.preventDefault();
document.activeElement.blur();
// bind move and end events
this.bindActivePointerEvents( event );
this.element.classList.add('is-pointer-down');
};
proto.handleDragStart = function() {
if ( !this.isEnabled ) return;
this._getPosition();
this.measureContainment();
// position _when_ drag began
this.startPosition.x = this.position.x;
this.startPosition.y = this.position.y;
// reset left/top style
this.setLeftTop();
this.dragPoint.x = 0;
this.dragPoint.y = 0;
this.element.classList.add('is-dragging');
// start animation
this.animate();
};
proto.measureContainment = function() {
let container = this.getContainer();
if ( !container ) return;
let elemSize = getSize( this.element );
let containerSize = getSize( container );
let {
borderLeftWidth,
borderRightWidth,
borderTopWidth,
borderBottomWidth,
} = containerSize;
let elemRect = this.element.getBoundingClientRect();
let containerRect = container.getBoundingClientRect();
let borderSizeX = borderLeftWidth + borderRightWidth;
let borderSizeY = borderTopWidth + borderBottomWidth;
let position = this.relativeStartPosition = {
x: elemRect.left - ( containerRect.left + borderLeftWidth ),
y: elemRect.top - ( containerRect.top + borderTopWidth ),
};
this.containSize = {
width: ( containerSize.width - borderSizeX ) - position.x - elemSize.width,
height: ( containerSize.height - borderSizeY ) - position.y - elemSize.height,
};
};
proto.getContainer = function() {
let containment = this.options.containment;
if ( !containment ) return;
let isElement = containment instanceof HTMLElement;
// use as element
if ( isElement ) return containment;
// querySelector if string
if ( typeof containment == 'string' ) {
return document.querySelector( containment );
}
// fallback to parent element
return this.element.parentNode;
};
// ----- move event ----- //
/**
* drag move
* @param {Event} event
* @param {Event | Touch} pointer
* @param {Object} moveVector - x and y coordinates
*/
proto.handleDragMove = function( event, pointer, moveVector ) {
if ( !this.isEnabled ) return;
let dragX = moveVector.x;
let dragY = moveVector.y;
let grid = this.options.grid;
let gridX = grid && grid[0];
let gridY = grid && grid[1];
dragX = applyGrid( dragX, gridX );
dragY = applyGrid( dragY, gridY );
dragX = this.containDrag( 'x', dragX, gridX );
dragY = this.containDrag( 'y', dragY, gridY );
// constrain to axis
dragX = this.options.axis == 'y' ? 0 : dragX;
dragY = this.options.axis == 'x' ? 0 : dragY;
this.position.x = this.startPosition.x + dragX;
this.position.y = this.startPosition.y + dragY;
// set dragPoint properties
this.dragPoint.x = dragX;
this.dragPoint.y = dragY;
};
function applyGrid( value, grid, method ) {
if ( !grid ) return value;
method = method || 'round';
return Math[ method ]( value/grid ) * grid;
}
proto.containDrag = function( axis, drag, grid ) {
if ( !this.options.containment ) return drag;
let measure = axis == 'x' ? 'width' : 'height';
let rel = this.relativeStartPosition[ axis ];
let min = applyGrid( -rel, grid, 'ceil' );
let max = this.containSize[ measure ];
max = applyGrid( max, grid, 'floor' );
return Math.max( min, Math.min( max, drag ) );
};
// ----- end event ----- //
proto.handlePointerUp = function() {
this.element.classList.remove('is-pointer-down');
};
proto.handleDragEnd = function() {
if ( !this.isEnabled ) return;
// use top left position when complete
this.element.style.transform = '';
this.setLeftTop();
this.element.classList.remove('is-dragging');
};
// -------------------------- animation -------------------------- //
proto.animate = function() {
// only render and animate if dragging
if ( !this.isDragging ) return;
this.positionDrag();
requestAnimationFrame( () => this.animate() );
};
// left/top positioning
proto.setLeftTop = function() {
let { x, y } = this.position;
this.element.style.left = `${x}px`;
this.element.style.top = `${y}px`;
};
proto.positionDrag = function() {
let { x, y } = this.dragPoint;
this.element.style.transform = `translate3d(${x}px, ${y}px, 0)`;
};
// ----- methods ----- //
/**
* @param {Number} x
* @param {Number} y
*/
proto.setPosition = function( x, y ) {
this.position.x = x;
this.position.y = y;
this.setLeftTop();
};
proto.enable = function() {
if ( this.isEnabled ) return;
this.isEnabled = true;
this.bindHandles();
};
proto.disable = function() {
if ( !this.isEnabled ) return;
this.isEnabled = false;
if ( this.isDragging ) this.dragEnd();
this.unbindHandles();
};
const resetCssProperties = [ 'transform', 'left', 'top', 'position' ];
proto.destroy = function() {
this.disable();
// reset styles
resetCssProperties.forEach( ( prop ) => {
this.element.style[ prop ] = '';
} );
// unbind handles
this.unbindHandles();
// remove jQuery data
if ( this.$element ) this.$element.removeData('draggabilly');
};
// ----- jQuery bridget ----- //
// required for jQuery bridget
proto._init = noop;
if ( jQuery && jQuery.bridget ) {
jQuery.bridget( 'draggabilly', Draggabilly );
}
// ----- ----- //
return Draggabilly;
} ) );