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


R
RubaXa 已提交
8
(function (factory) {
R
RubaXa 已提交
9 10
	"use strict";

R
RubaXa 已提交
11
	if (typeof define === "function" && define.amd) {
R
RubaXa 已提交
12
		define(factory);
R
RubaXa 已提交
13
	}
R
RubaXa 已提交
14
	else if (typeof module != "undefined" && typeof module.exports != "undefined") {
S
Scott Nelson 已提交
15 16
		module.exports = factory();
	}
R
RubaXa 已提交
17
	else if (typeof Package !== "undefined") {
18 19
		Sortable = factory();  // export for Meteor.js
	}
R
RubaXa 已提交
20
	else {
R
RubaXa 已提交
21
		/* jshint sub:true */
R
RubaXa 已提交
22 23
		window["Sortable"] = factory();
	}
R
RubaXa 已提交
24
})(function () {
R
RubaXa 已提交
25 26
	"use strict";

R
RubaXa 已提交
27 28 29 30
	var dragEl,
		ghostEl,
		cloneEl,
		rootEl,
R
RubaXa 已提交
31
		scrollEl,
R
RubaXa 已提交
32
		nextEl,
R
RubaXa 已提交
33

R
RubaXa 已提交
34 35
		lastEl,
		lastCSS,
R
RubaXa 已提交
36

R
RubaXa 已提交
37 38 39
		oldIndex,
		newIndex,

R
RubaXa 已提交
40
		activeGroup,
R
RubaXa 已提交
41
		autoScroll = {},
R
RubaXa 已提交
42

R
RubaXa 已提交
43 44
		tapEvt,
		touchEvt,
R
RubaXa 已提交
45

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

R
RubaXa 已提交
48 49 50
		win = window,
		document = win.document,
		parseInt = win.parseInt,
R
RubaXa 已提交
51 52 53

		supportDraggable = !!('draggable' in document.createElement('div')),

R
RubaXa 已提交
54

R
RubaXa 已提交
55
		_silent = false,
R
RubaXa 已提交
56

57
		_dispatchEvent = function (rootEl, name, targetEl, fromEl, startIndex, newIndex) {
R
RubaXa 已提交
58
			var evt = document.createEvent('Event');
R
RubaXa 已提交
59

60
			evt.initEvent(name, true, true);
R
RubaXa 已提交
61

62 63
			evt.item = targetEl || rootEl;
			evt.from = fromEl || rootEl;
R
RubaXa 已提交
64
			evt.clone = cloneEl;
R
RubaXa 已提交
65 66 67

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

69
			rootEl.dispatchEvent(evt);
R
RubaXa 已提交
70
		},
71

R
RubaXa 已提交
72
		_customEvents = 'onAdd onUpdate onRemove onStart onEnd onFilter onSort'.split(' '),
R
RubaXa 已提交
73

R
RubaXa 已提交
74
		noop = function () {},
R
RubaXa 已提交
75 76

		abs = Math.abs,
R
RubaXa 已提交
77
		slice = [].slice,
R
RubaXa 已提交
78

R
RubaXa 已提交
79
		touchDragOverListeners = []
R
RubaXa 已提交
80 81 82
	;


83

R
RubaXa 已提交
84 85 86
	/**
	 * @class  Sortable
	 * @param  {HTMLElement}  el
87
	 * @param  {Object}       [options]
R
RubaXa 已提交
88
	 */
R
RubaXa 已提交
89
	function Sortable(el, options) {
R
RubaXa 已提交
90 91 92 93
		this.el = el; // root element
		this.options = options = (options || {});


R
RubaXa 已提交
94
		// Default options
95 96
		var defaults = {
			group: Math.random(),
R
RubaXa 已提交
97
			sort: true,
R
RubaXa 已提交
98
			disabled: false,
99 100
			store: null,
			handle: null,
R
RubaXa 已提交
101 102 103
			scroll: true,
			scrollSensitivity: 30,
			scrollSpeed: 10,
R
RubaXa 已提交
104
			draggable: /[uo]l/i.test(el.nodeName) ? 'li' : '>*',
105 106
			ghostClass: 'sortable-ghost',
			ignore: 'a, img',
107
			filter: null,
R
RubaXa 已提交
108 109 110
			animation: 0,
			setData: function (dataTransfer, dragEl) {
				dataTransfer.setData('Text', dragEl.textContent);
111 112 113
			},
			dropBubble: false,
			dragoverBubble: false
R
RubaXa 已提交
114
		};
115

R
RubaXa 已提交
116

117 118
		// Set default options
		for (var name in defaults) {
R
RubaXa 已提交
119
			!(name in options) && (options[name] = defaults[name]);
120
		}
R
RubaXa 已提交
121

122

R
RubaXa 已提交
123 124
		var group = options.group;

R
RubaXa 已提交
125 126
		if (!group || typeof group != 'object') {
			group = options.group = { name: group };
R
RubaXa 已提交
127 128
		}

R
RubaXa 已提交
129

R
RubaXa 已提交
130
		['pull', 'put'].forEach(function (key) {
R
RubaXa 已提交
131 132
			if (!(key in group)) {
				group[key] = true;
R
RubaXa 已提交
133 134 135 136
			}
		});


137
		// Define events
138
		_customEvents.forEach(function (name) {
139
			options[name] = _bind(this, options[name] || noop);
140
			_on(el, name.substr(2).toLowerCase(), options[name]);
R
RubaXa 已提交
141
		}, this);
R
RubaXa 已提交
142 143


R
* JSDoc  
RubaXa 已提交
144
		// Export group name
R
RubaXa 已提交
145
		el[expando] = group.name + ' ' + (group.put.join ? group.put.join(' ') : '');
R
RubaXa 已提交
146 147


R
* JSDoc  
RubaXa 已提交
148
		// Bind all private methods
R
RubaXa 已提交
149 150
		for (var fn in this) {
			if (fn.charAt(0) === '_') {
R
RubaXa 已提交
151 152 153 154 155 156 157 158 159
				this[fn] = _bind(this, this[fn]);
			}
		}


		// Bind events
		_on(el, 'mousedown', this._onTapStart);
		_on(el, 'touchstart', this._onTapStart);

R
RubaXa 已提交
160 161
		_on(el, 'dragover', this);
		_on(el, 'dragenter', this);
R
RubaXa 已提交
162 163

		touchDragOverListeners.push(this._onDragOver);
164 165 166

		// Restore sorting
		options.store && this.sort(options.store.get(this));
R
RubaXa 已提交
167 168 169
	}


170
	Sortable.prototype = /** @lends Sortable.prototype */ {
R
RubaXa 已提交
171 172 173
		constructor: Sortable,


R
RubaXa 已提交
174
		_dragStarted: function () {
R
RubaXa 已提交
175 176 177
			if (rootEl && dragEl) {
				// Apply effect
				_toggleClass(dragEl, this.options.ghostClass, true);
R
RubaXa 已提交
178

R
RubaXa 已提交
179
				Sortable.active = this;
R
RubaXa 已提交
180

R
RubaXa 已提交
181 182 183
				// Drag start event
				_dispatchEvent(rootEl, 'start', dragEl, rootEl, oldIndex);
			}
R
RubaXa 已提交
184 185 186
		},


R
RubaXa 已提交
187
		_onTapStart: function (/**Event|TouchEvent*/evt) {
R
RubaXa 已提交
188 189
			var type = evt.type,
				touch = evt.touches && evt.touches[0],
R
RubaXa 已提交
190
				target = (touch || evt).target,
191
				originalTarget = target,
R
RubaXa 已提交
192 193 194
				options =  this.options,
				el = this.el,
				filter = options.filter;
R
RubaXa 已提交
195

R
RubaXa 已提交
196
			if (type === 'mousedown' && evt.button !== 0 || options.disabled) {
R
RubaXa 已提交
197
				return; // only left button or enabled
R
RubaXa 已提交
198 199
			}

200 201 202 203 204 205 206
			if (options.handle) {
				target = _closest(target, options.handle, el);
			}

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

			// get the index of the dragged element within its parent
R
RubaXa 已提交
207
			oldIndex = _index(target);
208

209
			// Check filter
R
RubaXa 已提交
210
			if (typeof filter === 'function') {
R
RubaXa 已提交
211
				if (filter.call(this, evt, target, this)) {
R
RubaXa 已提交
212
					_dispatchEvent(originalTarget, 'filter', target, el, oldIndex);
R
RubaXa 已提交
213
					evt.preventDefault();
R
RubaXa 已提交
214 215
					return; // cancel dnd
				}
216
			}
R
RubaXa 已提交
217
			else if (filter) {
R
RubaXa 已提交
218 219 220 221
				filter = filter.split(',').some(function (criteria) {
					criteria = _closest(originalTarget, criteria.trim(), el);

					if (criteria) {
R
RubaXa 已提交
222
						_dispatchEvent(criteria, 'filter', target, el, oldIndex);
R
RubaXa 已提交
223 224
						return true;
					}
225 226
				});

R
RubaXa 已提交
227 228
				if (filter) {
					evt.preventDefault();
229 230 231 232
					return; // cancel dnd
				}
			}

R
RubaXa 已提交
233
			// Prepare `dragstart`
R
RubaXa 已提交
234
			if (target && !dragEl && (target.parentNode === el)) {
R
RubaXa 已提交
235
				tapEvt = evt;
236 237 238 239 240 241 242

				rootEl = this.el;
				dragEl = target;
				nextEl = dragEl.nextSibling;
				activeGroup = this.options.group;

				dragEl.draggable = true;
R
RubaXa 已提交
243 244

				// Disable "draggable"
245
				options.ignore.split(',').forEach(function (criteria) {
Z
ziflex 已提交
246 247
					_find(target, criteria.trim(), _disableDraggable);
				});
R
RubaXa 已提交
248

R
RubaXa 已提交
249
				if (touch) {
R
RubaXa 已提交
250 251
					// Touch device support
					tapEvt = {
R
RubaXa 已提交
252 253 254
						target: target,
						clientX: touch.clientX,
						clientY: touch.clientY
R
RubaXa 已提交
255
					};
256

R
RubaXa 已提交
257
					this._onDragStart(tapEvt, 'touch');
R
RubaXa 已提交
258 259
					evt.preventDefault();
				}
R
RubaXa 已提交
260

261 262 263
				_on(document, 'mouseup', this._onDrop);
				_on(document, 'touchend', this._onDrop);
				_on(document, 'touchcancel', this._onDrop);
R
RubaXa 已提交
264

R
RubaXa 已提交
265
				_on(dragEl, 'dragend', this);
R
RubaXa 已提交
266 267
				_on(rootEl, 'dragstart', this._onDragStart);

R
RubaXa 已提交
268 269 270
				if (!supportDraggable) {
					this._onDragStart(tapEvt, true);
				}
R
RubaXa 已提交
271 272

				try {
R
RubaXa 已提交
273
					if (document.selection) {
R
RubaXa 已提交
274 275
						document.selection.empty();
					} else {
R
RubaXa 已提交
276
						window.getSelection().removeAllRanges();
R
RubaXa 已提交
277
					}
R
RubaXa 已提交
278 279
				} catch (err) {
				}
R
RubaXa 已提交
280 281 282
			}
		},

R
RubaXa 已提交
283 284
		_emulateDragOver: function () {
			if (touchEvt) {
R
RubaXa 已提交
285 286
				_css(ghostEl, 'display', 'none');

R
RubaXa 已提交
287
				var target = document.elementFromPoint(touchEvt.clientX, touchEvt.clientY),
R
RubaXa 已提交
288
					parent = target,
R
RubaXa 已提交
289 290
					groupName = this.options.group.name,
					i = touchDragOverListeners.length;
R
RubaXa 已提交
291

R
RubaXa 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
				if (parent) {
					do {
						if ((' ' + parent[expando] + ' ').indexOf(groupName) > -1) {
							while (i--) {
								touchDragOverListeners[i]({
									clientX: touchEvt.clientX,
									clientY: touchEvt.clientY,
									target: target,
									rootEl: parent
								});
							}

							break;
						}

						target = parent; // store last element
L
Larry Davis 已提交
308
					}
R
RubaXa 已提交
309 310
					/* jshint boss:true */
					while (parent = parent.parentNode);
R
RubaXa 已提交
311 312 313 314 315 316 317
				}

				_css(ghostEl, 'display', '');
			}
		},


R
RubaXa 已提交
318 319
		_onTouchMove: function (/**TouchEvent*/evt) {
			if (tapEvt) {
R
RubaXa 已提交
320
				var touch = evt.touches ? evt.touches[0] : evt,
R
RubaXa 已提交
321 322
					dx = touch.clientX - tapEvt.clientX,
					dy = touch.clientY - tapEvt.clientY,
R
RubaXa 已提交
323
					translate3d = evt.touches ? 'translate3d(' + dx + 'px,' + dy + 'px,0)' : 'translate(' + dx + 'px,' + dy + 'px)';
R
RubaXa 已提交
324 325

				touchEvt = touch;
R
RubaXa 已提交
326 327 328 329 330 331

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

R
RubaXa 已提交
332
				this._onDrag(touch);
M
Marius Petcu 已提交
333
				evt.preventDefault();
R
RubaXa 已提交
334 335 336 337
			}
		},


R
RubaXa 已提交
338
		_onDragStart: function (/**Event*/evt, /**boolean*/useFallback) {
R
RubaXa 已提交
339 340
			var dataTransfer = evt.dataTransfer,
				options = this.options;
R
RubaXa 已提交
341

342
			this._offUpEvents();
R
RubaXa 已提交
343

R
RubaXa 已提交
344 345 346 347 348 349
			if (activeGroup.pull == 'clone') {
				cloneEl = dragEl.cloneNode(true);
				_css(cloneEl, 'display', 'none');
				rootEl.insertBefore(cloneEl, dragEl);
			}

R
RubaXa 已提交
350
			if (useFallback) {
R
RubaXa 已提交
351 352 353
				var rect = dragEl.getBoundingClientRect(),
					css = _css(dragEl),
					ghostRect;
R
RubaXa 已提交
354

355
				ghostEl = dragEl.cloneNode(true);
R
RubaXa 已提交
356 357 358

				_css(ghostEl, 'top', rect.top - parseInt(css.marginTop, 10));
				_css(ghostEl, 'left', rect.left - parseInt(css.marginLeft, 10));
R
RubaXa 已提交
359 360
				_css(ghostEl, 'width', rect.width);
				_css(ghostEl, 'height', rect.height);
R
RubaXa 已提交
361 362 363 364
				_css(ghostEl, 'opacity', '0.8');
				_css(ghostEl, 'position', 'fixed');
				_css(ghostEl, 'zIndex', '100000');

R
RubaXa 已提交
365 366 367 368
				rootEl.appendChild(ghostEl);

				// Fixing dimensions.
				ghostRect = ghostEl.getBoundingClientRect();
R
RubaXa 已提交
369 370
				_css(ghostEl, 'width', rect.width * 2 - ghostRect.width);
				_css(ghostEl, 'height', rect.height * 2 - ghostRect.height);
R
RubaXa 已提交
371

R
RubaXa 已提交
372 373 374 375 376 377 378 379 380 381
				if (useFallback === 'touch') {
					// Bind touch events
					_on(document, 'touchmove', this._onTouchMove);
					_on(document, 'touchend', this._onDrop);
					_on(document, 'touchcancel', this._onDrop);
				} else {
					// Old brwoser
					_on(document, 'mousemove', this._onTouchMove);
					_on(document, 'mouseup', this._onDrop);
				}
R
RubaXa 已提交
382

R
RubaXa 已提交
383
				this._loopId = setInterval(this._emulateDragOver, 150);
R
RubaXa 已提交
384 385
			}
			else {
R
RubaXa 已提交
386 387 388 389
				if (dataTransfer) {
					dataTransfer.effectAllowed = 'move';
					options.setData && options.setData.call(this, dataTransfer, dragEl);
				}
R
RubaXa 已提交
390

R
RubaXa 已提交
391
				_on(document, 'drop', this);
R
RubaXa 已提交
392 393
			}

R
RubaXa 已提交
394 395 396
			scrollEl = options.scroll;

			if (scrollEl === true) {
R
RubaXa 已提交
397
				scrollEl = rootEl;
R
RubaXa 已提交
398 399 400 401 402 403 404 405 406 407

				do {
					if ((scrollEl.offsetWidth < scrollEl.scrollWidth) ||
						(scrollEl.offsetHeight < scrollEl.scrollHeight)
					) {
						break;
					}
				/* jshint boss:true */
				} while (scrollEl = scrollEl.parentNode);
			}
R
RubaXa 已提交
408 409

			setTimeout(this._dragStarted, 0);
R
RubaXa 已提交
410 411
		},

R
RubaXa 已提交
412
		_onDrag: _throttle(function (/**Event*/evt) {
R
RubaXa 已提交
413
			// Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=505521
R
RubaXa 已提交
414
			if (rootEl && this.options.scroll) {
R
RubaXa 已提交
415 416 417
				var el,
					rect,
					options = this.options,
R
RubaXa 已提交
418 419 420 421 422 423 424 425 426
					sens = options.scrollSensitivity,
					speed = options.scrollSpeed,

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

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

R
RubaXa 已提交
427 428
					vx,
					vy
R
RubaXa 已提交
429 430
				;

R
RubaXa 已提交
431 432

				if (scrollEl) {
R
RubaXa 已提交
433
					el = scrollEl;
R
RubaXa 已提交
434 435 436
					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);
R
RubaXa 已提交
437 438
				}

R
RubaXa 已提交
439 440 441 442 443 444 445 446 447 448

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

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


R
RubaXa 已提交
449 450 451 452
				if (autoScroll.vx !== vx || autoScroll.vy !== vy || autoScroll.el !== el) {
					autoScroll.el = el;
					autoScroll.vx = vx;
					autoScroll.vy = vy;
R
RubaXa 已提交
453

R
RubaXa 已提交
454 455 456 457 458 459 460 461 462 463 464 465
					clearInterval(autoScroll.pid);

					if (el) {
						autoScroll.pid = setInterval(function () {
							if (el === win) {
								win.scrollTo(win.scrollX + vx * speed, win.scrollY + vy * speed);
							} else {
								vy && (el.scrollTop += vy * speed);
								vx && (el.scrollLeft += vx * speed);
							}
						}, 24);
					}
R
RubaXa 已提交
466 467 468 469
				}
			}
		}, 30),

R
RubaXa 已提交
470

R
RubaXa 已提交
471
		_onDragOver: function (/**Event*/evt) {
R
RubaXa 已提交
472 473 474 475 476 477
			var el = this.el,
				target,
				dragRect,
				revert,
				options = this.options,
				group = options.group,
R
RubaXa 已提交
478
				groupPut = group.put,
479 480
				isOwner = (activeGroup === group),
				canSort = options.sort;
R
RubaXa 已提交
481

482 483 484 485
			if (evt.dataTransfer && evt.dataTransfer.effectAllowed !== 'move') {
				return;
			}

R
RubaXa 已提交
486 487
			if (evt.preventDefault !== void 0) {
				evt.preventDefault();
488
				!options.dragoverBubble && evt.stopPropagation();
R
RubaXa 已提交
489
			}
R
RubaXa 已提交
490

R
RubaXa 已提交
491
			if (!_silent && activeGroup && !options.disabled &&
492 493
				(isOwner
					? canSort || (revert = !rootEl.contains(dragEl))
R
RubaXa 已提交
494 495 496 497
					: activeGroup.pull && groupPut && (
						(activeGroup.name === group.name) || // by Name
						(groupPut.indexOf && ~groupPut.indexOf(activeGroup.name)) // by Array
					)
498
				) &&
R
RubaXa 已提交
499
				(evt.rootEl === void 0 || evt.rootEl === this.el)
R
RubaXa 已提交
500
			) {
R
RubaXa 已提交
501
				target = _closest(evt.target, options.draggable, el);
R
RubaXa 已提交
502 503 504
				dragRect = dragEl.getBoundingClientRect();


505
				if (revert) {
R
RubaXa 已提交
506 507
					_cloneHide(true);

508 509 510 511 512 513 514
					if (cloneEl || nextEl) {
						rootEl.insertBefore(dragEl, cloneEl || nextEl);
					}
					else if (!canSort) {
						rootEl.appendChild(dragEl);
					}

R
RubaXa 已提交
515 516
					return;
				}
R
RubaXa 已提交
517

R
RubaXa 已提交
518

R
RubaXa 已提交
519
				if ((el.children.length === 0) || (el.children[0] === ghostEl) ||
R
RubaXa 已提交
520
					(el === evt.target) && (target = _ghostInBottom(el, evt))
R
RubaXa 已提交
521
				) {
R
RubaXa 已提交
522 523 524 525 526 527
					if (target) {
						if (target.animated) {
							return;
						}
						targetRect = target.getBoundingClientRect();
					}
R
RubaXa 已提交
528

R
RubaXa 已提交
529 530
					_cloneHide(isOwner);

R
RubaXa 已提交
531
					el.appendChild(dragEl);
R
* anim  
RubaXa 已提交
532
					this._animate(dragRect, dragEl);
R
RubaXa 已提交
533
					target && this._animate(targetRect, target);
R
RubaXa 已提交
534
				}
R
RubaXa 已提交
535 536
				else if (target && !target.animated && target !== dragEl && (target.parentNode[expando] !== void 0)) {
					if (lastEl !== target) {
R
RubaXa 已提交
537
						lastEl = target;
R
RubaXa 已提交
538
						lastCSS = _css(target);
R
RubaXa 已提交
539 540 541
					}


R
RubaXa 已提交
542 543 544 545 546 547 548 549 550
					var targetRect = target.getBoundingClientRect(),
						width = targetRect.right - targetRect.left,
						height = targetRect.bottom - targetRect.top,
						floating = /left|right|inline/.test(lastCSS.cssFloat + lastCSS.display),
						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,
						after
R
RubaXa 已提交
551
					;
R
RubaXa 已提交
552

R
RubaXa 已提交
553 554 555
					_silent = true;
					setTimeout(_unsilent, 30);

R
RubaXa 已提交
556 557
					_cloneHide(isOwner);

R
RubaXa 已提交
558 559
					if (floating) {
						after = (target.previousElementSibling === dragEl) && !isWide || halfway && isWide;
R
RubaXa 已提交
560
					} else {
R
RubaXa 已提交
561
						after = (nextSibling !== dragEl) && !isLong || halfway && isLong;
R
RubaXa 已提交
562 563
					}

R
RubaXa 已提交
564
					if (after && !nextSibling) {
R
RubaXa 已提交
565 566 567
						el.appendChild(dragEl);
					} else {
						target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
R
RubaXa 已提交
568
					}
R
RubaXa 已提交
569

R
RubaXa 已提交
570 571
					this._animate(dragRect, dragEl);
					this._animate(targetRect, target);
R
RubaXa 已提交
572 573 574 575
				}
			}
		},

576 577 578 579 580 581
		_animate: function (prevRect, target) {
			var ms = this.options.animation;

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

R
RubaXa 已提交
582
				_css(target, 'transition', 'none');
583 584 585 586 587 588 589
				_css(target, 'transform', 'translate3d('
					+ (prevRect.left - currentRect.left) + 'px,'
					+ (prevRect.top - currentRect.top) + 'px,0)'
				);

				target.offsetWidth; // repaint

R
RubaXa 已提交
590
				_css(target, 'transition', 'all ' + ms + 'ms');
591 592
				_css(target, 'transform', 'translate3d(0,0,0)');

R
* anim  
RubaXa 已提交
593 594
				clearTimeout(target.animated);
				target.animated = setTimeout(function () {
595 596 597 598 599 600
					_css(target, 'transition', '');
					target.animated = false;
				}, ms);
			}
		},

601 602 603 604 605 606
		_offUpEvents: function () {
			_off(document, 'mouseup', this._onDrop);
			_off(document, 'touchmove', this._onTouchMove);
			_off(document, 'touchend', this._onDrop);
			_off(document, 'touchcancel', this._onDrop);
		},
R
RubaXa 已提交
607

R
RubaXa 已提交
608
		_onDrop: function (/**Event*/evt) {
609 610
			var el = this.el,
				options = this.options;
R
RubaXa 已提交
611

R
RubaXa 已提交
612
			clearInterval(this._loopId);
R
RubaXa 已提交
613
			clearInterval(autoScroll.pid);
R
RubaXa 已提交
614 615

			// Unbind events
R
RubaXa 已提交
616
			_off(document, 'drop', this);
R
RubaXa 已提交
617
			_off(document, 'mousemove', this._onTouchMove);
R
RubaXa 已提交
618
			_off(el, 'dragstart', this._onDragStart);
R
RubaXa 已提交
619

620
			this._offUpEvents();
R
RubaXa 已提交
621

R
RubaXa 已提交
622
			if (evt) {
R
RubaXa 已提交
623
				evt.preventDefault();
624
				!options.dropBubble && evt.stopPropagation();
R
RubaXa 已提交
625

R
RubaXa 已提交
626
				ghostEl && ghostEl.parentNode.removeChild(ghostEl);
R
RubaXa 已提交
627

R
RubaXa 已提交
628
				if (dragEl) {
R
RubaXa 已提交
629 630
					_off(dragEl, 'dragend', this);

631
					_disableDraggable(dragEl);
R
RubaXa 已提交
632 633
					_toggleClass(dragEl, this.options.ghostClass, false);

634
					if (rootEl !== dragEl.parentNode) {
R
RubaXa 已提交
635 636
						newIndex = _index(dragEl);

637
						// drag from one list and drop into another
R
RubaXa 已提交
638 639
						_dispatchEvent(dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex);
						_dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
R
RubaXa 已提交
640 641

						// Add event
R
RubaXa 已提交
642
						_dispatchEvent(dragEl, 'add', dragEl, rootEl, oldIndex, newIndex);
643 644

						// Remove event
R
RubaXa 已提交
645
						_dispatchEvent(rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex);
R
RubaXa 已提交
646
					}
R
RubaXa 已提交
647 648
					else {
						// Remove clone
R
RubaXa 已提交
649
						cloneEl && cloneEl.parentNode.removeChild(cloneEl);
R
RubaXa 已提交
650

R
RubaXa 已提交
651 652 653
						if (dragEl.nextSibling !== nextEl) {
							// Get the index of the dragged element within its parent
							newIndex = _index(dragEl);
R
RubaXa 已提交
654

R
RubaXa 已提交
655 656 657 658
							// drag & drop within the same list
							_dispatchEvent(rootEl, 'update', dragEl, rootEl, oldIndex, newIndex);
							_dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
						}
R
RubaXa 已提交
659
					}
660

R
RubaXa 已提交
661
					// Drag end event
R
RubaXa 已提交
662
					Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
R
RubaXa 已提交
663 664 665 666 667 668 669
				}

				// Set NULL
				rootEl =
				dragEl =
				ghostEl =
				nextEl =
R
RubaXa 已提交
670
				cloneEl =
R
RubaXa 已提交
671 672 673 674 675 676 677

				tapEvt =
				touchEvt =

				lastEl =
				lastCSS =

R
RubaXa 已提交
678 679
				activeGroup =
				Sortable.active = null;
680 681

				// Save sorting
R
RubaXa 已提交
682
				this.save();
R
RubaXa 已提交
683 684 685 686
			}
		},


R
RubaXa 已提交
687 688 689
		handleEvent: function (/**Event*/evt) {
			var type = evt.type;

R
RubaXa 已提交
690
			if (type === 'dragover' || type === 'dragenter') {
R
RubaXa 已提交
691
				this._onDrag(evt);
R
RubaXa 已提交
692
				this._onDragOver(evt);
R
RubaXa 已提交
693 694
				_globalDragOver(evt);
			}
R
RubaXa 已提交
695
			else if (type === 'drop' || type === 'dragend') {
R
RubaXa 已提交
696 697
				this._onDrop(evt);
			}
R
RubaXa 已提交
698 699 700
		},


701 702 703 704 705 706 707 708 709
		/**
		 * Serializes the item into an array of string.
		 * @returns {String[]}
		 */
		toArray: function () {
			var order = [],
				el,
				children = this.el.children,
				i = 0,
R
RubaXa 已提交
710
				n = children.length;
711 712 713

			for (; i < n; i++) {
				el = children[i];
R
RubaXa 已提交
714 715 716
				if (_closest(el, this.options.draggable, this.el)) {
					order.push(el.getAttribute('data-id') || _generateId(el));
				}
717 718 719 720 721 722 723 724 725 726 727
			}

			return order;
		},


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

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

R
RubaXa 已提交
733
				if (_closest(el, this.options.draggable, rootEl)) {
R
RubaXa 已提交
734 735 736
					items[id] = el;
				}
			}, this);
