plugin.js 2.9 KB

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