Sortable.js 17.6 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 31
	var dragEl,
		ghostEl,
		cloneEl,
		rootEl,
		nextEl,
R
RubaXa 已提交
32

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

R
RubaXa 已提交
36
		activeGroup,
R
RubaXa 已提交
37

R
RubaXa 已提交
38 39
		tapEvt,
		touchEvt,
R
RubaXa 已提交
40

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

R
RubaXa 已提交
43 44 45 46
		win = window,
		document = win.document,
		parseInt = win.parseInt,
		supportIEdnd = !!document.createElement('div').dragDrop,
R
RubaXa 已提交
47

R
RubaXa 已提交
48
		_silent = false,
R
RubaXa 已提交
49

R
RubaXa 已提交
50
		_dispatchEvent = function (rootEl, name, targetEl, fromEl) {
R
RubaXa 已提交
51
			var evt = document.createEvent('Event');
R
RubaXa 已提交
52

53 54 55 56 57
			evt.initEvent(name, true, true);
			evt.item = targetEl || rootEl;
			evt.from = fromEl || rootEl;

			rootEl.dispatchEvent(evt);
R
RubaXa 已提交
58
		},
59

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

R
RubaXa 已提交
62 63
		noop = function () {},
		slice = [].slice,
R
RubaXa 已提交
64

R
RubaXa 已提交
65
		touchDragOverListeners = []
R
RubaXa 已提交
66 67 68
	;


69

R
RubaXa 已提交
70 71 72
	/**
	 * @class  Sortable
	 * @param  {HTMLElement}  el
73
	 * @param  {Object}       [options]
R
RubaXa 已提交
74
	 */
R
RubaXa 已提交
75
	function Sortable(el, options) {
R
RubaXa 已提交
76 77 78 79
		this.el = el; // root element
		this.options = options = (options || {});


R
RubaXa 已提交
80
		// Default options
81 82
		var defaults = {
			group: Math.random(),
R
RubaXa 已提交
83
			sort: true,
84 85 86 87 88
			store: null,
			handle: null,
			draggable: el.children[0] && el.children[0].nodeName || (/[uo]l/i.test(el.nodeName) ? 'li' : '*'),
			ghostClass: 'sortable-ghost',
			ignore: 'a, img',
89
			filter: null,
R
RubaXa 已提交
90 91 92 93
			animation: 0,
			setData: function (dataTransfer, dragEl) {
				dataTransfer.setData('Text', dragEl.textContent);
			}
94 95
		};

R
RubaXa 已提交
96

97 98
		// Set default options
		for (var name in defaults) {
R
RubaXa 已提交
99
			!(name in options) && (options[name] = defaults[name]);
100
		}
R
RubaXa 已提交
101

102

R
RubaXa 已提交
103 104 105 106
		if (!options.group.name) {
			options.group = { name: options.group };
		}

R
RubaXa 已提交
107

R
RubaXa 已提交
108 109 110 111 112 113 114
		['pull', 'put'].forEach(function (key) {
			if (!(key in options.group)) {
				options.group[key] = true;
			}
		});


115
		// Define events
116
		_customEvents.forEach(function (name) {
117
			options[name] = _bind(this, options[name] || noop);
118
			_on(el, name.substr(2).toLowerCase(), options[name]);
R
RubaXa 已提交
119
		}, this);
R
RubaXa 已提交
120 121


R
* JSDoc  
RubaXa 已提交
122
		// Export group name
R
RubaXa 已提交
123
		el[expando] = options.group.name;
R
RubaXa 已提交
124 125


R
* JSDoc  
RubaXa 已提交
126
		// Bind all private methods
R
RubaXa 已提交
127 128
		for (var fn in this) {
			if (fn.charAt(0) === '_') {
R
RubaXa 已提交
129 130 131 132 133 134 135 136
				this[fn] = _bind(this, this[fn]);
			}
		}


		// Bind events
		_on(el, 'mousedown', this._onTapStart);
		_on(el, 'touchstart', this._onTapStart);
R
RubaXa 已提交
137
		supportIEdnd && _on(el, 'selectstart', this._onTapStart);
R
RubaXa 已提交
138 139 140 141 142

		_on(el, 'dragover', this._onDragOver);
		_on(el, 'dragenter', this._onDragOver);

		touchDragOverListeners.push(this._onDragOver);
143 144 145

		// Restore sorting
		options.store && this.sort(options.store.get(this));
R
RubaXa 已提交
146 147 148
	}


