This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
newPlugin.jquery.js
118 lines (105 loc) · 3.14 KB
/
newPlugin.jquery.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function(root, factory) {
if ( typeof define === 'function' && define.amd ) {
define([ 'jquery' ], factory);
} else if ( typeof module == 'object' && module.exports ) {
module.exports = function($) {
return factory($);
};
} else {
var $ = root.jQuery || root.Zepto;
if ( !$ )
throw new Error('jQuery or Zepto must be defined');
$.newPlugin = factory($);
}
})(this, function($, undefined) {
'use strict';
var noop = $.noop;
/**
* Default init options
* @type {Object}
*/
var defaultOptions = {
ready: function() { return true; }
};
/**
* Creates new plugin in `$.fn` object. If plugin/method exists factory throws error.
* @param {String} __pluginName plugin name must be unique relative to other plugins and internal jQuery methods
* @param {Function} Obj plugin constructor Function
* @param {(Object|Function)} options plugin options or callback Function
* @return {Object} returns plugin object itself
*/
return function(__pluginName, Obj, options) {
var ready;
Obj = typeof Obj == 'function' ? Obj : noop;
/**
* Using for debugging, etc
* @return {Boolean} if true, stores new Object to data
*/
if ( typeof options == 'function' ) {
ready = options;
} else {
options = $.extend({}, defaultOptions, options);
ready = options.ready;
}
/**
* Throw error if plugin name is not defined
*/
if ( !__pluginName || typeof __pluginName != 'string' ) {
throw new Error('Expected plugin name');
}
/**
* Checking for old plugin existence
*/
if ( $.fn[__pluginName] !== undefined ) {
throw new Error('Plugin "' + __pluginName + '" already exists');
}
/**
* Object template with default methods
*/
Obj.prototype = $.extend({
destroy : noop,
init: noop,
update: noop
}, Obj.prototype);
/**
* Creating new instance(s) for each given element
* @return {jQuery} returns jQuery object
*/
var fn = $.fn[__pluginName] = function() {
var args = [];
/**
* Using $.each because $.map function flatten arrays
*/
$.each(arguments, function(index, arg) { args.push(arg); });
var opt = args[0],
params = args.slice(1);
return this.each(function() {
var $self = $(this),
obj = $self.data(__pluginName),
oldData;
if ( obj instanceof Obj ) {
if ( typeof opt == 'string' && typeof obj[opt] == 'function' )
obj[opt].apply(obj, params);
else
obj.update.apply(obj, args);
} else {
/**
* Don't init new plugin if calling destroy method
*/
if ( opt != 'destroy' ) {
/**
* Data contains in element data-<__pluginName>
*/
if ( obj !== undefined )
oldData = obj;
obj = new Obj($self, opt, oldData);
if ( ready.call(obj) )
$self.data(__pluginName, obj);
}
}
});
};
fn.__constr__ = Obj;
return fn;
};
});