Sortable.js 36.3 KB
Newer Older
R
RubaXa 已提交
1 2 3 4 5 6
/**!
 * Sortable
 * @author	RubaXa   <trash@rubaxa.org>
 * @license MIT
 */

L
Lebedev Konstantin 已提交
7
(function sortableModule(factory) {
R
RubaXa 已提交
8 9
	"use strict";

R
RubaXa 已提交
10
	if (typeof define === "function" && define.amd) {
R
RubaXa 已提交
11
		define(factory);
R
RubaXa 已提交
12
	}
R
RubaXa 已提交
13
	else if (typeof module != "undefined" && typeof module.exports != "undefined") {
S
Scott Nelson 已提交
14 15
		module.exports = factory();
	}
R
RubaXa 已提交
16
	else {
R
RubaXa 已提交
17
		/* jshint sub:true */
R
RubaXa 已提交
18 19
		window["Sortable"] = factory();
	}
L
Lebedev Konstantin 已提交
20
})(function sortableFactory() {
R
RubaXa 已提交
21 22
	"use strict";

R
RubaXa 已提交
23
	if (typeof window === "undefined" || !window.document) {
L
Lebedev Konstantin 已提交
24
		return function sortableError() {
R
RubaXa 已提交
25 26
			throw new Error("Sortable.js requires a window with a document");
		};
O
Onoshko Dan 已提交
27
	}
R
RubaXa 已提交
28

R
RubaXa 已提交
29
	var dragEl,
30
		parentEl,
R
RubaXa 已提交
31 32 33 34
		ghostEl,
		cloneEl,
		rootEl,
		nextEl,
L
Lebedev Konstantin 已提交
35
		lastDownEl,
R
RubaXa 已提交
36

37 38
		scrollEl,
		scrollParentEl,
39
		scrollCustomFn,
40

R
RubaXa 已提交
41 42
		lastEl,
		lastCSS,
S
sp-kilobug 已提交
43
		lastParentCSS,
R
RubaXa 已提交
44

R
RubaXa 已提交
45 46 47
		oldIndex,
		newIndex,

R
RubaXa 已提交
48
		activeGroup,
R
RubaXa 已提交
49 50
		putSortable,

R
RubaXa 已提交
51
		autoScroll = {},
R
RubaXa 已提交
52

R
RubaXa 已提交
53 54
		tapEvt,
		touchEvt,
R
RubaXa 已提交
55

C
ChiefORZ 已提交
56 57
		moved,

58 59
		forRepaintDummy,

R
RubaXa 已提交
60
		/** @const */
61 62
		R_SPACE = /\s+/g,
		R_FLOAT = /left|right|inline/,
R
RubaXa 已提交
63

R
RubaXa 已提交
64
		expando = 'Sortable' + (new Date).getTime(),
R
RubaXa 已提交
65

R
RubaXa 已提交
66 67 68
		win = window,
		document = win.document,
		parseInt = win.parseInt,
R
RubaXa 已提交
69
		setTimeout = win.setTimeout,
R
RubaXa 已提交
70

R
* UPD  
RubaXa 已提交
71
		$ = win.jQuery || win.Zepto,
72 73
		Polymer = win.Polymer,

L
Lebedev Konstantin 已提交
74
		captureMode = false,
R
RubaXa 已提交
75
		passiveMode = false,
76

R
RubaXa 已提交
77
		supportDraggable = ('draggable' in document.createElement('div')),
78
		supportCssPointerEvents = (function (el) {
79
			// false when IE11
80
			if (!!navigator.userAgent.match(/(?:Trident.*rv[ :]?11\.|msie)/i)) {
81 82
				return false;
			}
83 84 85 86
			el = document.createElement('x');
			el.style.cssText = 'pointer-events:auto';
			return el.style.pointerEvents === 'auto';
		})(),
R
RubaXa 已提交
87

R
RubaXa 已提交
88
		_silent = false,
R
RubaXa 已提交
89

R
RubaXa 已提交
90
		abs = Math.abs,
R
RubaXa 已提交
91
		min = Math.min,
R
RubaXa 已提交
92

L
Lebedev Konstantin 已提交
93
		savedInputChecked = [],
94 95
		touchDragOverListeners = [],

96
		alwaysFalse = function () { return false; },
R
RubaXa 已提交
97

98 99 100
		_autoScroll = _throttle(function (/**Event*/evt, /**Object*/options, /**HTMLElement*/rootEl) {
			// Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=505521
			if (rootEl && options.scroll) {
J
Jang Jun 已提交
101 102
				var _this = rootEl[expando],
					el,
103 104 105 106 107 108 109 110 111 112 113
					rect,
					sens = options.scrollSensitivity,
					speed = options.scrollSpeed,

					x = evt.clientX,
					y = evt.clientY,

					winWidth = window.innerWidth,
					winHeight = window.innerHeight,

					vx,
114 115 116 117
					vy,

					scrollOffsetX,
					scrollOffsetY
D
desmaisons_david 已提交
118
				;
119 120 121 122 123

				// Delect scrollEl
				if (scrollParentEl !== rootEl) {
					scrollEl = options.scroll;
					scrollParentEl = rootEl;
124
					scrollCustomFn = options.scrollFn;
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

					if (scrollEl === true) {
						scrollEl = rootEl;

						do {
							if ((scrollEl.offsetWidth < scrollEl.scrollWidth) ||
								(scrollEl.offsetHeight < scrollEl.scrollHeight)
							) {
								break;
							}
							/* jshint boss:true */
						} while (scrollEl = scrollEl.parentNode);
					}
				}

				if (scrollEl) {
					el = scrollEl;
					rect = scrollEl.getBoundingClientRect();
					vx = (abs(rect.right - x) <= sens) - (abs(rect.left - x) <= sens);
					vy = (abs(rect.bottom - y) <= sens) - (abs(rect.top - y) <= sens);
				}


				if (!(vx || vy)) {
					vx = (winWidth - x <= sens) - (x <= sens);
					vy = (winHeight - y <= sens) - (y <= sens);

					/* jshint expr:true */
					(vx || vy) && (el = win);
				}


				if (autoScroll.vx !== vx || autoScroll.vy !== vy || autoScroll.el !== el) {
					autoScroll.el = el;
					autoScroll.vx = vx;
					autoScroll.vy = vy;

					clearInterval(autoScroll.pid);

					if (el) {
						autoScroll.pid = setInterval(function () {
166 167 168
							scrollOffsetY = vy ? vy * speed : 0;
							scrollOffsetX = vx ? vx * speed : 0;

D
desmaisons_david 已提交
169
							if ('function' === typeof(scrollCustomFn)) {
170 171 172
								return scrollCustomFn.call(_this, scrollOffsetX, scrollOffsetY, evt);
							}

173
							if (el === win) {
174
								win.scrollTo(win.pageXOffset + scrollOffsetX, win.pageYOffset + scrollOffsetY);
175
							} else {
176 177
								el.scrollTop += scrollOffsetY;
								el.scrollLeft += scrollOffsetX;
178 179 180 181 182
							}
						}, 24);
					}
				}
			}
R
RubaXa 已提交
183 184 185
		}, 30),

		_prepareGroup = function (options) {
R
RubaXa 已提交
186
			function toFn(value, pull) {
R
RubaXa 已提交
187
				if (value == null || value === true) {
188
					value = group.name;
R
RubaXa 已提交
189 190 191
					if (value == null) {
						return alwaysFalse;
					}
R
RubaXa 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
				}

				if (typeof value === 'function') {
					return value;
				} else {
					return function (to, from) {
						var fromGroup = from.options.group.name;

						return pull
							? value
							: value && (value.join
								? value.indexOf(fromGroup) > -1
								: (fromGroup == value)
							);
					};
				}
			}

R
RubaXa 已提交
210 211
			var group = {};
			var originalGroup = options.group;
R
RubaXa 已提交
212

R
RubaXa 已提交
213
			if (!originalGroup || typeof originalGroup != 'object') {
D
desmaisons_david 已提交
214
				originalGroup = {name: originalGroup};
R
RubaXa 已提交
215 216
			}

R
RubaXa 已提交
217 218 219
			group.name = originalGroup.name;
			group.checkPull = toFn(originalGroup.pull, true);
			group.checkPut = toFn(originalGroup.put);
L
Lebedev Konstantin 已提交
220
			group.revertClone = originalGroup.revertClone;
R
RubaXa 已提交
221 222

			options.group = group;
R
RubaXa 已提交
223
		}
D
desmaisons_david 已提交
224
	;
R
RubaXa 已提交
225

R
RubaXa 已提交
226 227 228 229
	// Detect support a passive mode
	try {
		window.addEventListener('test', null, Object.defineProperty({}, 'passive', {
			get: function () {
R
RubaXa 已提交
230 231 232
				// `false`, because everything starts to work incorrectly and instead of d'n'd,
				// begins the page has scrolled.
				passiveMode = false;
R
RubaXa 已提交
233 234 235 236 237 238 239
				captureMode = {
					capture: false,
					passive: passiveMode
				};
			}
		}));
	} catch (err) {}
R
RubaXa 已提交
240 241 242 243

	/**
	 * @class  Sortable
	 * @param  {HTMLElement}  el
244
	 * @param  {Object}       [options]
R
RubaXa 已提交
245
	 */
R
RubaXa 已提交
246
	function Sortable(el, options) {
R
RubaXa 已提交
247 248 249 250
		if (!(el && el.nodeType && el.nodeType === 1)) {
			throw 'Sortable: `el` must be HTMLElement, and not ' + {}.toString.call(el);
		}

R
RubaXa 已提交
251
		this.el = el; // root element
252
		this.options = options = _extend({}, options);
R
RubaXa 已提交
253 254


255 256 257
		// Export instance
		el[expando] = this;

R
RubaXa 已提交
258
		// Default options
259
		var defaults = {
R
RubaXa 已提交
260
			group: null,
R
RubaXa 已提交
261
			sort: true,
R
RubaXa 已提交
262
			disabled: false,
263 264
			store: null,
			handle: null,
R
RubaXa 已提交
265 266 267
			scroll: true,
			scrollSensitivity: 30,
			scrollSpeed: 10,
R
RubaXa 已提交
268
			draggable: /[uo]l/i.test(el.nodeName) ? 'li' : '>*',
269
			ghostClass: 'sortable-ghost',
R
RubaXa 已提交
270
			chosenClass: 'sortable-chosen',
R
RubaXa 已提交
271
			dragClass: 'sortable-drag',
272
			ignore: 'a, img',
273
			filter: null,
L
Lebedev Konstantin 已提交
274
			preventOnFilter: true,
R
RubaXa 已提交
275 276 277
			animation: 0,
			setData: function (dataTransfer, dragEl) {
				dataTransfer.setData('Text', dragEl.textContent);
278 279
			},
			dropBubble: false,
R
RubaXa 已提交
280
			dragoverBubble: false,
281
			dataIdAttr: 'data-id',
282
			delay: 0,
C
ChiefORZ 已提交
283 284
			forceFallback: false,
			fallbackClass: 'sortable-fallback',
285
			fallbackOnBody: false,
286
			fallbackTolerance: 0,
R
RubaXa 已提交
287
			fallbackOffset: {x: 0, y: 0},
288
			supportPointer: Sortable.supportPointer !== false
R
RubaXa 已提交
289
		};
290

R
RubaXa 已提交
291

292 293
		// Set default options
		for (var name in defaults) {
R
RubaXa 已提交
294
			!(name in options) && (options[name] = defaults[name]);
295
		}
R
RubaXa 已提交
296

R
RubaXa 已提交
297
		_prepareGroup(options);
R
RubaXa 已提交
298

R
* JSDoc  
RubaXa 已提交
299
		// Bind all private methods
R
RubaXa 已提交
300
		for (var fn in this) {
D
dev101 已提交
301
			if (fn.charAt(0) === '_' && typeof this[fn] === 'function') {
302
				this[fn] = this[fn].bind(this);
R
RubaXa 已提交
303 304 305
			}
		}

R
RubaXa 已提交
306 307
		// Setup drag mode
		this.nativeDraggable = options.forceFallback ? false : supportDraggable;
R
RubaXa 已提交
308 309 310 311

		// Bind events
		_on(el, 'mousedown', this._onTapStart);
		_on(el, 'touchstart', this._onTapStart);
R
RubaXa 已提交
312
		options.supportPointer && _on(el, 'pointerdown', this._onTapStart);
R
RubaXa 已提交
313

R
RubaXa 已提交
314
		if (this.nativeDraggable) {
315 316 317
			_on(el, 'dragover', this);
			_on(el, 'dragenter', this);
		}
R
RubaXa 已提交
318 319

		touchDragOverListeners.push(this._onDragOver);
320 321 322

		// Restore sorting
		options.store && this.sort(options.store.get(this));
R
RubaXa 已提交
323 324 325
	}


326
	Sortable.prototype = /** @lends Sortable.prototype */ {
R
RubaXa 已提交
327 328
		constructor: Sortable,

329
		_onTapStart: function (/** Event|TouchEvent */evt) {
330 331
			var _this = this,
				el = this.el,
332
				options = this.options,
333
				preventOnFilter = options.preventOnFilter,
334 335 336
				type = evt.type,
				touch = evt.touches && evt.touches[0],
				target = (touch || evt).target,
V
Varunkumar Nagarajan 已提交
337
				originalTarget = evt.target.shadowRoot && (evt.path && evt.path[0]) || target,
R
RubaXa 已提交
338 339
				filter = options.filter,
				startIndex;
R
RubaXa 已提交
340

L
Lebedev Konstantin 已提交
341 342 343
			_saveInputCheckedState(el);


R
RubaXa 已提交
344
			// Don't trigger start event when an element is been dragged, otherwise the evt.oldindex always wrong when set option.group.
W
why520crazy 已提交
345
			if (dragEl) {
346 347
				return;
			}
R
RubaXa 已提交
348

349
			if (/mousedown|pointerdown/.test(type) && evt.button !== 0 || options.disabled) {
350
				return; // only left button or enabled
R
RubaXa 已提交
351
			}
R
RubaXa 已提交
352

353 354 355 356
			// cancel dnd if original target is content editable
			if (originalTarget.isContentEditable) {
				return;
			}
357 358

			target = _closest(target, options.draggable, el);
359

360
			if (!target) {
361 362
				return;
			}
363

L
Lebedev Konstantin 已提交
364 365 366 367 368
			if (lastDownEl === target) {
				// Ignoring duplicate `down`
				return;
			}

R
RubaXa 已提交
369
			// Get the index of the dragged element within its parent
R
RubaXa 已提交
370
			startIndex = _index(target, options.draggable);
371 372 373 374

			// Check filter
			if (typeof filter === 'function') {
				if (filter.call(this, evt, target, this)) {
J
Joey Becker 已提交
375
					_dispatchEvent(_this, originalTarget, 'filter', target, el, el, startIndex);
L
Lebedev Konstantin 已提交
376
					preventOnFilter && evt.preventDefault();
377 378
					return; // cancel dnd
				}
379
			}
380 381 382
			else if (filter) {
				filter = filter.split(',').some(function (criteria) {
					criteria = _closest(originalTarget, criteria.trim(), el);
383

384
					if (criteria) {
J
Joey Becker 已提交
385
						_dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex);
386 387 388 389 390
						return true;
					}
				});

				if (filter) {
L
Lebedev Konstantin 已提交
391
					preventOnFilter && evt.preventDefault();
392
					return; // cancel dnd
393 394 395
				}
			}

396 397 398 399
			if (options.handle && !_closest(originalTarget, options.handle, el)) {
				return;
			}

400
			// Prepare `dragstart`
R
RubaXa 已提交
401
			this._prepareDragStart(evt, touch, target, startIndex);
402 403
		},

R
RubaXa 已提交
404
		_prepareDragStart: function (/** Event */evt, /** Touch */touch, /** HTMLElement */target, /** Number */startIndex) {
405 406 407 408 409
			var _this = this,
				el = _this.el,
				options = _this.options,
				ownerDocument = el.ownerDocument,
				dragStartFn;
410 411 412 413

			if (target && !dragEl && (target.parentNode === el)) {
				tapEvt = evt;

414
				rootEl = el;
415
				dragEl = target;
R
RubaXa 已提交
416
				parentEl = dragEl.parentNode;
417
				nextEl = dragEl.nextSibling;
D
desmaisons_david 已提交
418
				lastDownEl = target;
419
				activeGroup = options.group;
R
RubaXa 已提交
420
				oldIndex = startIndex;
421

422 423
				this._lastX = (touch || evt).clientX;
				this._lastY = (touch || evt).clientY;
S
sp-kilobug 已提交
424

R
RubaXa 已提交
425
				dragEl.style['will-change'] = 'all';
L
Lebedev Konstantin 已提交
426

427 428 429 430
				dragStartFn = function () {
					// Delayed drag has been triggered
					// we can re-enable the events: touchmove/mousemove
					_this._disableDelayedDrag();
431

432
					// Make the element draggable
433
					dragEl.draggable = _this.nativeDraggable;
434

R
RubaXa 已提交
435
					// Chosen item
R
RubaXa 已提交
436
					_toggleClass(dragEl, options.chosenClass, true);
437 438

					// Bind the events: dragstart/dragend
R
RubaXa 已提交
439
					_this._triggerDragStart(evt, touch);
T
TaliLavi 已提交
440 441

					// Drag start event
J
Joey Becker 已提交
442
					_dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex);
443 444
				};

R
RubaXa 已提交
445 446 447 448 449
				// Disable "draggable"
				options.ignore.split(',').forEach(function (criteria) {
					_find(dragEl, criteria.trim(), _disableDraggable);
				});

450 451 452
				_on(ownerDocument, 'mouseup', _this._onDrop);
				_on(ownerDocument, 'touchend', _this._onDrop);
				_on(ownerDocument, 'touchcancel', _this._onDrop);
453
				_on(ownerDocument, 'selectstart', _this);
R
RubaXa 已提交
454
				options.supportPointer && _on(ownerDocument, 'pointercancel', _this._onDrop);
455 456

				if (options.delay) {
457 458
					// If the user moves the pointer or let go the click or touch
					// before the delay has been reached:
459
					// disable the delayed drag
460 461 462
					_on(ownerDocument, 'mouseup', _this._disableDelayedDrag);
					_on(ownerDocument, 'touchend', _this._disableDelayedDrag);
					_on(ownerDocument, 'touchcancel', _this._disableDelayedDrag);
463 464
					_on(ownerDocument, 'mousemove', _this._disableDelayedDrag);
					_on(ownerDocument, 'touchmove', _this._disableDelayedDrag);
R
RubaXa 已提交
465
					options.supportPointer && _on(ownerDocument, 'pointermove', _this._disableDelayedDrag);
466 467 468 469

					_this._dragStartTimer = setTimeout(dragStartFn, options.delay);
				} else {
					dragStartFn();
470
				}
471

472

D
desmaisons_david 已提交
473
			}
474
		},
R
RubaXa 已提交
475

476 477
		_disableDelayedDrag: function () {
			var ownerDocument = this.el.ownerDocument;
478

479
			clearTimeout(this._dragStartTimer);
480 481 482
			_off(ownerDocument, 'mouseup', this._disableDelayedDrag);
			_off(ownerDocument, 'touchend', this._disableDelayedDrag);
			_off(ownerDocument, 'touchcancel', this._disableDelayedDrag);
483 484
			_off(ownerDocument, 'mousemove', this._disableDelayedDrag);
			_off(ownerDocument, 'touchmove', this._disableDelayedDrag);
485
			_off(ownerDocument, 'pointermove', this._disableDelayedDrag);
486
		},
R
RubaXa 已提交
487

488 489
		_triggerDragStart: function (/** Event */evt, /** Touch */touch) {
			touch = touch || (evt.pointerType == 'touch' ? evt : null);
490

491 492 493 494 495 496 497
			if (touch) {
				// Touch device support
				tapEvt = {
					target: dragEl,
					clientX: touch.clientX,
					clientY: touch.clientY
				};
498

499
				this._onDragStart(tapEvt, 'touch');
R
RubaXa 已提交
500
			}
R
RubaXa 已提交
501
			else if (!this.nativeDraggable) {
502 503 504 505 506
				this._onDragStart(tapEvt, true);
			}
			else {
				_on(dragEl, 'dragend', this);
				_on(rootEl, 'dragstart', this._onDragStart);
507 508
			}

509
			try {
510 511
				if (document.selection) {
					// Timeout neccessary for IE9
R
RubaXa 已提交
512
					_nextTick(function () {
513
						document.selection.empty();
514
					});
515 516
				} else {
					window.getSelection().removeAllRanges();
517
				}
518
			} catch (err) {
519
			}
520
		},
521

522 523
		_dragStarted: function () {
			if (rootEl && dragEl) {
R
RubaXa 已提交
524 525
				var options = this.options;

526
				// Apply effect
R
RubaXa 已提交
527 528
				_toggleClass(dragEl, options.ghostClass, true);
				_toggleClass(dragEl, options.dragClass, false);
R
RubaXa 已提交
529

530
				Sortable.active = this;
531

532
				// Drag start event
J
Joey Becker 已提交
533
				_dispatchEvent(this, rootEl, 'start', dragEl, rootEl, rootEl, oldIndex);
L
#1009  
Lebedev Konstantin 已提交
534 535
			} else {
				this._nulling();
536
			}
R
RubaXa 已提交
537 538
		},

R
RubaXa 已提交
539 540
		_emulateDragOver: function () {
			if (touchEvt) {
R
RubaXa 已提交
541
				if (this._lastX === touchEvt.clientX && this._lastY === touchEvt.clientY) {
542 543
					return;
				}
R
RubaXa 已提交
544 545 546

				this._lastX = touchEvt.clientX;
				this._lastY = touchEvt.clientY;
547

548 549 550
				if (!supportCssPointerEvents) {
					_css(ghostEl, 'display', 'none');
				}
R
RubaXa 已提交
551

L
Luiz Corte Real 已提交
552 553 554
				var target = document.elementFromPoint(touchEvt.clientX, touchEvt.clientY);
				var parent = target;
				var i = touchDragOverListeners.length;
R
RubaXa 已提交
555

556 557
				if (target && target.shadowRoot) {
					target = target.shadowRoot.elementFromPoint(touchEvt.clientX, touchEvt.clientY);
L
Luiz Corte Real 已提交
558
					parent = target;
559 560
				}

R
RubaXa 已提交
561 562
				if (parent) {
					do {
R
RubaXa 已提交
563
						if (parent[expando]) {
R
RubaXa 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
							while (i--) {
								touchDragOverListeners[i]({
									clientX: touchEvt.clientX,
									clientY: touchEvt.clientY,
									target: target,
									rootEl: parent
								});
							}

							break;
						}

						target = parent; // store last element
L
Larry Davis 已提交
577
					}
R
RubaXa 已提交
578 579
					/* jshint boss:true */
					while (parent = parent.parentNode);
R
RubaXa 已提交
580 581
				}

582 583 584
				if (!supportCssPointerEvents) {
					_css(ghostEl, 'display', '');
				}
R
RubaXa 已提交
585 586 587 588
			}
		},


R
RubaXa 已提交
589 590
		_onTouchMove: function (/**TouchEvent*/evt) {
			if (tapEvt) {
D
desmaisons_david 已提交
591
				var	options = this.options,
R
RubaXa 已提交
592
					fallbackTolerance = options.fallbackTolerance,
593
					fallbackOffset = options.fallbackOffset,
R
RubaXa 已提交
594
					touch = evt.touches ? evt.touches[0] : evt,
595 596
					dx = (touch.clientX - tapEvt.clientX) + fallbackOffset.x,
					dy = (touch.clientY - tapEvt.clientY) + fallbackOffset.y,
R
RubaXa 已提交
597
					translate3d = evt.touches ? 'translate3d(' + dx + 'px,' + dy + 'px,0)' : 'translate(' + dx + 'px,' + dy + 'px)';
S
sp-kilobug 已提交
598

599
				// only set the status to dragging, when we are actually dragging
R
RubaXa 已提交
600
				if (!Sortable.active) {
R
RubaXa 已提交
601 602 603 604
					if (fallbackTolerance &&
						min(abs(touch.clientX - this._lastX), abs(touch.clientY - this._lastY)) < fallbackTolerance
					) {
						return;
S
sp-kilobug 已提交
605
					}
R
RubaXa 已提交
606

607 608
					this._dragStarted();
				}
R
RubaXa 已提交
609

610 611
				// as well as creating the ghost element on the document body
				this._appendGhost();
R
RubaXa 已提交
612

A
Alex Wild 已提交
613
				moved = true;
R
RubaXa 已提交
614
				touchEvt = touch;
R
RubaXa 已提交
615 616 617 618 619 620

				_css(ghostEl, 'webkitTransform', translate3d);
				_css(ghostEl, 'mozTransform', translate3d);
				_css(ghostEl, 'msTransform', translate3d);
				_css(ghostEl, 'transform', translate3d);

M
Marius Petcu 已提交
621
				evt.preventDefault();
R
RubaXa 已提交
622 623 624
			}
		},

R
RubaXa 已提交
625 626
		_appendGhost: function () {
			if (!ghostEl) {
R
RubaXa 已提交
627 628
				var rect = dragEl.getBoundingClientRect(),
					css = _css(dragEl),
R
RubaXa 已提交
629
					options = this.options,
R
RubaXa 已提交
630
					ghostRect;
R
RubaXa 已提交
631

632
				ghostEl = dragEl.cloneNode(true);
R
RubaXa 已提交
633

R
RubaXa 已提交
634 635
				_toggleClass(ghostEl, options.ghostClass, false);
				_toggleClass(ghostEl, options.fallbackClass, true);
R
RubaXa 已提交
636
				_toggleClass(ghostEl, options.dragClass, true);
C
ChiefORZ 已提交
637

R
RubaXa 已提交
638 639
				_css(ghostEl, 'top', rect.top - parseInt(css.marginTop, 10));
				_css(ghostEl, 'left', rect.left - parseInt(css.marginLeft, 10));
R
RubaXa 已提交
640 641
				_css(ghostEl, 'width', rect.width);
				_css(ghostEl, 'height', rect.height);
R
RubaXa 已提交
642 643 644
				_css(ghostEl, 'opacity', '0.8');
				_css(ghostEl, 'position', 'fixed');
				_css(ghostEl, 'zIndex', '100000');
645
				_css(ghostEl, 'pointerEvents', 'none');
R
RubaXa 已提交
646

R
RubaXa 已提交
647
				options.fallbackOnBody && document.body.appendChild(ghostEl) || rootEl.appendChild(ghostEl);
R
RubaXa 已提交
648 649 650

				// Fixing dimensions.
				ghostRect = ghostEl.getBoundingClientRect();
R
RubaXa 已提交
651 652
				_css(ghostEl, 'width', rect.width * 2 - ghostRect.width);
				_css(ghostEl, 'height', rect.height * 2 - ghostRect.height);
653 654 655 656
			}
		},

		_onDragStart: function (/**Event*/evt, /**boolean*/useFallback) {
R
RubaXa 已提交
657 658
			var _this = this;
			var dataTransfer = evt.dataTransfer;
R
RubaXa 已提交
659
			var options = _this.options;
660

R
RubaXa 已提交
661
			_this._offUpEvents();
662

R
RubaXa 已提交
663
			if (activeGroup.checkPull(_this, _this, dragEl, evt)) {
664
				cloneEl = _clone(dragEl);
L
Lebedev Konstantin 已提交
665 666 667

				cloneEl.draggable = false;
				cloneEl.style['will-change'] = '';
L
Lebedev Konstantin 已提交
668

669
				_css(cloneEl, 'display', 'none');
R
RubaXa 已提交
670
				_toggleClass(cloneEl, _this.options.chosenClass, false);
L
Lebedev Konstantin 已提交
671

R
RubaXa 已提交
672
				// #1143: IFrame support workaround
R
RubaXa 已提交
673 674 675
				_this._cloneId = _nextTick(function () {
					rootEl.insertBefore(cloneEl, dragEl);
					_dispatchEvent(_this, rootEl, 'clone', dragEl);
R
RubaXa 已提交
676
				});
677 678
			}

R
RubaXa 已提交
679 680
			_toggleClass(dragEl, options.dragClass, true);

681
			if (useFallback) {
R
RubaXa 已提交
682 683
				if (useFallback === 'touch') {
					// Bind touch events
R
RubaXa 已提交
684 685 686
					_on(document, 'touchmove', _this._onTouchMove);
					_on(document, 'touchend', _this._onDrop);
					_on(document, 'touchcancel', _this._onDrop);
R
RubaXa 已提交
687 688 689 690 691

					if (options.supportPointer) {
						_on(document, 'pointermove', _this._onTouchMove);
						_on(document, 'pointerup', _this._onDrop);
					}
R
RubaXa 已提交
692 693
				} else {
					// Old brwoser
R
RubaXa 已提交
694 695
					_on(document, 'mousemove', _this._onTouchMove);
					_on(document, 'mouseup', _this._onDrop);
R
RubaXa 已提交
696
				}
R
RubaXa 已提交
697

R
RubaXa 已提交
698
				_this._loopId = setInterval(_this._emulateDragOver, 50);
R
RubaXa 已提交
699 700
			}
			else {
R
RubaXa 已提交
701 702
				if (dataTransfer) {
					dataTransfer.effectAllowed = 'move';
R
RubaXa 已提交
703
					options.setData && options.setData.call(_this, dataTransfer, dragEl);
R
RubaXa 已提交
704
				}
R
RubaXa 已提交
705

R
RubaXa 已提交
706
				_on(document, 'drop', _this);
R
RubaXa 已提交
707 708

				// #1143: Бывает элемент с IFrame внутри блокирует `drop`,
R
typo  
Roman Dvornov 已提交
709
				// поэтому если вызвался `mouseover`, значит надо отменять весь d'n'd.
R
RubaXa 已提交
710 711
				// Breaking Chrome 62+
				// _on(document, 'mouseover', _this);
R
RubaXa 已提交
712

R
RubaXa 已提交
713
				_this._dragStartId = _nextTick(_this._dragStarted);
R
RubaXa 已提交
714 715 716
			}
		},

R
RubaXa 已提交
717
		_onDragOver: function (/**Event*/evt) {
R
RubaXa 已提交
718 719 720
			var el = this.el,
				target,
				dragRect,
R
#930  
RubaXa 已提交
721
				targetRect,
R
RubaXa 已提交
722 723 724
				revert,
				options = this.options,
				group = options.group,
R
RubaXa 已提交
725
				activeSortable = Sortable.active,
726
				isOwner = (activeGroup === group),
727
				isMovingBetweenSortable = false,
728
				canSort = options.sort;
R
RubaXa 已提交
729

R
RubaXa 已提交
730
			if (evt.preventDefault !== void 0) {
R
RubaXa 已提交
731
				evt.preventDefault();
732
				!options.dragoverBubble && evt.stopPropagation();
R
RubaXa 已提交
733
			}
R
RubaXa 已提交
734

735 736 737 738
			if (dragEl.animated) {
				return;
			}

A
Alex Wild 已提交
739 740
			moved = true;

L
#1009  
Lebedev Konstantin 已提交
741
			if (activeSortable && !options.disabled &&
742
				(isOwner
R
RubaXa 已提交
743
					? canSort || (revert = !rootEl.contains(dragEl)) // Reverting item into the original list
R
RubaXa 已提交
744 745
					: (
						putSortable === this ||
746 747 748 749
						(
							(activeSortable.lastPullMode = activeGroup.checkPull(this, activeSortable, dragEl, evt)) &&
							group.checkPut(this, activeSortable, dragEl, evt)
						)
R
RubaXa 已提交
750
					)
751
				) &&
R
RubaXa 已提交
752
				(evt.rootEl === void 0 || evt.rootEl === this.el) // touch fallback
R
RubaXa 已提交
753
			) {
R
RubaXa 已提交
754 755 756 757 758 759 760
				// Smart auto-scrolling
				_autoScroll(evt, options, this.el);

				if (_silent) {
					return;
				}

R
RubaXa 已提交
761
				target = _closest(evt.target, options.draggable, el);
R
RubaXa 已提交
762
				dragRect = dragEl.getBoundingClientRect();
763 764

				if (putSortable !== this) {
D
desmaisons_david 已提交
765
					putSortable = this;
766 767
					isMovingBetweenSortable = true;
				}
R
RubaXa 已提交
768

769
				if (revert) {
L
Lebedev Konstantin 已提交
770
					_cloneHide(activeSortable, true);
R
RubaXa 已提交
771
					parentEl = rootEl; // actualization
R
RubaXa 已提交
772

773 774 775 776 777 778 779
					if (cloneEl || nextEl) {
						rootEl.insertBefore(dragEl, cloneEl || nextEl);
					}
					else if (!canSort) {
						rootEl.appendChild(dragEl);
					}

R
RubaXa 已提交
780 781
					return;
				}
R
RubaXa 已提交
782

R
RubaXa 已提交
783

R
RubaXa 已提交
784
				if ((el.children.length === 0) || (el.children[0] === ghostEl) ||
785
					(el === evt.target) && (_ghostIsLast(el, evt))
R
RubaXa 已提交
786
				) {
787 788 789 790 791
					//assign target only if condition is true
					if (el.children.length !== 0 && el.children[0] !== ghostEl && el === evt.target) {
						target = el.lastElementChild;
					}

R
RubaXa 已提交
792 793 794 795
					if (target) {
						if (target.animated) {
							return;
						}
R
RubaXa 已提交
796

R
RubaXa 已提交
797 798
						targetRect = target.getBoundingClientRect();
					}
R
RubaXa 已提交
799

L
Lebedev Konstantin 已提交
800
					_cloneHide(activeSortable, isOwner);
R
RubaXa 已提交
801

802
					if (_onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt) !== false) {
803 804
						if (!dragEl.contains(el)) {
							el.appendChild(dragEl);
R
RubaXa 已提交
805
							parentEl = el; // actualization
806
						}
R
RubaXa 已提交
807

R
RubaXa 已提交
808 809 810
						this._animate(dragRect, dragEl);
						target && this._animate(targetRect, target);
					}
R
RubaXa 已提交
811
				}
R
RubaXa 已提交
812 813
				else if (target && !target.animated && target !== dragEl && (target.parentNode[expando] !== void 0)) {
					if (lastEl !== target) {
R
RubaXa 已提交
814
						lastEl = target;
R
RubaXa 已提交
815
						lastCSS = _css(target);
S
sp-kilobug 已提交
816
						lastParentCSS = _css(target.parentNode);
R
RubaXa 已提交
817 818
					}

R
#930  
RubaXa 已提交
819
					targetRect = target.getBoundingClientRect();
R
RubaXa 已提交
820

R
#930  
RubaXa 已提交
821
					var width = targetRect.right - targetRect.left,
R
RubaXa 已提交
822
						height = targetRect.bottom - targetRect.top,
823
						floating = R_FLOAT.test(lastCSS.cssFloat + lastCSS.display)
S
sp-kilobug 已提交
824
							|| (lastParentCSS.display == 'flex' && lastParentCSS['flex-direction'].indexOf('row') === 0),
R
RubaXa 已提交
825 826 827 828
						isWide = (target.offsetWidth > dragEl.offsetWidth),
						isLong = (target.offsetHeight > dragEl.offsetHeight),
						halfway = (floating ? (evt.clientX - targetRect.left) / width : (evt.clientY - targetRect.top) / height) > 0.5,
						nextSibling = target.nextElementSibling,
829
						after = false
D
desmaisons_david 已提交
830
					;
R
RubaXa 已提交
831

832 833 834
					if (floating) {
						var elTop = dragEl.offsetTop,
							tgTop = target.offsetTop;
R
RubaXa 已提交
835

836 837 838 839 840 841 842 843
						if (elTop === tgTop) {
							after = (target.previousElementSibling === dragEl) && !isWide || halfway && isWide;
						}
						else if (target.previousElementSibling === dragEl || dragEl.previousElementSibling === target) {
							after = (evt.clientY - targetRect.top) / height > 0.5;
						} else {
							after = tgTop > elTop;
						}
D
desmaisons_david 已提交
844
						} else if (!isMovingBetweenSortable) {
845 846
						after = (nextSibling !== dragEl) && !isLong || halfway && isLong;
					}
R
RubaXa 已提交
847

848 849 850
					var moveVector = _onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, after);

					if (moveVector !== false) {
R
RubaXa 已提交
851 852 853
						if (moveVector === 1 || moveVector === -1) {
							after = (moveVector === 1);
						}
R
RubaXa 已提交
854

855 856 857 858
						_silent = true;
						setTimeout(_unsilent, 30);

						_cloneHide(activeSortable, isOwner);
R
RubaXa 已提交
859

860 861 862 863 864 865
						if (!dragEl.contains(el)) {
							if (after && !nextSibling) {
								el.appendChild(dragEl);
							} else {
								target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
							}
R
RubaXa 已提交
866
						}
R
RubaXa 已提交
867

R
RubaXa 已提交
868 869
						parentEl = dragEl.parentNode; // actualization

R
RubaXa 已提交
870 871 872
						this._animate(dragRect, dragEl);
						this._animate(targetRect, target);
					}
R
RubaXa 已提交
873 874 875 876
				}
			}
		},

