Browse Source

#318, #328: + Improved work with events transmitted through the options

pull/346/head
RubaXa 10 years ago
parent
commit
5c4e3dce2a
  1. 63
      Sortable.js
  2. 3
      ng-sortable.js

63
Sortable.js

@ -58,8 +58,10 @@
_silent = false, _silent = false,
_dispatchEvent = function (rootEl, name, targetEl, fromEl, startIndex, newIndex) { _dispatchEvent = function (sortable, rootEl, name, targetEl, fromEl, startIndex, newIndex) {
var evt = document.createEvent('Event'); var evt = document.createEvent('Event'),
options = (sortable || rootEl[expando]).options,
onName = 'on' + name.charAt(0).toUpperCase() + name.substr(1);
evt.initEvent(name, true, true); evt.initEvent(name, true, true);
@ -70,13 +72,13 @@
evt.oldIndex = startIndex; evt.oldIndex = startIndex;
evt.newIndex = newIndex; evt.newIndex = newIndex;
if (options[onName]) {
options[onName].call(sortable, evt);
}
rootEl.dispatchEvent(evt); rootEl.dispatchEvent(evt);
}, },
_customEvents = 'onAdd onUpdate onRemove onStart onEnd onFilter onSort'.split(' '),
noop = function () {},
abs = Math.abs, abs = Math.abs,
slice = [].slice, slice = [].slice,
@ -170,6 +172,10 @@
this.options = options = _extend({}, options); this.options = options = _extend({}, options);
// Export instance
el[expando] = this;
// Default options // Default options
var defaults = { var defaults = {
group: Math.random(), group: Math.random(),
@ -215,16 +221,7 @@
}); });
// Define events
_customEvents.forEach(function (name) {
options[name] = _bind(this, options[name] || noop);
_on(el, name.substr(2).toLowerCase(), options[name]);
}, this);
// Export options
options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' '; options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' ';
el[expando] = options;
// Bind all private methods // Bind all private methods
@ -253,7 +250,8 @@
constructor: Sortable, constructor: Sortable,
_onTapStart: function (/** Event|TouchEvent */evt) { _onTapStart: function (/** Event|TouchEvent */evt) {
var el = this.el, var _this = this,
el = this.el,
options = this.options, options = this.options,
type = evt.type, type = evt.type,
touch = evt.touches && evt.touches[0], touch = evt.touches && evt.touches[0],
@ -278,7 +276,7 @@
// Check filter // Check filter
if (typeof filter === 'function') { if (typeof filter === 'function') {
if (filter.call(this, evt, target, this)) { if (filter.call(this, evt, target, this)) {
_dispatchEvent(originalTarget, 'filter', target, el, oldIndex); _dispatchEvent(_this, originalTarget, 'filter', target, el, oldIndex);
evt.preventDefault(); evt.preventDefault();
return; // cancel dnd return; // cancel dnd
} }
@ -288,7 +286,7 @@
criteria = _closest(originalTarget, criteria.trim(), el); criteria = _closest(originalTarget, criteria.trim(), el);
if (criteria) { if (criteria) {
_dispatchEvent(criteria, 'filter', target, el, oldIndex); _dispatchEvent(_this, criteria, 'filter', target, el, oldIndex);
return true; return true;
} }
}); });
@ -404,7 +402,7 @@
Sortable.active = this; Sortable.active = this;
// Drag start event // Drag start event
_dispatchEvent(rootEl, 'start', dragEl, rootEl, oldIndex); _dispatchEvent(this, rootEl, 'start', dragEl, rootEl, oldIndex);
} }
}, },
@ -419,7 +417,7 @@
if (parent) { if (parent) {
do { do {
if (parent[expando] && parent[expando].groups.indexOf(groupName) > -1) { if (parent[expando] && parent[expando].options.groups.indexOf(groupName) > -1) {
while (i--) { while (i--) {
touchDragOverListeners[i]({ touchDragOverListeners[i]({
clientX: touchEvt.clientX, clientX: touchEvt.clientX,
@ -700,14 +698,14 @@
newIndex = _index(dragEl); newIndex = _index(dragEl);
// drag from one list and drop into another // drag from one list and drop into another
_dispatchEvent(dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(null, dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
// Add event // Add event
_dispatchEvent(dragEl, 'add', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(null, dragEl.parentNode, 'add', dragEl, rootEl, oldIndex, newIndex);
// Remove event // Remove event
_dispatchEvent(rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex);
} }
else { else {
// Remove clone // Remove clone
@ -718,13 +716,13 @@
newIndex = _index(dragEl); newIndex = _index(dragEl);
// drag & drop within the same list // drag & drop within the same list
_dispatchEvent(rootEl, 'update', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
} }
} }
// Drag end event // Drag end event
Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, oldIndex, newIndex); Sortable.active && _dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
} }
// Nulling // Nulling
@ -853,11 +851,9 @@
* Destroy * Destroy
*/ */
destroy: function () { destroy: function () {
var el = this.el, options = this.options; var el = this.el;
_customEvents.forEach(function (name) { el[expando] = null;
_off(el, name.substr(2).toLowerCase(), options[name]);
});
_off(el, 'mousedown', this._onTapStart); _off(el, 'mousedown', this._onTapStart);
_off(el, 'touchstart', this._onTapStart); _off(el, 'touchstart', this._onTapStart);
@ -865,7 +861,7 @@
_off(el, 'dragover', this); _off(el, 'dragover', this);
_off(el, 'dragenter', this); _off(el, 'dragenter', this);
//remove draggable attributes // Remove draggable attributes
Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) { Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
el.removeAttribute('draggable'); el.removeAttribute('draggable');
}); });
@ -874,7 +870,7 @@
this._onDrop(); this._onDrop();
this.el = null; this.el = el = null;
} }
}; };
@ -1091,7 +1087,6 @@
throttle: _throttle, throttle: _throttle,
closest: _closest, closest: _closest,
toggleClass: _toggleClass, toggleClass: _toggleClass,
dispatchEvent: _dispatchEvent,
index: _index index: _index
}; };

3
ng-sortable.js

@ -167,8 +167,11 @@
scope.$watch(ngSortable + '.' + name, function (value) { scope.$watch(ngSortable + '.' + name, function (value) {
if (value !== void 0) { if (value !== void 0) {
options[name] = value; options[name] = value;
if (!/^on[A-Z]/.test(name)) {
sortable.option(name, value); sortable.option(name, value);
} }
}
}); });
}); });
} }

Loading…
Cancel
Save