plugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Plugin boilerplate
  3. */
  4. module.exports = (function() {
  5. var AP = Array.prototype;
  6. return function(plugin) {
  7. plugin = $.extend(true, {
  8. name: 'plugin',
  9. defaults: {
  10. disabledExtensions: 'none'
  11. },
  12. methods: {},
  13. global: {},
  14. }, plugin);
  15. $[plugin.name] = $.extend({
  16. addExtension: function(extension) {
  17. plugin.global.extensions.push(extension);
  18. }
  19. }, plugin.global);
  20. function Plugin(element, options) {
  21. this.opts = $.extend({}, plugin.defaults, options);
  22. this.el = element;
  23. this._name = plugin.name;
  24. this._init();
  25. }
  26. Plugin._extended = {};
  27. Plugin.prototype._hasExtension = function(extension) {
  28. var self = this;
  29. return plugin.global.extensions.filter(function(ext) {
  30. return ext.name == extension && self.opts.disabledExtensions.indexOf(ext.name) < 0;
  31. }).length;
  32. };
  33. Plugin.prototype._extend = function(extensions) {
  34. var self = this;
  35. $.each(extensions, function(i, extension) {
  36. $.extend(self.opts, $.extend(true, extension.options, self.opts));
  37. $.each(extension.methods, function(method, fn) {
  38. if (self.opts.disabledExtensions.indexOf(extension.name) > -1) {
  39. return;
  40. }
  41. if (Plugin.prototype[method.split(':')[0]]) {
  42. Plugin._extended[method] = Plugin._extended[method] || [];
  43. Plugin._extended[method].push({ name: extension.name, fn: fn });
  44. } else {
  45. Plugin.prototype[method] = fn;
  46. }
  47. });
  48. });
  49. };
  50. Plugin.prototype._inject = function(method) {
  51. var args = [].slice.call(arguments, 1);
  52. if (typeof method == 'function') return method.call(this);
  53. var self = this;
  54. if (Plugin._extended[method]) {
  55. $.each(Plugin._extended[method], function(i, plugin) {
  56. plugin.fn.apply(self, args);
  57. });
  58. }
  59. };
  60. Plugin.prototype._init = $.noop;
  61. Plugin.prototype[plugin.name] = function(method) {
  62. if (!method) return this;
  63. try { return this[method].apply(this, AP.slice.call(arguments, 1)); }
  64. catch(e) {}
  65. };
  66. $.extend(Plugin.prototype, plugin.methods);
  67. $.fn[plugin.name] = function() {
  68. var args = AP.slice.call(arguments)
  69. , methodArray = typeof args[0] == 'string' && args[0].split(':')
  70. , method = methodArray[methodArray.length > 1 ? 1 : 0]
  71. , prefix = methodArray.length > 1 && methodArray[0]
  72. , opts = typeof args[0] == 'object' && args[0]
  73. , params = args.slice(1)
  74. , ret;
  75. if (prefix) {
  76. method = prefix + method.substr(0,1).toUpperCase() + method.substr(1,method.length-1);
  77. }
  78. this.each(function() {
  79. var instance = $.data(this, plugin.name);
  80. // Method
  81. if (instance) {
  82. return ret = instance[plugin.name].apply(instance, [method].concat(params));
  83. }
  84. // Init
  85. return $.data(this, plugin.name, new Plugin(this, opts));
  86. });
  87. return prefix ? ret : this;
  88. };
  89. };
  90. }());