cradova 3.1.0 → 3.1.2

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 ADDED
@@ -0,0 +1,1358 @@
1
+
2
+ /*
3
+ ============================================================================="
4
+ ██████╗ ██████╗ █████═╗ ███████╗ ███████╗ ██╗ ██╗ █████╗
5
+ ██╔════╝ ██╔══██╗ ██╔═╗██║ █ ██ ██╔═════╝█ ██║ ██║ ██╔═╗██
6
+ ██║ ██████╔╝ ███████║ █ ██ ██║ ██ ██║ ██║ ██████╗
7
+ ██║ ██╔══██╗ ██║ ██║ █ ██ ██║ ██ ╚██╗ ██╔╝ ██║ ██╗
8
+ ╚██████╗ ██║ ██║ ██║ ██║ ███████╔╝ ████████ ╚███╔╝ ██║ ██║
9
+ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚════╝ ╚══╝ ╚═╝ ╚═╝
10
+ =============================================================================
11
+ Cradova FrameWork
12
+ @version 3.0.0
13
+ License: Apache V2
14
+ Copyright 2022 Friday Candour.
15
+ Repository - https://github.com/fridaycandour/cradova
16
+ =============================================================================
17
+ */
18
+ // lib/parts/fns.ts
19
+ var isNode = (element) => element instanceof HTMLElement || element instanceof DocumentFragment;
20
+ var cradovaEvent = class {
21
+ constructor() {
22
+ this.listeners = {};
23
+ }
24
+ addEventListener(eventName, callback) {
25
+ if (!this.listeners[eventName]) {
26
+ this.listeners[eventName] = [];
27
+ }
28
+ this.listeners[eventName].push(callback);
29
+ }
30
+ // removeEventListener(eventName: string, num: number) {
31
+ // this.listeners[eventName].splice(num, 1);
32
+ // }
33
+ dispatchEvent(eventName, eventArgs) {
34
+ const eventListeners = this.listeners[eventName] || [];
35
+ for (; eventListeners.length !== 0; ) {
36
+ eventListeners.shift()?.call(void 0, eventArgs);
37
+ }
38
+ }
39
+ };
40
+ var CradovaEvent = new cradovaEvent();
41
+ function Rhoda(l) {
42
+ const fg = new DocumentFragment();
43
+ for (let ch of l) {
44
+ if (Array.isArray(ch)) {
45
+ fg.appendChild(Rhoda(ch));
46
+ } else {
47
+ if (ch instanceof Ref) {
48
+ ch = ch.render(void 0);
49
+ }
50
+ if (typeof ch === "function") {
51
+ ch = ch();
52
+ if (typeof ch === "function") {
53
+ ch = ch();
54
+ }
55
+ }
56
+ if (typeof ch === "string" || typeof ch === "number") {
57
+ fg.appendChild(document.createTextNode(ch));
58
+ continue;
59
+ }
60
+ if (isNode(ch)) {
61
+ fg.appendChild(ch);
62
+ } else {
63
+ if (typeof ch !== "undefined") {
64
+ throw new Error(
65
+ " \u2718 Cradova err: invalid child type: " + ch + " (" + typeof ch + ")"
66
+ );
67
+ }
68
+ }
69
+ }
70
+ }
71
+ return fg;
72
+ }
73
+ function css(identifier) {
74
+ if (Array.isArray(identifier)) {
75
+ identifier = identifier[0];
76
+ }
77
+ if (typeof identifier === "string") {
78
+ let styTag = document.querySelector("style");
79
+ if (styTag !== null) {
80
+ styTag.textContent = identifier + styTag.textContent;
81
+ return;
82
+ }
83
+ styTag = document.createElement("style");
84
+ styTag.textContent = identifier;
85
+ document.head.appendChild(styTag);
86
+ }
87
+ }
88
+ function assert(condition, ...elements) {
89
+ if (condition) {
90
+ return elements;
91
+ }
92
+ return void 0;
93
+ }
94
+ function assertOr(condition, ifTrue, ifFalse) {
95
+ if (condition) {
96
+ return ifTrue;
97
+ }
98
+ return ifFalse;
99
+ }
100
+ function loop(datalist2, component) {
101
+ if (typeof component !== "function") {
102
+ throw new Error(
103
+ " \u2718 Cradova err : Invalid component type, must be a function that returns html "
104
+ );
105
+ }
106
+ return Array.isArray(datalist2) ? datalist2.map(component) : void 0;
107
+ }
108
+ var Ref = class {
109
+ constructor(component) {
110
+ this.effects = [];
111
+ this.effectuate = null;
112
+ this.rendered = false;
113
+ this.published = false;
114
+ this.preRendered = null;
115
+ this.reference = new reference();
116
+ // hooks management
117
+ this._state = [];
118
+ this._state_track = {};
119
+ this._state_index = 0;
120
+ this.component = component.bind(this);
121
+ }
122
+ preRender(data2) {
123
+ let html = this.component(data2);
124
+ if (typeof html === "function") {
125
+ html = html();
126
+ }
127
+ if (isNode(html)) {
128
+ this.preRendered = html;
129
+ }
130
+ if (!this.preRendered) {
131
+ throw new Error(
132
+ " \u2718 Cradova err : Invalid component type for cradova Ref got - " + this.preRendered
133
+ );
134
+ }
135
+ this.reference._appendDomForce("html", this.preRendered);
136
+ }
137
+ destroyPreRendered() {
138
+ this.preRendered = null;
139
+ }
140
+ /**
141
+ * Cradova Ref
142
+ * ---
143
+ * returns html with cradova reference
144
+ * @param data
145
+ * @returns () => HTMLElement
146
+ */
147
+ render(data2, stash) {
148
+ this.effects = [];
149
+ this.rendered = false;
150
+ let html = this.component(data2);
151
+ if (typeof html === "function") {
152
+ html = html();
153
+ }
154
+ if (!html) {
155
+ html = this.preRendered;
156
+ }
157
+ if (stash) {
158
+ this.stash = data2;
159
+ }
160
+ if (isNode(html)) {
161
+ this.reference._appendDomForce("html", html);
162
+ this.effector.apply(this);
163
+ this.rendered = true;
164
+ this.published = true;
165
+ } else {
166
+ console.error(" \u2718 Cradova err : Invalid html content, got - " + html);
167
+ }
168
+ return html;
169
+ }
170
+ instance() {
171
+ return this.reference.html;
172
+ }
173
+ _setExtra(Extra) {
174
+ this.Signal = Extra;
175
+ }
176
+ _roll_state(data2, idx, get = false) {
177
+ if (!get) {
178
+ this._state[idx] = data2;
179
+ }
180
+ return this._state[idx];
181
+ }
182
+ _effect(fn) {
183
+ if (!this.rendered) {
184
+ this.effects.push(fn.bind(this));
185
+ }
186
+ }
187
+ async effector() {
188
+ if (!this.rendered) {
189
+ for (let i2 = 0; i2 < this.effects.length; i2++) {
190
+ await this.effects[i2].apply(this);
191
+ }
192
+ this.effects = [];
193
+ }
194
+ if (this.effectuate) {
195
+ this.effectuate();
196
+ this.effectuate = null;
197
+ }
198
+ }
199
+ /**
200
+ * Cradova Ref
201
+ * ---
202
+ * update ref component with new data and update the dom.
203
+ * @param data
204
+ * @returns
205
+ */
206
+ updateState(data2, stash) {
207
+ if (!this.rendered) {
208
+ this.effectuate = () => {
209
+ if (this.published) {
210
+ this.Activate(data2);
211
+ }
212
+ };
213
+ } else {
214
+ if (this.published) {
215
+ this.Activate(data2);
216
+ }
217
+ }
218
+ if (stash) {
219
+ this.stash = data2;
220
+ }
221
+ }
222
+ async Activate(data2) {
223
+ this._state_index = 0;
224
+ this.published = false;
225
+ if (!this.rendered) {
226
+ return;
227
+ }
228
+ let html = this.component(data2);
229
+ if (typeof html === "function") {
230
+ html = html();
231
+ }
232
+ if (isNode(html)) {
233
+ this.reference.html.insertAdjacentElement("beforebegin", html);
234
+ this.reference.html.remove();
235
+ this.published = true;
236
+ this.reference._appendDomForce("html", html);
237
+ CradovaEvent.dispatchEvent("onmountEvent");
238
+ } else {
239
+ console.error(" \u2718 Cradova err : Invalid html content, got - " + html);
240
+ }
241
+ }
242
+ };
243
+ var frag = function(children) {
244
+ const par = document.createDocumentFragment();
245
+ for (let i2 = 0; i2 < children.length; i2++) {
246
+ let a2 = children[i2];
247
+ if (typeof a2 === "function") {
248
+ a2 = a2();
249
+ }
250
+ if (isNode(a2)) {
251
+ par.appendChild(a2);
252
+ continue;
253
+ }
254
+ if (a2 instanceof String) {
255
+ par.appendChild(document.createTextNode(a2));
256
+ continue;
257
+ }
258
+ console.error(" \u2718 Cradova err: wrong element type" + a2);
259
+ throw new TypeError(" \u2718 Cradova err: invalid element");
260
+ }
261
+ return par;
262
+ };
263
+ var lazy = class {
264
+ constructor(cb) {
265
+ this._cb = cb;
266
+ }
267
+ async load() {
268
+ let content = await this._cb();
269
+ if (typeof content === "function") {
270
+ content = await content();
271
+ } else {
272
+ content = await content;
273
+ }
274
+ const def = content;
275
+ if (def.default) {
276
+ this.content = def?.default;
277
+ }
278
+ }
279
+ };
280
+ var reference = class {
281
+ bindAs(name) {
282
+ return [this, name];
283
+ }
284
+ _appendDomForce(name, Element) {
285
+ this[name] = Element;
286
+ }
287
+ };
288
+ function useState(initialValue, ActiveRef) {
289
+ ActiveRef._state_index += 1;
290
+ const idx = ActiveRef._state_index;
291
+ if (!ActiveRef._state_track[idx]) {
292
+ ActiveRef._roll_state(initialValue, idx);
293
+ ActiveRef._state_track[idx] = true;
294
+ }
295
+ function setState(newState) {
296
+ ActiveRef._roll_state(newState, idx);
297
+ ActiveRef.updateState(newState);
298
+ }
299
+ return [ActiveRef._roll_state(null, idx, true), setState];
300
+ }
301
+ function useEffect(effect, ActiveRef) {
302
+ ActiveRef._effect(effect);
303
+ }
304
+ function useRef() {
305
+ return new reference();
306
+ }
307
+
308
+ // lib/parts/track.ts
309
+ function dispatch(element, state) {
310
+ if (!element) {
311
+ return;
312
+ }
313
+ if (typeof state === "object" && !Array.isArray(state)) {
314
+ for (const key in state) {
315
+ if (key === "style" && typeof state[key] === "object") {
316
+ for (const [k, v] of Object.entries(state[key])) {
317
+ if (typeof element.style[k] !== "undefined" && k !== "src" && typeof v === "string") {
318
+ element.style[k] = v;
319
+ } else {
320
+ throw new Error(
321
+ "\u2718 Cradova err : " + k + " is not a valid css style property"
322
+ );
323
+ }
324
+ }
325
+ continue;
326
+ }
327
+ const stateK = state;
328
+ if (typeof element[key] === "function") {
329
+ if (key.startsWith("on")) {
330
+ element[key] = stateK[key];
331
+ } else {
332
+ element[key].apply(element);
333
+ }
334
+ continue;
335
+ }
336
+ if (key === "text") {
337
+ element.innerText = stateK[key];
338
+ continue;
339
+ }
340
+ if (key === "tree") {
341
+ element.innerHTML = "";
342
+ element.appendChild(frag([stateK[key]]));
343
+ continue;
344
+ }
345
+ if (key.includes("data-")) {
346
+ element.setAttribute(key, stateK[key]);
347
+ continue;
348
+ }
349
+ element[key] = stateK[key];
350
+ }
351
+ }
352
+ if (typeof state === "string") {
353
+ element.innerText = state;
354
+ return;
355
+ }
356
+ if (typeof state === "function") {
357
+ element.appendChild(frag([state]));
358
+ return;
359
+ }
360
+ if (state instanceof HTMLElement) {
361
+ element.appendChild(state);
362
+ }
363
+ if (!(typeof state === "object" && !Array.isArray(state))) {
364
+ throw new Error(
365
+ " \u2718 Cradova err: Cradova got invalid state => \n" + String(state)
366
+ );
367
+ }
368
+ }
369
+
370
+ // lib/parts/createSignal.ts
371
+ var createSignal = class {
372
+ constructor(initial, props) {
373
+ this.persistName = "";
374
+ this.actions = {};
375
+ this.ref = [];
376
+ this.value = initial;
377
+ if (props && props.persistName) {
378
+ this.persistName = props.persistName;
379
+ const key = localStorage.getItem(props.persistName);
380
+ if (key && key !== "undefined") {
381
+ this.value = JSON.parse(key);
382
+ }
383
+ if (typeof initial === "object") {
384
+ for (const key2 in initial) {
385
+ if (!Object.prototype.hasOwnProperty.call(this.value, key2)) {
386
+ this.value[key2] = initial[key2];
387
+ }
388
+ }
389
+ }
390
+ }
391
+ }
392
+ /**
393
+ * Cradova Signal
394
+ * ----
395
+ * set signal value
396
+ * @param value - signal value
397
+ * @returns void
398
+ */
399
+ set(value, shouldRefRender) {
400
+ if (typeof value === "function") {
401
+ this.value = value(this.value);
402
+ } else {
403
+ this.value = value;
404
+ }
405
+ if (this.persistName) {
406
+ localStorage.setItem(this.persistName, JSON.stringify(this.value));
407
+ }
408
+ if (this.ref.length && shouldRefRender !== false) {
409
+ this._updateState();
410
+ }
411
+ if (this.callback) {
412
+ this.callback(this.value);
413
+ }
414
+ }
415
+ /**
416
+ * Cradova Signal
417
+ * ----
418
+ * set a key value if it's an object
419
+ * @param key - key of the key
420
+ * @param value - value of the key
421
+ * @returns void
422
+ */
423
+ setKey(key, value, shouldRefRender) {
424
+ if (typeof this.value === "object" && !Array.isArray(this.value)) {
425
+ this.value[key] = value;
426
+ if (this.persistName) {
427
+ localStorage.setItem(this.persistName, JSON.stringify(this.value));
428
+ }
429
+ if (this.ref.length && shouldRefRender !== false) {
430
+ this._updateState();
431
+ }
432
+ if (this.callback) {
433
+ this.callback(this.value);
434
+ }
435
+ } else {
436
+ throw new Error(
437
+ `\u2718 Cradova err : can't set key ${String(
438
+ key
439
+ )} . store.value is not a javascript object`
440
+ );
441
+ }
442
+ }
443
+ /**
444
+ * Cradova Signal
445
+ * ----
446
+ * set a key to signal an action
447
+ * @param name - name of the action
448
+ * @param action function to execute
449
+ */
450
+ createAction(name, action) {
451
+ if (typeof name === "string" && typeof action === "function") {
452
+ this.actions[name] = action;
453
+ } else {
454
+ throw new Error(
455
+ `\u2718 Cradova err : can't create action, ${name} is not a function`
456
+ );
457
+ }
458
+ }
459
+ /**
460
+ * Cradova Signal
461
+ * ----
462
+ * creates man y actions at a time
463
+ * @param name - name of the action
464
+ * @param action function to execute
465
+ */
466
+ createActions(Actions) {
467
+ for (const [name, action] of Object.entries(Actions)) {
468
+ if (typeof name === "string" && typeof action === "function") {
469
+ this.actions[name] = action;
470
+ } else {
471
+ throw new Error(
472
+ `\u2718 Cradova err : can't create action, ${name} is not a function`
473
+ );
474
+ }
475
+ }
476
+ }
477
+ /**
478
+ * Cradova Signal
479
+ * ----
480
+ * fires an action if available
481
+ * @param key - string key of the action
482
+ * @param data - data for the action
483
+ */
484
+ fireAction(key, data2) {
485
+ this._updateState(key, data2);
486
+ if (typeof this.actions[key] === "function") {
487
+ return this.actions[key].call(this, data2);
488
+ } else {
489
+ throw Error("\u2718 Cradova err : action " + key + " does not exist!");
490
+ }
491
+ }
492
+ /**
493
+ * Cradova
494
+ * ---
495
+ * is used to bind signal data to elements and Refs
496
+ *
497
+ * @param prop
498
+ * @returns something
499
+ */
500
+ bind(prop) {
501
+ if (typeof this.value === "object" && typeof this.value[prop] !== "undefined") {
502
+ return [this, prop];
503
+ } else {
504
+ throw new Error(
505
+ "\u2718 Cradova err : can't bind an unavailable property! " + prop
506
+ );
507
+ }
508
+ }
509
+ _updateState(name, data2) {
510
+ if (name && data2) {
511
+ this.ref.map((ent) => {
512
+ if (ent._event === name) {
513
+ if (ent._element_property && ent._signalProperty) {
514
+ ent.ref?.updateState({
515
+ [ent._element_property]: data2[ent._signalProperty]
516
+ });
517
+ return;
518
+ }
519
+ if (ent._element_property) {
520
+ ent.ref.updateState({
521
+ [ent._element_property]: data2
522
+ });
523
+ return;
524
+ }
525
+ if (ent._signalProperty) {
526
+ ent.ref.updateState(data2[ent._signalProperty]);
527
+ return;
528
+ }
529
+ }
530
+ });
531
+ } else {
532
+ for (let i2 = 0; i2 < this.ref.length; i2++) {
533
+ const ent = this.ref[i2];
534
+ if (ent._element_property && ent._signalProperty) {
535
+ ent.ref.updateState({
536
+ [ent._element_property]: this.value[ent._signalProperty]
537
+ });
538
+ continue;
539
+ }
540
+ if (ent._element_property) {
541
+ ent.ref.updateState({
542
+ [ent._element_property]: this.value
543
+ });
544
+ continue;
545
+ }
546
+ if (ent._signalProperty) {
547
+ ent.ref.updateState(this.value[ent._signalProperty]);
548
+ continue;
549
+ }
550
+ if (!ent._element_property && !ent._signalProperty) {
551
+ ent.ref.updateState(this.value);
552
+ continue;
553
+ }
554
+ }
555
+ }
556
+ }
557
+ /**
558
+ * Cradova Signal
559
+ * ----
560
+ * set a auto - rendering component for this store
561
+ *
562
+ * @param Ref component to bind to.
563
+ * @param path a property in the object to send to attached ref
564
+ */
565
+ bindRef(ref, binding = { signalProperty: "", _element_property: "" }) {
566
+ if (ref.render) {
567
+ ref.render = ref.render.bind(ref, this.value);
568
+ }
569
+ if (ref._setExtra) {
570
+ ref._setExtra(this);
571
+ }
572
+ if (ref && ref.updateState) {
573
+ this.ref.push({
574
+ ref,
575
+ _signalProperty: binding.signalProperty,
576
+ _element_property: binding._element_property,
577
+ _event: binding.event
578
+ });
579
+ return;
580
+ }
581
+ throw new Error(
582
+ "\u2718 Cradova err : Invalid parameters for binding ref to Signal"
583
+ );
584
+ }
585
+ /**
586
+ * Cradova Signal
587
+ * ----
588
+ * set a update listener on value changes
589
+ * @param callback
590
+ */
591
+ listen(callback) {
592
+ this.callback = callback;
593
+ }
594
+ /**
595
+ * Cradova Signal
596
+ * ----
597
+ * clear the history on local storage
598
+ *
599
+ */
600
+ clearPersist() {
601
+ if (this.persistName) {
602
+ localStorage.removeItem(this.persistName);
603
+ }
604
+ }
605
+ };
606
+
607
+ // lib/parts/Screen.ts
608
+ var localTree = new reference();
609
+ var Screen = class {
610
+ constructor(cradova_screen_initials) {
611
+ /**
612
+ * this is a set of added html to the screen
613
+ */
614
+ this._secondaryChildren = [];
615
+ /**
616
+ * error handler for the screen
617
+ */
618
+ this._errorHandler = null;
619
+ this._packed = false;
620
+ this._template = document.createElement("div");
621
+ this._persist = true;
622
+ this._delegatedRoutesCount = -1;
623
+ const { template: template2, name, persist, renderInParallel, transition } = cradova_screen_initials;
624
+ this._html = template2;
625
+ this._name = name;
626
+ this._transition = transition;
627
+ this._template.setAttribute("id", "cradova-screen-set");
628
+ if (renderInParallel === true) {
629
+ this._delegatedRoutesCount = 0;
630
+ this._persist = false;
631
+ } else {
632
+ if (typeof persist === "boolean") {
633
+ this._persist = persist;
634
+ }
635
+ }
636
+ }
637
+ get _delegatedRoutes() {
638
+ if (this._delegatedRoutesCount > 100) {
639
+ return -1;
640
+ }
641
+ return this._delegatedRoutesCount;
642
+ }
643
+ set _delegatedRoutes(count) {
644
+ if (count) {
645
+ this._delegatedRoutesCount += 1;
646
+ }
647
+ }
648
+ setErrorHandler(errorHandler) {
649
+ this._errorHandler = errorHandler;
650
+ }
651
+ async _package() {
652
+ if (typeof this._html === "function") {
653
+ let fuc = await this._html.apply(this);
654
+ if (typeof fuc === "function") {
655
+ fuc = fuc();
656
+ if (isNode(fuc)) {
657
+ this._template.innerHTML = "";
658
+ this._template.appendChild(fuc);
659
+ }
660
+ } else {
661
+ if (isNode(fuc)) {
662
+ this._template.innerHTML = "";
663
+ this._template.appendChild(fuc);
664
+ } else {
665
+ throw new Error(
666
+ ` \u2718 Cradova err: template function for the screen with name '${this._name}' returned ${fuc} instead of html`
667
+ );
668
+ }
669
+ }
670
+ }
671
+ if (this._secondaryChildren.length) {
672
+ this._template.appendChild(frag(this._secondaryChildren));
673
+ }
674
+ }
675
+ onActivate(cb) {
676
+ this._callBack = cb;
677
+ }
678
+ onDeactivate(cb) {
679
+ this._deCallBack = cb;
680
+ }
681
+ addChildren(...addOns) {
682
+ this._secondaryChildren.push(...addOns);
683
+ }
684
+ async _deActivate() {
685
+ if (this._deCallBack) {
686
+ await this._deCallBack();
687
+ }
688
+ }
689
+ async _Activate(force = false) {
690
+ if (!this._persist || force || !this._packed) {
691
+ await this._package();
692
+ this._packed = true;
693
+ }
694
+ if (this._transition) {
695
+ this._template.classList.add(this._transition);
696
+ }
697
+ document.title = this._name;
698
+ localTree.doc.innerHTML = "";
699
+ localTree.doc.appendChild(this._template);
700
+ CradovaEvent.dispatchEvent("onmountEvent");
701
+ window.scrollTo({
702
+ top: 0,
703
+ left: 0,
704
+ // @ts-ignore
705
+ behavior: "instant"
706
+ });
707
+ if (this._callBack) {
708
+ await this._callBack();
709
+ }
710
+ }
711
+ };
712
+
713
+ // lib/parts/Router.ts
714
+ var RouterBox = {};
715
+ RouterBox["lastNavigatedRouteController"] = null;
716
+ RouterBox["nextRouteController"] = null;
717
+ RouterBox["lastNavigatedRoute"] = null;
718
+ RouterBox["pageShow"] = null;
719
+ RouterBox["pageHide"] = null;
720
+ RouterBox["errorHandler"] = null;
721
+ RouterBox["loadingScreen"] = null;
722
+ RouterBox["params"] = {};
723
+ RouterBox["routes"] = {};
724
+ RouterBox["pageevents"] = [];
725
+ RouterBox["start_pageevents"] = async function(url) {
726
+ setTimeout(() => {
727
+ for (let ci = 0; ci < RouterBox["pageevents"].length; ci++) {
728
+ RouterBox["pageevents"][ci](url);
729
+ }
730
+ }, 100);
731
+ };
732
+ var checker = (url) => {
733
+ if (RouterBox.routes[url]) {
734
+ return [RouterBox.routes[url], { path: url }];
735
+ }
736
+ if (RouterBox.routes[url + "/"]) {
737
+ return [RouterBox.routes[url], { path: url }];
738
+ }
739
+ for (const path in RouterBox.routes) {
740
+ if (!path.includes(":")) {
741
+ continue;
742
+ }
743
+ const urlFixtures = url.split("/");
744
+ const pathFixtures = path.split("/");
745
+ if (url.endsWith("/")) {
746
+ urlFixtures.pop();
747
+ }
748
+ let fixturesX = 0;
749
+ let fixturesY = 0;
750
+ urlFixtures.shift();
751
+ pathFixtures.shift();
752
+ if (pathFixtures.length === urlFixtures.length) {
753
+ const routesParams = { _path: "" };
754
+ for (let i2 = 0; i2 < pathFixtures.length; i2++) {
755
+ if (pathFixtures[i2].includes(":")) {
756
+ fixturesY++;
757
+ continue;
758
+ }
759
+ if (urlFixtures[i2] === pathFixtures[i2]) {
760
+ fixturesX++;
761
+ }
762
+ }
763
+ if (fixturesX + fixturesY === pathFixtures.length) {
764
+ for (let i2 = 0; i2 < pathFixtures.length; i2++) {
765
+ if (pathFixtures[i2].includes(":")) {
766
+ routesParams[pathFixtures[i2].split(":")[1]] = urlFixtures[i2];
767
+ }
768
+ }
769
+ routesParams._path = path;
770
+ return [RouterBox.routes[path], routesParams];
771
+ }
772
+ }
773
+ }
774
+ return [];
775
+ };
776
+ RouterBox.route = (path, screen) => {
777
+ if (typeof screen !== "undefined") {
778
+ if (screen && !screen._Activate) {
779
+ console.error(" \u2718 Cradova err: not a valid screen ", screen);
780
+ throw new Error(" \u2718 Cradova err: Not a valid cradova screen component");
781
+ }
782
+ return RouterBox.routes[path] = screen;
783
+ }
784
+ return void 0;
785
+ };
786
+ RouterBox.router = async function(_e, _force) {
787
+ let url, route, params;
788
+ if (!url) {
789
+ url = window.location.pathname;
790
+ }
791
+ if (url === RouterBox["lastNavigatedRoute"]) {
792
+ return;
793
+ }
794
+ if (RouterBox["nextRouteController"]) {
795
+ route = RouterBox["nextRouteController"];
796
+ RouterBox["nextRouteController"] = null;
797
+ } else {
798
+ [route, params] = checker(url);
799
+ }
800
+ if (typeof route !== "undefined") {
801
+ try {
802
+ if (typeof route === "function") {
803
+ if (RouterBox["LoadingScreen"] && RouterBox["LoadingScreen"]._Activate) {
804
+ await RouterBox["LoadingScreen"]._Activate();
805
+ }
806
+ route = await route();
807
+ if (!route) {
808
+ if (RouterBox["lastNavigatedRoute"]) {
809
+ history.pushState({}, url, RouterBox["lastNavigatedRoute"]);
810
+ }
811
+ return;
812
+ }
813
+ }
814
+ if (route._delegatedRoutes !== -1) {
815
+ route._delegatedRoutes = true;
816
+ route = new Screen({
817
+ name: route._name,
818
+ template: route._html
819
+ });
820
+ RouterBox.routes[url] = route;
821
+ }
822
+ if (params) {
823
+ RouterBox.params.params = params;
824
+ }
825
+ await route._Activate(_force);
826
+ RouterBox["start_pageevents"](url);
827
+ RouterBox["lastNavigatedRouteController"] && RouterBox["lastNavigatedRouteController"]._deActivate();
828
+ RouterBox["lastNavigatedRoute"] = url;
829
+ RouterBox["lastNavigatedRouteController"] = route;
830
+ } catch (error) {
831
+ if (route && route["_errorHandler"]) {
832
+ route._errorHandler(error);
833
+ } else {
834
+ if (RouterBox["errorHandler"]) {
835
+ RouterBox["errorHandler"](error);
836
+ } else {
837
+ console.error(error);
838
+ throw new Error(
839
+ " \u2718 Cradova err: consider adding error boundary to the specific screen "
840
+ );
841
+ }
842
+ }
843
+ }
844
+ } else {
845
+ if (RouterBox.routes["/*"]) {
846
+ await RouterBox.routes["/*"]._Activate(_force);
847
+ }
848
+ }
849
+ };
850
+ var RouterClass = class {
851
+ /** cradova router
852
+ * ---
853
+ * Registers a route.
854
+ *
855
+ * accepts an object containing
856
+ *
857
+ * @param {string} path Route path.
858
+ * @param screen the cradova screen.
859
+ */
860
+ BrowserRoutes(obj) {
861
+ for (const path in obj) {
862
+ let screen = obj[path];
863
+ if (typeof screen === "object" && typeof screen.then === "function" || typeof screen === "function") {
864
+ RouterBox["routes"][path] = async () => {
865
+ screen = await (typeof screen === "function" ? await screen() : await screen);
866
+ return RouterBox.route(path, screen?.default || screen);
867
+ };
868
+ } else {
869
+ RouterBox.route(path, screen);
870
+ }
871
+ }
872
+ this._mount();
873
+ }
874
+ /**
875
+ Go back in Navigation history
876
+ */
877
+ back() {
878
+ history.go(-1);
879
+ }
880
+ /**
881
+ Go forward in Navigation history
882
+ */
883
+ forward() {
884
+ history.go(1);
885
+ }
886
+ /**
887
+ * Cradova Router
888
+ * ------
889
+ *
890
+ * Navigates to a designated screen in your app
891
+ *
892
+ * @param href string
893
+ * @param data object
894
+ * @param force boolean
895
+ */
896
+ navigate(href, data2 = null, force = false) {
897
+ if (typeof href !== "string") {
898
+ throw new TypeError(
899
+ " \u2718 Cradova err: href must be a defined path but got " + href + " instead"
900
+ );
901
+ }
902
+ let route = null, params;
903
+ if (href.includes("://")) {
904
+ window.location.href = href;
905
+ } else {
906
+ if (href === window.location.pathname) {
907
+ return;
908
+ }
909
+ [route, params] = checker(href);
910
+ if (route) {
911
+ RouterBox["nextRouteController"] = route;
912
+ window.history.pushState({}, "", href);
913
+ }
914
+ RouterBox.params.params = params;
915
+ RouterBox.params.data = data2;
916
+ RouterBox.router(null, force);
917
+ }
918
+ }
919
+ /**
920
+ * Cradova
921
+ * ---
922
+ * Loading screen for your app
923
+ *
924
+ * lazy loaded loading use
925
+ *
926
+ * @param screen
927
+ */
928
+ setLoadingScreen(screen) {
929
+ if (screen instanceof Screen) {
930
+ RouterBox["LoadingScreen"] = screen;
931
+ } else {
932
+ throw new Error(
933
+ " \u2718 Cradova err: Loading Screen should be a cradova screen class"
934
+ );
935
+ }
936
+ }
937
+ /** cradova router
938
+ * ---
939
+ * Listen for navigation events
940
+ *
941
+ * @param callback () => void
942
+ */
943
+ onPageEvent(callback) {
944
+ if (typeof callback === "function") {
945
+ RouterBox["pageevents"].push(callback);
946
+ } else {
947
+ throw new Error(
948
+ " \u2718 Cradova err: callback for pageShow event is not a function"
949
+ );
950
+ }
951
+ }
952
+ /** cradova router
953
+ * ---
954
+ * get a screen ready before time.
955
+ *
956
+ * @param {string} path Route path.
957
+ * @param data data for the screen.
958
+ */
959
+ async packageScreen(path, data2 = {}) {
960
+ if (!RouterBox.routes[path]) {
961
+ console.error(" \u2718 Cradova err: no screen with path " + path);
962
+ throw new Error(
963
+ " \u2718 Cradova err: cradova err: Not a defined screen path"
964
+ );
965
+ }
966
+ let [route, params] = checker(path);
967
+ if (!route._Activate && typeof route === "function") {
968
+ route = await route();
969
+ }
970
+ if (route._delegatedRoutes !== -1) {
971
+ route._delegatedRoutes = true;
972
+ route = new Screen({
973
+ name: route._name,
974
+ template: route._html
975
+ });
976
+ RouterBox.routes[path] = route;
977
+ }
978
+ route._package(Object.assign(data2, params || {}));
979
+ route._packed = true;
980
+ }
981
+ /**
982
+ * Cradova Router
983
+ * ------
984
+ *
985
+ * return last set router params
986
+ *
987
+ * .
988
+ */
989
+ getParams() {
990
+ return RouterBox["params"];
991
+ }
992
+ /**
993
+ * Cradova
994
+ * ---
995
+ * Error Handler for your app
996
+ *
997
+ * @param callback
998
+ * @param path? page path
999
+ */
1000
+ addErrorHandler(callback) {
1001
+ if (typeof callback === "function") {
1002
+ RouterBox["errorHandler"] = callback;
1003
+ } else {
1004
+ throw new Error(
1005
+ " \u2718 Cradova err: callback for error event is not a function"
1006
+ );
1007
+ }
1008
+ }
1009
+ _mount() {
1010
+ let doc = document.querySelector("[data-wrapper=app]");
1011
+ if (!doc) {
1012
+ doc = document.createElement("div");
1013
+ doc.setAttribute("data-wrapper", "app");
1014
+ document.body.appendChild(doc);
1015
+ localTree._appendDomForce("doc", doc);
1016
+ } else {
1017
+ localTree._appendDomForce("doc", doc);
1018
+ }
1019
+ window.addEventListener("pageshow", RouterBox.router);
1020
+ window.addEventListener("popstate", (_e) => {
1021
+ _e.preventDefault();
1022
+ RouterBox.router();
1023
+ });
1024
+ }
1025
+ };
1026
+ var Router = new RouterClass();
1027
+
1028
+ // lib/parts/elements.ts
1029
+ var makeElement = (element, ElementChildrenAndPropertyList) => {
1030
+ let props = {}, text = null;
1031
+ if (ElementChildrenAndPropertyList.length !== 0) {
1032
+ for (let i2 = 0; i2 < ElementChildrenAndPropertyList.length; i2++) {
1033
+ let child = ElementChildrenAndPropertyList[i2];
1034
+ if (typeof child === "function") {
1035
+ child = child();
1036
+ }
1037
+ if (child instanceof Ref) {
1038
+ child = child.render();
1039
+ }
1040
+ if (isNode(child)) {
1041
+ element.appendChild(child);
1042
+ continue;
1043
+ }
1044
+ if (Array.isArray(child)) {
1045
+ element.appendChild(Rhoda(child));
1046
+ continue;
1047
+ }
1048
+ if (typeof child === "string" || typeof child === "number") {
1049
+ text = child;
1050
+ continue;
1051
+ }
1052
+ if (typeof child === "object") {
1053
+ props = Object.assign(props, child);
1054
+ continue;
1055
+ }
1056
+ if (typeof child !== "undefined") {
1057
+ console.error(" \u2718 Cradova err: got", { child });
1058
+ throw new Error(
1059
+ " \u2718 Cradova err: invalid child type: (" + typeof child + ")"
1060
+ );
1061
+ }
1062
+ }
1063
+ } else {
1064
+ return element;
1065
+ }
1066
+ if (typeof props === "object" && element) {
1067
+ for (const [prop, value] of Object.entries(props)) {
1068
+ if (prop === "style" && typeof value === "object") {
1069
+ Object.assign(element.style, value);
1070
+ continue;
1071
+ }
1072
+ if (Array.isArray(value)) {
1073
+ if (value[0] instanceof createSignal) {
1074
+ element["updateState"] = dispatch.bind(null, element);
1075
+ value[0].bindRef(
1076
+ element,
1077
+ {
1078
+ _element_property: prop,
1079
+ signalProperty: value[1]
1080
+ }
1081
+ );
1082
+ continue;
1083
+ }
1084
+ if (prop == "reference" && value[0] instanceof reference) {
1085
+ element["updateState"] = dispatch.bind(null, element);
1086
+ value[0]._appendDomForce(
1087
+ value[1],
1088
+ element
1089
+ );
1090
+ continue;
1091
+ }
1092
+ }
1093
+ if (prop === "onmount" && typeof props["onmount"] === "function") {
1094
+ const ev = () => {
1095
+ props.onmount?.apply(element);
1096
+ props["onmount"] = void 0;
1097
+ };
1098
+ CradovaEvent.addEventListener("onmountEvent", ev);
1099
+ continue;
1100
+ }
1101
+ if (prop.includes("data-")) {
1102
+ element.setAttribute(prop, value);
1103
+ continue;
1104
+ }
1105
+ if (prop.includes("aria-")) {
1106
+ element.setAttribute(prop, value);
1107
+ continue;
1108
+ }
1109
+ if (prop === "href" && typeof value === "string") {
1110
+ const href = value || "";
1111
+ if (!href.includes("://")) {
1112
+ element.addEventListener(
1113
+ "click",
1114
+ (e) => {
1115
+ e.preventDefault();
1116
+ Router.navigate(
1117
+ element.pathname
1118
+ );
1119
+ if (href.includes("#")) {
1120
+ const l = href.split("#").at(-1);
1121
+ document.getElementById("#" + l)?.scrollIntoView();
1122
+ }
1123
+ }
1124
+ );
1125
+ }
1126
+ element.setAttribute(prop, value);
1127
+ continue;
1128
+ }
1129
+ if (typeof element.style[prop] !== "undefined" && prop !== "src") {
1130
+ element.style[prop] = value;
1131
+ continue;
1132
+ }
1133
+ element[prop] = value;
1134
+ }
1135
+ }
1136
+ if (text) {
1137
+ element.appendChild(document.createTextNode(text));
1138
+ }
1139
+ return element;
1140
+ };
1141
+ var make = function(descriptor) {
1142
+ if (!descriptor.length) {
1143
+ return ["DIV"];
1144
+ }
1145
+ if (Array.isArray(descriptor)) {
1146
+ descriptor = descriptor[0];
1147
+ }
1148
+ let innerValue = "";
1149
+ if (descriptor.includes("|")) {
1150
+ [descriptor, innerValue] = descriptor.split("|");
1151
+ if (!descriptor) {
1152
+ return ["P", void 0, void 0, innerValue];
1153
+ }
1154
+ }
1155
+ let tag;
1156
+ if (!descriptor.includes("#")) {
1157
+ descriptor = descriptor.split(".");
1158
+ tag = descriptor.shift();
1159
+ if (!tag) {
1160
+ tag = "DIV";
1161
+ }
1162
+ return [tag, void 0, descriptor.join(" "), innerValue];
1163
+ } else {
1164
+ if (!descriptor.includes(".")) {
1165
+ descriptor = descriptor.split("#");
1166
+ tag = descriptor.shift();
1167
+ if (!tag) {
1168
+ tag = "DIV";
1169
+ }
1170
+ if (descriptor[0].includes(" ")) {
1171
+ descriptor = [descriptor[0].split(" ")[1]];
1172
+ }
1173
+ return [tag, descriptor[0], void 0, innerValue];
1174
+ }
1175
+ }
1176
+ descriptor = descriptor.split(".");
1177
+ const classes = [];
1178
+ const IDs = [];
1179
+ tag = !descriptor[0].includes("#") && descriptor.shift();
1180
+ if (!tag) {
1181
+ tag = "DIV";
1182
+ }
1183
+ for (let i2 = 0; i2 < descriptor.length; i2++) {
1184
+ if (descriptor[i2].includes("#")) {
1185
+ const item = descriptor[i2].split("#");
1186
+ IDs.push(item[1]);
1187
+ if (i2 === 0) {
1188
+ tag = item[0];
1189
+ continue;
1190
+ }
1191
+ classes.push(item[0]);
1192
+ continue;
1193
+ }
1194
+ classes.push(descriptor[i2]);
1195
+ }
1196
+ return [tag || "DIV", IDs[0], classes.join(" "), innerValue];
1197
+ };
1198
+ var cra = (tag) => {
1199
+ const extend = (...Children_and_Properties) => makeElement(document.createElement(tag), Children_and_Properties);
1200
+ return extend;
1201
+ };
1202
+ var a = cra("a");
1203
+ var area = cra("area");
1204
+ var article = cra("article");
1205
+ var aside = cra("aside");
1206
+ var audio = cra("audio");
1207
+ var b = cra("b");
1208
+ var base = cra("base");
1209
+ var blockquote = cra("blockquote");
1210
+ var br = cra("br");
1211
+ var button = cra("button");
1212
+ var canvas = cra("canvas");
1213
+ var caption = cra("caption");
1214
+ var code = cra("code");
1215
+ var col = cra("col");
1216
+ var colgroup = cra("colgroup");
1217
+ var data = cra("data");
1218
+ var datalist = cra("datalist");
1219
+ var details = cra("details");
1220
+ var dialog = cra("dialog");
1221
+ var div = cra("div");
1222
+ var em = cra("em");
1223
+ var embed = cra("embed");
1224
+ var figure = cra("figure");
1225
+ var footer = cra("footer");
1226
+ var form = cra("form");
1227
+ var h1 = cra("h1");
1228
+ var h2 = cra("h2");
1229
+ var h3 = cra("h3");
1230
+ var h4 = cra("h4");
1231
+ var h5 = cra("h5");
1232
+ var h6 = cra("h6");
1233
+ var head = cra("head");
1234
+ var header = cra("header");
1235
+ var hr = cra("hr");
1236
+ var i = cra("i");
1237
+ var iframe = cra("iframe");
1238
+ var img = cra("img");
1239
+ var input = cra("input");
1240
+ var label = cra("label");
1241
+ var legend = cra("legend");
1242
+ var li = cra("li");
1243
+ var link = cra("link");
1244
+ var main = cra("main");
1245
+ var menu = cra("menu");
1246
+ var nav = cra("nav");
1247
+ var object = cra("object");
1248
+ var ol = cra("ol");
1249
+ var optgroup = cra("optgroup");
1250
+ var option = cra("option");
1251
+ var p = cra("p");
1252
+ var pre = cra("pre");
1253
+ var progress = cra("progress");
1254
+ var q = cra("q");
1255
+ var section = cra("section");
1256
+ var select = cra("select");
1257
+ var source = cra("source");
1258
+ var span = cra("span");
1259
+ var strong = cra("strong");
1260
+ var summary = cra("summary");
1261
+ var table = cra("table");
1262
+ var tbody = cra("tbody");
1263
+ var td = cra("td");
1264
+ var template = cra("template");
1265
+ var textarea = cra("textarea");
1266
+ var th = cra("th");
1267
+ var title = cra("title");
1268
+ var tr = cra("tr");
1269
+ var track = cra("track");
1270
+ var u = cra("u");
1271
+ var ul = cra("ul");
1272
+ var video = cra("video");
1273
+
1274
+ // lib/parts/ajax.ts
1275
+ function Ajax(url, opts = {}) {
1276
+ const { method, data: data2, header: header2, callbacks } = opts;
1277
+ if (typeof url !== "string") {
1278
+ throw new Error("\u2718 Cradova err : Ajax invalid url " + url);
1279
+ }
1280
+ return new Promise(function(resolve, reject) {
1281
+ const ajax = new XMLHttpRequest();
1282
+ const formData = new FormData();
1283
+ if (callbacks && typeof callbacks === "object") {
1284
+ for (const [k, v] of Object.entries(callbacks)) {
1285
+ ajax.addEventListener(k, v);
1286
+ }
1287
+ }
1288
+ ajax.addEventListener("load", function() {
1289
+ resolve(ajax.response);
1290
+ });
1291
+ if (data2 && typeof data2 === "object") {
1292
+ for (const [k, v] of Object.entries(data2)) {
1293
+ let value = v;
1294
+ if (typeof value === "object" && value && !value.name) {
1295
+ value = JSON.stringify(value);
1296
+ formData.set(k, value);
1297
+ }
1298
+ if (typeof value === "string") {
1299
+ formData.set(k, value);
1300
+ }
1301
+ }
1302
+ }
1303
+ ajax.addEventListener("error", () => {
1304
+ if (!navigator.onLine) {
1305
+ reject(
1306
+ JSON.stringify({
1307
+ message: `this device is offline!`
1308
+ })
1309
+ );
1310
+ } else {
1311
+ reject(
1312
+ JSON.stringify({
1313
+ message: `problem with the action, please try again!`
1314
+ })
1315
+ );
1316
+ }
1317
+ });
1318
+ if (!method) {
1319
+ ajax.open(data2 && typeof data2 === "object" ? "POST" : "GET", url, true);
1320
+ } else {
1321
+ ajax.open(method, url, true);
1322
+ }
1323
+ if (header2 && typeof header2 === "object") {
1324
+ Object.keys(header2).forEach(function(key) {
1325
+ ajax.setRequestHeader(key, header2[key]);
1326
+ });
1327
+ }
1328
+ ajax.send(formData);
1329
+ });
1330
+ }
1331
+
1332
+ // lib/index.ts
1333
+ var _2 = (...element_initials) => {
1334
+ const {
1335
+ 0: tag,
1336
+ 1: id,
1337
+ 2: className,
1338
+ 3: innerText
1339
+ } = make(element_initials[0]);
1340
+ const element = tag ? document.createElement(tag) : new DocumentFragment();
1341
+ if (tag) {
1342
+ if (className) {
1343
+ element.className = className;
1344
+ }
1345
+ if (id) {
1346
+ element.id = id;
1347
+ }
1348
+ if (innerText) {
1349
+ element.innerText = innerText;
1350
+ }
1351
+ element_initials.shift();
1352
+ }
1353
+ return makeElement(element, element_initials);
1354
+ };
1355
+ var lib_default = _2;
1356
+ //! get url hash here and scroll into view
1357
+
1358
+ export { Ajax, CradovaEvent, Ref, Rhoda, Router, Screen, a, area, article, aside, assert, assertOr, audio, b, base, blockquote, br, button, canvas, caption, code, col, colgroup, cradovaEvent, createSignal, css, data, datalist, lib_default as default, details, dialog, div, em, embed, figure, footer, form, frag, h1, h2, h3, h4, h5, h6, head, header, hr, i, iframe, img, input, isNode, label, lazy, legend, li, link, loop, main, make, makeElement, menu, nav, object, ol, optgroup, option, p, pre, progress, q, reference, section, select, source, span, strong, summary, table, tbody, td, template, textarea, th, title, tr, track, u, ul, useEffect, useRef, useState, video };