| 1 |
let plugins = []; |
| 2 |
|
| 3 |
const defaults = { |
| 4 |
initializeByDefault: true |
| 5 |
}; |
| 6 |
|
| 7 |
export default { |
| 8 |
mount(plugin) { |
| 9 |
// Set default static properties |
| 10 |
for (let option in defaults) { |
| 11 |
if (defaults.hasOwnProperty(option) && !(option in plugin)) { |
| 12 |
plugin[option] = defaults[option]; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
plugins.forEach(p => { |
| 17 |
if (p.pluginName === plugin.pluginName) { |
| 18 |
throw (`Sortable: Cannot mount plugin ${ plugin.pluginName } more than once`); |
| 19 |
} |
| 20 |
}); |
| 21 |
|
| 22 |
plugins.push(plugin); |
| 23 |
}, |
| 24 |
pluginEvent(eventName, sortable, evt) { |
| 25 |
this.eventCanceled = false; |
| 26 |
evt.cancel = () => { |
| 27 |
this.eventCanceled = true; |
| 28 |
}; |
| 29 |
const eventNameGlobal = eventName + 'Global'; |
| 30 |
plugins.forEach(plugin => { |
| 31 |
if (!sortable[plugin.pluginName]) return; |
| 32 |
// Fire global events if it exists in this sortable |
| 33 |
if ( |
| 34 |
sortable[plugin.pluginName][eventNameGlobal] |
| 35 |
) { |
| 36 |
sortable[plugin.pluginName][eventNameGlobal]({ sortable, ...evt }); |
| 37 |
} |
| 38 |
|
| 39 |
// Only fire plugin event if plugin is enabled in this sortable, |
| 40 |
// and plugin has event defined |
| 41 |
if ( |
| 42 |
sortable.options[plugin.pluginName] && |
| 43 |
sortable[plugin.pluginName][eventName] |
| 44 |
) { |
| 45 |
sortable[plugin.pluginName][eventName]({ sortable, ...evt }); |
| 46 |
} |
| 47 |
}); |
| 48 |
}, |
| 49 |
initializePlugins(sortable, el, defaults, options) { |
| 50 |
plugins.forEach(plugin => { |
| 51 |
const pluginName = plugin.pluginName; |
| 52 |
if (!sortable.options[pluginName] && !plugin.initializeByDefault) return; |
| 53 |
|
| 54 |
let initialized = new plugin(sortable, el, sortable.options); |
| 55 |
initialized.sortable = sortable; |
| 56 |
initialized.options = sortable.options; |
| 57 |
sortable[pluginName] = initialized; |
| 58 |
|
| 59 |
// Add default options from plugin |
| 60 |
Object.assign(defaults, initialized.defaults); |
| 61 |
}); |
| 62 |
|
| 63 |
for (let option in sortable.options) { |
| 64 |
if (!sortable.options.hasOwnProperty(option)) continue; |
| 65 |
let modified = this.modifyOption(sortable, option, sortable.options[option]); |
| 66 |
if (typeof(modified) !== 'undefined') { |
| 67 |
sortable.options[option] = modified; |
| 68 |
} |
| 69 |
} |
| 70 |
}, |
| 71 |
getEventProperties(name, sortable) { |
| 72 |
let eventProperties = {}; |
| 73 |
plugins.forEach(plugin => { |
| 74 |
if (typeof(plugin.eventProperties) !== 'function') return; |
| 75 |
Object.assign(eventProperties, plugin.eventProperties.call(sortable[plugin.pluginName], name)); |
| 76 |
}); |
| 77 |
|
| 78 |
return eventProperties; |
| 79 |
}, |
| 80 |
modifyOption(sortable, name, value) { |
| 81 |
let modifiedValue; |
| 82 |
plugins.forEach(plugin => { |
| 83 |
// Plugin must exist on the Sortable |
| 84 |
if (!sortable[plugin.pluginName]) return; |
| 85 |
|
| 86 |
// If static option listener exists for this option, call in the context of the Sortable's instance of this plugin |
| 87 |
if (plugin.optionListeners && typeof(plugin.optionListeners[name]) === 'function') { |
| 88 |
modifiedValue = plugin.optionListeners[name].call(sortable[plugin.pluginName], value); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
return modifiedValue; |
| 93 |
} |
| 94 |
}; |
| 95 |
|