From 01dd3d329d52af39684f9ecf4a9a97742e72c599 Mon Sep 17 00:00:00 2001 From: "e.fattal" Date: Fri, 14 Jun 2019 15:34:42 +0200 Subject: [PATCH] Fix https://github.com/SortableJS/Sortable/issues/1539 (when Array.prototype is modified) --- src/Animation.js | 29 ++++++++--------- src/PluginManager.js | 77 ++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/Animation.js b/src/Animation.js index eff0110ed..a59334207 100644 --- a/src/Animation.js +++ b/src/Animation.js @@ -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) { @@ -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); @@ -90,7 +89,7 @@ export default function AnimationStateManager() { isScrolledPast(target, fromRect, 'right', 'left') || isScrolledPast(target, fromRect, 'left', 'right') ) - ) continue; + ) return; if (target.thisAnimationDuration) { @@ -137,7 +136,7 @@ export default function AnimationStateManager() { }).bind({ animationStates, i: Number(i) }), time); target.thisAnimationDuration = time; } - } + }); clearTimeout(animationCallbackId); diff --git a/src/PluginManager.js b/src/PluginManager.js index 4b479fe5a..5269c1b46 100644 --- a/src/PluginManager.js +++ b/src/PluginManager.js @@ -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]); @@ -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; + }); } };