jsquery_node 1.0.0

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.js ADDED
@@ -0,0 +1,335 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ export const { $, JSQuery } = (() => {
7
+ var _Element_instances, _Element_TriggerEvent;
8
+ class ElementArray extends Array {
9
+ on(e, func, s) {
10
+ this.forEach((v) => v.on(e, func, s));
11
+ return this;
12
+ }
13
+ rect() {
14
+ return this.map((v) => v.rect());
15
+ }
16
+ hasClass(c) {
17
+ return this.map((v) => v.hasClass(c));
18
+ }
19
+ is(q) {
20
+ return this.map((v) => v.is(q));
21
+ }
22
+ checked(val) {
23
+ if (val) {
24
+ this.forEach((v) => v.checked(val));
25
+ return this;
26
+ }
27
+ return this.map((v) => v.checked());
28
+ }
29
+ trigger(e) {
30
+ this.forEach((v) => v.trigger(e));
31
+ return this;
32
+ }
33
+ css(styles) {
34
+ this.forEach((v) => v.css(styles));
35
+ return this;
36
+ }
37
+ props(props) {
38
+ this.forEach((v) => v.props(props));
39
+ return this;
40
+ }
41
+ class(names) {
42
+ this.forEach((v) => v.class(names));
43
+ return this;
44
+ }
45
+ removeClass(names) {
46
+ this.forEach((v) => v.removeClass(names));
47
+ return this;
48
+ }
49
+ toggleClass(names) {
50
+ this.forEach((v) => v.toggleClass(names));
51
+ return this;
52
+ }
53
+ remove() {
54
+ this.forEach((v) => v.remove());
55
+ return this;
56
+ }
57
+ new() {
58
+ const temp = new ElementArray();
59
+ this.forEach((v) => temp.push(v.new()));
60
+ return temp;
61
+ }
62
+ //events
63
+ click(func, s) {
64
+ this.forEach((v) => v.click(func, s));
65
+ return this;
66
+ }
67
+ }
68
+ function toArray(elt) {
69
+ return elt instanceof NodeList || elt instanceof HTMLCollection;
70
+ }
71
+ class Element {
72
+ static from(elt) {
73
+ if (elt == null) {
74
+ return null;
75
+ }
76
+ if (toArray(elt)) {
77
+ return ElementArray.from(elt).map((v) => new this(v));
78
+ }
79
+ return new this(elt);
80
+ }
81
+ new() {
82
+ return Element.from(this.elt);
83
+ }
84
+ constructor(elt) {
85
+ _Element_instances.add(this);
86
+ this.elt = elt;
87
+ }
88
+ on(e, func, s) {
89
+ this.elt.addEventListener(e, func, s);
90
+ return this;
91
+ }
92
+ removeEvent(e, func, s) {
93
+ this.elt.removeEventListener(e, func, s);
94
+ return this;
95
+ }
96
+ trigger(e) {
97
+ this.elt[e]();
98
+ return this;
99
+ }
100
+ css(styles) {
101
+ for (const [name, val] of Object.entries(styles)) {
102
+ this.elt.style[name] = val;
103
+ }
104
+ return this;
105
+ }
106
+ getCss(style) {
107
+ return this.elt.style[style];
108
+ }
109
+ props(props) {
110
+ for (const [name, val] of Object.entries(props)) {
111
+ if (val === null) {
112
+ this.elt.removeAttribute(name);
113
+ }
114
+ else {
115
+ this.elt.setAttribute(name, val);
116
+ }
117
+ }
118
+ return this;
119
+ }
120
+ getProp(name) {
121
+ return this.elt.getAttribute(name);
122
+ }
123
+ id(val) {
124
+ if (val == undefined)
125
+ return this.getProp("id");
126
+ this.props({ id: val });
127
+ return this;
128
+ }
129
+ class(names) {
130
+ if (Array.isArray(names)) {
131
+ names.forEach((name) => this.elt.classList.add(name));
132
+ }
133
+ else {
134
+ this.elt.classList.add(names);
135
+ }
136
+ return this;
137
+ }
138
+ removeClass(names) {
139
+ if (Array.isArray(names)) {
140
+ names.forEach((name) => this.elt.classList.remove(name));
141
+ }
142
+ else {
143
+ this.elt.classList.remove(names);
144
+ }
145
+ return this;
146
+ }
147
+ toggleClass(names) {
148
+ if (Array.isArray(names)) {
149
+ names.forEach((name) => this.elt.classList.toggle(name));
150
+ }
151
+ else {
152
+ this.elt.classList.toggle(names);
153
+ }
154
+ return this;
155
+ }
156
+ hasClass(name) {
157
+ return this.elt.classList.contains(name);
158
+ }
159
+ $(q) {
160
+ return J.from(this.elt.querySelector(q));
161
+ }
162
+ all(q) {
163
+ return J.from(this.elt.querySelectorAll(q));
164
+ }
165
+ is(q) {
166
+ return this.elt.matches(q);
167
+ }
168
+ child(children) {
169
+ if (Array.isArray(children)) {
170
+ children.forEach((child) => this.elt.appendChild(toElt(child)));
171
+ }
172
+ else {
173
+ this.elt.appendChild(toElt(children));
174
+ }
175
+ return this;
176
+ }
177
+ remove() {
178
+ this.elt.remove();
179
+ return this;
180
+ }
181
+ get children() {
182
+ return Element.from(this.elt.children);
183
+ }
184
+ html(val) {
185
+ if (val == undefined)
186
+ return this.elt.innerHTML;
187
+ this.elt.innerHTML = val;
188
+ return this;
189
+ }
190
+ text(val) {
191
+ if (val == undefined)
192
+ return this.elt.textContent;
193
+ this.elt.textContent = val;
194
+ return this;
195
+ }
196
+ rect() {
197
+ return this.elt.getBoundingClientRect().toJSON();
198
+ }
199
+ value(val) {
200
+ if (val == undefined)
201
+ return this.getProp("value");
202
+ this.props({ value: val });
203
+ return this;
204
+ }
205
+ checked(val) {
206
+ if (val == undefined)
207
+ return this.getProp("checked") !== null;
208
+ this.props({ checked: val ? "" : null });
209
+ return this;
210
+ }
211
+ //events
212
+ click(func, s) {
213
+ __classPrivateFieldGet(this, _Element_instances, "m", _Element_TriggerEvent).call(this, "click", func, s);
214
+ return this;
215
+ }
216
+ }
217
+ _Element_instances = new WeakSet(), _Element_TriggerEvent = function _Element_TriggerEvent(e, func, s) {
218
+ if (!func) {
219
+ this.trigger(e);
220
+ return;
221
+ }
222
+ this.on(e, func, s);
223
+ };
224
+ function toElt(elt) {
225
+ if (elt instanceof Element)
226
+ return elt.elt;
227
+ return elt;
228
+ }
229
+ class Extension {
230
+ constructor() {
231
+ if (this.constructor === Extension) {
232
+ throw new Error("you can't make an instance of class: JSQuery.Extension");
233
+ }
234
+ }
235
+ get() {
236
+ return {
237
+ $: this.$(),
238
+ Element: this.Element(),
239
+ static_Element: this.static_Element(),
240
+ static_ElementArray: this.static_ElementArray(),
241
+ ElementArray: this.ElementArray(),
242
+ JSQuery: this.JSQuery(),
243
+ };
244
+ }
245
+ $() {
246
+ return {};
247
+ }
248
+ Element() {
249
+ return {};
250
+ }
251
+ ElementArray() {
252
+ return {};
253
+ }
254
+ static_Element() {
255
+ return {};
256
+ }
257
+ static_ElementArray() {
258
+ return {};
259
+ }
260
+ JSQuery() {
261
+ return {};
262
+ }
263
+ }
264
+ function J(q) {
265
+ return Element.from(document.querySelector(q));
266
+ }
267
+ J.from = (elt) => {
268
+ return Element.from(elt);
269
+ };
270
+ J.all = (q) => {
271
+ return Element.from(document.querySelectorAll(q));
272
+ };
273
+ let head;
274
+ J.head = () => {
275
+ if (!head)
276
+ head = Element.from(document.head);
277
+ return head;
278
+ };
279
+ let body;
280
+ J.body = () => {
281
+ if (!body)
282
+ body = Element.from(document.body);
283
+ return body;
284
+ };
285
+ let doc;
286
+ J.doc = () => {
287
+ if (!doc)
288
+ doc = Element.from(document);
289
+ return doc;
290
+ };
291
+ J.create = (t) => {
292
+ return Element.from(document.createElement(t));
293
+ };
294
+ const JSQuery = {
295
+ Element,
296
+ ElementArray,
297
+ Extension,
298
+ Plugin: Extension,
299
+ Caching: class Caching extends Extension {
300
+ $() {
301
+ return {
302
+ cache(func) {
303
+ const f = (...args) => {
304
+ const val = JSON.stringify(args);
305
+ if (f.cache.hasOwnProperty(val)) {
306
+ return f.cache[val];
307
+ }
308
+ const temp = func(...args);
309
+ f.cache[val] = temp;
310
+ return temp;
311
+ };
312
+ f.cache = {};
313
+ return f;
314
+ },
315
+ };
316
+ }
317
+ },
318
+ };
319
+ J.loadExtension = (extend) => {
320
+ if (Object.getPrototypeOf(extend) !== Extension) {
321
+ throw new Error("the class is not a child of JSQuery.Extension or the inputed class is an instance");
322
+ }
323
+ body = undefined;
324
+ head = undefined;
325
+ const items = new extend().get();
326
+ Object.assign(J, items.$);
327
+ Object.assign(JSQuery, items.JSQuery);
328
+ Object.assign(Element.prototype, items.Element);
329
+ Object.assign(ElementArray.prototype, items.ElementArray);
330
+ Object.assign(Element, items.static_Element);
331
+ Object.assign(ElementArray, items.static_ElementArray);
332
+ };
333
+ J.loadPlugin = J.loadExtension;
334
+ return { $: J, JSQuery };
335
+ })();
package/jsquery.ts ADDED
@@ -0,0 +1,336 @@
1
+ export const { $, JSQuery } = (() => {
2
+ class ElementArray extends Array<Element> {
3
+ on<K extends keyof DocumentEventMap>(e: K, func: (this: HTMLElement, ev: DocumentEventMap[K])=>any, s?: boolean | AddEventListenerOptions) {
4
+ this.forEach((v) => v.on(e, func, s));
5
+ return this;
6
+ }
7
+ rect() {
8
+ return this.map((v) => v.rect());
9
+ }
10
+ hasClass(c:string) {
11
+ return this.map((v) => v.hasClass(c));
12
+ }
13
+ is(q:string) {
14
+ return this.map((v) => v.is(q));
15
+ }
16
+ checked(val?: boolean): this | boolean[] {
17
+ if (val) {
18
+ this.forEach((v) => v.checked(val));
19
+ return this;
20
+ }
21
+ return this.map((v) => v.checked()) as any;
22
+ }
23
+ trigger(e: any) {
24
+ this.forEach((v) => v.trigger(e));
25
+ return this;
26
+ }
27
+ css(styles: Record<string, any>) {
28
+ this.forEach((v) => v.css(styles));
29
+ return this;
30
+ }
31
+ props(props: Record<string, any>) {
32
+ this.forEach((v) => v.props(props));
33
+ return this;
34
+ }
35
+ class(names: string | string[]) {
36
+ this.forEach((v) => v.class(names));
37
+ return this;
38
+ }
39
+ removeClass(names: string | string[]) {
40
+ this.forEach((v) => v.removeClass(names));
41
+ return this;
42
+ }
43
+ toggleClass(names: string | string[]) {
44
+ this.forEach((v) => v.toggleClass(names));
45
+ return this;
46
+ }
47
+ remove() {
48
+ this.forEach((v) => v.remove());
49
+ return this;
50
+ }
51
+ new() {
52
+ const temp = new ElementArray();
53
+ this.forEach((v) => temp.push(v.new()));
54
+ return temp;
55
+ }
56
+ //events
57
+ click(func: (this:HTMLElement, ev: any) => any, s: boolean | AddEventListenerOptions) {
58
+ this.forEach((v) => v.click(func, s));
59
+ return this;
60
+ }
61
+ }
62
+
63
+ function toArray(elt: NodeList | HTMLCollection | HTMLElement) {
64
+ return elt instanceof NodeList || elt instanceof HTMLCollection;
65
+ }
66
+
67
+ class Element {
68
+ elt: HTMLElement;
69
+ #TriggerEvent(e: any, func: (this:HTMLElement, ev: any) => any, s: boolean | AddEventListenerOptions) {
70
+ if (!func) {
71
+ this.trigger(e);
72
+ return;
73
+ }
74
+ this.on(e, func, s);
75
+ }
76
+ static from(elt: HTMLElement | NodeList | HTMLCollection) {
77
+ if (elt == null) {
78
+ return null;
79
+ }
80
+ if (toArray(elt)) {
81
+ return ElementArray.from(elt).map((v) => new this(v as HTMLElement));
82
+ }
83
+ return new this(elt);
84
+ }
85
+ new() {
86
+ return Element.from(this.elt);
87
+ }
88
+ constructor(elt:HTMLElement) {
89
+ this.elt = elt;
90
+ }
91
+ on(e: any, func: (this:HTMLElement, ev: any) => any, s: boolean | AddEventListenerOptions) {
92
+ this.elt.addEventListener(e, func, s);
93
+ return this;
94
+ }
95
+ removeEvent(e: any, func: (this:HTMLElement, ev: any) => any, s: boolean | AddEventListenerOptions) {
96
+ this.elt.removeEventListener(e, func, s);
97
+ return this;
98
+ }
99
+ trigger(e: any) {
100
+ this.elt[e]();
101
+ return this;
102
+ }
103
+ css(styles: Record<string, any>) {
104
+ for (const [name, val] of Object.entries(styles)) {
105
+ this.elt.style[name] = val;
106
+ }
107
+ return this;
108
+ }
109
+ getCss(style: string) {
110
+ return this.elt.style[style];
111
+ }
112
+ props(props: Record<string, any>) {
113
+ for (const [name, val] of Object.entries(props)) {
114
+ if(val === null) {
115
+ this.elt.removeAttribute(name);
116
+ } else {
117
+ this.elt.setAttribute(name, val);
118
+ }
119
+ }
120
+ return this;
121
+ }
122
+ getProp(name: string) {
123
+ return this.elt.getAttribute(name);
124
+ }
125
+ id(val: string | undefined): string | this {
126
+ if (val == undefined) return this.getProp("id");
127
+ this.props({ id: val });
128
+ return this;
129
+ }
130
+ class(names: string | string[]) {
131
+ if (Array.isArray(names)) {
132
+ names.forEach((name) => this.elt.classList.add(name));
133
+ } else {
134
+ this.elt.classList.add(names);
135
+ }
136
+ return this;
137
+ }
138
+ removeClass(names: string | string[]) {
139
+ if (Array.isArray(names)) {
140
+ names.forEach((name) => this.elt.classList.remove(name));
141
+ } else {
142
+ this.elt.classList.remove(names);
143
+ }
144
+ return this;
145
+ }
146
+ toggleClass(names: string | string[]) {
147
+ if (Array.isArray(names)) {
148
+ names.forEach((name) => this.elt.classList.toggle(name));
149
+ } else {
150
+ this.elt.classList.toggle(names);
151
+ }
152
+ return this;
153
+ }
154
+ hasClass(name: string) {
155
+ return this.elt.classList.contains(name);
156
+ }
157
+ $(q: any) {
158
+ return J.from(this.elt.querySelector(q));
159
+ }
160
+ all(q: any) {
161
+ return J.from(this.elt.querySelectorAll(q));
162
+ }
163
+ is(q:string) {
164
+ return this.elt.matches(q);
165
+ }
166
+ child(children: Element|Element[]) {
167
+ if (Array.isArray(children)) {
168
+ children.forEach((child:Element) => this.elt.appendChild(toElt(child)));
169
+ } else {
170
+ this.elt.appendChild(toElt(children));
171
+ }
172
+ return this;
173
+ }
174
+ remove() {
175
+ this.elt.remove();
176
+ return this;
177
+ }
178
+ get children() {
179
+ return Element.from(this.elt.children);
180
+ }
181
+ html(val?:string): string|this {
182
+ if (val == undefined) return this.elt.innerHTML;
183
+ this.elt.innerHTML = val;
184
+ return this;
185
+ }
186
+ text(val?: string): string | this {
187
+ if (val == undefined) return this.elt.textContent;
188
+ this.elt.textContent = val;
189
+ return this;
190
+ }
191
+ rect() {
192
+ return this.elt.getBoundingClientRect().toJSON();
193
+ }
194
+ value(val?: string | undefined): string | this {
195
+ if (val == undefined) return this.getProp("value");
196
+ this.props({value: val});
197
+ return this;
198
+ }
199
+ checked(val?: boolean): boolean| this {
200
+ if (val == undefined) return this.getProp("checked") !== null;
201
+ this.props({checked: val? "": null});
202
+ return this;
203
+ }
204
+ //events
205
+ click(func: (this:HTMLElement, ev: any) => any, s: boolean | AddEventListenerOptions) {
206
+ this.#TriggerEvent("click", func, s);
207
+ return this;
208
+ }
209
+ }
210
+
211
+ function toElt(elt: Element): HTMLElement {
212
+ if (elt instanceof Element) return elt.elt;
213
+ return elt;
214
+ }
215
+
216
+ class Extension {
217
+ constructor() {
218
+ if (this.constructor === Extension) {
219
+ throw new Error(
220
+ "you can't make an instance of class: JSQuery.Extension"
221
+ );
222
+ }
223
+ }
224
+ get() {
225
+ return {
226
+ $: this.$(),
227
+ Element: this.Element(),
228
+ static_Element: this.static_Element(),
229
+ static_ElementArray: this.static_ElementArray(),
230
+ ElementArray: this.ElementArray(),
231
+ JSQuery: this.JSQuery(),
232
+ };
233
+ }
234
+ $() {
235
+ return {};
236
+ }
237
+ Element() {
238
+ return {};
239
+ }
240
+ ElementArray() {
241
+ return {};
242
+ }
243
+
244
+ static_Element() {
245
+ return {};
246
+ }
247
+ static_ElementArray() {
248
+ return {};
249
+ }
250
+
251
+ JSQuery() {
252
+ return {};
253
+ }
254
+ }
255
+
256
+ function J(q: any) {
257
+ return Element.from(document.querySelector(q));
258
+ }
259
+
260
+ J.from = (elt: HTMLElement | NodeList | HTMLCollection) => {
261
+ return Element.from(elt);
262
+ };
263
+
264
+ J.all = (q: any) => {
265
+ return Element.from(document.querySelectorAll(q));
266
+ };
267
+
268
+ let head: Element;
269
+ J.head = () => {
270
+ if (!head) head = Element.from(document.head) as Element;
271
+ return head;
272
+ };
273
+
274
+ let body: Element;
275
+ J.body = () => {
276
+ if (!body) body = Element.from(document.body) as Element;
277
+ return body;
278
+ };
279
+
280
+ let doc: Element;
281
+ J.doc = () => {
282
+ if (!doc) doc = Element.from(document as any) as Element;
283
+ return doc;
284
+ };
285
+
286
+ J.create = (t: any) => {
287
+ return Element.from(document.createElement(t));
288
+ };
289
+
290
+ const JSQuery = {
291
+ Element,
292
+ ElementArray,
293
+ Extension,
294
+ Plugin: Extension,
295
+ Caching: class Caching extends Extension {
296
+ $() {
297
+ return {
298
+ cache<T extends (...args:any[])=>any>(func: T): T {
299
+ const f = (...args: Parameters<T>): ReturnType<T> => {
300
+ const val = JSON.stringify(args);
301
+ if (f.cache.hasOwnProperty(val)) {
302
+ return f.cache[val];
303
+ }
304
+ const temp = func(...args);
305
+ f.cache[val] = temp;
306
+ return temp;
307
+ };
308
+ f.cache = {};
309
+ return f as any;
310
+ },
311
+ };
312
+ }
313
+ },
314
+ };
315
+
316
+ J.loadExtension = (extend: new()=>Extension) => {
317
+ if (Object.getPrototypeOf(extend) !== Extension) {
318
+ throw new Error(
319
+ "the class is not a child of JSQuery.Extension or the inputed class is an instance"
320
+ );
321
+ }
322
+ body = undefined;
323
+ head = undefined;
324
+ const items = new extend().get();
325
+ Object.assign(J, items.$);
326
+ Object.assign(JSQuery, items.JSQuery);
327
+ Object.assign(Element.prototype, items.Element);
328
+ Object.assign(ElementArray.prototype, items.ElementArray);
329
+ Object.assign(Element, items.static_Element);
330
+ Object.assign(ElementArray, items.static_ElementArray);
331
+ }
332
+
333
+ J.loadPlugin = J.loadExtension;
334
+
335
+ return { $: J, JSQuery };
336
+ })();
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "jsquery_node",
3
+ "version": "1.0.0",
4
+ "description": "jsquery",
5
+ "homepage": "https://github.com/chickencuber/jsquery_node#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/chickencuber/jsquery_node/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/chickencuber/jsquery_node.git"
12
+ },
13
+ "license": "MIT",
14
+ "author": "chickencuber",
15
+ "type": "commonjs",
16
+ "main": "jsquery.js",
17
+ "scripts": {
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "devDependencies": {
21
+ "typescript": "^5.9.3"
22
+ }
23
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "module": "esnext",
5
+ "target": "es2020"
6
+ }
7
+ }