-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkquery.js
More file actions
254 lines (223 loc) · 6.36 KB
/
mkquery.js
File metadata and controls
254 lines (223 loc) · 6.36 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
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
function $(args, options={}) {
if (typeof args === 'string')
options.__str__ = true
else {
options.__str__ = false
if (args.__isProxy) {
options.__isp__ = true
}
else {
options.__isp__ = false
}
}
return new MQ(args, options)
}
class MQ {
#nl
constructor (n, o) {
if (!o.__str__) {
if (o.__isp__) {
return new NodeWrapper(n.get())
}
return new NodeWrapper(n)
}
delete o.__str__
if (n.startsWith('<') && n.endsWith('>')) {
return MQ.__create__(n.slice(1,-1), o)
}
try {
this.#nl = [...document.querySelectorAll(n)]
}
catch (err) {
throw new MQInvalidSelectorError("Invalid Selector")
}
this.#wrap()
if (this.#nl.length === 0) {
this.length = 0
}
else {
this.length = this.#nl.length
}
return this.#nl.length == 1 ? this.first() : this
}
first() {return this.#nl[0]}
last() {return this.#nl[this.#nl.length - 1]}
nth(n) {
n = parseInt(n)
if (isNaN(n)) throw new MQIllegalArguementError("Illegal Argument")
n += n < 0 ? this.#nl.length : 0
if (!(0 < n < this.#nl.length)) throw new MQIndexOutOfBoundsError("Index Out Of Bounds")
return this.#nl[n]
}
each(c) {
if (typeof c !== 'function') throw new MQIllegalArguementError("Illegal Argument")
for(let _ of this.#nl) {
c(_)
}
return this
}
static __create__(n, o) {
let _
try {
_ = new NodeWrapper(document.createElement(n))
}
catch (err) {
console.log(err)
throw new MQElementCreationError(`Cannot create element ${n}`)
}
for(let __ of Object.keys(o)) {
_.get()[__] = o[__]
}
return _
}
#wrap() {
this.#nl = this.#nl.map(_ => new NodeWrapper(_))
}
* list() {
yield* this.#nl
}
length() {return this.#nl.length}
css(p, v) {
if (!p || !v) throw new MQIllegalArguementError("Poperty and Value must be specified")
for(let _ of this.#nl) {
_.css(p, v)
}
}
click() {return this.each(e => e.click())}
html(t) {
if (!t) throw new MQIllegalArguementError("No arguments specified for NodeWrapper.html(t)")
return this.each(e => e.html(t))
}
text(t) {
if (!t) throw new MQIllegalArguementError("No arguments specified for NodeWrapper.text(t)")
return this.each(e => e.text(t))
}
on(e,c,u) {return this.each(q => q.on(e, c, u))}
appendTo(n) {return this.each(e => e.appendTo(n))}
addClass(...c) {return this.each(e => e.addClass(...c))}
removeClass(...c) {return this.each(e => e.removeClass(...c))}
}
class NodeWrapper {
constructor(n) {
this.n = n
this.length = 1
this.__isProxy = false
return NodeWrapper.#proxify(this)
}
click() {
this.n.click()
return this
}
html(t) {
if (!t) return this.n.innerHTML
this.n.innerHTML = `${t}`
return this
}
text(t) {
if (!t) return this.n.innerText
this.n.innerText = `${t}`
return this
}
css(p, v) {
if (!p) return this.n.style
if (!v) return getComputedStyle(this.n).getPropertyValue(`${p}`)
this.n.style.setProperty(`${p}`, `${v}`)
return this
}
on(e, c, u) {
if (!e || !c) throw new MQEventBindError(`Cannot bind callback ${c} to event ${e}`)
if (typeof c !== 'function') throw new MQEventBindError(`Cannot bind callback ${c} to event ${e}, because ${c} is not a function`)
this.n.addEventListener(e, c, u)
return this
}
append(...n) {
if (!n) return this
for(let _ of n) {
if (_ instanceof NodeWrapper) {
this.n.appendChild(_.get())
} else {
this.n.appendChild(_)
}
}
return this
}
appendTo(n) {
let k
if (typeof n === 'string') k = (new MQ(n, {__str__:true})).get()
else if (n instanceof NodeWrapper) k = n.get()
else k = n
k.appendChild(this.n)
return this
}
get() {return this.n}
addClass(...c) {
if (!c) throw new MQIllegalArguementError(`Cannot add class ${c}`)
for(let _ of c) {
this.n.classList.add(_)
}
return this
}
removeClass(...c) {
if (!c) throw new MQIllegalArguementError(`Cannot add class ${c}`)
for(let _ of c) {
this.n.classList.remove(_)
}
return this
}
find(n) {
if (typeof n === 'string') {
const _ = this.n.querySelector(`${n}`)
if (_) return new NodeWrapper(_)
return null
} else {
throw new MQIllegalArguementError("Can only find by by CSS Selector")
}
}
contains(n) {
if (n instanceof NodeWrapper) {
return this.n.contains(n.get())
}
else if (n instanceof HTMLElement) {
return this.n.contains(n)
}
else {
throw new MQIllegalArguementError("contains can only find HTMLElement or NodeWrapper objects")
}
}
static #proxify(t) {
if (t.__isProxy) return t
t.__isProxy = true
return new Proxy(t, {
get: (_t, p, r) => {
return (p in _t) ? _t[p] : t.n[p]
},
set: (_t, p, v, r) => {
if (p in _t) {
_t[p] = v
}
else {
t[p] = v
}
},
})
}
list() {
return [this]
}
}
class MQError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}
}
}
class MQInvalidSelectorError extends MQError {}
class MQEventBindError extends MQError {}
class MQIllegalArguementError extends MQError {}
class MQIndexOutOfBoundsError extends MQError {}
class MQElementCreationError extends MQError {}