149
	Sortable.prototype = /** @lends Sortable.prototype */ {
R
RubaXa 已提交
150 151 152
		constructor: Sortable,


R
RubaXa 已提交
153
		_applyEffects: function () {
R
RubaXa 已提交
154 155 156 157
			_toggleClass(dragEl, this.options.ghostClass, true);
		},


R
RubaXa 已提交
158 159 160 161 162 163 164
		_onTapStart: function (/**Event|TouchEvent*/evt) {
			var touch = evt.touches && evt.touches[0],
				target = (touch || evt).target,
				options =  this.options,
				el = this.el,
				filter = options.filter;

R
RubaXa 已提交
165

R
RubaXa 已提交
166
			if (evt.type === 'mousedown' && evt.button !== 0) {
R
RubaXa 已提交
167 168 169
				return; // only left button
			}

170
			// Check filter
R
RubaXa 已提交
171 172
			if (typeof filter === 'function') {
				if (filter.call(this, target, this)) {
R
RubaXa 已提交
173 174 175
					_dispatchEvent(el, 'filter', target);
					return; // cancel dnd
				}
176
			}
R
RubaXa 已提交
177
			else if (filter) {
178 179 180 181 182 183 184 185 186 187
				filter = filter.split(',').filter(function (criteria) {
					return _closest(target, criteria.trim(), el);
				});

				if (filter.length) {
					_dispatchEvent(el, 'filter', target);
					return; // cancel dnd
				}
			}

R
RubaXa 已提交
188
			if (options.handle) {
R
RubaXa 已提交
189
				target = _closest(target, options.handle, el);
R
RubaXa 已提交
190 191
			}

R
RubaXa 已提交
192
			target = _closest(target, options.draggable, el);
R
RubaXa 已提交
193

R
RubaXa 已提交
194
			// IE 9 Support
R
RubaXa 已提交
195 196
			if (target && evt.type == 'selectstart') {
				if (target.tagName != 'A' && target.tagName != 'IMG') {
197 198 199
					target.dragDrop();
				}
			}
N
Nicolas 已提交
200

R
RubaXa 已提交
201
			if (target && !dragEl && (target.parentNode === el)) {
R
RubaXa 已提交
202
				tapEvt = evt;
203 204 205 206 207 208 209

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

				dragEl.draggable = true;
R
RubaXa 已提交
210 211

				// Disable "draggable"
212
				options.ignore.split(',').forEach(function (criteria) {
Z
ziflex 已提交
213 214
					_find(target, criteria.trim(), _disableDraggable);
				});
R
RubaXa 已提交
215

R
RubaXa 已提交
216
				if (touch) {
R
RubaXa 已提交
217 218
					// Touch device support
					tapEvt = {
R
RubaXa 已提交
219 220 221
						target: target,
						clientX: touch.clientX,
						clientY: touch.clientY
R
RubaXa 已提交
222
					};
223

R
RubaXa 已提交
224 225 226
					this._onDragStart(tapEvt, true);
					evt.preventDefault();
				}
R
RubaXa 已提交
227

228 229 230
				_on(document, 'mouseup', this._onDrop);
				_on(document, 'touchend', this._onDrop);
				_on(document, 'touchcancel', this._onDrop);
R
RubaXa 已提交
231 232

				_on(this.el, 'dragstart', this._onDragStart);
R
RubaXa 已提交
233
				_on(this.el, 'dragend', this._onDrop);
R
RubaXa 已提交
234 235 236 237
				_on(document, 'dragover', _globalDragOver);


				try {
R
RubaXa 已提交
238
					if (document.selection) {
R
RubaXa 已提交
239 240
						document.selection.empty();
					} else {
R
RubaXa 已提交
241
						window.getSelection().removeAllRanges();
R
RubaXa 已提交
242
					}
R
RubaXa 已提交
243 244
				} catch (err) {
				}
245 246


247
				_dispatchEvent(dragEl, 'start');
R
RubaXa 已提交
248 249


R
RubaXa 已提交
250 251 252 253 254
				if (activeGroup.pull == 'clone') {
					cloneEl = dragEl.cloneNode(true);
					_css(cloneEl, 'display', 'none');
					rootEl.insertBefore(cloneEl, dragEl);
				}
R
RubaXa 已提交
255 256 257
			}
		},

R
RubaXa 已提交
258 259
		_emulateDragOver: function () {
			if (touchEvt) {
R
RubaXa 已提交
260 261
				_css(ghostEl, 'display', 'none');

R
RubaXa 已提交
262 263 264 265
				var target = document.elementFromPoint(touchEvt.clientX, touchEvt.clientY),
					parent = target,
					groupName = this.options.group.name,
					i = touchDragOverListeners.length;
R
RubaXa 已提交
266

R
RubaXa 已提交
267
				if (parent) {
L
Larry Davis 已提交
268
					do {
R
RubaXa 已提交
269 270
						if (parent[expando] === groupName) {
							while (i--) {
L
Larry Davis 已提交
271 272 273 274 275 276 277
								touchDragOverListeners[i]({
									clientX: touchEvt.clientX,
									clientY: touchEvt.clientY,
									target: target,
									rootEl: parent
								});
							}
R
RubaXa 已提交
278

L
Larry Davis 已提交
279
							break;
R
RubaXa 已提交
280
						}
R
RubaXa 已提交
281

L
Larry Davis 已提交
282 283
						target = parent; // store last element
					}
R
RubaXa 已提交
284 285
					/* jshint boss:true */
					while (parent = parent.parentNode);
R
RubaXa 已提交
286 287 288 289 290 291 292
				}

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


R
RubaXa 已提交
293 294 295 296 297 298
		_onTouchMove: function (/**TouchEvent*/evt) {
			if (tapEvt) {
				var touch = evt.touches[0],
					dx = touch.clientX - tapEvt.clientX,
					dy = touch.clientY - tapEvt.clientY,
					translate3d = 'translate3d(' + dx + 'px,' + dy + 'px,0)';
R
RubaXa 已提交
299 300

				touchEvt = touch;
R
RubaXa 已提交
301 302 303 304 305 306

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

M
Marius Petcu 已提交
307
				evt.preventDefault();
R
RubaXa 已提交
308 309 310 311
			}
		},


R
RubaXa 已提交
312
		_onDragStart: function (/**Event*/evt, /**boolean*/isTouch) {
R
RubaXa 已提交
313 314
			var dataTransfer = evt.dataTransfer,
				options = this.options;
R
RubaXa 已提交
315

316
			this._offUpEvents();
R
RubaXa 已提交
317

R
RubaXa 已提交
318 319 320 321
			if (isTouch) {
				var rect = dragEl.getBoundingClientRect(),
					css = _css(dragEl),
					ghostRect;
R
RubaXa 已提交
322

323
				ghostEl = dragEl.cloneNode(true);
R
RubaXa 已提交
324 325 326

				_css(ghostEl, 'top', rect.top - parseInt(css.marginTop, 10));
				_css(ghostEl, 'left', rect.left - parseInt(css.marginLeft, 10));
R
RubaXa 已提交
327 328
				_css(ghostEl, 'width', rect.width);
				_css(ghostEl, 'height', rect.height);
R
RubaXa 已提交
329 330 331 332
				_css(ghostEl, 'opacity', '0.8');
				_css(ghostEl, 'position', 'fixed');
				_css(ghostEl, 'zIndex', '100000');

R
RubaXa 已提交
333 334 335 336
				rootEl.appendChild(ghostEl);

				// Fixing dimensions.
				ghostRect = ghostEl.getBoundingClientRect();
R
RubaXa 已提交
337 338
				_css(ghostEl, 'width', rect.width * 2 - ghostRect.width);
				_css(ghostEl, 'height', rect.height * 2 - ghostRect.height);
R
RubaXa 已提交
339 340 341 342

				// Bind touch events
				_on(document, 'touchmove', this._onTouchMove);
				_on(document, 'touchend', this._onDrop);
M
Marius Petcu 已提交
343
				_on(document, 'touchcancel', this._onDrop);
R
RubaXa 已提交
344

R
RubaXa 已提交
345
				this._loopId = setInterval(this._emulateDragOver, 150);
R
RubaXa 已提交
346 347 348
			}
			else {
				dataTransfer.effectAllowed = 'move';
R
RubaXa 已提交
349
				options.setData && options.setData.call(this, dataTransfer, dragEl);
R
RubaXa 已提交
350 351 352 353 354 355 356 357

				_on(document, 'drop', this._onDrop);
			}

			setTimeout(this._applyEffects);
		},


R
RubaXa 已提交
358
		_onDragOver: function (/**Event*/evt) {
R
RubaXa 已提交
359 360 361 362 363 364
			var el = this.el,
				target,
				dragRect,
				revert,
				options = this.options,
				group = options.group,
R
RubaXa 已提交
365
				groupPut = group.put,
R
RubaXa 已提交
366 367
				isOwner = (activeGroup === group);

R
RubaXa 已提交
368
			if (!_silent &&
R
RubaXa 已提交
369 370
				(activeGroup.name === group.name || groupPut && groupPut.indexOf && groupPut.indexOf(activeGroup.name) > -1) &&
				(isOwner && (options.sort || (revert = !rootEl.contains(dragEl))) || groupPut && activeGroup.pull) &&
R
RubaXa 已提交
371
				(evt.rootEl === void 0 || evt.rootEl === this.el)
R
RubaXa 已提交
372
			) {
R
RubaXa 已提交
373 374 375
				target = _closest(evt.target, this.options.draggable, el);
				dragRect = dragEl.getBoundingClientRect();

R
RubaXa 已提交
376
				if (cloneEl && (cloneEl.state !== isOwner)) {
R
RubaXa 已提交
377 378 379 380 381
					_css(cloneEl, 'display', isOwner ? 'none' : '');
					!isOwner && cloneEl.state && rootEl.insertBefore(cloneEl, dragEl);
					cloneEl.state = isOwner;
				}

R
RubaXa 已提交
382
				if (revert && cloneEl) {
R
RubaXa 已提交
383 384 385
					rootEl.insertBefore(dragEl, cloneEl);
					return;
				}
R
RubaXa 已提交
386

R
RubaXa 已提交
387
				if ((el.children.length === 0) || (el.children[0] === ghostEl) ||
R
RubaXa 已提交
388
					(el === evt.target) && (target = _ghostInBottom(el, evt))
R
RubaXa 已提交
389
				) {
R
RubaXa 已提交
390 391 392 393 394 395
					if (target) {
						if (target.animated) {
							return;
						}
						targetRect = target.getBoundingClientRect();
					}
R
RubaXa 已提交
396

R
RubaXa 已提交
397
					el.appendChild(dragEl);
R
* anim  
RubaXa 已提交
398
					this._animate(dragRect, dragEl);
R
RubaXa 已提交
399
					target && this._animate(targetRect, target);
R
RubaXa 已提交
400
				}
R
RubaXa 已提交
401 402
				else if (target && !target.animated && target !== dragEl && (target.parentNode[expando] !== void 0)) {
					if (lastEl !== target) {
R
RubaXa 已提交
403
						lastEl = target;
R
RubaXa 已提交
404
						lastCSS = _css(target);
R
RubaXa 已提交
405 406 407
					}


R
RubaXa 已提交
408 409 410 411 412 413 414 415 416
					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 已提交
417
					;
R
RubaXa 已提交
418

R
RubaXa 已提交
419 420 421
					_silent = true;
					setTimeout(_unsilent, 30);

R
RubaXa 已提交
422 423
					if (floating) {
						after = (target.previousElementSibling === dragEl) && !isWide || halfway && isWide;
R
RubaXa 已提交
424
					} else {
R
RubaXa 已提交
425
						after = (nextSibling !== dragEl) && !isLong || halfway && isLong;
R
RubaXa 已提交
426 427
					}

R
RubaXa 已提交
428
					if (after && !nextSibling) {
R
RubaXa 已提交
429 430 431
						el.appendChild(dragEl);
					} else {
						target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
R
RubaXa 已提交
432
					}
R
RubaXa 已提交
433 434 435
					
					this._animate(dragRect, dragEl);
					this._animate(targetRect, target);
R
RubaXa 已提交
436 437 438 439
				}
			}
		},

440 441 442 443 444 445
		_animate: function (prevRect, target) {
			var ms = this.options.animation;

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

R
RubaXa 已提交
446
				_css(target, 'transition', 'none');
447 448 449 450 451 452 453
				_css(target, 'transform', 'translate3d('
					+ (prevRect.left - currentRect.left) + 'px,'
					+ (prevRect.top - currentRect.top) + 'px,0)'
				);

				target.offsetWidth; // repaint

R
RubaXa 已提交
454
				_css(target, 'transition', 'all ' + ms + 'ms');
455 456
				_css(target, 'transform', 'translate3d(0,0,0)');

R
* anim  
RubaXa 已提交
457 458
				clearTimeout(target.animated);
				target.animated = setTimeout(function () {
459 460 461 462 463 464
					_css(target, 'transition', '');
					target.animated = false;
				}, ms);
			}
		},

465 466 467 468 469 470
		_offUpEvents: function () {
			_off(document, 'mouseup', this._onDrop);
			_off(document, 'touchmove', this._onTouchMove);
			_off(document, 'touchend', this._onDrop);
			_off(document, 'touchcancel', this._onDrop);
		},
R
RubaXa 已提交
471

R
RubaXa 已提交
472
		_onDrop: function (/**Event*/evt) {
R
RubaXa 已提交
473 474 475 476 477 478
			clearInterval(this._loopId);

			// Unbind events
			_off(document, 'drop', this._onDrop);
			_off(document, 'dragover', _globalDragOver);

R
RubaXa 已提交
479
			_off(this.el, 'dragend', this._onDrop);
R
RubaXa 已提交
480
			_off(this.el, 'dragstart', this._onDragStart);
N
Nicolas 已提交
481
			_off(this.el, 'selectstart', this._onTapStart);
R
RubaXa 已提交
482

483
			this._offUpEvents();
R
RubaXa 已提交
484

R
RubaXa 已提交
485
			if (evt) {
R
RubaXa 已提交
486
				evt.preventDefault();
R
RubaXa 已提交
487
				evt.stopPropagation();
R
RubaXa 已提交
488

R
RubaXa 已提交
489
				ghostEl && ghostEl.parentNode.removeChild(ghostEl);
R
RubaXa 已提交
490

R
RubaXa 已提交
491
				if (dragEl) {
492
					_disableDraggable(dragEl);
R
RubaXa 已提交
493 494
					_toggleClass(dragEl, this.options.ghostClass, false);

R
RubaXa 已提交
495
					if (!rootEl.contains(dragEl)) {
496 497
						_dispatchEvent(dragEl, 'sort');
						_dispatchEvent(rootEl, 'sort');
R
RubaXa 已提交
498 499

						// Add event
500 501 502 503
						_dispatchEvent(dragEl, 'add', dragEl, rootEl);

						// Remove event
						_dispatchEvent(rootEl, 'remove', dragEl);
R
RubaXa 已提交
504
					}
R
RubaXa 已提交
505
					else if (dragEl.nextSibling !== nextEl) {
R
RubaXa 已提交
506
						// Update event
507
						_dispatchEvent(dragEl, 'update');
508
						_dispatchEvent(dragEl, 'sort');
R
RubaXa 已提交
509

R
RubaXa 已提交
510
						cloneEl && cloneEl.parentNode.removeChild(cloneEl);
R
RubaXa 已提交
511
					}
512

513
					_dispatchEvent(rootEl, 'end');
R
RubaXa 已提交
514 515 516 517 518 519 520
				}

				// Set NULL
				rootEl =
				dragEl =
				ghostEl =
				nextEl =
R
RubaXa 已提交
521
				cloneEl =
R
RubaXa 已提交
522 523 524 525 526 527 528 529

				tapEvt =
				touchEvt =

				lastEl =
				lastCSS =

				activeGroup = null;
530 531 532

				// Save sorting
				this.options.store && this.options.store.set(this);
R
RubaXa 已提交
533 534 535 536
			}
		},


537 538 539 540 541 542 543 544 545
		/**
		 * Serializes the item into an array of string.
		 * @returns {String[]}
		 */
		toArray: function () {
			var order = [],
				el,
				children = this.el.children,
				i = 0,
R
RubaXa 已提交
546
				n = children.length;
547 548 549

			for (; i < n; i++) {
				el = children[i];
R
RubaXa 已提交
550 551 552
				if (_closest(el, this.options.draggable, this.el)) {
					order.push(el.getAttribute('data-id') || _generateId(el));
				}
553 554 555 556 557 558 559 560 561 562 563
			}

			return order;
		},


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

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

R
RubaXa 已提交
569
				if (_closest(el, this.options.draggable, rootEl)) {
R
RubaXa 已提交
570 571 572
					items[id] = el;
				}
			}, this);
