-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjQuery.js
220 lines (190 loc) · 7.16 KB
/
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
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
// jQuery
// Clone by Aldrin Caballero
// January 01, 2023
// Do not copy the code without permission of the owner
// It doesn't make you as a programmer
const d = document,
w = window,
str = "string";
function $ (selector) {
"use strict";
try {
let doc =
selector &&
(typeof selector != str ? selector : d.querySelectorAll(selector));
let methods = {
ready: function (clbck) {
typeof clbck == "function"
? doc == w
? (doc.onload = clbck)
: doc.addEventListener("DOMContentLoaded", clbck)
: console.warn(
"Warning: Missing function argument at .ready( clbck: Function ) method."
);
},
// use to add event to html elements
on: function (ev, clbck) {
if (typeof ev == str && typeof clbck == "function") {
if (typeof selector != str) {
doc.addEventListener(ev, clbck);
} else {
doc.forEach((element) => {
element.addEventListener(ev, clbck);
});
}
} else {
console.warn("Warning: Error on .on( ev: Any, clbck: Function ) method.");
}
},
// same as on( ) method
bind: function (ev, clbck) {
this.on(ev, clbck);
},
// Encode a set of form elements as a string for submission
serialize: function () {
return this.toURLSearchParams(undefined);
},
// Encode a set of FormData or form elements as an array of names and values
serializeArray: function () {
return this.toURLSearchParams("array");
},
// Encode a set of FormData or form elements as an object of names and values pair
serializeObject: function () {
return this.toURLSearchParams("object");
},
// use to set or return innerText content of html element in the first occurence
text: function () {
return (arguments[0] != undefined &&
arguments.length == 1 &&
(() =>
typeof selector != str &&
(() => (doc.innerText = arguments[0]))())()) ||
(typeof selector == str &&
arguments.length == 1 &&
arguments != undefined &&
(() => (doc[0].innerText = arguments[0]))()) ||
(arguments[0] == undefined && typeof selector != str)
? doc.innerText
: doc[0].innerText;
},
// use to set and get html attribute
attr: function () {
return (arguments != undefined &&
arguments.length == 2 &&
(() =>
typeof selector != str &&
(() => (doc.setAttribute(arguments[0], arguments[1])))())()) ||
(typeof selector == str &&
arguments.length == 2 &&
arguments != undefined &&
(() => (doc[0].setAttribute(arguments[0], arguments[1])))()) ||
(arguments != undefined && typeof selector != str && arguments.length == 1)
? doc.getAttribute(arguments[0])
: doc[0].getAttribute(arguments[0]) || console.warn("Warning: Missing html attribute as an argument at .attr( attr: String, val: String | undefined ) method");
},
// Use to remove html attribute
removeAttr: function (attr) {
switch (this.switchSelector()) {
case 'object':
return attr && arguments.length == 1 && typeof attr == str && (() => attr.split(' ').forEach(e => doc.removeAttribute(e)))();
break;
case 'nodeList':
return attr && arguments.length == 1 && typeof attr == str && (() => doc.forEach(e => attr.split(' ').forEach(e => doc.forEach(el => el.removeAttribute(e)))))();
break;
}
},
// use to set or return innerHTML content of html element in the first occurence
html: function () {
return (arguments[0] != undefined &&
arguments.length == 1 &&
(() =>
typeof selector != str &&
(() => (doc.innerHTML = arguments[0]))())()) ||
(typeof selector == str &&
arguments.length == 1 &&
arguments != undefined &&
(() => (doc[0].innerHTML = arguments[0]))()) ||
(arguments[0] == undefined && typeof selector != str)
? doc.innerHTML
: doc[0].innerHTML;
},
// use to set or return value of html input element in the first occurence
val: function () {
return (arguments[0] != undefined &&
arguments.length == 1 &&
(() =>
typeof selector != str &&
(() => (doc.value = arguments[0]))())()) ||
(typeof selector == str &&
arguments.length == 1 &&
arguments != undefined &&
(() => (doc[0].value = arguments[0]))()) ||
(arguments[0] == undefined && typeof selector != str)
? doc.value
: doc[0].value;
},
};
Object.defineProperty(methods, "switchSelector", {
enumerable: !1,
writable: !1,
configurable: !1,
value: function () {
return typeof selector != str ? "object" : "nodeList"
}
});
Object.defineProperty(methods, "toURLSearchParams", {
enumerable: !1,
configurable: !1,
writable: !1,
value: function (type) {
const arr = [],
obj = {};
let url = new URLSearchParams();
if (typeof selector != str)
for (const [k, v] of new FormData(doc).entries()) url.append(k, v);
else if (doc == "[object FormData]")
for (const [k, v] of doc.entries()) url.append(k, v);
else
for (const [k, v] of new FormData(doc[0]).entries())
url.append(k, v);
switch (type) {
case "array":
for (const [n, v] of url.entries())
arr.push({ name: n, value: v });
break;
case "object":
for (const [k, v] of url.entries())
Object.assign(obj, { [k]: v });
break;
}
return type == "array"
? arr
: type == "object"
? obj
: url.toString();
}
});
return methods;
} catch (err) {
console.warn(err);
}
};
const jQuery = {
// use to create a serialized representation of a plain object for use in a URL query string or Ajax request.
param: function (obj) {
let url = new URLSearchParams();
if (
typeof obj === "object" &&
obj !== null &&
Object.getPrototypeOf(obj) === Object.prototype
) {
for (const [k, v] of Object.entries(obj)) {
url.append(k, v);
}
return url.toString();
}
console.warn(
"Warning: Missing or invalid object argument at .param( obj: Object ) method."
);
},
};