737 738 739

			order.forEach(function (id) {
				if (items[id]) {
R
RubaXa 已提交
740 741
					rootEl.removeChild(items[id]);
					rootEl.appendChild(items[id]);
742 743 744 745 746
				}
			});
		},


R
RubaXa 已提交
747 748 749 750 751 752 753 754 755
		/**
		 * Save the current sorting
		 */
		save: function () {
			var store = this.options.store;
			store && store.set(this);
		},


756 757 758 759 760 761 762 763 764 765 766
		/**
		 * 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);
		},


767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
		/**
		 * 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;
			}
		},


784 785 786 787
		/**
		 * Destroy
		 */
		destroy: function () {
R
RubaXa 已提交
788 789
			var el = this.el, options = this.options;

790 791 792 793
			_customEvents.forEach(function (name) {
				_off(el, name.substr(2).toLowerCase(), options[name]);
			});

R
RubaXa 已提交
794 795 796
			_off(el, 'mousedown', this._onTapStart);
			_off(el, 'touchstart', this._onTapStart);

R
RubaXa 已提交
797 798
			_off(el, 'dragover', this);
			_off(el, 'dragenter', this);
R
RubaXa 已提交
799

800
			//remove draggable attributes
R
RubaXa 已提交
801
			Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
802 803 804
				el.removeAttribute('draggable');
			});