573 574 575 576


			order.forEach(function (id) {
				if (items[id]) {
R
RubaXa 已提交
577 578
					rootEl.removeChild(items[id]);
					rootEl.appendChild(items[id]);
579 580 581 582 583
				}
			});
		},


584 585 586 587 588 589 590 591 592 593 594
		/**
		 * 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);
		},


595 596 597 598
		/**
		 * Destroy
		 */
		destroy: function () {
R
RubaXa 已提交
599 600
			var el = this.el, options = this.options;

601 602 603 604
			_customEvents.forEach(function (name) {
				_off(el, name.substr(2).toLowerCase(), options[name]);
			});

R
RubaXa 已提交
605 606
			_off(el, 'mousedown', this._onTapStart);
			_off(el, 'touchstart', this._onTapStart);
N
Nicolas 已提交
607
			_off(el, 'selectstart', this._onTapStart);
R
RubaXa 已提交
608 609 610 611

			_off(el, 'dragover', this._onDragOver);
			_off(el, 'dragenter', this._onDragOver);

612
			//remove draggable attributes
R
RubaXa 已提交
613
			Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
614 615 616
				el.removeAttribute('draggable');
			});

R
RubaXa 已提交
617 618 619 620 621 622 623 624
			touchDragOverListeners.splice(touchDragOverListeners.indexOf(this._onDragOver), 1);

			this._onDrop();

			this.el = null;
		}
	};