877 878 879 880 881 882
		_animate: function (prevRect, target) {
			var ms = this.options.animation;

			if (ms) {
				var currentRect = target.getBoundingClientRect();

L
Lebedev Konstantin 已提交
883 884 885 886
				if (prevRect.nodeType === 1) {
					prevRect = prevRect.getBoundingClientRect();
				}

R
RubaXa 已提交
887
				_css(target, 'transition', 'none');
888 889 890 891 892
				_css(target, 'transform', 'translate3d('
					+ (prevRect.left - currentRect.left) + 'px,'
					+ (prevRect.top - currentRect.top) + 'px,0)'
				);

893
				forRepaintDummy = target.offsetWidth; // repaint
894

R
RubaXa 已提交
895
				_css(target, 'transition', 'all ' + ms + 'ms');
896 897
				_css(target, 'transform', 'translate3d(0,0,0)');

R
* anim  
RubaXa 已提交
898 899
				clearTimeout(target.animated);
				target.animated = setTimeout(function () {
900
					_css(target, 'transition', '');
901
					_css(target, 'transform', '');
902 903 904 905 906
					target.animated = false;
				}, ms);
			}
		},

907
		_offUpEvents: function () {
908 909
			var ownerDocument = this.el.ownerDocument;

910
			_off(document, 'touchmove', this._onTouchMove);
911
			_off(document, 'pointermove', this._onTouchMove);
912 913
			_off(ownerDocument, 'mouseup', this._onDrop);
			_off(ownerDocument, 'touchend', this._onDrop);
914
			_off(ownerDocument, 'pointerup', this._onDrop);
915
			_off(ownerDocument, 'touchcancel', this._onDrop);
진유정 已提交
916
			_off(ownerDocument, 'pointercancel', this._onDrop);
917
			_off(ownerDocument, 'selectstart', this);
918
		},
