-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththin.js
326 lines (247 loc) · 9.27 KB
/
thin.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(function (window, document) {
"use strict";
var prototypePatch = Object.getPrototypeOf(document.querySelectorAll("html")).constructor.name === "Object";
function bindLoad(listener, waitForAll) {
if (waitForAll === true) {
window.addEventListener("load", listener);
return;
}
document.addEventListener("DOMContentLoaded", listener);
}
function forEach(object, callback, thisArg) {
window.Array.prototype.forEach.call(object, callback, thisArg);
}
function getInheritedPrototypes(object) {
var prototypes = [],
parentPrototype = Object.getPrototypeOf(object);
if (parentPrototype !== null) {
prototypes = prototypes.concat(getInheritedPrototypes(parentPrototype));
prototypes.push((prototypePatch ? parentPrototype : parentPrototype.constructor).toString().match("(?:function|\\[object) ([A-Za-z0-9]+)")[1].replace("Prototype", ""));
}
return prototypes;
}
function generateRandomClassName() {
var randomClassName;
do {
randomClassName = "thin-rcn-" + Math.random().toString(36).substring(7);
} while (document.querySelector("." + randomClassName));
return randomClassName;
}
function convertToNodeList() {
var randomClassName = generateRandomClassName(),
nodeList;
forEach(arguments, function (object) {
var prototypes = getInheritedPrototypes(object);
if (prototypes.indexOf("NodeList") >= 0 || prototypes.indexOf("Element") >= 0) {
object.addClass(randomClassName);
}
});
nodeList = document.querySelectorAll("." + randomClassName);
nodeList.removeClass(randomClassName);
return nodeList;
}
var originalMethods = {
"Element": {
"setAttribute": window.Element.prototype.setAttribute,
"removeAttribute": window.Element.prototype.removeAttribute,
"addEventListener": window.Element.prototype.addEventListener,
"removeEventListener": window.Element.prototype.removeEventListener
}
},
thinFunctionSignatures = {
"string": {
thisArg: document,
callback: document.querySelectorAll
},
"function, boolean?": {
thisArg: this,
callback: bindLoad
},
"object:Element|NodeList+": {
thisArg: this,
callback: convertToNodeList
}
};
function callEach(object, functionName, args) {
var returnValues = [];
object.forEach(function (item) {
returnValues.push(item[functionName].apply(item, args));
});
return returnValues;
}
function getTypeString(variable) {
var type = typeof variable;
if (type !== "object") {
return type;
}
return type + ":{" + getInheritedPrototypes(variable).join("}{") + "}";
}
function getSignature(args) {
var types = [];
forEach(args, function (variable) {
types.push(getTypeString(variable));
});
return types.join(", ");
}
function generateRegex(signature) {
var parameters = signature.split(new RegExp(", ?"));
parameters = parameters.map(function (parameter, index) {
parameter = (index > 0 ? ", " : "") + parameter;
// convert optional parameters
parameter = parameter.replace(new RegExp("^(, )?(.*)\\?$"), "($1$2)?");
// convert multiple parameters
parameter = parameter.replace(new RegExp("^(, )?(.*)\\+$"), "($1$2(, )?)+");
// convert object type parameter to handle inheritance correctly
parameter = parameter.replace(new RegExp("^(\\(?(?:, )?)object:((?:[^\\|\\(\\)]+\\|?)+)(\\)\\?|\\(, \\)\\?\\)\\+)?$"), "$1object:({[^}]+})*{($2)}({[^}]+})*$3");
return parameter;
});
return new RegExp("^" + parameters.join("") + "$");
}
function matchSignature(args) {
var thinSignature,
calledSignature = getSignature(args);
for (thinSignature in thinFunctionSignatures) {
if (calledSignature.match(generateRegex(thinSignature)) !== null) {
return thinFunctionSignatures[thinSignature];
}
}
}
window.Thin = function () {
var signature = matchSignature(arguments);
return signature.callback.apply(signature.thisArg, arguments);
};
/**
* Element
*/
// Classes
window.Element.prototype.addClass = function () {
forEach(arguments, function (name) {
this.classList.add(name);
}, this);
return this;
};
window.Element.prototype.removeClass = function () {
forEach(arguments, function (name) {
this.classList.remove(name);
}, this);
return this;
};
window.Element.prototype.hasClass = function (name) {
return this.classList.contains(name);
};
// Attributes
window.Element.prototype.setAttribute = function (name, value) {
var attributes = name;
if (typeof attributes === "string") {
attributes = {};
attributes[name] = value;
}
for (name in attributes) {
originalMethods.Element.setAttribute.call(this, name, attributes[name]);
}
return this;
};
window.Element.prototype.removeAttribute = function () {
forEach(arguments, function (name) {
originalMethods.Element.removeAttribute.call(this, name);
}, this);
return this;
};
// Properties
window.Element.prototype.setProperty = function (name, value) {
var properties = name;
if (typeof properties === "string") {
properties = {};
properties[name] = value;
}
for (name in properties) {
if (this.hasProperty(name)) {
this[name] = properties[name];
}
}
return this;
};
window.Element.prototype.getProperty = function (name) {
return this[name];
};
window.Element.prototype.hasProperty = function (name) {
return (name in this);
};
// Events
window.Element.prototype.addEventListener = function (type, listener, useCapture) {
originalMethods.Element.addEventListener.call(this, type, listener, useCapture);
return this;
};
window.Element.prototype.removeEventListener = function (type, listener, useCapture) {
originalMethods.Element.removeEventListener.call(this, type, listener, useCapture);
return this;
};
/**
* NodeList
*/
window.NodeList.prototype.forEach = function (callback, thisArg) {
forEach(this, callback, thisArg);
return this;
};
// Classes
window.NodeList.prototype.addClass = function () {
callEach(this, "addClass", arguments);
return this;
};
window.NodeList.prototype.removeClass = function () {
callEach(this, "removeClass", arguments);
return this;
};
window.NodeList.prototype.hasClass = function (name) {
var results = callEach(this, "hasClass", [name]);
return (results.indexOf(true) >= 0);
};
// Attributes
window.NodeList.prototype.getAttribute = function (name) {
return this[0].getAttribute(name);
};
window.NodeList.prototype.setAttribute = function (name, value) {
callEach(this, "setAttribute", [name, value]);
return this;
};
window.NodeList.prototype.removeAttribute = function () {
callEach(this, "removeAttribute", arguments);
return this;
};
window.NodeList.prototype.hasAttribute = function (name) {
var results = callEach(this, "hasAttribute", [name]);
return (results.indexOf(true) >= 0);
};
// Properties
window.NodeList.prototype.setProperty = function (name, value) {
callEach(this, "setProperty", [name, value]);
return this;
};
window.NodeList.prototype.getProperty = function (name) {
return this[0].getProperty(name);
};
window.NodeList.prototype.hasProperty = function (name) {
var results = callEach(this, "hasProperty", [name]);
return (results.indexOf(true) >= 0);
};
// Events
window.NodeList.prototype.addEventListener = function (type, listener, useCapture) {
callEach(this, "addEventListener", [type, listener, useCapture]);
return this;
};
window.NodeList.prototype.removeEventListener = function (type, listener, useCapture) {
callEach(this, "removeEventListener", [type, listener, useCapture]);
return this;
};
// Query
window.NodeList.prototype.querySelector = function (selector) {
var results = callEach(this, "querySelector", [selector]);
return results.filter(function (elements) {
return elements !== null;
})[0] || [];
};
window.NodeList.prototype.querySelectorAll = function (selector) {
var results = callEach(this, "querySelectorAll", [selector]);
return convertToNodeList.apply(null, results);
};
}(window, document));