/** * Plugin boilerplate */ module.exports = (function() { var AP = Array.prototype; return function(plugin) { plugin = $.extend(true, { name: 'plugin', defaults: { disabledExtensions: 'none' }, methods: {}, global: {}, }, plugin); $[plugin.name] = $.extend({ addExtension: function(extension) { plugin.global.extensions.push(extension); } }, plugin.global); function Plugin(element, options) { this.opts = $.extend({}, plugin.defaults, options); this.el = element; this._name = plugin.name; this._init(); } Plugin._extended = {}; Plugin.prototype._hasExtension = function(extension) { var self = this; return plugin.global.extensions.filter(function(ext) { return ext.name == extension && self.opts.disabledExtensions.indexOf(ext.name) < 0; }).length; }; Plugin.prototype._extend = function(extensions) { var self = this; $.each(extensions, function(i, extension) { $.extend(self.opts, $.extend(true, extension.options, self.opts)); $.each(extension.methods, function(method, fn) { if (self.opts.disabledExtensions.indexOf(extension.name) > -1) { return; } if (Plugin.prototype[method.split(':')[0]]) { Plugin._extended[method] = Plugin._extended[method] || []; Plugin._extended[method].push({ name: extension.name, fn: fn }); } else { Plugin.prototype[method] = fn; } }); }); }; Plugin.prototype._inject = function(method) { var args = [].slice.call(arguments, 1); if (typeof method == 'function') return method.call(this); var self = this; if (Plugin._extended[method]) { $.each(Plugin._extended[method], function(i, plugin) { plugin.fn.apply(self, args); }); } }; Plugin.prototype._init = $.noop; Plugin.prototype[plugin.name] = function(method) { if (!method) return this; try { return this[method].apply(this, AP.slice.call(arguments, 1)); } catch(e) {} }; $.extend(Plugin.prototype, plugin.methods); $.fn[plugin.name] = function() { var args = AP.slice.call(arguments) , methodArray = typeof args[0] == 'string' && args[0].split(':') , method = methodArray[methodArray.length > 1 ? 1 : 0] , prefix = methodArray.length > 1 && methodArray[0] , opts = typeof args[0] == 'object' && args[0] , params = args.slice(1) , ret; if (prefix) { method = prefix + method.substr(0,1).toUpperCase() + method.substr(1,method.length-1); } this.each(function() { var instance = $.data(this, plugin.name); // Method if (instance) { return ret = instance[plugin.name].apply(instance, [method].concat(params)); } // Init return $.data(this, plugin.name, new Plugin(this, opts)); }); return prefix ? ret : this; }; }; }());