R
RubaXa 已提交
919

R
RubaXa 已提交
920
		_onDrop: function (/**Event*/evt) {
921 922
			var el = this.el,
				options = this.options;
R
RubaXa 已提交
923

R
RubaXa 已提交
924
			clearInterval(this._loopId);
R
RubaXa 已提交
925
			clearInterval(autoScroll.pid);
R
RubaXa 已提交
926
			clearTimeout(this._dragStartTimer);
927

R
RubaXa 已提交
928 929 930
			_cancelNextTick(this._cloneId);
			_cancelNextTick(this._dragStartId);

R
RubaXa 已提交
931
			// Unbind events
R
RubaXa 已提交
932
			_off(document, 'mouseover', this);
R
RubaXa 已提交
933
			_off(document, 'mousemove', this._onTouchMove);
934

R
RubaXa 已提交
935
			if (this.nativeDraggable) {
936 937 938
				_off(document, 'drop', this);
				_off(el, 'dragstart', this._onDragStart);
			}
R
RubaXa 已提交
939

940
			this._offUpEvents();
R
RubaXa 已提交
941

R
RubaXa 已提交
942
			if (evt) {
R
RubaXa 已提交
943
				if (moved) {
R
RubaXa 已提交
944
					evt.preventDefault();
C
ChiefORZ 已提交
945 946
					!options.dropBubble && evt.stopPropagation();
				}
R
RubaXa 已提交
947

B
Billy Hardy 已提交
948
				ghostEl && ghostEl.parentNode && ghostEl.parentNode.removeChild(ghostEl);
R
RubaXa 已提交
949

L
Lebedev Konstantin 已提交
950 951
				if (rootEl === parentEl || Sortable.active.lastPullMode !== 'clone') {
					// Remove clone
B
Billy Hardy 已提交
952
					cloneEl && cloneEl.parentNode && cloneEl.parentNode.removeChild(cloneEl);
L
Lebedev Konstantin 已提交
953 954
				}

R
RubaXa 已提交
955
				if (dragEl) {
R
RubaXa 已提交
956
					if (this.nativeDraggable) {
957 958
						_off(dragEl, 'dragend', this);
					}
R
RubaXa 已提交
959

960
					_disableDraggable(dragEl);
L
Lebedev Konstantin 已提交
961
					dragEl.style['will-change'] = '';
R
RubaXa 已提交
962 963

					// Remove class's
R
RubaXa 已提交
964
					_toggleClass(dragEl, this.options.ghostClass, false);
R
RubaXa 已提交
965
					_toggleClass(dragEl, this.options.chosenClass, false);
R
RubaXa 已提交
966

967
					// Drag stop event
S
Sam Wray 已提交
968
					_dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, evt);
969

970
					if (rootEl !== parentEl) {
971
						newIndex = _index(dragEl, options.draggable);
R
RubaXa 已提交
972

973
						if (newIndex >= 0) {
974
							// Add event
S
Sam Wray 已提交
975
							_dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
976

977
							// Remove event
S
Sam Wray 已提交
978
							_dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
N
n00dl3 已提交
979

N
n00dl3 已提交
980
							// drag from one list and drop into another
S
Sam Wray 已提交
981 982
							_dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
							_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
983
						}
R
RubaXa 已提交
984
					}
R
RubaXa 已提交
985 986 987
					else {
						if (dragEl.nextSibling !== nextEl) {
							// Get the index of the dragged element within its parent
988
							newIndex = _index(dragEl, options.draggable);
989 990

							if (newIndex >= 0) {
991
								// drag & drop within the same list
S
Sam Wray 已提交
992 993
								_dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
								_dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
994
							}
R
RubaXa 已提交
995
						}
R
RubaXa 已提交
996
					}
997

R
RubaXa 已提交
998
					if (Sortable.active) {
999
						/* jshint eqnull:true */
L
Lebedev Konstantin 已提交
1000
						if (newIndex == null || newIndex === -1) {
R
RubaXa 已提交
1001 1002 1003
							newIndex = oldIndex;
						}

S
Sam Wray 已提交
1004
						_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
R
RubaXa 已提交
1005 1006 1007 1008

						// Save sorting
						this.save();
					}
R
RubaXa 已提交
1009
				}
R
RubaXa 已提交
1010

1011
			}
