Skip to content

Commit

Permalink
Fix SortableJS#1539 (when Array.prototype is modified)
Browse files Browse the repository at this point in the history
  • Loading branch information
e.fattal committed Jun 14, 2019
1 parent 0169c75 commit 01dd3d3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
29 changes: 14 additions & 15 deletions src/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ export default function AnimationStateManager() {
if (!this.options.animation) return;
let children = [].slice.call(this.el.children);

for (let i in children) {
if (css(children[i], 'display') === 'none' || children[i] === Sortable.ghost) continue;
children.forEach(child => {
if (css(child, 'display') === 'none' || child === Sortable.ghost) return;
animationStates.push({
target: children[i],
rect: getRect(children[i])
target: child,
rect: getRect(child)
});
let fromRect = getRect(children[i]);
let fromRect = getRect(child);

// If animating: compensate for current animation
if (children[i].thisAnimationDuration) {
let childMatrix = matrix(children[i], true);
if (child.thisAnimationDuration) {
let childMatrix = matrix(child, true);
if (childMatrix) {
fromRect.top -= childMatrix.f;
fromRect.left -= childMatrix.e;
}
}

children[i].fromRect = fromRect;
}
child.fromRect = fromRect;
});
},

addAnimationState(state) {
Expand All @@ -50,15 +50,14 @@ export default function AnimationStateManager() {
let animating = false,
animationTime = 0;

for (let i in animationStates) {
animationStates.forEach((animationState, i) => {
let time = 0,
animatingThis = false,
target = animationStates[i].target,
target = animationState.target,
fromRect = target.fromRect,
toRect = getRect(target),
prevFromRect = target.prevFromRect,
prevToRect = target.prevToRect,
animatingRect = animationStates[i].rect,
animatingRect = animationState.rect,
targetMatrix = matrix(target, true);


Expand Down Expand Up @@ -90,7 +89,7 @@ export default function AnimationStateManager() {
isScrolledPast(target, fromRect, 'right', 'left') ||
isScrolledPast(target, fromRect, 'left', 'right')
)
) continue;
) return;


if (target.thisAnimationDuration) {
Expand Down Expand Up @@ -137,7 +136,7 @@ export default function AnimationStateManager() {
}).bind({ animationStates, i: Number(i) }), time);
target.thisAnimationDuration = time;
}
}
});


clearTimeout(animationCallbackId);
Expand Down
77 changes: 39 additions & 38 deletions src/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,39 @@ export default {
pluginEvent(eventName, sortable, evt) {
this.eventCanceled = false;
const eventNameGlobal = eventName + 'Global';
for (let i in plugins) {
if (!sortable[plugins[i].pluginName]) continue;
// Fire global events if it exists in this sortable
if (
sortable[plugins[i].pluginName][eventNameGlobal]
) {
this.eventCanceled = !!sortable[plugins[i].pluginName][eventNameGlobal]({ sortable, ...evt });
}
plugins.forEach(plugin => {
if (sortable[plugin.pluginName]) {
// Fire global events if it exists in this sortable
if (
sortable[plugin.pluginName][eventNameGlobal]
) {
this.eventCanceled = !!sortable[plugin.pluginName][eventNameGlobal]({sortable, ...evt});
}

// Only fire plugin event if plugin is enabled in this sortable,
// and plugin has event defined
if (
sortable.options[plugins[i].pluginName] &&
sortable[plugins[i].pluginName][eventName]
) {
this.eventCanceled = this.eventCanceled || !!sortable[plugins[i].pluginName][eventName]({ sortable, ...evt });
// Only fire plugin event if plugin is enabled in this sortable,
// and plugin has event defined
if (
sortable.options[plugin.pluginName] &&
sortable[plugin.pluginName][eventName]
) {
this.eventCanceled = this.eventCanceled || !!sortable[plugin.pluginName][eventName]({sortable, ...evt});
}
}
}
});
},
initializePlugins(sortable, el, defaults) {
for (let i in plugins) {
const pluginName = plugins[i].pluginName;
if (!sortable.options[pluginName] && !plugins[i].initializeByDefault) continue;
plugins.forEach(plugin => {
const pluginName = plugin.pluginName;
if (sortable.options[pluginName] || plugin.initializeByDefault) {

let initialized = new plugins[i](sortable, el);
initialized.sortable = sortable;
sortable[pluginName] = initialized;
let initialized = new plugin(sortable, el);
initialized.sortable = sortable;
sortable[pluginName] = initialized;

// Add default options from plugin
Object.assign(defaults, initialized.options);
}
// Add default options from plugin
Object.assign(defaults, initialized.options);
}
});

for (let option in sortable.options) {
let modified = this.modifyOption(sortable, option, sortable.options[option]);
Expand All @@ -56,23 +58,22 @@ export default {
},
getEventOptions(name, sortable) {
let eventOptions = {};
for (let i in plugins) {
if (typeof(plugins[i].eventOptions) !== 'function') continue;
Object.assign(eventOptions, plugins[i].eventOptions.call(sortable, name));
}
plugins.forEach(plugin => {
if (typeof(plugin.eventOptions) === 'function') {
Object.assign(eventOptions, plugins[i].eventOptions.call(sortable, name));
}
});
return eventOptions;
},
modifyOption(sortable, name, value) {
let modifiedValue;
for (let i in plugins) {
return plugins.find(plugin => {
// Plugin must exist on the Sortable
if (!sortable[plugins[i].pluginName]) continue;

// If static option listener exists for this option, call in the context of the Sortable's instance of this plugin
if (plugins[i].optionListeners && typeof(plugins[i].optionListeners[name]) === 'function') {
modifiedValue = plugins[i].optionListeners[name].call(sortable[plugins[i].pluginName], value);
if (sortable[plugin.pluginName]) {
// If static option listener exists for this option, call in the context of the Sortable's instance of this plugin
if (plugin.optionListeners && typeof (plugin.optionListeners[name]) === 'function') {
return plugin.optionListeners[name].call(sortable[plugin.pluginName], value);
}
}
}
return modifiedValue;
});
}
};

0 comments on commit 01dd3d3

Please sign in to comment.