-
Notifications
You must be signed in to change notification settings - Fork 5
/
weya.ts
435 lines (322 loc) · 17.7 KB
/
weya.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const API = {
document: document
}
const TAGS = {
svg: "a altGlyph altGlyphDef altGlyphItem animate animateColor animateMotion animateTransform circle clipPath color-profile cursor defs desc ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter font font-face font-face-format font-face-name font-face-src font-face-uri foreignObject g glyph glyphRef hkern image line linearGradient marker mask metadata missing-glyph mpath path pattern polygon polyline radialGradient rect script set stop style svg symbol text textPath title tref tspan use view vkern switch foreignObject",
html: "a abbr address article aside audio b bdi bdo blockquote body button canvas caption cite code colgroup datalist dd del details dfn div dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup html i iframe ins kbd label legend li main map mark menu meter nav noscript object ol optgroup option output p pre progress q rp rt ruby s samp script section select small span strong style sub summary sup table tbody td textarea tfoot th thead time title tr u ul video",
htmlVoid: "area base br col command embed hr img input keygen link meta param source track wbr"
}
let TAGS_DICT = {}
for (let ns in TAGS) {
let tags = TAGS[ns].split(" ")
for (let tag of tags) {
TAGS_DICT[tag] = ns
}
}
const NAMESPACES = {
svg: "http://www.w3.org/2000/svg",
html: "http://www.w3.org/1999/xhtml",
htmlVoid: null
}
export type WeyaElement = HTMLElement | SVGElement
export interface WeyaTemplateFunction {
($: WeyaElementFunction): void
}
type WeyaElementArg = (string | AttributesInterface | WeyaTemplateFunction | WeyaElement)
export interface WeyaElementFunction {
// With tag
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, text: string): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, text: string, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, attrs: AttributesInterface, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, text: string): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, text: string, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, attrs: AttributesInterface, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
// With tag and selector
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, text: string): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, text: string, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, attrs: AttributesInterface, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, text: string): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, text: string, attrs: AttributesInterface): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
<K extends keyof HTMLElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, attrs: AttributesInterface, func: WeyaTemplateFunction): HTMLElementTagNameMap[K]
//Repeat for SVG
// With tag
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, text: string): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, text: string, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, attrs: AttributesInterface, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, text: string): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, text: string, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, parent: WeyaElement, attrs: AttributesInterface, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
// With tag and selector
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, text: string): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, text: string, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, attrs: AttributesInterface, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, text: string): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, text: string, attrs: AttributesInterface): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
<K extends keyof SVGElementTagNameMap>(this: WeyaContext | void, tag: K, selector: string, parent: WeyaElement, attrs: AttributesInterface, func: WeyaTemplateFunction): SVGElementTagNameMap[K]
// css selector
(this: WeyaContext | void, selector: string): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement): WeyaElement
(this: WeyaContext | void, selector: string, attrs: AttributesInterface): WeyaElement
(this: WeyaContext | void, selector: string, text: string): WeyaElement
(this: WeyaContext | void, selector: string, text: string, attrs: AttributesInterface): WeyaElement
(this: WeyaContext | void, selector: string, func: WeyaTemplateFunction): WeyaElement
(this: WeyaContext | void, selector: string, attrs: AttributesInterface, func: WeyaTemplateFunction): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement, attrs: AttributesInterface): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement, text: string): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement, text: string, attrs: AttributesInterface): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement, func: WeyaTemplateFunction): WeyaElement
(this: WeyaContext | void, selector: string, parent: WeyaElement, attrs: AttributesInterface, func: WeyaTemplateFunction): WeyaElement
// Default tag = div
(this: WeyaContext | void): HTMLDivElement
(this: WeyaContext | void, parent: WeyaElement): HTMLDivElement
(this: WeyaContext | void, attrs: AttributesInterface): HTMLDivElement
(this: WeyaContext | void, text: string): HTMLDivElement
(this: WeyaContext | void, text: string, attrs: AttributesInterface): HTMLDivElement
(this: WeyaContext | void, func: WeyaTemplateFunction): HTMLDivElement
(this: WeyaContext | void, parent: WeyaElement, attrs: AttributesInterface): HTMLDivElement
(this: WeyaContext | void, parent: WeyaElement, text: string): HTMLDivElement
(this: WeyaContext | void, parent: WeyaElement, text: string, attrs: AttributesInterface): HTMLDivElement
(this: WeyaContext | void, parent: WeyaElement, func: WeyaTemplateFunction): HTMLDivElement
}
interface WeyaContext {
_elem?: WeyaElement,
}
interface Parameters {
def: ElemDef | null,
text: string | null,
attrs: AttributesInterface | null,
func: WeyaTemplateFunction | null,
parent: WeyaElement
}
interface StylesInterface {
[prop: string]: string | null
}
interface EventsInterface {
[prop: string]: EventListenerOrEventListenerObject
}
interface DataInterface {
[prop: string]: any
}
interface AttributesInterface {
style?: StylesInterface
on?: EventsInterface
data?: DataInterface
// Other Attributes can be string or null
[prop: string]: string | number | null | StylesInterface | EventsInterface
}
interface ElemDef {
tag: string,
id: string | null,
classes: string[]
}
function parseDefinition(str: string): ElemDef {
let res: ElemDef = {
tag: '',
id: null,
classes: []
}
let parts = str.split(".")
let first = parts[0]
let firstParts = first.split('#')
res.tag = firstParts[0]
if (firstParts.length == 2) {
res.id = firstParts[1]
} else if (firstParts.length > 2) {
throw Error("Invalid Definition: " + str)
}
if (parts.length > 1) {
res.classes = parts.slice(1)
}
return res
}
const HEADER_TAGS = {h1: true, h2: true, h3: true, h4: true, h5: true, h6: true}
function isValidTag(str: string): boolean {
if (HEADER_TAGS[str] != null) {
return true
}
for (let c of str) {
if (c.toLowerCase() !== c || c.toUpperCase() === c)
return false
}
return true
}
function getParameters(args: WeyaElementArg[]) {
let params: Parameters = {
def: null,
text: null,
attrs: null,
func: null,
parent: null
}
if (args.length == 0) {
params.def = parseDefinition('div')
} else if (typeof args[0] == 'string') {
let arg0 = <string>args[0]
if (isValidTag(arg0) && args.length > 1 && (typeof args[1] == 'string')) {
const arg1 = <string>args[1]
if (arg1.length > 1 && (arg1[0] === '.' || arg1[0] === '#')) {
arg0 += arg1
args = args.slice(1)
}
}
params.def = parseDefinition(arg0)
args = args.slice(1)
}
for (let arg of args) {
switch (typeof arg) {
case "function":
params.func = arg as WeyaTemplateFunction
break
case "object":
if (arg instanceof HTMLElement || arg instanceof SVGElement) {
params.parent = arg as WeyaElement
} else {
params.attrs = arg as AttributesInterface
}
break
case "string":
params.text = arg as string
}
}
return params
}
function domAPICreate(): WeyaElementFunction {
function setStyles(elem: WeyaElement, styles: StylesInterface) {
for (let k in styles) {
let v = styles[k]
if (v != null) {
elem.style.setProperty(k, v)
} else {
elem.style.removeProperty(k)
}
}
}
function setEvents(elem: WeyaElement, events: EventsInterface) {
for (let k in events) {
let names = k.split('|')
for (let name of names) {
elem.addEventListener(name, events[k], false)
}
}
}
function setData(elem: WeyaElement, data: DataInterface) {
for (let k in data) {
elem[k] = data[k]
}
}
function setAttributes(elem: WeyaElement, attrs: AttributesInterface) {
for (let k in attrs) {
let v = attrs[k]
switch (k) {
case "style":
setStyles(elem, v as StylesInterface)
break
case "on":
setEvents(elem, v as EventsInterface)
break
case "data":
setData(elem, v as DataInterface)
break
default:
if (v != null) {
elem.setAttribute(k, v as string)
} else {
elem.removeAttribute(k)
}
}
}
}
let setIdClass: typeof setIdClassNew
function setIdClassNew(elem: WeyaElement, idClass: ElemDef) {
if (idClass.id != null) {
elem.id = idClass.id
}
for (let c of idClass.classes) {
elem.classList.add(c)
}
}
function setIdClassFallback(elem: WeyaElement, idClass: ElemDef) {
if (idClass.id != null) {
elem.id = idClass.id
}
if (idClass.classes.length > 0) {
let className = idClass.classes.join(" ")
elem.setAttribute("class", className)
}
}
function switchIdClass() {
let elem = API.document.createElementNS("http://www.w3.org/2000/svg", "g")
if (!elem.classList) {
setIdClass = setIdClassFallback
} else {
setIdClass = setIdClassNew
}
}
switchIdClass()
function append(this: WeyaContext, ...args: WeyaElementArg[]): WeyaElement {
let params = getParameters(args)
let parent = params.parent
if (this != null && this._elem != null) {
if (parent != null) {
throw Error("Cannot set a parent within a context")
}
parent = this._elem
}
let elem: WeyaElement
if (params.def == null) {
elem = parent
} else {
let tag = params.def.tag
let ns = NAMESPACES[TAGS_DICT[tag]]
if (ns != null) {
elem = API.document.createElementNS(ns, tag) as WeyaElement
} else {
elem = API.document.createElement(tag)
}
if (params.def != null) {
setIdClass(elem, params.def)
}
if (params.attrs != null) {
setAttributes(elem, params.attrs)
}
if (parent != null) {
parent.appendChild(elem)
}
}
if (params.func != null) {
let state: WeyaContext = {
_elem: elem
}
params.func(append.bind(state))
} else if (params.text != null) {
elem.textContent = params.text
}
return elem
}
return <WeyaElementFunction>append
}
export let Weya = domAPICreate()