R
RubaXa 已提交
1012

1013 1014
			this._nulling();
		},
R
RubaXa 已提交
1015

D
desmaisons_david 已提交
1016
		_nulling: function() {
L
Lebedev Konstantin 已提交
1017
			rootEl =
D
desmaisons_david 已提交
1018 1019 1020 1021 1022 1023
			dragEl =
			parentEl =
			ghostEl =
			nextEl =
			cloneEl =
			lastDownEl =
R
RubaXa 已提交
1024

D
desmaisons_david 已提交
1025 1026
			scrollEl =
			scrollParentEl =
C
ChiefORZ 已提交
1027

D
desmaisons_david 已提交
1028 1029
			tapEvt =
			touchEvt =
R
RubaXa 已提交
1030

D
desmaisons_david 已提交
1031 1032
			moved =
			newIndex =
R
RubaXa 已提交
1033

D
desmaisons_david 已提交
1034 1035
			lastEl =
			lastCSS =
R
RubaXa 已提交
1036

D
desmaisons_david 已提交
1037 1038 1039
			putSortable =
			activeGroup =
			Sortable.active = null;
L
Lebedev Konstantin 已提交
1040 1041 1042 1043 1044

			savedInputChecked.forEach(function (el) {
				el.checked = true;
			});
			savedInputChecked.length = 0;
1045
		},