R
RubaXa 已提交
805 806 807 808 809 810 811 812
			touchDragOverListeners.splice(touchDragOverListeners.indexOf(this._onDragOver), 1);

			this._onDrop();

			this.el = null;
		}
	};

813

R
RubaXa 已提交
814 815 816 817 818 819 820 821 822
	function _cloneHide(state) {
		if (cloneEl && (cloneEl.state !== state)) {
			_css(cloneEl, 'display', state ? 'none' : '');
			!state && cloneEl.state && rootEl.insertBefore(cloneEl, dragEl);
			cloneEl.state = state;
		}
	}


R
RubaXa 已提交
823
	function _bind(ctx, fn) {
R
RubaXa 已提交
824
		var args = slice.call(arguments, 2);
R
RubaXa 已提交
825
		return	fn.bind ? fn.bind.apply(fn, [ctx].concat(args)) : function () {
R
RubaXa 已提交
826 827 828 829 830
			return fn.apply(ctx, args.concat(slice.call(arguments)));
		};
	}


R
RubaXa 已提交
831
	function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) {
R
RubaXa 已提交
832
		if (el) {
R
RubaXa 已提交
833 834 835
			ctx = ctx || document;
			selector = selector.split('.');

R
RubaXa 已提交
836 837
			var tag = selector.shift().toUpperCase(),
				re = new RegExp('\\s(' + selector.join('|') + ')\\s', 'g');
R
RubaXa 已提交
838 839

			do {
R
RubaXa 已提交
840
				if (
R
RubaXa 已提交
841
					(tag === '>*' && el.parentNode === ctx) || (
R
raphj 已提交
842
						(tag === '' || el.nodeName.toUpperCase() == tag) &&
R
RubaXa 已提交
843 844
						(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
					)
R
RubaXa 已提交
845 846
				) {
					return el;
R
RubaXa 已提交
847 848
				}
			}
R
RubaXa 已提交
849
			while (el !== ctx && (el = el.parentNode));
R
RubaXa 已提交
850 851
		}

R
RubaXa 已提交
852
		return null;
R
RubaXa 已提交
853 854 855
	}


856
	function _globalDragOver(/**Event*/evt) {
R
RubaXa 已提交
857 858 859 860 861
		evt.dataTransfer.dropEffect = 'move';
		evt.preventDefault();
	}


R
RubaXa 已提交
862
	function _on(el, event, fn) {
R
RubaXa 已提交
863 864 865 866
		el.addEventListener(event, fn, false);
	}


R
RubaXa 已提交
867
	function _off(el, event, fn) {
R
RubaXa 已提交
868 869 870 871
		el.removeEventListener(event, fn, false);
	}


R
RubaXa 已提交
872 873 874
	function _toggleClass(el, name, state) {
		if (el) {
			if (el.classList) {
R
RubaXa 已提交
875 876 877
				el.classList[state ? 'add' : 'remove'](name);
			}
			else {
R
RubaXa 已提交
878 879
				var className = (' ' + el.className + ' ').replace(/\s+/g, ' ').replace(' ' + name + ' ', '');
				el.className = className + (state ? ' ' + name : '');
R
RubaXa 已提交
880 881 882 883 884
			}
		}
	}


R
RubaXa 已提交
885
	function _css(el, prop, val) {
R
RubaXa 已提交
886 887
		var style = el && el.style;

R
RubaXa 已提交
888 889 890
		if (style) {
			if (val === void 0) {
				if (document.defaultView && document.defaultView.getComputedStyle) {
R
RubaXa 已提交
891 892
					val = document.defaultView.getComputedStyle(el, '');
				}
R
RubaXa 已提交
893 894
				else if (el.currentStyle) {
					val = el.currentStyle;
R
RubaXa 已提交
895
				}
R
RubaXa 已提交
896 897 898 899 900 901 902 903 904

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

				style[prop] = val + (typeof val === 'string' ? '' : 'px');
R
RubaXa 已提交
905 906 907 908 909
			}
		}
	}


R
RubaXa 已提交
910 911
	function _find(ctx, tagName, iterator) {
		if (ctx) {
R
RubaXa 已提交
912
			var list = ctx.getElementsByTagName(tagName), i = 0, n = list.length;
R
RubaXa 已提交
913

R
RubaXa 已提交
914 915
			if (iterator) {
				for (; i < n; i++) {
R
RubaXa 已提交
916 917 918
					iterator(list[i], i);
				}
			}
R
RubaXa 已提交
919

R
RubaXa 已提交
920
			return list;
R
RubaXa 已提交
921
		}
R
RubaXa 已提交
922 923

		return [];
R
RubaXa 已提交
924 925 926
	}


R
RubaXa 已提交
927
	function _disableDraggable(el) {
R
RubaXa 已提交
928
		el.draggable = false;
R
RubaXa 已提交
929 930 931
	}


R
RubaXa 已提交
932
	function _unsilent() {
R
RubaXa 已提交
933 934 935 936
		_silent = false;
	}


R
RubaXa 已提交
937
	/** @returns {HTMLElement|false} */
R
RubaXa 已提交
938
	function _ghostInBottom(el, evt) {
R
RubaXa 已提交
939 940
		var lastEl = el.lastElementChild, rect = lastEl.getBoundingClientRect();
		return (evt.clientY - (rect.top + rect.height) > 5) && lastEl; // min delta
R
RubaXa 已提交
941 942 943
	}


944 945 946 947 948 949 950
	/**
	 * Generate id
	 * @param   {HTMLElement} el
	 * @returns {String}
	 * @private
	 */
	function _generateId(el) {
R
RubaXa 已提交
951
		var str = el.tagName + el.className + el.src + el.href + el.textContent,
952
			i = str.length,
R
RubaXa 已提交
953
			sum = 0;
954

955 956 957
		while (i--) {
			sum += str.charCodeAt(i);
		}
958

959 960 961
		return sum.toString(36);
	}

962 963 964
	/**
	 * Returns the index of an element within its parent
	 * @param el
965 966
	 * @returns {number}
	 * @private
967 968 969
	 */
	function _index(/**HTMLElement*/el) {
		var index = 0;
970 971 972 973
		while (el && (el = el.previousElementSibling)) {
			if (el.nodeName.toUpperCase() !== 'TEMPLATE') {
				index++;
			}
974 975 976
		}
		return index;
	}
R
RubaXa 已提交
977

R
RubaXa 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	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);
			}
		};
	}


R
RubaXa 已提交
1000 1001 1002 1003 1004 1005 1006
	// Export utils
	Sortable.utils = {
		on: _on,
		off: _off,
		css: _css,
		find: _find,
		bind: _bind,
R
RubaXa 已提交
1007 1008 1009
		is: function (el, selector) {
			return !!_closest(el, selector, el);
		},
R
RubaXa 已提交
1010
		throttle: _throttle,
R
RubaXa 已提交
1011
		closest: _closest,
1012
		toggleClass: _toggleClass,
1013 1014
		dispatchEvent: _dispatchEvent,
		index: _index
R
RubaXa 已提交
1015 1016 1017
	};


R
RubaXa 已提交
1018
	Sortable.version = '1.0.1';
1019

R
RubaXa 已提交
1020

1021 1022 1023 1024 1025 1026
	/**
	 * Create sortable instance
	 * @param {HTMLElement}  el
	 * @param {Object}      [options]
	 */
	Sortable.create = function (el, options) {
R
RubaXa 已提交
1027
		return new Sortable(el, options);
1028
	};
R
RubaXa 已提交
1029 1030

	// Export
1031
	return Sortable;
R
RubaXa 已提交
1032
});