-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathAddToJQueryPrototype.js
More file actions
85 lines (79 loc) · 2.97 KB
/
Copy pathAddToJQueryPrototype.js
File metadata and controls
85 lines (79 loc) · 2.97 KB
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
export function addToJQueryPrototype(pluginName, createPlugin, $){
const firstChar = pluginName.charAt(0);
const firstCharLower = firstChar.toLowerCase();
if (firstCharLower == firstChar) {
throw new Error(`Plugin name '${pluginName}' should be started from upper case char`)
}
const prototypableName = firstCharLower + pluginName.slice(1)
const noConflictPrototypable = $.fn[prototypableName];
const noConflictPrototypableForInstance = $.fn[pluginName];
const dataKey = `DashboardCode.${pluginName}`;
function createInstance(options, e, $e){
const optionsRef = (typeof options === 'object') || options instanceof Function ? options:null;
let instance = createPlugin(e, optionsRef,
() => {
$e.removeData(dataKey)
});
$e.data(dataKey, instance);
return instance;
}
function prototypable(options){
let output=[];
this.each( function (i, e) {
let $e = $(e);
let instance = $e.data(dataKey)
let isMethodName = typeof options === 'string';
if (!instance) {
if (isMethodName && /Dispose/.test(options))
return;
instance = createInstance(options, e, $e);
}
if (isMethodName) {
let methodName = options;
if (typeof instance[methodName] === 'undefined') {
let lMethodName = methodName.charAt(0).toLowerCase() + methodName.slice(1)
if ( typeof instance[lMethodName] === 'undefined') {
throw new Error(`No method named '${methodName}'`)
} else {
methodName = lMethodName;
}
}
let result = instance[methodName]();
if (result !== undefined){
output.push(result);
}
}
})
if (output.length==0)
return this;
else if (output.length==1)
return output[0];
else
return output;
}
function prototypableForInstance(options){
let instance = this.data(dataKey);
if (instance)
return instance;
else if (this.length === 1){
return createInstance(options, this.get(0), this);
} else if (this.length > 1){
let output=[];
this.each(function(i, e){
output.push(createInstance(options, e, $(e)));
})
return output;
}
}
$.fn[prototypableName] = prototypable;
$.fn[prototypableName].noConflict = function () {
$.fn[prototypableName] = noConflictPrototypable
return prototypable;
}
$.fn[pluginName] = prototypableForInstance;
$.fn[pluginName].noConflict = function () {
$.fn[pluginName] = noConflictPrototypableForInstance
return prototypableForInstance;
}
return prototypable;
}