R
RubaXa 已提交
1046

R
RubaXa 已提交
1047
		handleEvent: function (/**Event*/evt) {
1048 1049 1050 1051 1052 1053 1054 1055
			switch (evt.type) {
				case 'drop':
				case 'dragend':
					this._onDrop(evt);
					break;

				case 'dragover':
				case 'dragenter':
D
desmaisons_david 已提交
1056 1057 1058 1059
					if (dragEl) {
						this._onDragOver(evt);
						_globalDragOver(evt);
					}
1060
					break;
R
RubaXa 已提交
1061

R
RubaXa 已提交
1062 1063 1064 1065
				case 'mouseover':
					this._onDrop(evt);
					break;

1066 1067 1068
				case 'selectstart':
					evt.preventDefault();
					break;
R
RubaXa 已提交
1069
			}
R
RubaXa 已提交
1070 1071 1072
		},


1073 1074 1075 1076 1077 1078 1079 1080 1081
		/**
		 * Serializes the item into an array of string.
		 * @returns {String[]}
		 */
		toArray: function () {
			var order = [],
				el,
				children = this.el.children,
				i = 0,
R
RubaXa 已提交
1082 1083
				n = children.length,
				options = this.options;
1084 1085 1086

			for (; i < n; i++) {
				el = children[i];
R
RubaXa 已提交
1087
				if (_closest(el, options.draggable, this.el)) {
R
RubaXa 已提交
1088
					order.push(el.getAttribute(options.dataIdAttr) || _generateId(el));
R
RubaXa 已提交
1089
				}
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
			}

			return order;
		},


		/**
		 * Sorts the elements according to the array.
		 * @param  {String[]}  order  order of the items
		 */
		sort: function (order) {
R
RubaXa 已提交
1101
			var items = {}, rootEl = this.el;
1102 1103

			this.toArray().forEach(function (id, i) {
R
RubaXa 已提交
1104 1105
				var el = rootEl.children[i];

R
RubaXa 已提交
1106
				if (_closest(el, this.options.draggable, rootEl)) {
R
RubaXa 已提交
1107 1108 1109
					items[id] = el;
				}
			}, this);
1110 1111 1112

			order.forEach(function (id) {
				if (items[id]) {
R
RubaXa 已提交
1113 1114
					rootEl.removeChild(items[id]);
					rootEl.appendChild(items[id]);
1115 1116 1117 1118 1119
				}
			});
		},


