jsquery_node 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jsquery.ts +156 -170
- package/package.json +1 -1
package/jsquery.ts
CHANGED
|
@@ -1,72 +1,51 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
checked(): boolean[];
|
|
17
|
-
checked(val: boolean): this;
|
|
18
|
-
checked(val?: boolean): this | boolean[] {
|
|
19
|
-
if (val !== undefined) {
|
|
20
|
-
this.forEach((v) => v.checked(val));
|
|
21
|
-
return this;
|
|
1
|
+
export namespace JSQuery {
|
|
2
|
+
function toArray(elt: NodeList | HTMLCollection | HTMLElement) {
|
|
3
|
+
return elt instanceof NodeList || elt instanceof HTMLCollection;
|
|
4
|
+
}
|
|
5
|
+
function toElt(elt: Element): HTMLElement {
|
|
6
|
+
if (elt instanceof Element) return elt.elt;
|
|
7
|
+
return elt;
|
|
8
|
+
}
|
|
9
|
+
export class Extension {
|
|
10
|
+
constructor() {
|
|
11
|
+
if (this.constructor === Extension) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"you can't make an instance of class: JSQuery.Extension"
|
|
14
|
+
);
|
|
22
15
|
}
|
|
23
|
-
return this.map((v) => v.checked()) as any;
|
|
24
16
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.forEach((v) => v.props(props));
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
class(names: string | string[]) {
|
|
38
|
-
this.forEach((v) => v.class(names));
|
|
39
|
-
return this;
|
|
17
|
+
get() {
|
|
18
|
+
return {
|
|
19
|
+
$: this.$(),
|
|
20
|
+
Element: this.Element(),
|
|
21
|
+
static_Element: this.static_Element(),
|
|
22
|
+
static_ElementArray: this.static_ElementArray(),
|
|
23
|
+
ElementArray: this.ElementArray(),
|
|
24
|
+
JSQuery: this.JSQuery(),
|
|
25
|
+
};
|
|
40
26
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return this;
|
|
27
|
+
$() {
|
|
28
|
+
return {};
|
|
44
29
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return this;
|
|
30
|
+
Element() {
|
|
31
|
+
return {};
|
|
48
32
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return this;
|
|
33
|
+
ElementArray() {
|
|
34
|
+
return {};
|
|
52
35
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return temp;
|
|
36
|
+
|
|
37
|
+
static_Element() {
|
|
38
|
+
return {};
|
|
57
39
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.forEach((v) => v.click(func, s));
|
|
61
|
-
return this;
|
|
40
|
+
static_ElementArray() {
|
|
41
|
+
return {};
|
|
62
42
|
}
|
|
63
|
-
}
|
|
64
43
|
|
|
65
|
-
|
|
66
|
-
|
|
44
|
+
JSQuery() {
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
67
47
|
}
|
|
68
|
-
|
|
69
|
-
class Element {
|
|
48
|
+
export class Element {
|
|
70
49
|
elt: HTMLElement;
|
|
71
50
|
#TriggerEvent(e: any, func: (this:HTMLElement, ev: any) => any, s?: boolean | AddEventListenerOptions) {
|
|
72
51
|
if (!func) {
|
|
@@ -221,136 +200,143 @@ export const { $, JSQuery } = (() => {
|
|
|
221
200
|
return this;
|
|
222
201
|
}
|
|
223
202
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
203
|
+
export class Caching extends Extension {
|
|
204
|
+
$() {
|
|
205
|
+
return {
|
|
206
|
+
cache<T extends (...args:any[])=>any>(func: T): T {
|
|
207
|
+
const f = (...args: Parameters<T>): ReturnType<T> => {
|
|
208
|
+
const val = JSON.stringify(args);
|
|
209
|
+
if (f.cache.hasOwnProperty(val)) {
|
|
210
|
+
return f.cache[val];
|
|
211
|
+
}
|
|
212
|
+
const temp = func(...args);
|
|
213
|
+
f.cache[val] = temp;
|
|
214
|
+
return temp;
|
|
215
|
+
};
|
|
216
|
+
f.cache = {};
|
|
217
|
+
return f as any;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
228
221
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
222
|
+
export class ElementArray extends Array<Element> {
|
|
223
|
+
on<K extends keyof DocumentEventMap>(e: K, func: (this: HTMLElement, ev: DocumentEventMap[K])=>any, s?: boolean | AddEventListenerOptions) {
|
|
224
|
+
this.forEach((v) => v.on(e, func, s));
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
rect() {
|
|
228
|
+
return this.map((v) => v.rect());
|
|
229
|
+
}
|
|
230
|
+
hasClass(c:string) {
|
|
231
|
+
return this.map((v) => v.hasClass(c));
|
|
232
|
+
}
|
|
233
|
+
is(q:string) {
|
|
234
|
+
return this.map((v) => v.is(q));
|
|
235
|
+
}
|
|
236
|
+
checked(): boolean[];
|
|
237
|
+
checked(val: boolean): this;
|
|
238
|
+
checked(val?: boolean): this | boolean[] {
|
|
239
|
+
if (val !== undefined) {
|
|
240
|
+
this.forEach((v) => v.checked(val));
|
|
241
|
+
return this;
|
|
236
242
|
}
|
|
243
|
+
return this.map((v) => v.checked()) as any;
|
|
237
244
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
Element: this.Element(),
|
|
242
|
-
static_Element: this.static_Element(),
|
|
243
|
-
static_ElementArray: this.static_ElementArray(),
|
|
244
|
-
ElementArray: this.ElementArray(),
|
|
245
|
-
JSQuery: this.JSQuery(),
|
|
246
|
-
};
|
|
245
|
+
trigger(e: any) {
|
|
246
|
+
this.forEach((v) => v.trigger(e));
|
|
247
|
+
return this;
|
|
247
248
|
}
|
|
248
|
-
|
|
249
|
-
|
|
249
|
+
css(styles: Record<string, any>) {
|
|
250
|
+
this.forEach((v) => v.css(styles));
|
|
251
|
+
return this;
|
|
250
252
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
+
props(props: Record<string, any>) {
|
|
254
|
+
this.forEach((v) => v.props(props));
|
|
255
|
+
return this;
|
|
253
256
|
}
|
|
254
|
-
|
|
255
|
-
|
|
257
|
+
class(names: string | string[]) {
|
|
258
|
+
this.forEach((v) => v.class(names));
|
|
259
|
+
return this;
|
|
256
260
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return
|
|
261
|
+
removeClass(names: string | string[]) {
|
|
262
|
+
this.forEach((v) => v.removeClass(names));
|
|
263
|
+
return this;
|
|
260
264
|
}
|
|
261
|
-
|
|
262
|
-
|
|
265
|
+
toggleClass(names: string | string[]) {
|
|
266
|
+
this.forEach((v) => v.toggleClass(names));
|
|
267
|
+
return this;
|
|
263
268
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
return
|
|
269
|
+
remove() {
|
|
270
|
+
this.forEach((v) => v.remove());
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
new() {
|
|
274
|
+
const temp = new ElementArray();
|
|
275
|
+
this.forEach((v) => temp.push(v.new() as any));
|
|
276
|
+
return temp;
|
|
277
|
+
}
|
|
278
|
+
//events
|
|
279
|
+
click(func: (this:HTMLElement, ev: any) => any, s?: boolean | AddEventListenerOptions) {
|
|
280
|
+
this.forEach((v) => v.click(func, s));
|
|
281
|
+
return this;
|
|
267
282
|
}
|
|
268
283
|
}
|
|
284
|
+
export const Plugin = Extension;
|
|
285
|
+
export type Plugin = Extension;
|
|
286
|
+
}
|
|
269
287
|
|
|
270
|
-
function J(q: any): Element | null {
|
|
271
|
-
return Element.from(document.querySelector(q)) as any;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
J.from = ((elt:any) => {
|
|
275
|
-
return Element.from(elt);
|
|
276
|
-
}) as typeof Element.from;
|
|
277
|
-
|
|
278
|
-
J.all = (q: any): ElementArray => {
|
|
279
|
-
return Element.from(document.querySelectorAll(q)) as any;
|
|
280
|
-
};
|
|
281
288
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return head;
|
|
286
|
-
};
|
|
289
|
+
function J(q: any): JSQuery.Element | null {
|
|
290
|
+
return JSQuery.Element.from(document.querySelector(q)) as any;
|
|
291
|
+
};
|
|
287
292
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return body;
|
|
292
|
-
};
|
|
293
|
+
J.from = ((elt:any) => {
|
|
294
|
+
return JSQuery.Element.from(elt);
|
|
295
|
+
}) as typeof JSQuery.Element.from;
|
|
293
296
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return doc;
|
|
298
|
-
};
|
|
297
|
+
J.all = (q: any): JSQuery.ElementArray => {
|
|
298
|
+
return JSQuery.Element.from(document.querySelectorAll(q)) as any;
|
|
299
|
+
};
|
|
299
300
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
301
|
+
let head: JSQuery.Element;
|
|
302
|
+
J.head = () => {
|
|
303
|
+
if (!head) head = JSQuery.Element.from(document.head) as JSQuery.Element;
|
|
304
|
+
return head;
|
|
305
|
+
};
|
|
303
306
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
Caching: class Caching extends Extension {
|
|
310
|
-
$() {
|
|
311
|
-
return {
|
|
312
|
-
cache<T extends (...args:any[])=>any>(func: T): T {
|
|
313
|
-
const f = (...args: Parameters<T>): ReturnType<T> => {
|
|
314
|
-
const val = JSON.stringify(args);
|
|
315
|
-
if (f.cache.hasOwnProperty(val)) {
|
|
316
|
-
return f.cache[val];
|
|
317
|
-
}
|
|
318
|
-
const temp = func(...args);
|
|
319
|
-
f.cache[val] = temp;
|
|
320
|
-
return temp;
|
|
321
|
-
};
|
|
322
|
-
f.cache = {};
|
|
323
|
-
return f as any;
|
|
324
|
-
},
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
},
|
|
328
|
-
};
|
|
307
|
+
let body: JSQuery.Element;
|
|
308
|
+
J.body = () => {
|
|
309
|
+
if (!body) body = JSQuery.Element.from(document.body) as JSQuery.Element;
|
|
310
|
+
return body;
|
|
311
|
+
};
|
|
329
312
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
body = undefined;
|
|
337
|
-
head = undefined;
|
|
338
|
-
const items = new extend().get();
|
|
339
|
-
Object.assign(J, items.$);
|
|
340
|
-
Object.assign(JSQuery, items.JSQuery);
|
|
341
|
-
Object.assign(Element.prototype, items.Element);
|
|
342
|
-
Object.assign(ElementArray.prototype, items.ElementArray);
|
|
343
|
-
Object.assign(Element, items.static_Element);
|
|
344
|
-
Object.assign(ElementArray, items.static_ElementArray);
|
|
345
|
-
}
|
|
313
|
+
let doc: JSQuery.Element;
|
|
314
|
+
J.doc = () => {
|
|
315
|
+
if (!doc) doc = JSQuery.Element.from(document as any) as JSQuery.Element;
|
|
316
|
+
return doc;
|
|
317
|
+
};
|
|
346
318
|
|
|
347
|
-
|
|
319
|
+
J.create = (t: any): JSQuery.Element => {
|
|
320
|
+
return JSQuery.Element.from(document.createElement(t)) as any;
|
|
321
|
+
};
|
|
348
322
|
|
|
349
|
-
|
|
350
|
-
|
|
323
|
+
J.loadExtension = (extend: new()=>JSQuery.Extension) => {
|
|
324
|
+
if (Object.getPrototypeOf(extend) !== JSQuery.Extension) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
"the class is not a child of JSQuery.Extension or the inputed class is an instance"
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
body = undefined;
|
|
330
|
+
head = undefined;
|
|
331
|
+
const items = new extend().get();
|
|
332
|
+
Object.assign(J, items.$);
|
|
333
|
+
Object.assign(JSQuery, items.JSQuery);
|
|
334
|
+
Object.assign(JSQuery.Element.prototype, items.Element);
|
|
335
|
+
Object.assign(JSQuery.ElementArray.prototype, items.ElementArray);
|
|
336
|
+
Object.assign(JSQuery.Element, items.static_Element);
|
|
337
|
+
Object.assign(JSQuery.ElementArray, items.static_ElementArray);
|
|
338
|
+
}
|
|
351
339
|
|
|
352
|
-
|
|
353
|
-
export
|
|
354
|
-
export type Extension = typeof JSQuery.Extension;
|
|
355
|
-
export type Plugin = typeof JSQuery.Extension;
|
|
340
|
+
J.loadPlugin = J.loadExtension;
|
|
341
|
+
export const $ = J;
|
|
356
342
|
|