cradova 3.5.0 → 3.5.1

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/dist/index.js CHANGED
@@ -1,835 +1 @@
1
- // src/primitives/classes.ts
2
- class cradovaEvent {
3
- static refid = 0;
4
- afterMount = [];
5
- beforeMountActive = [];
6
- afterDeactivate = [];
7
- dispatchEvent(eventName) {
8
- const eventListeners = this[eventName];
9
- if (eventName.includes("Active")) {
10
- for (let i = 0;i < eventListeners.length; i++) {
11
- eventListeners[i]();
12
- }
13
- return;
14
- }
15
- while (eventListeners.length !== 0) {
16
- eventListeners.shift()();
17
- }
18
- }
19
- }
20
- var CradovaEvent = new cradovaEvent;
21
-
22
- class Comp {
23
- id;
24
- component;
25
- effects = [];
26
- effectuate = null;
27
- rendered = false;
28
- published = false;
29
- preRendered = null;
30
- reference = null;
31
- subData = null;
32
- _state = [];
33
- _state_index = 0;
34
- test;
35
- constructor(component) {
36
- this.component = component.bind(this);
37
- cradovaEvent.refid += 1;
38
- this.id = cradovaEvent.refid;
39
- }
40
- preRender() {
41
- this.preRendered = this.render();
42
- }
43
- render() {
44
- this.effects = [];
45
- this.rendered = false;
46
- if (!this.preRendered) {
47
- this._state_index = 0;
48
- this._state = [];
49
- const html = this.component();
50
- if (html instanceof HTMLElement) {
51
- this.reference = html;
52
- this.effector.apply(this);
53
- this.rendered = true;
54
- this.published = true;
55
- } else {
56
- console.error(" \u2718 Cradova err : Invalid html content, got - " + html);
57
- }
58
- return html;
59
- } else {
60
- return this.preRendered;
61
- }
62
- }
63
- _effect(fn) {
64
- if (!this.rendered) {
65
- this.effects.push(fn.bind(this));
66
- }
67
- }
68
- async effector() {
69
- for (let i = 0;i < this.effects.length; i++) {
70
- const fn = await this.effects[i].apply(this);
71
- if (typeof fn === "function") {
72
- CradovaEvent.afterDeactivate.push(fn);
73
- }
74
- }
75
- this.effects = [];
76
- if (this.effectuate) {
77
- this.effectuate();
78
- this.effectuate = null;
79
- }
80
- }
81
- recall() {
82
- if (!this.rendered) {
83
- this.effectuate = () => {
84
- if (this.published) {
85
- this.activate();
86
- }
87
- };
88
- } else {
89
- if (this.published) {
90
- setTimeout(() => {
91
- this.activate();
92
- });
93
- }
94
- }
95
- }
96
- async activate() {
97
- this.published = false;
98
- if (!this.rendered) {
99
- return;
100
- }
101
- this._state_index = 0;
102
- const node = this.reference;
103
- if (document.contains(node)) {
104
- const html = this.component();
105
- if (html instanceof HTMLElement) {
106
- node.insertAdjacentElement("beforebegin", html);
107
- node.remove();
108
- this.published = true;
109
- this.reference = html;
110
- CradovaEvent.dispatchEvent("afterMount");
111
- } else {
112
- console.error(" \u2718 Cradova err : Invalid html, got - " + html);
113
- }
114
- } else {
115
- this.reference = null;
116
- this.rendered = false;
117
- }
118
- }
119
- }
120
-
121
- class Signal {
122
- pn;
123
- subs;
124
- pipe;
125
- constructor(initial, props) {
126
- this.pipe = initial;
127
- this.subs = {};
128
- if (props && props.persistName) {
129
- this.pn = props.persistName;
130
- const key = localStorage.getItem(props.persistName);
131
- if (key && key !== "undefined") {
132
- this.pipe = JSON.parse(key);
133
- }
134
- if (typeof initial === "object") {
135
- for (const key2 in initial) {
136
- if (!Object.prototype.hasOwnProperty.call(this.pipe, key2)) {
137
- this.pipe[key2] = initial[key2];
138
- }
139
- }
140
- }
141
- }
142
- }
143
- publish(eventName, data) {
144
- this.pipe[eventName] = data;
145
- const subs = this.subs[eventName] || [];
146
- for (let i = 0;i < subs.length; i++) {
147
- const comp = subs[i];
148
- comp.subData = data;
149
- comp.recall();
150
- }
151
- if (this.pn) {
152
- localStorage.setItem(this.pn, JSON.stringify(this.pipe));
153
- }
154
- }
155
- subscribe(eventName, comp) {
156
- if (comp instanceof Comp) {
157
- if (this.subs[eventName]?.find((cmp) => cmp.id === comp.id)) {
158
- return;
159
- }
160
- if (!this.subs[eventName]) {
161
- this.subs[eventName] = [comp];
162
- } else {
163
- this.subs[eventName].push(comp);
164
- }
165
- if (this.pipe[eventName]) {
166
- comp.subData = this.pipe[eventName];
167
- }
168
- }
169
- }
170
- clearPersist() {
171
- if (this.pn) {
172
- localStorage.removeItem(this.pn);
173
- }
174
- }
175
- }
176
-
177
- class Page {
178
- _name;
179
- _html;
180
- _template = document.createElement("div");
181
- _callBack;
182
- _deCallBack;
183
- _dropped = false;
184
- _snapshot;
185
- _snapshot_html;
186
- constructor(pageParams) {
187
- const { template, name } = pageParams;
188
- this._html = template;
189
- this._name = name || "Document";
190
- this._template.setAttribute("id", "page");
191
- this._snapshot = pageParams.snapshotIsolation || false;
192
- }
193
- async _takeSnapShot() {
194
- if (RouterBox.doc.dataset["snapshot"] === "true")
195
- return;
196
- try {
197
- const response = await fetch(location.href);
198
- if (!response.ok)
199
- throw new Error("Failed to fetch the page");
200
- const html = await response.text();
201
- const parser = new DOMParser;
202
- const doc = parser.parseFromString(html, "text/html");
203
- doc.title = this._name;
204
- const wrapper = doc.querySelector('[data-wrapper="app"]');
205
- if (wrapper) {
206
- wrapper.setAttribute("data-snapshot", "true");
207
- wrapper.innerHTML = this._snapshot_html;
208
- } else {
209
- console.error("Wrapper or template is not found");
210
- return;
211
- }
212
- const snapshot = doc.documentElement.outerHTML;
213
- await fetch(`${location.origin}`, {
214
- body: snapshot,
215
- method: "POST",
216
- headers: {
217
- "Content-Type": "text/html",
218
- "cradova-snapshot": location.href
219
- }
220
- });
221
- } catch (error) {
222
- console.error("Snapshot error:", error);
223
- }
224
- this._snapshot_html = undefined;
225
- }
226
- onActivate(cb) {
227
- this._callBack = cb;
228
- }
229
- onDeactivate(cb) {
230
- this._deCallBack = cb;
231
- }
232
- async _deActivate() {
233
- this._deCallBack && await this._deCallBack();
234
- }
235
- drop(state) {
236
- if (typeof state === "boolean") {
237
- this._dropped = state;
238
- return;
239
- } else
240
- return this._dropped;
241
- }
242
- async _activate() {
243
- if (this._dropped) {
244
- history.go(-1);
245
- return;
246
- }
247
- let html = this._html.apply(this);
248
- if (html instanceof HTMLElement) {
249
- this._template.innerHTML = "";
250
- this._template.appendChild(html);
251
- } else {
252
- throw new Error(` \u2718 Cradova err: template function for the page returned ${html} instead of html`);
253
- }
254
- document.title = this._name;
255
- RouterBox.doc.innerHTML = "";
256
- CradovaEvent.dispatchEvent("beforeMountActive");
257
- if (this._snapshot)
258
- this._snapshot_html = this._template.outerHTML;
259
- RouterBox.doc.appendChild(this._template);
260
- CradovaEvent.dispatchEvent("afterMount");
261
- window.scrollTo({
262
- top: 0,
263
- left: 0,
264
- behavior: "instant"
265
- });
266
- this._callBack && await this._callBack();
267
- CradovaEvent.dispatchEvent("afterDeactivate");
268
- if (this._snapshot)
269
- this._takeSnapShot();
270
- }
271
- }
272
-
273
- class RouterBoxClass {
274
- doc = null;
275
- lastNavigatedRouteController;
276
- nextRouteController;
277
- lastNavigatedRoute;
278
- pageShow = null;
279
- pageHide = null;
280
- errorHandler;
281
- loadingPage = null;
282
- pageData = { params: {} };
283
- routes = {};
284
- pageevents = [];
285
- paused = false;
286
- async start_pageevents(url) {
287
- setTimeout(() => {
288
- for (let ci = 0;ci < this.pageevents.length; ci++) {
289
- this.pageevents[ci](url);
290
- }
291
- }, 50);
292
- }
293
- route(path, page) {
294
- if (!page) {
295
- console.error(" \u2718 Cradova err: not a valid page ", page);
296
- }
297
- return this.routes[path] = page;
298
- }
299
- async router(_e, _force) {
300
- const url = window.location.href;
301
- let route, params;
302
- if (this.paused) {
303
- window.location.hash = "paused";
304
- return;
305
- }
306
- if (this.nextRouteController) {
307
- route = this.nextRouteController;
308
- this.nextRouteController = undefined;
309
- } else {
310
- [route, params] = this.checker(url);
311
- }
312
- if (typeof route !== "undefined") {
313
- try {
314
- if (typeof route === "function") {
315
- if (this.loadingPage instanceof Page) {
316
- await this.loadingPage._activate();
317
- }
318
- route = await route();
319
- if (!route) {
320
- if (this.lastNavigatedRoute) {
321
- history.pushState({}, url, this.lastNavigatedRoute);
322
- }
323
- return;
324
- }
325
- }
326
- if (params) {
327
- this.pageData.params = params;
328
- }
329
- await route._activate();
330
- this.start_pageevents(url);
331
- this.lastNavigatedRouteController && this.lastNavigatedRouteController._deActivate();
332
- this.lastNavigatedRoute = url;
333
- this.lastNavigatedRouteController = route;
334
- } catch (error) {
335
- if (typeof this.errorHandler === "function") {
336
- this.errorHandler(error, url);
337
- } else {
338
- console.error(error);
339
- }
340
- }
341
- } else {
342
- if (this.routes["*"]) {
343
- await this.routes["*"]._activate();
344
- }
345
- }
346
- }
347
- checker(url) {
348
- if (url[0] !== "/") {
349
- url = url.slice(url.indexOf("/", 8));
350
- }
351
- if (this.routes[url]) {
352
- return [this.routes[url], { path: url }];
353
- }
354
- if (url.includes("?")) {
355
- let search;
356
- const params = {};
357
- [url, search] = url.split("?");
358
- new URLSearchParams(search).forEach((val, key) => {
359
- params[key] = val;
360
- });
361
- if (this.routes[url]) {
362
- return [this.routes[url], params];
363
- }
364
- }
365
- for (const path in this.routes) {
366
- if (path.includes(":")) {
367
- const urlFixtures = url.split("/");
368
- const pathFixtures = path.split("/");
369
- if (url.endsWith("/")) {
370
- urlFixtures.pop();
371
- }
372
- let fixturesX = 0;
373
- let fixturesY = 0;
374
- if (pathFixtures.length === urlFixtures.length) {
375
- for (let i = 0;i < pathFixtures.length; i++) {
376
- if (pathFixtures[i].includes(":")) {
377
- fixturesY++;
378
- continue;
379
- }
380
- if (urlFixtures[i] === pathFixtures[i]) {
381
- fixturesX++;
382
- }
383
- }
384
- if (fixturesX + fixturesY === pathFixtures.length) {
385
- const routesParams = {};
386
- for (let i = 0;i < pathFixtures.length; i++) {
387
- if (pathFixtures[i].includes(":")) {
388
- routesParams[pathFixtures[i].split(":")[1]] = urlFixtures[i];
389
- }
390
- }
391
- return [this.routes[path], routesParams];
392
- }
393
- }
394
- }
395
- if (path.includes("*")) {
396
- const p = path.slice(0, -1);
397
- if (url.startsWith(p)) {
398
- return [this.routes[path], { extraPath: url.slice(p.length) }];
399
- }
400
- }
401
- }
402
- return [];
403
- }
404
- }
405
- var RouterBox = new RouterBoxClass;
406
-
407
- class Router {
408
- static BrowserRoutes(obj) {
409
- for (const path in obj) {
410
- const page = obj[path];
411
- if (typeof page === "object" && typeof page.then === "function" || typeof page === "function") {
412
- RouterBox.routes[path] = async () => {
413
- const pagepp = typeof page === "function" ? await page() : await page;
414
- return RouterBox.route(path, pagepp?.default || pagepp);
415
- };
416
- } else {
417
- RouterBox.route(path, page);
418
- }
419
- }
420
- Router._mount();
421
- }
422
- static back() {
423
- history.go(-1);
424
- }
425
- static forward() {
426
- history.go(1);
427
- }
428
- static pauseNavigation() {
429
- RouterBox["paused"] = true;
430
- window.location.hash = "paused";
431
- }
432
- static resumeNavigation() {
433
- RouterBox["paused"] = false;
434
- window.location.replace(window.location.pathname + window.location.search);
435
- history.go(-1);
436
- }
437
- static navigate(href, data) {
438
- if (typeof href !== "string") {
439
- console.error(" \u2718 Cradova err: href must be a defined path but got " + href + " instead");
440
- }
441
- let route = null, params;
442
- if (href.includes(".")) {
443
- window.location.href = href;
444
- } else {
445
- [route, params] = RouterBox.checker(href);
446
- if (route) {
447
- RouterBox.nextRouteController = route;
448
- window.history.pushState({}, "", href);
449
- }
450
- RouterBox.pageData.params = params;
451
- RouterBox.pageData.data = data;
452
- RouterBox.router(null);
453
- }
454
- }
455
- static setLoadingPage(page) {
456
- if (page instanceof Page) {
457
- RouterBox.loadingPage = page;
458
- } else {
459
- throw new Error(" \u2718 Cradova err: Loading Page should be a cradova page class");
460
- }
461
- }
462
- static onPageEvent(callback) {
463
- if (typeof callback === "function") {
464
- RouterBox["pageevents"].push(callback);
465
- } else {
466
- throw new Error(" \u2718 Cradova err: callback for page events event is not a function");
467
- }
468
- }
469
- static get PageData() {
470
- return RouterBox.pageData;
471
- }
472
- static addErrorHandler(callback) {
473
- if (typeof callback === "function") {
474
- RouterBox["errorHandler"] = callback;
475
- } else {
476
- throw new Error(" \u2718 Cradova err: callback for error event is not a function");
477
- }
478
- }
479
- static _mount() {
480
- let doc = document.querySelector("[data-wrapper=app]");
481
- if (doc) {
482
- RouterBox.doc = doc;
483
- } else {
484
- throw new Error(`\u2718 Cradova err: please add '<div data-wrapper="app"></div>' to the body of your index.html file `);
485
- }
486
- window.addEventListener("pageshow", () => RouterBox.router());
487
- window.addEventListener("popstate", (_e) => {
488
- _e.preventDefault();
489
- RouterBox.router();
490
- });
491
- }
492
- }
493
- // src/primitives/functions.ts
494
- function Rhoda(l) {
495
- const fg = new DocumentFragment;
496
- for (let ch of l) {
497
- if (Array.isArray(ch)) {
498
- fg.appendChild(Rhoda(ch));
499
- } else {
500
- if (ch instanceof Comp) {
501
- ch = ch.render();
502
- }
503
- if (typeof ch === "function") {
504
- ch = ch();
505
- if (typeof ch === "function") {
506
- ch = ch();
507
- }
508
- }
509
- if (ch instanceof HTMLElement || ch instanceof DocumentFragment) {
510
- fg.appendChild(ch);
511
- continue;
512
- }
513
- if (typeof ch === "string") {
514
- fg.appendChild(document.createTextNode(ch));
515
- }
516
- }
517
- }
518
- return fg;
519
- }
520
- function $if(condition, ...elements) {
521
- if (condition) {
522
- return elements;
523
- }
524
- }
525
- function $ifelse(condition, ifTrue, ifFalse) {
526
- if (condition) {
527
- return ifTrue;
528
- }
529
- return ifFalse;
530
- }
531
- function $case(value, ...elements) {
532
- return (key) => {
533
- if (key === value) {
534
- return elements;
535
- }
536
- return;
537
- };
538
- }
539
- function $switch(key, ...cases) {
540
- let elements;
541
- if (cases.length) {
542
- for (let i = 0;i < cases.length; i++) {
543
- elements = cases[i](key);
544
- if (elements) {
545
- break;
546
- }
547
- }
548
- }
549
- return elements;
550
- }
551
- function loop(datalist, component) {
552
- return Array.isArray(datalist) ? datalist.map(component) : undefined;
553
- }
554
- function useState(newState, Comp2) {
555
- Comp2._state_index += 1;
556
- const idx = Comp2._state_index;
557
- if (idx >= Comp2._state.length) {
558
- Comp2._state[idx] = newState;
559
- }
560
- function setState(newState2) {
561
- if (typeof newState2 === "function") {
562
- newState2 = newState2(Comp2._state[idx]);
563
- }
564
- Comp2._state[idx] = newState2;
565
- Comp2.recall();
566
- }
567
- return [Comp2._state[idx], setState];
568
- }
569
- function useEffect(effect, Comp2) {
570
- Comp2._effect(effect);
571
- }
572
- function useRef() {
573
- return new __raw_ref;
574
- }
575
-
576
- class __raw_ref {
577
- tree = {};
578
- bindAs(name) {
579
- return [this, name];
580
- }
581
- elem(name) {
582
- const elem = this.tree[name];
583
- if (document.contains(elem)) {
584
- return elem;
585
- }
586
- this.tree[name] = undefined;
587
- }
588
- swap(name1, name2) {
589
- [this.tree[name1], this.tree[name2]] = [this.tree[name2], this.tree[name1]];
590
- }
591
- _append(name, Element) {
592
- this.tree[name] = Element;
593
- }
594
- }
595
- var makeElement = (element, ElementChildrenAndPropertyList) => {
596
- const props = {};
597
- let text = undefined;
598
- if (ElementChildrenAndPropertyList.length !== 0) {
599
- for (let i = 0;i < ElementChildrenAndPropertyList.length; i++) {
600
- let child = ElementChildrenAndPropertyList[i];
601
- if (typeof child === "function") {
602
- child = child();
603
- }
604
- if (child instanceof Comp) {
605
- child = child.render();
606
- }
607
- if (child instanceof HTMLElement || child instanceof DocumentFragment) {
608
- element.appendChild(child);
609
- continue;
610
- }
611
- if (Array.isArray(child)) {
612
- element.appendChild(Rhoda(child));
613
- continue;
614
- }
615
- if (typeof child === "string") {
616
- text = child;
617
- continue;
618
- }
619
- if (typeof child === "object") {
620
- Object.assign(props, child);
621
- continue;
622
- }
623
- }
624
- } else {
625
- return element;
626
- }
627
- if (typeof props === "object" && element) {
628
- for (const [prop, value] of Object.entries(props)) {
629
- if (prop === "style" && typeof value === "object") {
630
- Object.assign(element.style, value);
631
- continue;
632
- }
633
- if (prop === "href") {
634
- const href = value || "";
635
- if (!href.includes("://")) {
636
- element.addEventListener("click", (e) => {
637
- e.preventDefault();
638
- Router.navigate(element.href);
639
- if (href.includes("#")) {
640
- const l = href.split("#").at(-1);
641
- document.getElementById("#" + l)?.scrollIntoView();
642
- }
643
- });
644
- }
645
- element.setAttribute(prop, value);
646
- continue;
647
- }
648
- if (Array.isArray(value)) {
649
- if (prop == "reference" && value[0] instanceof __raw_ref) {
650
- value[0]._append(value[1], element);
651
- continue;
652
- }
653
- }
654
- if (prop === "onmount") {
655
- CradovaEvent.afterMount.push(() => {
656
- typeof props["onmount"] === "function" && props["onmount"].apply(element);
657
- });
658
- continue;
659
- }
660
- if (prop.includes("data-") || prop.includes("aria-")) {
661
- element.setAttribute(prop, value);
662
- continue;
663
- }
664
- element[prop] = value;
665
- }
666
- }
667
- if (text !== undefined) {
668
- element.appendChild(document.createTextNode(text));
669
- }
670
- return element;
671
- };
672
- var cra = (tag) => {
673
- return (...Children_and_Properties) => makeElement(document.createElement(tag), Children_and_Properties);
674
- };
675
- var frag = function(children) {
676
- const par = document.createDocumentFragment();
677
- for (let i = 0;i < children.length; i++) {
678
- let html = children[i];
679
- if (typeof html === "function") {
680
- html = html();
681
- }
682
- if (html instanceof HTMLElement || html instanceof DocumentFragment) {
683
- par.appendChild(html);
684
- continue;
685
- }
686
- if (html instanceof String) {
687
- par.appendChild(document.createTextNode(html));
688
- continue;
689
- }
690
- console.error(" \u2718 Cradova err: wrong element type" + html);
691
- }
692
- return par;
693
- };
694
- // src/primitives/dom-objects.ts
695
- var a = cra("a");
696
- var audio = cra("audio");
697
- var br = cra("br");
698
- var button = cra("button");
699
- var canvas = cra("canvas");
700
- var caption = cra("caption");
701
- var datalist = cra("datalist");
702
- var details = cra("details");
703
- var dialog = cra("dialog");
704
- var div = cra("div");
705
- var footer = cra("footer");
706
- var form = cra("form");
707
- var h1 = cra("h1");
708
- var h2 = cra("h2");
709
- var h3 = cra("h3");
710
- var h4 = cra("h4");
711
- var h5 = cra("h5");
712
- var h6 = cra("h6");
713
- var head = cra("head");
714
- var header = cra("header");
715
- var hr = cra("hr");
716
- var i = cra("i");
717
- var iframe = cra("iframe");
718
- var img = cra("img");
719
- var input = cra("input");
720
- var label = cra("label");
721
- var li = cra("li");
722
- var main = cra("main");
723
- var nav = cra("nav");
724
- var ol = cra("ol");
725
- var optgroup = cra("optgroup");
726
- var option = cra("option");
727
- var p = cra("p");
728
- var progress = cra("progress");
729
- var q = cra("q");
730
- var section = cra("section");
731
- var select = cra("select");
732
- var source = cra("source");
733
- var span = cra("span");
734
- var strong = cra("strong");
735
- var summary = cra("summary");
736
- var table = cra("table");
737
- var tbody = cra("tbody");
738
- var td = cra("td");
739
- var textarea = cra("textarea");
740
- var th = cra("th");
741
- var title = cra("title");
742
- var tr = cra("tr");
743
- var u = cra("u");
744
- var ul = cra("ul");
745
- var video = cra("video");
746
- var svg = (svg2, properties) => {
747
- const span2 = document.createElement("span");
748
- span2.innerHTML = svg2;
749
- return makeElement(span2, properties || []);
750
- };
751
- var raw = (html) => {
752
- const div2 = document.createElement("div");
753
- if (Array.isArray(html)) {
754
- div2.innerHTML = html[0];
755
- } else {
756
- if (typeof html === "string") {
757
- div2.innerHTML = html;
758
- }
759
- }
760
- const df = new DocumentFragment;
761
- df.append(...Array.from(div2.children));
762
- return df;
763
- };
764
- export {
765
- video,
766
- useState,
767
- useRef,
768
- useEffect,
769
- ul,
770
- u,
771
- tr,
772
- title,
773
- th,
774
- textarea,
775
- td,
776
- tbody,
777
- table,
778
- svg,
779
- summary,
780
- strong,
781
- span,
782
- source,
783
- select,
784
- section,
785
- raw,
786
- q,
787
- progress,
788
- p,
789
- option,
790
- optgroup,
791
- ol,
792
- nav,
793
- makeElement,
794
- main,
795
- loop,
796
- li,
797
- label,
798
- input,
799
- img,
800
- iframe,
801
- i,
802
- hr,
803
- header,
804
- head,
805
- h6,
806
- h5,
807
- h4,
808
- h3,
809
- h2,
810
- h1,
811
- frag,
812
- form,
813
- footer,
814
- div,
815
- dialog,
816
- details,
817
- datalist,
818
- cra,
819
- caption,
820
- canvas,
821
- button,
822
- br,
823
- audio,
824
- a,
825
- Signal,
826
- Router,
827
- Rhoda,
828
- Page,
829
- CradovaEvent,
830
- Comp,
831
- $switch,
832
- $ifelse,
833
- $if,
834
- $case
835
- };
1
+ class U{static compId=0;afterMount=[];beforeMountActive=[];afterDeactivate=[];dispatchEvent(H){const L=this[H];if(H.includes("Active")){for(let T=0;T<L.length;T++)L[T]();return}while(L.length!==0)L.shift()()}}var J=new U;class G{id;component;effects=[];effectuate=null;rendered=!1;published=!1;preRendered=null;reference=null;subData=null;_state=[];_state_index=0;constructor(H){this.component=H.bind(this),U.compId+=1,this.id=U.compId}preRender(){this.preRendered=this.render()}render(){if(this.effects=[],this.rendered=!1,!this.preRendered){this._state_index=0,this._state=[];const H=this.component();if(H instanceof HTMLElement)this.reference=H,this.effector.apply(this),this.rendered=!0,this.published=!0;else console.error(" \u2718 Cradova err : Invalid html content, got - "+H);return H}else return this.preRendered}_effect(H){if(!this.rendered)this.effects.push(H.bind(this))}async effector(){for(let H=0;H<this.effects.length;H++){const L=await this.effects[H].apply(this);if(typeof L==="function")J.afterDeactivate.push(L)}if(this.effects=[],this.effectuate)this.effectuate(),this.effectuate=null}recall(){if(!this.rendered)this.effectuate=()=>{if(this.published)this.activate()};else if(this.published)setTimeout(()=>{this.activate()})}async activate(){if(this.published=!1,!this.rendered)return;this._state_index=0;const H=this.reference;if(document.contains(H)){const L=this.component();if(L instanceof HTMLElement)H.insertAdjacentElement("beforebegin",L),H.remove(),this.published=!0,this.reference=L,J.dispatchEvent("afterMount");else console.error(" \u2718 Cradova err : Invalid html, got - "+L)}else this.reference=null,this.rendered=!1}}class Z{pn;subs;pipe;constructor(H,L){if(this.pipe=H,this.subs={},L&&L.persistName){this.pn=L.persistName;const T=localStorage.getItem(L.persistName);if(T&&T!=="undefined")this.pipe=JSON.parse(T);if(typeof H==="object"){for(let M in H)if(!Object.prototype.hasOwnProperty.call(this.pipe,M))this.pipe[M]=H[M]}}}publish(H,L){this.pipe[H]=L;const T=this.subs[H]||[];for(let M=0;M<T.length;M++){const E=T[M];E.subData=L,E.recall()}if(this.pn)localStorage.setItem(this.pn,JSON.stringify(this.pipe))}subscribe(H,L){if(L instanceof G){if(this.subs[H]?.find((T)=>T.id===L.id))return;if(!this.subs[H])this.subs[H]=[L];else this.subs[H].push(L);if(this.pipe[H])L.subData=this.pipe[H]}}clearPersist(){if(this.pn)localStorage.removeItem(this.pn)}}class q{_name;_html;_template=document.createElement("div");_dropped=!1;_snapshot;_snapshot_html;_deCB;constructor(H){const{template:L,name:T}=H;this._html=L,this._name=T||"Document",this._template.setAttribute("id","page"),this._snapshot=H.snapshotIsolation||!1}async _takeSnapShot(){if(A.doc.dataset.snapshot==="true")return;try{const H=await fetch(location.href);if(!H.ok)throw new Error("Failed to fetch the page");const L=await H.text(),M=new DOMParser().parseFromString(L,"text/html");M.title=this._name;const E=M.querySelector('[data-wrapper="app"]');if(E)E.setAttribute("data-snapshot","true"),E.innerHTML=this._snapshot_html;else{console.error("Wrapper or template is not found");return}const I=M.documentElement.outerHTML;await fetch(`${location.origin}`,{body:I,method:"POST",headers:{"Content-Type":"text/html","cradova-snapshot":location.href}})}catch(H){console.error("Snapshot error:",H)}this._snapshot_html=void 0}set onDeactivate(H){this._deCB=H}drop(H){if(typeof H==="boolean"){this._dropped=H;return}else return this._dropped}async _activate(){if(this._dropped){history.go(-1);return}let H=this._html.apply(this);if(H instanceof HTMLElement)this._template.innerHTML="",this._template.appendChild(H);else throw new Error(` \u2718 Cradova err: template function for the page returned ${H} instead of html`);if(document.title=this._name,A.doc.innerHTML="",J.dispatchEvent("beforeMountActive"),this._snapshot)this._snapshot_html=this._template.outerHTML;if(A.doc.appendChild(this._template),J.dispatchEvent("afterMount"),window.scrollTo({top:0,left:0,behavior:"instant"}),J.dispatchEvent("afterDeactivate"),this._snapshot)this._takeSnapShot()}}class Q{doc=null;lastNavigatedRouteController;nextRouteController;lastNavigatedRoute;pageShow=null;pageHide=null;errorHandler;loadingPage=null;pageData={params:{}};routes={};paused=!1;route(H,L){if(!L)console.error(" \u2718 Cradova err: not a valid page ",L);return this.routes[H]=L}async router(H,L){const T=window.location.href;let M,E;if(this.paused){window.location.hash="paused";return}if(this.nextRouteController)M=this.nextRouteController,this.nextRouteController=void 0;else[M,E]=this.checker(T);if(typeof M!=="undefined")try{if(typeof M==="function"){if(this.loadingPage instanceof q)await this.loadingPage._activate();if(M=await M(),!M){if(this.lastNavigatedRoute)history.pushState({},T,this.lastNavigatedRoute);return}}if(E)this.pageData.params=E;await M._activate(),this.lastNavigatedRouteController&&this.lastNavigatedRouteController._deCB?.(),this.lastNavigatedRoute=T,this.lastNavigatedRouteController=M}catch(I){if(typeof this.errorHandler==="function")this.errorHandler(I,T);else console.error(I)}else if(this.routes["*"])await this.routes["*"]._activate()}checker(H){if(H[0]!=="/")H=H.slice(H.indexOf("/",8));if(this.routes[H])return[this.routes[H],{path:H}];if(H.includes("?")){let L;const T={};if([H,L]=H.split("?"),new URLSearchParams(L).forEach((M,E)=>{T[E]=M}),this.routes[H])return[this.routes[H],T]}for(let L in this.routes){if(L.includes(":")){const T=H.split("/"),M=L.split("/");if(H.endsWith("/"))T.pop();let E=0,I=0;if(M.length===T.length){for(let D=0;D<M.length;D++){if(M[D].includes(":")){I++;continue}if(T[D]===M[D])E++}if(E+I===M.length){const D={};for(let O=0;O<M.length;O++)if(M[O].includes(":"))D[M[O].split(":")[1]]=T[O];return[this.routes[L],D]}}}if(L.includes("*")){const T=L.slice(0,-1);if(H.startsWith(T))return[this.routes[L],{extraPath:H.slice(T.length)}]}}return[]}}var A=new Q;class Y{static BrowserRoutes(H){for(let L in H){const T=H[L];if(typeof T==="object"&&typeof T.then==="function"||typeof T==="function")A.routes[L]=async()=>{const M=typeof T==="function"?await T():await T;return A.route(L,M)};else A.route(L,T)}Y._mount()}static pauseNavigation(){A.paused=!0,window.location.hash="paused"}static resumeNavigation(){A.paused=!1,window.location.replace(window.location.pathname+window.location.search),history.go(-1)}static navigate(H,L){if(typeof H!=="string")console.error(" \u2718 Cradova err: href must be a defined path but got "+H+" instead");let T=null,M;if(H.includes("."))window.location.href=H;else{if([T,M]=A.checker(H),T)A.nextRouteController=T,window.history.pushState({},"",H);A.pageData.params=M,A.pageData.data=L,A.router(null)}}static setLoadingPage(H){if(H instanceof q)A.loadingPage=H;else throw new Error(" \u2718 Cradova err: Loading Page should be a cradova page class")}static get PageData(){return A.pageData}static addErrorHandler(H){if(typeof H==="function")A.errorHandler=H;else throw new Error(" \u2718 Cradova err: callback for error event is not a function")}static _mount(){let H=document.querySelector("[data-wrapper=app]");if(H)A.doc=H;else throw new Error(`\u2718 Cradova err: please add '<div data-wrapper="app"></div>' to the body of your index.html file `);window.addEventListener("pageshow",()=>A.router()),window.addEventListener("popstate",(L)=>{L.preventDefault(),A.router()})}}function S(H){const L=new DocumentFragment;for(let T of H)if(Array.isArray(T))L.appendChild(S(T));else{if(T instanceof G)T=T.render();if(typeof T==="function"){if(T=T(),typeof T==="function")T=T()}if(T instanceof HTMLElement||T instanceof DocumentFragment){L.appendChild(T);continue}if(typeof T==="string")L.appendChild(document.createTextNode(T))}return L}function x(H,...L){if(H)return L}function j(H,L,T){if(H)return L;return T}function y(H,...L){return(T)=>{if(T===H)return L;return}}function g(H,...L){let T;if(L.length){for(let M=0;M<L.length;M++)if(T=L[M](H),T)break}return T}function b(H,L){return Array.isArray(H)?H.map(L):void 0}function w(H,L){L._state_index+=1;const T=L._state_index;if(T>=L._state.length)L._state[T]=H;function M(E){if(typeof E==="function")E=E(L._state[T]);L._state[T]=E,L.recall()}return[L._state[T],M]}function _(H,L){L._effect(H)}function P(){return new z}class z{tree={};bindAs(H){return[this,H]}elem(H){const L=this.tree[H];if(document.contains(L))return L;this.tree[H]=void 0}swap(H,L){[this.tree[H],this.tree[L]]=[this.tree[L],this.tree[H]]}_append(H,L){this.tree[H]=L}}var K=(H,L)=>{const T={};let M=void 0;if(L.length!==0)for(let E=0;E<L.length;E++){let I=L[E];if(typeof I==="function")I=I();if(I instanceof G)I=I.render();if(I instanceof HTMLElement||I instanceof DocumentFragment){H.appendChild(I);continue}if(Array.isArray(I)){H.appendChild(S(I));continue}if(typeof I==="string"){M=I;continue}if(typeof I==="object"){Object.assign(T,I);continue}}else return H;if(typeof T==="object"&&H)for(let[E,I]of Object.entries(T)){if(E==="style"&&typeof I==="object"){Object.assign(H.style,I);continue}if(E==="href"){const D=I||"";if(!D.includes("://"))H.addEventListener("click",(O)=>{if(O.preventDefault(),Y.navigate(H.href),D.includes("#")){const W=D.split("#").at(-1);document.getElementById("#"+W)?.scrollIntoView()}});H.setAttribute(E,I);continue}if(E==="onmount"){J.afterMount.push(()=>{typeof T.onmount==="function"&&T.onmount.apply(H)});continue}if(E.includes("data-")||E.includes("aria-")){H.setAttribute(E,I);continue}if(Array.isArray(I)){if(E=="ref"&&I[0]instanceof z){I[0]._append(I[1],H);continue}}H[E]=I}if(M!==void 0)H.appendChild(document.createTextNode(M));return H},V=(H)=>{return(...L)=>K(document.createElement(H),L)},F=function(H){const L=document.createDocumentFragment();for(let T=0;T<H.length;T++){let M=H[T];if(typeof M==="function")M=M();if(M instanceof HTMLElement||M instanceof DocumentFragment){L.appendChild(M);continue}if(M instanceof String){L.appendChild(document.createTextNode(M));continue}console.error(" \u2718 Cradova err: wrong element type"+M)}return L};var N=V("a"),B=V("audio"),c=V("button"),f=V("canvas"),R=V("div"),s=V("footer"),C=V("form"),o=V("h1"),v=V("h2"),d=V("h3"),i=V("h4"),h=V("h5"),p=V("h6"),u=V("header"),m=V("i"),t=V("iframe"),l=V("img"),r=V("input"),a=V("label"),e=V("li"),HH=V("main"),LH=V("nav"),TH=V("ol"),MH=V("optgroup"),EH=V("option"),IH=V("p"),VH=V("section"),AH=V("select"),DH=V("source"),OH=V("span"),JH=V("textarea"),GH=V("ul"),UH=V("video"),YH=(H,L)=>{const T=document.createElement("span");return T.innerHTML=H,K(T,L||[])},qH=(H)=>{const L=document.createElement("div");if(Array.isArray(H))L.innerHTML=H[0];else if(typeof H==="string")L.innerHTML=H;const T=new DocumentFragment;return T.append(...Array.from(L.children)),T};export{UH as video,w as useState,P as useRef,_ as useEffect,GH as ul,JH as textarea,YH as svg,OH as span,DH as source,AH as select,VH as section,qH as raw,IH as p,EH as option,MH as optgroup,TH as ol,LH as nav,K as makeElement,HH as main,b as loop,e as li,a as label,r as input,l as img,t as iframe,m as i,u as header,p as h6,h as h5,i as h4,d as h3,v as h2,o as h1,F as frag,C as form,s as footer,R as div,V as cra,f as canvas,c as button,B as audio,N as a,Z as Signal,Y as Router,S as Rhoda,q as Page,J as CradovaEvent,G as Comp,g as $switch,j as $ifelse,x as $if,y as $case};