625

R
RubaXa 已提交
626
	function _bind(ctx, fn) {
R
RubaXa 已提交
627
		var args = slice.call(arguments, 2);
R
RubaXa 已提交
628
		return	fn.bind ? fn.bind.apply(fn, [ctx].concat(args)) : function () {
R
RubaXa 已提交
629 630 631 632 633
			return fn.apply(ctx, args.concat(slice.call(arguments)));
		};
	}


R
RubaXa 已提交
634 635
	function _closest(el, selector, ctx) {
		if (selector === '*') {
R
RubaXa 已提交
636 637
			return el;
		}
R
RubaXa 已提交
638
		else if (el) {
R
RubaXa 已提交
639 640 641
			ctx = ctx || document;
			selector = selector.split('.');

R
RubaXa 已提交
642 643
			var tag = selector.shift().toUpperCase(),
				re = new RegExp('\\s(' + selector.join('|') + ')\\s', 'g');
R
RubaXa 已提交
644 645

			do {
R
RubaXa 已提交
646 647 648 649 650
				if (
					(tag === '' || el.nodeName == tag) &&
					(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
				) {
					return el;
R
RubaXa 已提交
651 652
				}
			}
R
RubaXa 已提交
653
			while (el !== ctx && (el = el.parentNode));
R
RubaXa 已提交
654 655
		}

R
RubaXa 已提交
656
		return null;
R
RubaXa 已提交
657 658 659
	}


R
RubaXa 已提交
660
	function _globalDragOver(evt) {
R
RubaXa 已提交
661 662 663 664 665
		evt.dataTransfer.dropEffect = 'move';
		evt.preventDefault();
	}


R
RubaXa 已提交
666
	function _on(el, event, fn) {
R
RubaXa 已提交
667 668 669 670
		el.addEventListener(event, fn, false);
	}


R
RubaXa 已提交
671
	function _off(el, event, fn) {
R
RubaXa 已提交
672 673 674 675
		el.removeEventListener(event, fn, false);
	}


R
RubaXa 已提交
676 677 678
	function _toggleClass(el, name, state) {
		if (el) {
			if (el.classList) {
R
RubaXa 已提交
679 680 681
				el.classList[state ? 'add' : 'remove'](name);
			}
			else {
R
RubaXa 已提交
682 683
				var className = (' ' + el.className + ' ').replace(/\s+/g, ' ').replace(' ' + name + ' ', '');
				el.className = className + (state ? ' ' + name : '');
R
RubaXa 已提交
684 685 686 687 688
			}
		}
	}


R
RubaXa 已提交
689
	function _css(el, prop, val) {
R
RubaXa 已提交
690 691
		var style = el && el.style;

R
RubaXa 已提交
692 693 694
		if (style) {
			if (val === void 0) {
				if (document.defaultView && document.defaultView.getComputedStyle) {
R
RubaXa 已提交
695 696
					val = document.defaultView.getComputedStyle(el, '');
				}
R
RubaXa 已提交
697 698
				else if (el.currentStyle) {
					val = el.currentStyle;
R
RubaXa 已提交
699
				}
R
RubaXa 已提交
700 701 702 703 704 705 706 707 708

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

				style[prop] = val + (typeof val === 'string' ? '' : 'px');
R
RubaXa 已提交
709 710 711 712 713
			}
		}
	}


R
RubaXa 已提交
714 715
	function _find(ctx, tagName, iterator) {
		if (ctx) {
R
RubaXa 已提交
716
			var list = ctx.getElementsByTagName(tagName), i = 0, n = list.length;
R
RubaXa 已提交
717

R
RubaXa 已提交
718 719
			if (iterator) {
				for (; i < n; i++) {
R
RubaXa 已提交
720 721 722
					iterator(list[i], i);
				}
			}
R
RubaXa 已提交
723

R
RubaXa 已提交
724
			return list;
R
RubaXa 已提交
725
		}
R
RubaXa 已提交
726 727

		return [];
R
RubaXa 已提交
728 729 730
	}


R
RubaXa 已提交
731
	function _disableDraggable(el) {
R
RubaXa 已提交
732
		el.draggable = false;
R
RubaXa 已提交
733 734 735
	}


R
RubaXa 已提交
736
	function _unsilent() {
R
RubaXa 已提交
737 738 739 740
		_silent = false;
	}


R
RubaXa 已提交
741
	/** @returns {HTMLElement|false} */
R
RubaXa 已提交
742
	function _ghostInBottom(el, evt) {
R
RubaXa 已提交
743 744
		var lastEl = el.lastElementChild, rect = lastEl.getBoundingClientRect();
		return (evt.clientY - (rect.top + rect.height) > 5) && lastEl; // min delta
R
RubaXa 已提交
745 746 747
	}


748 749 750 751 752 753 754
	/**
	 * Generate id
	 * @param   {HTMLElement} el
	 * @returns {String}
	 * @private
	 */
	function _generateId(el) {
R
RubaXa 已提交
755
		var str = el.tagName + el.className + el.src + el.href + el.textContent,
756
			i = str.length,
R
RubaXa 已提交
757
			sum = 0;
758

759 760 761
		while (i--) {
			sum += str.charCodeAt(i);
		}
762

763 764 765
		return sum.toString(36);
	}

R
RubaXa 已提交
766 767 768 769 770 771 772 773 774

	// Export utils
	Sortable.utils = {
		on: _on,
		off: _off,
		css: _css,
		find: _find,
		bind: _bind,
		closest: _closest,
775 776
		toggleClass: _toggleClass,
		dispatchEvent: _dispatchEvent
R
RubaXa 已提交
777 778 779
	};


780
	Sortable.version = '0.6.0';
R
RubaXa 已提交
781

782

783 784 785 786 787 788
	/**
	 * Create sortable instance
	 * @param {HTMLElement}  el
	 * @param {Object}      [options]
	 */
	Sortable.create = function (el, options) {
R
RubaXa 已提交
789
		return new Sortable(el, options);
790
	};
R
RubaXa 已提交
791 792

	// Export
793
	return Sortable;
R
RubaXa 已提交
794
});