R
RubaXa 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128
		/**
		 * Save the current sorting
		 */
		save: function () {
			var store = this.options.store;
			store && store.set(this);
		},


1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		/**
		 * For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
		 * @param   {HTMLElement}  el
		 * @param   {String}       [selector]  default: `options.draggable`
		 * @returns {HTMLElement|null}
		 */
		closest: function (el, selector) {
			return _closest(el, selector || this.options.draggable, this.el);
		},


1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
		/**
		 * Set/get option
		 * @param   {string} name
		 * @param   {*}      [value]
		 * @returns {*}
		 */
		option: function (name, value) {
			var options = this.options;

			if (value === void 0) {
				return options[name];
			} else {
				options[name] = value;
R
RubaXa 已提交
1153 1154 1155 1156

				if (name === 'group') {
					_prepareGroup(options);
				}
1157 1158 1159 1160
			}
		},


1161 1162 1163 1164
		/**
		 * Destroy
		 */
		destroy: function () {
1165
			var el = this.el;
R
RubaXa 已提交
1166

1167
			el[expando] = null;
1168

R
RubaXa 已提交
1169 1170
			_off(el, 'mousedown', this._onTapStart);
			_off(el, 'touchstart', this._onTapStart);
1171
			_off(el, 'pointerdown', this._onTapStart);
R
RubaXa 已提交
1172

R
RubaXa 已提交
1173
			if (this.nativeDraggable) {
1174 1175 1176
				_off(el, 'dragover', this);
				_off(el, 'dragenter', this);
			}
R
RubaXa 已提交
1177

1178
			// Remove draggable attributes
R
RubaXa 已提交
1179
			Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
1180 1181 1182
				el.removeAttribute('draggable');
			});

