This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dollar.ts
213 lines (188 loc) · 5.99 KB
/
dollar.ts
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
import { once } from "./utils";
type NestedAttribute = { [s: string]: string } | string;
interface Attributes {
[s: string]: NestedAttribute;
style?: { [s: string]: string };
html?: string;
}
interface DollarEventTarget<T> {
on: (name: string, func: (event: Event) => void) => T & DollarEventTarget<T>;
off: (name: string, func: (event: Event) => void) => T & DollarEventTarget<T>;
}
interface DollarElement<T> extends DollarEventTarget<T> {
setattr: (attrs: Attributes) => T & DollarElement<T>;
}
interface DollarHTMLElement<T> extends DollarElement<T> {
css: (attrs: { [s: string]: string }) => T & DollarHTMLElement<T>;
html: (attrs: string) => T & DollarHTMLElement<T>;
}
const on = function (
names: string,
func: (event: Event) => void,
context: EventTarget
) {
names.split(" ").forEach((event) => {
context.addEventListener(event, func);
});
return context;
};
const off = function (
names: string,
func: (event: Event) => void,
context: EventTarget
) {
names.split(" ").forEach((event) => {
context.removeEventListener(event, func);
});
return context;
};
/**
* Sets the attributes of a context element based on the `attrs` Attribute object.
*
* Important to note is the attribute key specialization.
* HTML Elements have a hard-coded set of reserved keys, such `html`: if the input key is `html`
* and the input context is of type HTMLElement, the keyword is specialized, and the context
* element's `innerHTML` attribute is modified.
*
* @param attrs input attributes.
* @param context context element.
* @param func function to execute at each [key, value] call.
*/
const setAttributes = function <T extends Element>(attrs: Attributes, context: T) {
Object.entries(attrs).forEach(([key, value]) => {
if (context instanceof HTMLElement) {
if (key === "html") {
context.innerHTML = JSON.stringify(value);
} else if (key === "styles" && typeof value === "object") {
Object.entries(value).forEach(([styleKey, styleValue]) => {
context.style.setProperty(styleKey, styleValue);
});
}
}
if (context instanceof Element) {
if (value == null) {
context.removeAttribute(key);
} else if (typeof value === "string") {
context.setAttribute(key, value);
}
}
});
return context;
};
const eventTargetFuncs = {
on: function (name: string, func: (event: Event) => void) {
on(name, func, this);
return this;
},
off: function (name: string, func: (event: Event) => void) {
off(name, func, this);
return this;
}
};
const elementFuncs = {
setattr: function (attrs: Attributes) {
setAttributes(attrs, this);
return this;
}
};
const htmlElementFuncs = {
css: function (styles: NestedAttribute) {
setAttributes(
{
styles
},
this
);
return this;
},
html: function (s: string) {
setAttributes(
{
html: s
},
this
);
return this;
}
};
const dollarFuncs = {
...eventTargetFuncs,
...elementFuncs,
...htmlElementFuncs
};
const foldFunctions = function (funcs) {
const folded = {};
Object.keys(funcs).forEach((key) => {
folded[key] = function (...args) {
return this.forEach((el) => {
el[key](...args);
});
};
});
return folded;
};
// Function decelerations to aid in proper type hinting.
function $<T>(
query: T | string,
context?: Document | Element
): T extends HTMLElement
? T & DollarHTMLElement<T>
: T extends Element
? T & DollarElement<T>
: T & DollarEventTarget<T>;
/**
* The dollar 💲in dollar.ts. Wrapper around .querySelector in the case of an input
* query string, and a simple pipe in the case of anything else (usually of type Element).
*
* The resultant object is an intersection of T & DollarElement. So if the input object is
* of type HTMLElement, that type is preserved and &'d with DollarElement.
*
* @param query input query string or element.
* @param context context element or document.
*/
function $<T>(query: T, context = document) {
const node = typeof query === "string" ? context.querySelector(query) : query;
if (node == null) {
return undefined;
} else {
return Object.assign(node, dollarFuncs);
}
}
function $$<T>(
query: string | NodeListOf<Element> | Array<T> | T,
context?: Document | Element
): T extends HTMLElement
? Array<T & DollarHTMLElement<T>>
: T extends Element
? Array<T & DollarElement<T>>
: Array<T & DollarEventTarget<T>>;
/**
* Like `$`, `$$` is essentially a wrapper for .querySelectorAll, which notoriously returns
* a NodeList object.
*
* In the case of an Array or NodeListOf<Element>, we simply pipe the nodes to
* Array.from and preserve there attributes. In the case of a string, we call
* `context.querySelectorAll(query)` and do the same.
*
* After completion, we add the set of DollarElement prototype functions to the resultant object,
* but with the special functionality of looping over each element, and then executing
* the function call (hence dollarFoldedFunctions; they're folded over the array).
*
* @param query query element[s] or a query string.
* @param context context element or document.
*/
function $$<T>(query: T, context: Document | Element = document) {
const nodes =
query instanceof NodeList || query instanceof Array
? query
: typeof query === "string"
? context.querySelectorAll(query)
: [query];
if (nodes == null) {
return undefined;
} else {
const arr = Array.from(nodes).map((el) => Object.assign(el, dollarFuncs));
return Object.assign(arr, foldFunctions(dollarFuncs));
}
}
export { $, $$ };