|
58 | 58 |
|
59 | 59 | _silent = false, |
60 | 60 |
|
61 | | - _dispatchEvent = function (rootEl, name, targetEl, fromEl, startIndex, newIndex) { |
62 | | - var evt = document.createEvent('Event'); |
| 61 | + _dispatchEvent = function (sortable, rootEl, name, targetEl, fromEl, startIndex, newIndex) { |
| 62 | + var evt = document.createEvent('Event'), |
| 63 | + options = (sortable || rootEl[expando]).options, |
| 64 | + onName = 'on' + name.charAt(0).toUpperCase() + name.substr(1); |
63 | 65 |
|
64 | 66 | evt.initEvent(name, true, true); |
65 | 67 |
|
|
70 | 72 | evt.oldIndex = startIndex; |
71 | 73 | evt.newIndex = newIndex; |
72 | 74 |
|
| 75 | + if (options[onName]) { |
| 76 | + options[onName].call(sortable, evt); |
| 77 | + } |
| 78 | + |
73 | 79 | rootEl.dispatchEvent(evt); |
74 | 80 | }, |
75 | 81 |
|
76 | | - _customEvents = 'onAdd onUpdate onRemove onStart onEnd onFilter onSort'.split(' '), |
77 | | - |
78 | | - noop = function () {}, |
79 | | - |
80 | 82 | abs = Math.abs, |
81 | 83 | slice = [].slice, |
82 | 84 |
|
|
170 | 172 | this.options = options = _extend({}, options); |
171 | 173 |
|
172 | 174 |
|
| 175 | + // Export instance |
| 176 | + el[expando] = this; |
| 177 | + |
| 178 | + |
173 | 179 | // Default options |
174 | 180 | var defaults = { |
175 | 181 | group: Math.random(), |
|
215 | 221 | }); |
216 | 222 |
|
217 | 223 |
|
218 | | - // Define events |
219 | | - _customEvents.forEach(function (name) { |
220 | | - options[name] = _bind(this, options[name] || noop); |
221 | | - _on(el, name.substr(2).toLowerCase(), options[name]); |
222 | | - }, this); |
223 | | - |
224 | | - |
225 | | - // Export options |
226 | 224 | options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' '; |
227 | | - el[expando] = options; |
228 | 225 |
|
229 | 226 |
|
230 | 227 | // Bind all private methods |
|
253 | 250 | constructor: Sortable, |
254 | 251 |
|
255 | 252 | _onTapStart: function (/** Event|TouchEvent */evt) { |
256 | | - var el = this.el, |
| 253 | + var _this = this, |
| 254 | + el = this.el, |
257 | 255 | options = this.options, |
258 | 256 | type = evt.type, |
259 | 257 | touch = evt.touches && evt.touches[0], |
|
278 | 276 | // Check filter |
279 | 277 | if (typeof filter === 'function') { |
280 | 278 | if (filter.call(this, evt, target, this)) { |
281 | | - _dispatchEvent(originalTarget, 'filter', target, el, oldIndex); |
| 279 | + _dispatchEvent(_this, originalTarget, 'filter', target, el, oldIndex); |
282 | 280 | evt.preventDefault(); |
283 | 281 | return; // cancel dnd |
284 | 282 | } |
|
288 | 286 | criteria = _closest(originalTarget, criteria.trim(), el); |
289 | 287 |
|
290 | 288 | if (criteria) { |
291 | | - _dispatchEvent(criteria, 'filter', target, el, oldIndex); |
| 289 | + _dispatchEvent(_this, criteria, 'filter', target, el, oldIndex); |
292 | 290 | return true; |
293 | 291 | } |
294 | 292 | }); |
|
404 | 402 | Sortable.active = this; |
405 | 403 |
|
406 | 404 | // Drag start event |
407 | | - _dispatchEvent(rootEl, 'start', dragEl, rootEl, oldIndex); |
| 405 | + _dispatchEvent(this, rootEl, 'start', dragEl, rootEl, oldIndex); |
408 | 406 | } |
409 | 407 | }, |
410 | 408 |
|
|
419 | 417 |
|
420 | 418 | if (parent) { |
421 | 419 | do { |
422 | | - if (parent[expando] && parent[expando].groups.indexOf(groupName) > -1) { |
| 420 | + if (parent[expando] && parent[expando].options.groups.indexOf(groupName) > -1) { |
423 | 421 | while (i--) { |
424 | 422 | touchDragOverListeners[i]({ |
425 | 423 | clientX: touchEvt.clientX, |
|
700 | 698 | newIndex = _index(dragEl); |
701 | 699 |
|
702 | 700 | // drag from one list and drop into another |
703 | | - _dispatchEvent(dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex); |
704 | | - _dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); |
| 701 | + _dispatchEvent(null, dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex); |
| 702 | + _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); |
705 | 703 |
|
706 | 704 | // Add event |
707 | | - _dispatchEvent(dragEl, 'add', dragEl, rootEl, oldIndex, newIndex); |
| 705 | + _dispatchEvent(null, dragEl.parentNode, 'add', dragEl, rootEl, oldIndex, newIndex); |
708 | 706 |
|
709 | 707 | // Remove event |
710 | | - _dispatchEvent(rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex); |
| 708 | + _dispatchEvent(this, rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex); |
711 | 709 | } |
712 | 710 | else { |
713 | 711 | // Remove clone |
|
718 | 716 | newIndex = _index(dragEl); |
719 | 717 |
|
720 | 718 | // drag & drop within the same list |
721 | | - _dispatchEvent(rootEl, 'update', dragEl, rootEl, oldIndex, newIndex); |
722 | | - _dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); |
| 719 | + _dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex); |
| 720 | + _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); |
723 | 721 | } |
724 | 722 | } |
725 | 723 |
|
726 | 724 | // Drag end event |
727 | | - Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, oldIndex, newIndex); |
| 725 | + Sortable.active && _dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex); |
728 | 726 | } |
729 | 727 |
|
730 | 728 | // Nulling |
|
853 | 851 | * Destroy |
854 | 852 | */ |
855 | 853 | destroy: function () { |
856 | | - var el = this.el, options = this.options; |
| 854 | + var el = this.el; |
857 | 855 |
|
858 | | - _customEvents.forEach(function (name) { |
859 | | - _off(el, name.substr(2).toLowerCase(), options[name]); |
860 | | - }); |
| 856 | + el[expando] = null; |
861 | 857 |
|
862 | 858 | _off(el, 'mousedown', this._onTapStart); |
863 | 859 | _off(el, 'touchstart', this._onTapStart); |
864 | 860 |
|
865 | 861 | _off(el, 'dragover', this); |
866 | 862 | _off(el, 'dragenter', this); |
867 | 863 |
|
868 | | - //remove draggable attributes |
| 864 | + // Remove draggable attributes |
869 | 865 | Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) { |
870 | 866 | el.removeAttribute('draggable'); |
871 | 867 | }); |
|
874 | 870 |
|
875 | 871 | this._onDrop(); |
876 | 872 |
|
877 | | - this.el = null; |
| 873 | + this.el = el = null; |
878 | 874 | } |
879 | 875 | }; |
880 | 876 |
|
|
1091 | 1087 | throttle: _throttle, |
1092 | 1088 | closest: _closest, |
1093 | 1089 | toggleClass: _toggleClass, |
1094 | | - dispatchEvent: _dispatchEvent, |
1095 | 1090 | index: _index |
1096 | 1091 | }; |
1097 | 1092 |
|
|
0 commit comments