R
RubaXa 已提交
1183 1184 1185 1186
			touchDragOverListeners.splice(touchDragOverListeners.indexOf(this._onDragOver), 1);

			this._onDrop();

1187
			this.el = el = null;
R
RubaXa 已提交
1188 1189 1190
		}
	};

1191

L
Lebedev Konstantin 已提交
1192
	function _cloneHide(sortable, state) {
1193 1194 1195 1196
		if (sortable.lastPullMode !== 'clone') {
			state = true;
		}

R
RubaXa 已提交
1197 1198
		if (cloneEl && (cloneEl.state !== state)) {
			_css(cloneEl, 'display', state ? 'none' : '');
L
Lebedev Konstantin 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

			if (!state) {
				if (cloneEl.state) {
					if (sortable.options.group.revertClone) {
						rootEl.insertBefore(cloneEl, nextEl);
						sortable._animate(dragEl, cloneEl);
					} else {
						rootEl.insertBefore(cloneEl, dragEl);
					}
				}
			}

R
RubaXa 已提交
1211 1212 1213 1214 1215
			cloneEl.state = state;
		}
	}


R
RubaXa 已提交
1216
	function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) {
R
RubaXa 已提交
1217
		if (el) {
R
RubaXa 已提交
1218 1219 1220
			ctx = ctx || document;

			do {
R
RubaXa 已提交
1221
				if ((selector === '>*' && el.parentNode === ctx) || _matches(el, selector)) {
R
RubaXa 已提交
1222
					return el;
R
RubaXa 已提交
1223
				}
R
#930  
RubaXa 已提交
1224 1225
				/* jshint boss:true */
			} while (el = _getParentOrHost(el));
R
RubaXa 已提交
1226 1227
		}

R
RubaXa 已提交
1228
		return null;
R
RubaXa 已提交
1229 1230 1231
	}


N
Nikita SVIRIDENKO 已提交
1232 1233 1234 1235 1236
	function _getParentOrHost(el) {
		var parent = el.host;

		return (parent && parent.nodeType) ? parent : el.parentNode;
	}
1237 1238


1239
	function _globalDragOver(/**Event*/evt) {
1240 1241 1242
		if (evt.dataTransfer) {
			evt.dataTransfer.dropEffect = 'move';
		}
R
RubaXa 已提交
1243
		evt.preventDefault();
R
RubaXa 已提交
1244 1245 1246
	}


R
RubaXa 已提交
1247
	function _on(el, event, fn) {
1248
		el.addEventListener(event, fn, captureMode);
R
RubaXa 已提交
1249 1250 1251
	}


R
RubaXa 已提交
1252
	function _off(el, event, fn) {
1253
		el.removeEventListener(event, fn, captureMode);
R
RubaXa 已提交
1254 1255 1256
	}


R
RubaXa 已提交
1257 1258 1259
	function _toggleClass(el, name, state) {
		if (el) {
			if (el.classList) {
R
RubaXa 已提交
1260 1261 1262
				el.classList[state ? 'add' : 'remove'](name);
			}
			else {
1263 1264
				var className = (' ' + el.className + ' ').replace(R_SPACE, ' ').replace(' ' + name + ' ', ' ');
				el.className = (className + (state ? ' ' + name : '')).replace(R_SPACE, ' ');
R
RubaXa 已提交
1265 1266 1267 1268 1269
			}
		}
	}


R
RubaXa 已提交
1270
	function _css(el, prop, val) {
R
RubaXa 已提交
1271 1272
		var style = el && el.style;

R
RubaXa 已提交
1273 1274 1275
		if (style) {
			if (val === void 0) {
				if (document.defaultView && document.defaultView.getComputedStyle) {
R
RubaXa 已提交
1276 1277
					val = document.defaultView.getComputedStyle(el, '');
				}
R
RubaXa 已提交
1278 1279
				else if (el.currentStyle) {
					val = el.currentStyle;
R
RubaXa 已提交
1280
				}
R
RubaXa 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289

				return prop === void 0 ? val : val[prop];
			}
			else {
				if (!(prop in style)) {
					prop = '-webkit-' + prop;
				}

				style[prop] = val + (typeof val === 'string' ? '' : 'px');
R
RubaXa 已提交
1290 1291 1292 1293 1294
			}
		}
	}


R
RubaXa 已提交
1295 1296
	function _find(ctx, tagName, iterator) {
		if (ctx) {
R
RubaXa 已提交
1297
			var list = ctx.getElementsByTagName(tagName), i = 0, n = list.length;
R
RubaXa 已提交
1298

R
RubaXa 已提交
1299 1300
			if (iterator) {
				for (; i < n; i++) {
R
RubaXa 已提交
1301 1302 1303
					iterator(list[i], i);
				}
			}
R
RubaXa 已提交
1304

R
RubaXa 已提交
1305
			return list;
R
RubaXa 已提交
1306
		}
R
RubaXa 已提交
1307 1308

		return [];
R
RubaXa 已提交
1309 1310 1311
	}


R
RubaXa 已提交
1312

S
Sam Wray 已提交
1313
	function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt) {
1314 1315
		sortable = (sortable || rootEl[expando]);

R
RubaXa 已提交
1316
		var evt = document.createEvent('Event'),
1317
			options = sortable.options,
R
RubaXa 已提交
1318 1319 1320 1321
			onName = 'on' + name.charAt(0).toUpperCase() + name.substr(1);

		evt.initEvent(name, true, true);

J
Joey Becker 已提交
1322
		evt.to = toEl || rootEl;
R
RubaXa 已提交
1323 1324 1325 1326 1327 1328 1329
		evt.from = fromEl || rootEl;
		evt.item = targetEl || rootEl;
		evt.clone = cloneEl;

		evt.oldIndex = startIndex;
		evt.newIndex = newIndex;

1330 1331
		evt.originalEvent = originalEvt;

R
RubaXa 已提交
1332 1333
		rootEl.dispatchEvent(evt);

R
RubaXa 已提交
1334 1335 1336 1337 1338 1339
		if (options[onName]) {
			options[onName].call(sortable, evt);
		}
	}


