Skip to content

Commit 5c4e3dc

Browse files
committed
#318, #328: + Improved work with events transmitted through the options
1 parent fcdb0c9 commit 5c4e3dc

2 files changed

Lines changed: 33 additions & 35 deletions

File tree

Sortable.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858

5959
_silent = false,
6060

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);
6365

6466
evt.initEvent(name, true, true);
6567

@@ -70,13 +72,13 @@
7072
evt.oldIndex = startIndex;
7173
evt.newIndex = newIndex;
7274

75+
if (options[onName]) {
76+
options[onName].call(sortable, evt);
77+
}
78+
7379
rootEl.dispatchEvent(evt);
7480
},
7581

76-
_customEvents = 'onAdd onUpdate onRemove onStart onEnd onFilter onSort'.split(' '),
77-
78-
noop = function () {},
79-
8082
abs = Math.abs,
8183
slice = [].slice,
8284

@@ -170,6 +172,10 @@
170172
this.options = options = _extend({}, options);
171173

172174

175+
// Export instance
176+
el[expando] = this;
177+
178+
173179
// Default options
174180
var defaults = {
175181
group: Math.random(),
@@ -215,16 +221,7 @@
215221
});
216222

217223

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
226224
options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' ';
227-
el[expando] = options;
228225

229226

230227
// Bind all private methods
@@ -253,7 +250,8 @@
253250
constructor: Sortable,
254251

255252
_onTapStart: function (/** Event|TouchEvent */evt) {
256-
var el = this.el,
253+
var _this = this,
254+
el = this.el,
257255
options = this.options,
258256
type = evt.type,
259257
touch = evt.touches && evt.touches[0],
@@ -278,7 +276,7 @@
278276
// Check filter
279277
if (typeof filter === 'function') {
280278
if (filter.call(this, evt, target, this)) {
281-
_dispatchEvent(originalTarget, 'filter', target, el, oldIndex);
279+
_dispatchEvent(_this, originalTarget, 'filter', target, el, oldIndex);
282280
evt.preventDefault();
283281
return; // cancel dnd
284282
}
@@ -288,7 +286,7 @@
288286
criteria = _closest(originalTarget, criteria.trim(), el);
289287

290288
if (criteria) {
291-
_dispatchEvent(criteria, 'filter', target, el, oldIndex);
289+
_dispatchEvent(_this, criteria, 'filter', target, el, oldIndex);
292290
return true;
293291
}
294292
});
@@ -404,7 +402,7 @@
404402
Sortable.active = this;
405403

406404
// Drag start event
407-
_dispatchEvent(rootEl, 'start', dragEl, rootEl, oldIndex);
405+
_dispatchEvent(this, rootEl, 'start', dragEl, rootEl, oldIndex);
408406
}
409407
},
410408

@@ -419,7 +417,7 @@
419417

420418
if (parent) {
421419
do {
422-
if (parent[expando] && parent[expando].groups.indexOf(groupName) > -1) {
420+
if (parent[expando] && parent[expando].options.groups.indexOf(groupName) > -1) {
423421
while (i--) {
424422
touchDragOverListeners[i]({
425423
clientX: touchEvt.clientX,
@@ -700,14 +698,14 @@
700698
newIndex = _index(dragEl);
701699

702700
// 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);
705703

706704
// Add event
707-
_dispatchEvent(dragEl, 'add', dragEl, rootEl, oldIndex, newIndex);
705+
_dispatchEvent(null, dragEl.parentNode, 'add', dragEl, rootEl, oldIndex, newIndex);
708706

709707
// Remove event
710-
_dispatchEvent(rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex);
708+
_dispatchEvent(this, rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex);
711709
}
712710
else {
713711
// Remove clone
@@ -718,13 +716,13 @@
718716
newIndex = _index(dragEl);
719717

720718
// 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);
723721
}
724722
}
725723

726724
// Drag end event
727-
Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
725+
Sortable.active && _dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
728726
}
729727

730728
// Nulling
@@ -853,19 +851,17 @@
853851
* Destroy
854852
*/
855853
destroy: function () {
856-
var el = this.el, options = this.options;
854+
var el = this.el;
857855

858-
_customEvents.forEach(function (name) {
859-
_off(el, name.substr(2).toLowerCase(), options[name]);
860-
});
856+
el[expando] = null;
861857

862858
_off(el, 'mousedown', this._onTapStart);
863859
_off(el, 'touchstart', this._onTapStart);
864860

865861
_off(el, 'dragover', this);
866862
_off(el, 'dragenter', this);
867863

868-
//remove draggable attributes
864+
// Remove draggable attributes
869865
Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
870866
el.removeAttribute('draggable');
871867
});
@@ -874,7 +870,7 @@
874870

875871
this._onDrop();
876872

877-
this.el = null;
873+
this.el = el = null;
878874
}
879875
};
880876

@@ -1091,7 +1087,6 @@
10911087
throttle: _throttle,
10921088
closest: _closest,
10931089
toggleClass: _toggleClass,
1094-
dispatchEvent: _dispatchEvent,
10951090
index: _index
10961091
};
10971092

ng-sortable.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@
167167
scope.$watch(ngSortable + '.' + name, function (value) {
168168
if (value !== void 0) {
169169
options[name] = value;
170-
sortable.option(name, value);
170+
171+
if (!/^on[A-Z]/.test(name)) {
172+
sortable.option(name, value);
173+
}
171174
}
172175
});
173176
});

0 commit comments

Comments
 (0)