2013-10-04 02:45:36 +02:00
|
|
|
/**
|
|
|
|
* Plugin boilerplate
|
|
|
|
*/
|
|
|
|
module.exports = (function() {
|
|
|
|
|
|
|
|
var AP = Array.prototype;
|
|
|
|
|
|
|
|
return function(plugin) {
|
|
|
|
|
2013-10-04 02:50:18 +02:00
|
|
|
plugin = $.extend(true, {
|
2013-10-04 02:45:36 +02:00
|
|
|
name: 'plugin',
|
2013-10-04 02:50:18 +02:00
|
|
|
defaults: {
|
|
|
|
disabledExtensions: 'none'
|
|
|
|
},
|
2013-10-04 02:45:36 +02:00
|
|
|
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 = {};
|
|
|
|
|
2013-10-04 02:50:18 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-10-04 02:45:36 +02:00
|
|
|
Plugin.prototype._extend = function(extensions) {
|
|
|
|
|
2013-10-04 02:50:18 +02:00
|
|
|
var self = this;
|
2013-10-04 02:45:36 +02:00
|
|
|
|
|
|
|
$.each(extensions, function(i, extension) {
|
|
|
|
|
|
|
|
$.extend(self.opts, $.extend(true, extension.options, self.opts));
|
|
|
|
|
|
|
|
$.each(extension.methods, function(method, fn) {
|
|
|
|
|
2013-10-04 02:50:18 +02:00
|
|
|
if (self.opts.disabledExtensions.indexOf(extension.name) > -1) {
|
2013-10-04 02:45:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Plugin.prototype[method]) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|