index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var _ = require("lodash");
  2. /**
  3. * @constructor
  4. */
  5. var EasyExtender = function (plugins, hooks) {
  6. this.plugins = {};
  7. this.pluginOptions = {};
  8. this.returnValues = {};
  9. this.hooks = hooks;
  10. this.defaultPlugins = plugins;
  11. return this;
  12. };
  13. /**
  14. * @returns {EasyExtender}
  15. */
  16. EasyExtender.prototype.init = function () {
  17. if (this.defaultPlugins) {
  18. var required = Object.keys(this.defaultPlugins);
  19. required.forEach(function (name) {
  20. if (_.isUndefined(this.plugins[name])) {
  21. this.plugins[name] = this.defaultPlugins[name];
  22. }
  23. }, this);
  24. }
  25. return this;
  26. };
  27. /**
  28. * Call the '.plugin()' method of all registered plugins
  29. */
  30. EasyExtender.prototype.initUserPlugins = function () {
  31. var args = _.toArray(arguments);
  32. var userPlugins = _.difference(Object.keys(this.plugins), Object.keys(this.defaultPlugins));
  33. if (userPlugins.length) {
  34. userPlugins.forEach(function (plugin) {
  35. var pluginOptions = {};
  36. if (this.pluginOptions) {
  37. pluginOptions = this.pluginOptions[plugin];
  38. }
  39. this.returnValues[plugin].push({
  40. value: this.get(plugin).apply(null, [pluginOptions].concat(args))
  41. });
  42. if (!_.isUndefined(pluginOptions) && !_.isUndefined(pluginOptions.enabled)) {
  43. if (pluginOptions.enabled) {
  44. this.enablePlugin(plugin);
  45. } else {
  46. this.disablePlugin(plugin);
  47. }
  48. } else {
  49. this.enablePlugin(plugin);
  50. }
  51. }, this);
  52. }
  53. };
  54. /**
  55. * Wrap a module in East Extender pattern
  56. * @param module
  57. * @param localName
  58. */
  59. EasyExtender.prototype.wrap = function (module, localName) {
  60. this.registerPlugin({
  61. "plugin:name": localName,
  62. "plugin": function () {
  63. return module;
  64. }
  65. });
  66. };
  67. /**
  68. * @param {String} name
  69. * @returns {Function|Boolean}
  70. */
  71. EasyExtender.prototype.get = function (name) {
  72. if (!_.isUndefined(this.plugins[name])) {
  73. return this.plugins[name].plugin || false;
  74. }
  75. return false;
  76. };
  77. /**
  78. * @param {Object} module
  79. * @param {Object} [opts]
  80. * @param {Function} [cb]
  81. */
  82. EasyExtender.prototype.registerPlugin = function (module, opts, cb) {
  83. var pluginOptions;
  84. if (!_.isFunction(module.plugin)) {
  85. return _.isFunction(cb) ? cb("Module must implement a .plugin() method") : false;
  86. }
  87. if (!cb && opts) {
  88. if (_.isFunction(opts)) {
  89. cb = opts;
  90. } else {
  91. pluginOptions = opts;
  92. }
  93. }
  94. var name = _.isUndefined(module["plugin:name"]) ? _.uniqueId() : module["plugin:name"];
  95. this.pluginOptions[name] = pluginOptions;
  96. this.returnValues[name] = [];
  97. this.plugins[name] = module;
  98. if (_.isFunction(cb)) {
  99. cb(null);
  100. }
  101. this.disablePlugin(name);
  102. return this;
  103. };
  104. /**
  105. *
  106. * @param name
  107. */
  108. EasyExtender.prototype.getPlugin = function (module) {
  109. if (_.isString(module)) {
  110. module = this.plugins[module];
  111. }
  112. if (!module) {
  113. return false;
  114. }
  115. return module;
  116. };
  117. /**
  118. *
  119. * @param name
  120. */
  121. EasyExtender.prototype.disablePlugin = function (module) {
  122. module = this.getPlugin(module);
  123. module._enabled = false;
  124. return module;
  125. };
  126. /**
  127. * @param name
  128. */
  129. EasyExtender.prototype.enablePlugin = function (module) {
  130. module = this.getPlugin(module);
  131. module._enabled = true;
  132. return module;
  133. };
  134. /**
  135. * @param name
  136. * @returns {*}
  137. */
  138. EasyExtender.prototype.hook = function (name) {
  139. // Get any extra args
  140. var args = Array.prototype.slice.call(arguments, 1);
  141. // build a list of hook funcs
  142. var funcs = [];
  143. _.each(this.plugins, function (plugin) {
  144. if (plugin.hooks) {
  145. if (!_.isUndefined(plugin.hooks[name])) {
  146. funcs.push(plugin.hooks[name]);
  147. }
  148. }
  149. });
  150. return this.hooks[name].apply(this, [funcs].concat(args));
  151. };
  152. /**
  153. * @param name
  154. * @returns {*}
  155. */
  156. EasyExtender.prototype.getReturnValues = function (name) {
  157. return this.returnValues[name] || [];
  158. };
  159. module.exports = EasyExtender;