PluginManager.js 2.6 KB

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