1340
	function _onMove(fromEl, toEl, dragEl, dragRect, targetEl, targetRect, originalEvt, willInsertAfter) {
R
RubaXa 已提交
1341 1342 1343 1344 1345
		var evt,
			sortable = fromEl[expando],
			onMoveFn = sortable.options.onMove,
			retVal;

C
ChiefORZ 已提交
1346 1347
		evt = document.createEvent('Event');
		evt.initEvent('move', true, true);
R
RubaXa 已提交
1348

C
ChiefORZ 已提交
1349 1350 1351 1352 1353 1354
		evt.to = toEl;
		evt.from = fromEl;
		evt.dragged = dragEl;
		evt.draggedRect = dragRect;
		evt.related = targetEl || toEl;
		evt.relatedRect = targetRect || toEl.getBoundingClientRect();
1355
		evt.willInsertAfter = willInsertAfter;
R
RubaXa 已提交
1356

1357 1358
		evt.originalEvent = originalEvt;

C
ChiefORZ 已提交
1359
		fromEl.dispatchEvent(evt);
R
RubaXa 已提交
1360

C
ChiefORZ 已提交
1361
		if (onMoveFn) {
1362
			retVal = onMoveFn.call(sortable, evt, originalEvt);
R
RubaXa 已提交
1363 1364
		}

R
RubaXa 已提交
1365
		return retVal;
R
RubaXa 已提交
1366 1367 1368
	}


R
RubaXa 已提交
1369
	function _disableDraggable(el) {
R
RubaXa 已提交
1370
		el.draggable = false;
R
RubaXa 已提交
1371 1372 1373
	}


R
RubaXa 已提交
1374
	function _unsilent() {
R
RubaXa 已提交
1375 1376 1377 1378
		_silent = false;
	}


R
RubaXa 已提交
1379
	/** @returns {HTMLElement|false} */
1380
	function _ghostIsLast(el, evt) {
R
RubaXa 已提交
1381
		var lastEl = el.lastElementChild,
R
RubaXa 已提交
1382
			rect = lastEl.getBoundingClientRect();
R
RubaXa 已提交
1383

R
RubaXa 已提交
1384
		// 5 — min delta
R
RubaXa 已提交
1385
		// abs — нельзя добавлять, а то глюки при наведении сверху
1386 1387
		return (evt.clientY - (rect.top + rect.height) > 5) ||
			(evt.clientX - (rect.left + rect.width) > 5);
R
RubaXa 已提交
1388 1389 1390
	}


1391 1392 1393 1394 1395 1396 1397
	/**
	 * Generate id
	 * @param   {HTMLElement} el
	 * @returns {String}
	 * @private
	 */
	function _generateId(el) {
R
RubaXa 已提交
1398
		var str = el.tagName + el.className + el.src + el.href + el.textContent,
1399
			i = str.length,
R
RubaXa 已提交
1400
			sum = 0;
1401

1402 1403 1404
		while (i--) {
			sum += str.charCodeAt(i);
		}
1405

1406 1407 1408
		return sum.toString(36);
	}

1409
	/**
1410 1411
	 * Returns the index of an element within its parent for a selected set of
	 * elements
1412
	 * @param  {HTMLElement} el
1413
	 * @param  {selector} selector
1414
	 * @return {number}
1415
	 */
1416
	function _index(el, selector) {
1417 1418
		var index = 0;

1419 1420 1421
		if (!el || !el.parentNode) {
			return -1;
		}
1422

1423
		while (el && (el = el.previousElementSibling)) {
R
RubaXa 已提交
1424
			if ((el.nodeName.toUpperCase() !== 'TEMPLATE') && (selector === '>*' || _matches(el, selector))) {
1425 1426
				index++;
			}
1427
		}
1428

1429 1430
		return index;
	}
R
RubaXa 已提交
1431

1432
	function _matches(/**HTMLElement*/el, /**String*/selector) {
1433
		if (el) {
1434 1435 1436 1437 1438
			if (el.matches) {
				return el.matches(selector);
			} else if (el.msMatchesSelector) {
				return el.msMatchesSelector(selector);
			}
1439
		}
1440 1441

		return false;
1442 1443
	}

R
RubaXa 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
	function _throttle(callback, ms) {
		var args, _this;

		return function () {
			if (args === void 0) {
				args = arguments;
				_this = this;

				setTimeout(function () {
					if (args.length === 1) {
						callback.call(_this, args[0]);
					} else {
						callback.apply(_this, args);
					}

					args = void 0;
				}, ms);
			}
		};
	}

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	function _extend(dst, src) {
		if (dst && src) {
			for (var key in src) {
				if (src.hasOwnProperty(key)) {
					dst[key] = src[key];
				}
			}
		}

		return dst;
	}

1477
	function _clone(el) {
C
camargo 已提交
1478
		if (Polymer && Polymer.dom) {
C
camargo 已提交
1479
			return Polymer.dom(el).cloneNode(true);
C
camargo 已提交
1480 1481
		}
		else if ($) {
C
camargo 已提交
1482
			return $(el).clone(true)[0];
C
camargo 已提交
1483 1484
		}
		else {
C
camargo 已提交
1485
			return el.cloneNode(true);
C
camargo 已提交
1486
		}
1487 1488
	}

L
Lebedev Konstantin 已提交
1489
	function _saveInputCheckedState(root) {
1490
		savedInputChecked.length = 0;
1491

L
Lebedev Konstantin 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500
		var inputs = root.getElementsByTagName('input');
		var idx = inputs.length;

		while (idx--) {
			var el = inputs[idx];
			el.checked && savedInputChecked.push(el);
		}
	}

R
RubaXa 已提交
1501
	function _nextTick(fn) {
R
RubaXa 已提交
1502 1503 1504 1505 1506
		return setTimeout(fn, 0);
	}

	function _cancelNextTick(id) {
		return clearTimeout(id);
R
RubaXa 已提交
1507 1508
	}

C
camargo 已提交
1509
	// Fixed #973:
L
Lebedev Konstantin 已提交
1510 1511 1512 1513 1514 1515
	_on(document, 'touchmove', function (evt) {
		if (Sortable.active) {
			evt.preventDefault();
		}
	});

R
RubaXa 已提交
1516 1517 1518 1519 1520 1521
	// Export utils
	Sortable.utils = {
		on: _on,
		off: _off,
		css: _css,
		find: _find,
R
RubaXa 已提交
1522 1523 1524
		is: function (el, selector) {
			return !!_closest(el, selector, el);
		},
1525
		extend: _extend,
R
RubaXa 已提交
1526
		throttle: _throttle,
R
RubaXa 已提交
1527
		closest: _closest,
1528
		toggleClass: _toggleClass,
1529
		clone: _clone,
R
RubaXa 已提交
1530
		index: _index,
R
RubaXa 已提交
1531 1532
		nextTick: _nextTick,
		cancelNextTick: _cancelNextTick
R
RubaXa 已提交
1533 1534 1535
	};


1536 1537 1538 1539 1540 1541
	/**
	 * Create sortable instance
	 * @param {HTMLElement}  el
	 * @param {Object}      [options]
	 */
	Sortable.create = function (el, options) {
R
RubaXa 已提交
1542
		return new Sortable(el, options);
1543
	};
R
RubaXa 已提交
1544

R
RubaXa 已提交
1545 1546

	// Export
1547
	Sortable.version = '1.7.0';
1548
	return Sortable;
R
RubaXa 已提交
1549
});