-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
706 lines (706 loc) · 25 KB
/
index.js
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// ================================================
// =============== LISS exported types ============
// ================================================
export var ShadowCfg;
(function (ShadowCfg) {
ShadowCfg["NONE"] = "none";
ShadowCfg["OPEN"] = "open";
ShadowCfg["CLOSE"] = "closed";
})(ShadowCfg || (ShadowCfg = {}));
;
// ================================================
// =============== LISS Class =====================
// ================================================
let __cstr_host = null;
// https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
const CAN_HAVE_SHADOW = [
null, 'article', 'aside', 'blockquote', 'body', 'div',
'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'main',
'nav', 'p', 'section', 'span'
];
function _canHasShadow(tag) {
return CAN_HAVE_SHADOW.includes(_element2tagname(tag));
}
export default function LISS({ extends: p_extends, host: p_host, dependancies: p_deps, attributes: p_attrs, params, content, css, shadow: p_shadow, } = {}) {
//TODO merge prop if extends LISS...
const host = p_host ?? HTMLElement;
const _extends = p_extends ?? Object;
const attributes = p_attrs ?? [];
const dependancies = p_deps ? [...p_deps] : [];
const canHasShadow = _canHasShadow(host);
const shadow = p_shadow ?? (canHasShadow ? ShadowCfg.CLOSE : ShadowCfg.NONE);
if (!canHasShadow && shadow !== ShadowCfg.NONE)
throw new Error(`Host element ${_element2tagname(host)} does not support ShadowRoot`);
// CONTENT processing
if (content !== undefined) {
dependancies.push((async () => {
content = await content;
if (content instanceof HTMLTemplateElement)
content = content.innerHTML;
if (typeof content === "string") {
content = content.trim(); // Never return a text node of whitespace as the result
if (content === '')
content = undefined;
}
if (content instanceof Response)
content = await content.text();
return LISSBase.Parameters.content = content;
})());
}
// CSS processing
let stylesheets = [];
if (css !== undefined) {
if (!Array.isArray(css))
css = [css];
stylesheets = new Array(css.length);
const fetch_css = (async (css) => {
css = await css;
if (css instanceof CSSStyleSheet)
return css;
if (css instanceof HTMLStyleElement)
return css.sheet;
let style = new CSSStyleSheet();
if (typeof css === "string") {
style.replace(css);
return style;
}
//if( css instanceof Response )
style.replace(await css.text());
return style;
});
dependancies.push(...css.map(async (css, idx) => stylesheets[idx] = await fetch_css(css)));
}
// @ts-ignore
class LISSBase extends _extends {
#host; // prevents issue #1...
constructor() {
super();
// h4ck, okay because JS is monothreaded.
if (__cstr_host === null)
throw new Error("Please do not directly call this constructor");
this.#host = __cstr_host;
__cstr_host = null;
}
get host() {
return this.#host;
}
get attrs() {
return this.#host.attrs;
}
setAttrDefault(attr, value) {
return this.#host.setAttrDefault(attr, value);
}
get params() {
return this.#host.params;
}
get content() {
return this.#host.content;
}
static Parameters = {
host,
dependancies,
attributes,
params,
content,
stylesheets,
shadow,
};
onAttrChanged(_name, _oldValue, _newValue) { }
get isInDOM() {
return this.#host.isInDOM;
}
onDOMConnected() { }
onDOMDisconnected() { }
}
return LISSBase;
}
//TODO: other options...
function extendsLISS(Liss, parameters) {
// TODO: other options...
const attrs = [...Liss.Parameters.attributes, ...parameters.attributes];
const params = Object.assign({}, Liss.Parameters, { attributes: attrs });
// @ts-ignore : because TS stupid
class ExtendedLISS extends Liss {
constructor(...t) {
// @ts-ignore : because TS stupid
super(...t);
}
get attrs() {
return super.attrs;
}
static Parameters = params;
}
return ExtendedLISS;
}
LISS.extendsLISS = extendsLISS;
// ================================================
// =============== LISSHost class =================
// ================================================
function buildLISSHost(Liss, _params = {}) {
const { host, attributes, content, stylesheets, shadow, } = Liss.Parameters;
const alreadyDeclaredCSS = new Set();
const GET = Symbol('get');
const SET = Symbol('set');
const properties = Object.fromEntries(attributes.map(n => [n, {
enumerable: true,
get: function () { return this[GET](n); },
set: function (value) { return this[SET](n, value); }
}]));
class Attributes {
#data;
#defaults;
#setter;
[GET](name) {
return this.#data[name] ?? this.#defaults[name] ?? null;
}
;
[SET](name, value) {
return this.#setter(name, value); // required to get a clean object when doing {...attrs}
}
constructor(data, defaults, setter) {
this.#data = data;
this.#defaults = defaults;
this.#setter = setter;
Object.defineProperties(this, properties);
}
}
// @ts-ignore : because TS is stupid.
class LISSHostBase extends host {
#params;
constructor(params = {}) {
super();
this.#params = Object.assign({}, Liss.Parameters.params, _params, params);
this.#waitInit = new Promise((resolve) => {
if (this.isInit)
return resolve(this.#API);
this.#resolve = resolve;
});
}
/**** public API *************/
get isInit() {
return this.#API !== null;
}
async initialize(params = {}) {
if (this.isInit)
throw new Error('Element already initialized!');
Object.assign(this.#params, params);
const api = await this.init();
if (this.#isInDOM)
api.onDOMConnected();
return api;
}
get LISSSync() {
if (!this.isInit)
throw new Error('Accessing API before WebComponent initialization!');
return this.#API;
}
get LISS() {
return this.#waitInit;
}
/*** init ***/
#waitInit;
#resolve = null;
#API = null;
#isInDOM = false;
get isInDOM() {
return this.#isInDOM;
}
disconnectedCallback() {
this.#isInDOM = false;
this.#API.onDOMDisconnected();
}
connectedCallback() {
this.#isInDOM = true;
if (!this.isInit) {
this.init();
return;
}
this.#API.onDOMConnected();
}
async init() {
customElements.upgrade(this);
// shadow
this.#content = this;
if (shadow !== 'none') {
this.#content = this.attachShadow({ mode: shadow });
//@ts-ignore
this.#content.addEventListener('click', onClickEvent);
//@ts-ignore
this.#content.addEventListener('dblclick', onClickEvent);
}
// attrs
for (let obs of attributes)
this.#attributes[obs] = this.getAttribute(obs);
// css
if (shadow !== 'none')
this.#content.adoptedStyleSheets.push(sharedCSS);
if (stylesheets.length) {
if (shadow !== 'none')
this.#content.adoptedStyleSheets.push(...stylesheets);
else {
const cssselector = this.CSSSelector;
// if not yet inserted :
if (!alreadyDeclaredCSS.has(cssselector)) {
let style = document.createElement('style');
style.setAttribute('for', cssselector);
let html_stylesheets = "";
for (let style of stylesheets)
for (let rule of style.cssRules)
html_stylesheets += rule.cssText + '\n';
style.innerHTML = html_stylesheets.replace(':host', `:is(${cssselector})`);
document.head.append(style);
alreadyDeclaredCSS.add(cssselector);
}
}
}
// content
if (content !== undefined) {
let template_elem = document.createElement('template');
let str = content.replace(/\$\{(.+?)\}/g, (_, match) => this.getAttribute(match) ?? '');
template_elem.innerHTML = str;
this.#content.append(...template_elem.content.childNodes);
}
// build
// h4ck, okay because JS is monothreaded.
__cstr_host = this;
let obj = new Liss();
if (obj instanceof Promise)
obj = await obj;
this.#API = obj;
// default slot
if (this.hasShadow && this.#content.childNodes.length === 0)
this.#content.append(document.createElement('slot'));
if (this.#resolve !== null)
this.#resolve(this.#API);
return this.#API;
}
get params() {
return this.#params;
}
/*** content ***/
#content = null;
get content() {
return this.#content;
}
getPart(name) {
return this.hasShadow
? this.#content?.querySelector(`::part(${name})`)
: this.#content?.querySelector(`[part="${name}"]`);
}
getParts(name) {
return this.hasShadow
? this.#content?.querySelectorAll(`::part(${name})`)
: this.#content?.querySelectorAll(`[part="${name}"]`);
}
get hasShadow() {
return shadow !== 'none';
}
/*** CSS ***/
get CSSSelector() {
return this.hasShadow
? this.tagName
: `${this.tagName}[is="${this.getAttribute("is")}"]`;
}
/*** attrs ***/
#attrs_flag = false;
#attributes = {};
#attributesDefaults = {};
#attrs = new Attributes(this.#attributes, this.#attributesDefaults, (name, value) => {
this.#attributes[name] = value;
this.#attrs_flag = true; // do not trigger onAttrsChanged.
if (value === null)
this.removeAttribute(name);
else
this.setAttribute(name, value);
});
setAttrDefault(name, value) {
if (value === null)
delete this.#attributesDefaults[name];
else
this.#attributesDefaults[name] = value;
}
get attrs() {
return this.#attrs;
}
static observedAttributes = attributes;
attributeChangedCallback(name, oldValue, newValue) {
if (this.#attrs_flag) {
this.#attrs_flag = false;
return;
}
this.#attributes[name] = newValue;
if (!this.isInit)
return;
if (this.#API.onAttrChanged(name, oldValue, newValue) === false) {
this.#attrs[name] = oldValue; // revert the change.
}
}
}
;
return LISSHostBase;
}
// ================================================
// =============== LISS define ====================
// ================================================
const _DOMContentLoaded = new Promise((resolve) => {
if (document.readyState === "interactive" || document.readyState === "complete")
return resolve();
document.addEventListener('DOMContentLoaded', () => {
resolve();
}, true);
});
LISS.define = async function (tagname, ComponentClass, { dependancies, params } = {}) {
dependancies ??= [];
params ??= {};
const Class = ComponentClass.Parameters.host;
let LISSBase = ComponentClass;
let htmltag = _element2tagname(Class) ?? undefined;
await Promise.all([_DOMContentLoaded, ...dependancies, ...LISSBase.Parameters.dependancies]);
const LISSclass = buildLISSHost(ComponentClass, params);
const opts = htmltag === undefined ? {}
: { extends: htmltag };
customElements.define(tagname, LISSclass, opts);
};
// ================================================
// =============== LISS ShadowRoot tools ==========
// ================================================
const sharedCSS = new CSSStyleSheet();
document.adoptedStyleSheets.push(sharedCSS);
LISS.insertGlobalCSSRules = function (css) {
let css_style;
if (css instanceof HTMLStyleElement)
css_style = css.sheet;
if (typeof css === "string") {
css_style = new CSSStyleSheet();
css_style.replaceSync(css);
}
for (let rule of css_style.cssRules)
sharedCSS.insertRule(rule.cssText);
};
const DELEGATED_EVENTS = {
"click": [],
"dblclick": []
};
const ALREADY_PROCESSED = Symbol();
function onClickEvent(ev) {
if (ev[ALREADY_PROCESSED] === true)
return;
ev[ALREADY_PROCESSED] = true;
const handlers = DELEGATED_EVENTS[ev.type];
for (let elem of ev.composedPath()) {
if (elem instanceof ShadowRoot || elem === document || elem === window)
continue;
var target = elem;
for (let [selector, handler] of handlers) {
if (target.matches(selector))
handler(ev);
}
}
}
LISS.insertGlobalDelegatedListener = function (event_name, selector, handler) {
DELEGATED_EVENTS[event_name].push([selector, handler]);
};
document.addEventListener('click', onClickEvent);
document.addEventListener('dblclick', onClickEvent);
LISS.closest = function closest(selector, element) {
while (true) {
var result = element.closest(selector);
if (result !== null)
return result;
const root = element.getRootNode();
if (!("host" in root))
return null;
element = root.host;
}
};
async function build(tagname, { params = {}, initialize = true, content = [], parent = undefined, id = undefined, classes = [], cssvars = {}, attrs = {}, data = {}, listeners = {} } = {}) {
if (!initialize && parent === null)
throw new Error("A parent must be given if initialize is false");
let CustomClass = await customElements.whenDefined(tagname);
let elem = new CustomClass(params);
// Fix issue #2
if (elem.tagName.toLowerCase() !== tagname)
elem.setAttribute("is", tagname);
if (id !== undefined)
elem.id = id;
if (classes.length > 0)
elem.classList.add(...classes);
for (let name in cssvars)
elem.style.setProperty(`--${name}`, cssvars[name]);
for (let name in attrs) {
let value = attrs[name];
if (typeof value === "boolean")
elem.toggleAttribute(name, value);
else
elem.setAttribute(name, value);
}
for (let name in data) {
let value = data[name];
if (value === false)
delete elem.dataset[name];
else if (value === true)
elem.dataset[name] = "";
else
elem.dataset[name] = value;
}
if (!Array.isArray(content))
content = [content];
elem.replaceChildren(...content);
for (let name in listeners)
elem.addEventListener(name, listeners[name]);
if (parent !== undefined)
parent.append(elem);
if (!elem.isInit && initialize)
return await LISS.initialize(elem);
return await LISS.getLISS(elem);
}
LISS.build = build;
LISS.whenDefined = async function (tagname, callback) {
await customElements.whenDefined(tagname);
if (callback !== undefined)
callback();
return;
};
LISS.whenAllDefined = async function (tagnames, callback) {
await Promise.all(tagnames.map(t => customElements.whenDefined(t)));
if (callback !== undefined)
callback();
};
LISS.isDefined = function (name) {
return customElements.get(name);
};
LISS.selector = function (name) {
if (name === undefined) // just an h4ck
return "";
return `:is(${name}, [is="${name}"])`;
};
LISS.getLISS = async function (element) {
await LISS.whenDefined(LISS.getName(element));
return element.LISS; // ensure initialized.
};
LISS.getLISSSync = function (element) {
if (!LISS.isDefined(LISS.getName(element)))
throw new Error(`${name} hasn't been defined yet.`);
let host = element;
if (!host.isInit)
throw new Error("Instance hasn't been initialized yet.");
return host.LISSSync;
};
LISS.initialize = async function (element) {
await LISS.whenDefined(LISS.getName(element));
return await element.initialize(); // ensure initialization.
};
LISS.getName = function (element) {
const name = element.getAttribute('is') ?? element.tagName.toLowerCase();
if (!name.includes('-'))
throw new Error(`Element ${name} is not a WebComponent`);
return name;
};
function _buildQS(selector, tagname_or_parent, parent = document) {
if (tagname_or_parent !== undefined && typeof tagname_or_parent !== 'string') {
parent = tagname_or_parent;
tagname_or_parent = undefined;
}
return [`${selector}${LISS.selector(tagname_or_parent)}`, parent];
}
async function qs(selector, tagname_or_parent, parent = document) {
[selector, parent] = _buildQS(selector, tagname_or_parent, parent);
let result = await LISS.qso(selector, parent);
if (result === null)
throw new Error(`Element ${selector} not found`);
return result;
}
LISS.qs = qs;
async function qso(selector, tagname_or_parent, parent = document) {
[selector, parent] = _buildQS(selector, tagname_or_parent, parent);
const element = parent.querySelector(selector);
if (element === null)
return null;
return await LISS.getLISS(element);
}
LISS.qso = qso;
async function qsa(selector, tagname_or_parent, parent = document) {
[selector, parent] = _buildQS(selector, tagname_or_parent, parent);
const elements = parent.querySelectorAll(selector);
let idx = 0;
const promises = new Array(elements.length);
for (let element of elements)
promises[idx++] = LISS.getLISS(element);
return await Promise.all(promises);
}
LISS.qsa = qsa;
async function qsc(selector, tagname_or_parent, element) {
const res = _buildQS(selector, tagname_or_parent, element);
const result = res[1].closest(res[0]);
if (result === null)
return null;
return await LISS.getLISS(result);
}
LISS.qsc = qsc;
function qsSync(selector, tagname_or_parent, parent = document) {
[selector, parent] = _buildQS(selector, tagname_or_parent, parent);
const element = parent.querySelector(selector);
if (element === null)
throw new Error(`Element ${selector} not found`);
return LISS.getLISSSync(element);
}
LISS.qsSync = qsSync;
function qsaSync(selector, tagname_or_parent, parent = document) {
[selector, parent] = _buildQS(selector, tagname_or_parent, parent);
const elements = parent.querySelectorAll(selector);
let idx = 0;
const result = new Array(elements.length);
for (let element of elements)
result[idx++] = LISS.getLISSSync(element);
return result;
}
LISS.qsaSync = qsaSync;
function qscSync(selector, tagname_or_parent, element) {
const res = _buildQS(selector, tagname_or_parent, element);
const result = res[1].closest(res[0]);
if (result === null)
return null;
return LISS.getLISSSync(result);
}
LISS.qscSync = qscSync;
// ================================================
// =============== LISS Auto ======================
// ================================================
export class LISS_Auto extends LISS({ attributes: ["src"] }) {
#known_tag = new Set();
#directory;
#sw;
constructor() {
super();
this.#sw = new Promise(async (resolve) => {
await navigator.serviceWorker.register(`./sw.js`);
if (navigator.serviceWorker.controller)
resolve();
navigator.serviceWorker.addEventListener('controllerchange', () => {
resolve();
});
});
const src = this.attrs.src;
if (src === null)
throw new Error("src attribute is missing.");
this.#directory = src[0] === '.'
? `${window.location.pathname}/${src}`
: src;
new MutationObserver((mutations) => {
for (let mutation of mutations)
for (let addition of mutation.addedNodes)
if (addition instanceof Element)
this.#addTag(addition.tagName);
}).observe(document, { childList: true, subtree: true });
for (let elem of document.querySelectorAll("*"))
this.#addTag(elem.tagName);
}
resources() {
return [
"index.js",
"index.html",
"index.css"
];
}
defineWebComponent(tagname, files, opts) {
const js = files["index.js"];
const content = files["index.html"];
let klass = null;
if (js !== undefined)
klass = js(opts);
else if (content !== undefined)
klass = class WebComponent extends LISS(opts) {
};
if (klass === null)
throw new Error(`Missing files for WebComponent ${tagname}.`);
return LISS.define(tagname, klass);
}
async #addTag(tagname) {
tagname = tagname.toLowerCase();
if (tagname === 'liss-auto' || tagname === 'bliss-auto' || !tagname.includes('-') || this.#known_tag.has(tagname))
return;
this.#known_tag.add(tagname);
await this.#sw; // ensure SW is installed.
const filenames = this.resources();
const resources = await Promise.all(filenames.map(file => file.endsWith('.js')
? _import(`${this.#directory}/${tagname}/${file}`, true)
: _fetchText(`${this.#directory}/${tagname}/${file}`, true)));
const files = {};
for (let i = 0; i < filenames.length; ++i)
if (resources[i] !== undefined)
files[filenames[i]] = resources[i];
const content = files["index.html"];
const css = files["index.css"];
const opts = {
...content !== undefined && { content },
...css !== undefined && { css },
};
return this.defineWebComponent(tagname, files, opts);
}
}
LISS.define("liss-auto", LISS_Auto);
;
export class CstmEvent extends CustomEvent {
get type() { return super.type; }
constructor(type, args) {
super(type, { detail: args });
}
}
// ================================================
// =============== LISS internal tools ============
// ================================================
async function fetchResource(resource) {
resource = await resource;
if (!(resource instanceof Response))
resource = await fetch(resource);
return await resource.text();
}
async function _fetchText(uri, isLissAuto = false) {
const options = isLissAuto
? { headers: { "liss-auto": "true" } }
: {};
const response = await fetch(uri, options);
if (response.status !== 200)
return undefined;
if (isLissAuto && response.headers.get("status") === "404")
return undefined;
return await response.text();
}
async function _import(uri, isLissAuto = false) {
// test for the module existance.
if (isLissAuto && await _fetchText(uri, isLissAuto) === undefined)
return undefined;
try {
return (await import(/* webpackIgnore: true */ uri)).default;
}
catch (e) {
console.log(e);
return undefined;
}
}
// from https://stackoverflow.com/questions/51000461/html-element-tag-name-from-constructor
const HTMLCLASS_REGEX = /HTML(\w+)Element/;
const elementNameLookupTable = {
'UList': 'ul',
'TableCaption': 'caption',
'TableCell': 'td', // th
'TableCol': 'col', //'colgroup',
'TableRow': 'tr',
'TableSection': 'tbody', //['thead', 'tbody', 'tfoot'],
'Quote': 'q',
'Paragraph': 'p',
'OList': 'ol',
'Mod': 'ins', //, 'del'],
'Media': 'video', // 'audio'],
'Image': 'img',
'Heading': 'h1', //, 'h2', 'h3', 'h4', 'h5', 'h6'],
'Directory': 'dir',
'DList': 'dl',
'Anchor': 'a'
};
function _element2tagname(Class) {
if (Class === HTMLElement)
return null;
let htmltag = HTMLCLASS_REGEX.exec(Class.name)[1];
return elementNameLookupTable[htmltag] ?? htmltag.toLowerCase();
}