forked from mattdrose/jquery-boiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.boiler.js
executable file
·80 lines (68 loc) · 2.41 KB
/
jquery.boiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
( function( factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define([ 'jquery' ], factory );
} else if ( typeof exports === 'object' ) {
// Node/CommonJS
module.exports = factory( require( 'jquery' ));
} else {
// Browser globals
factory( jQuery );
}
})( function( $ ) {
'use strict';
$.boiler = function( namespace, base ) {
$.fn[namespace] = function() {
var args = Array.prototype.slice.call( arguments );
var method = args[0];
var options = args.slice( 1 );
return this.each( function() {
var cachedPlugin = $( this ).data( namespace );
if ( cachedPlugin === undefined ) {
// make a clone of the base
var plugin = {};
$.extend( plugin, base );
// cache the element
plugin.el = this;
plugin.$el = $( this );
// cache the plugin
plugin.$el.data( namespace, plugin );
// cache the options
if ( method !== undefined ) plugin.options = method;
// check for data attributes
if ( plugin.data ) {
var dataList = plugin.data;
plugin.data = {};
$.each( dataList, function( index, name ) {
plugin.data[name] = plugin.$el.data( name );
});
}
// overwrite options with data and defaults
plugin.settings = $.extend({}, plugin.defaults, plugin.options, plugin.data );
// check for events
if ( plugin.events ) {
$.each( plugin.events, function( eventString, handler ) {
var tmp = eventString.split( ' ' );
var event = tmp[0];
var selector = tmp.slice( 1 ).join( ' ' ) || null;
plugin.$el.on( event, selector, function( e ) {
plugin[handler].call( plugin, e, this );
});
});
}
// fire up the plugin!
if ( plugin.init ) plugin.init();
} else if ( cachedPlugin[method] && method.charAt( 0 ) !== '_' ) {
// if method is a function
if ( typeof cachedPlugin[method] === 'function' ) {
cachedPlugin[method].apply( cachedPlugin, options );
// otherwise, treat it as a propery and reset it
} else {
cachedPlugin[method] = options[0];
}
}
});
};
};
return $;
});