cradova 3.1.4 → 3.1.5

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 DELETED
@@ -1,1396 +0,0 @@
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
- async 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
- async 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
- dom(name) {
285
- return this[name];
286
- }
287
- _appendDomForce(name, Element) {
288
- this[name] = Element;
289
- }
290
- };
291
- function useState(initialValue, ActiveRef) {
292
- ActiveRef._state_index += 1;
293
- const idx = ActiveRef._state_index;
294
- if (!ActiveRef._state_track[idx]) {
295
- ActiveRef._roll_state(initialValue, idx);
296
- ActiveRef._state_track[idx] = true;
297
- }
298
- function setState(newState) {
299
- ActiveRef._roll_state(newState, idx);
300
- ActiveRef.updateState(newState);
301
- }
302
- return [ActiveRef._roll_state(null, idx, true), setState];
303
- }
304
- function useEffect(effect, ActiveRef) {
305
- ActiveRef._effect(effect);
306
- }
307
- function useRef() {
308
- return new reference();
309
- }
310
-
311
- // lib/parts/track.ts
312
- function dispatch(element, state) {
313
- if (!element) {
314
- return;
315
- }
316
- if (typeof state === "object" && !Array.isArray(state)) {
317
- for (const key in state) {
318
- if (key === "style" && typeof state[key] === "object") {
319
- for (const [k, v] of Object.entries(state[key])) {
320
- if (typeof element.style[k] !== "undefined" && k !== "src" && typeof v === "string") {
321
- element.style[k] = v;
322
- } else {
323
- throw new Error(
324
- "\u2718 Cradova err : " + k + " is not a valid css style property"
325
- );
326
- }
327
- }
328
- continue;
329
- }
330
- const stateK = state;
331
- if (typeof element[key] === "function") {
332
- if (key.startsWith("on")) {
333
- element[key] = stateK[key];
334
- } else {
335
- element[key].apply(element);
336
- }
337
- continue;
338
- }
339
- if (key === "text") {
340
- element.innerText = stateK[key];
341
- continue;
342
- }
343
- if (key === "tree") {
344
- element.innerHTML = "";
345
- element.appendChild(frag([stateK[key]]));
346
- continue;
347
- }
348
- if (key.includes("data-")) {
349
- element.setAttribute(key, stateK[key]);
350
- continue;
351
- }
352
- element[key] = stateK[key];
353
- }
354
- }
355
- if (typeof state === "string") {
356
- element.innerText = state;
357
- return;
358
- }
359
- if (typeof state === "function") {
360
- element.appendChild(frag([state]));
361
- return;
362
- }
363
- if (state instanceof HTMLElement) {
364
- element.appendChild(state);
365
- }
366
- if (!(typeof state === "object" && !Array.isArray(state))) {
367
- throw new Error(
368
- " \u2718 Cradova err: Cradova got invalid state => \n" + String(state)
369
- );
370
- }
371
- }
372
-
373
- // lib/parts/createSignal.ts
374
- var createSignal = class {
375
- constructor(initial, props) {
376
- this.persistName = "";
377
- this.actions = {};
378
- this.ref = [];
379
- this.value = initial;
380
- if (props && props.persistName) {
381
- this.persistName = props.persistName;
382
- const key = localStorage.getItem(props.persistName);
383
- if (key && key !== "undefined") {
384
- this.value = JSON.parse(key);
385
- }
386
- if (typeof initial === "object") {
387
- for (const key2 in initial) {
388
- if (!Object.prototype.hasOwnProperty.call(this.value, key2)) {
389
- this.value[key2] = initial[key2];
390
- }
391
- }
392
- }
393
- }
394
- }
395
- /**
396
- * Cradova Signal
397
- * ----
398
- * set signal value
399
- * @param value - signal value
400
- * @returns void
401
- */
402
- set(value, shouldRefRender) {
403
- if (typeof value === "function") {
404
- this.value = value(this.value);
405
- } else {
406
- this.value = value;
407
- }
408
- if (this.persistName) {
409
- localStorage.setItem(this.persistName, JSON.stringify(this.value));
410
- }
411
- if (this.ref.length && shouldRefRender !== false) {
412
- this._updateState();
413
- }
414
- if (this.callback) {
415
- this.callback(this.value);
416
- }
417
- }
418
- /**
419
- * Cradova Signal
420
- * ----
421
- * set a key value if it's an object
422
- * @param key - key of the key
423
- * @param value - value of the key
424
- * @returns void
425
- */
426
- setKey(key, value, shouldRefRender) {
427
- if (typeof this.value === "object" && !Array.isArray(this.value)) {
428
- this.value[key] = value;
429
- if (this.persistName) {
430
- localStorage.setItem(this.persistName, JSON.stringify(this.value));
431
- }
432
- if (this.ref.length && shouldRefRender !== false) {
433
- this._updateState();
434
- }
435
- if (this.callback) {
436
- this.callback(this.value);
437
- }
438
- } else {
439
- throw new Error(
440
- `\u2718 Cradova err : can't set key ${String(
441
- key
442
- )} . store.value is not a javascript object`
443
- );
444
- }
445
- }
446
- /**
447
- * Cradova Signal
448
- * ----
449
- * set a key to signal an action
450
- * @param name - name of the action
451
- * @param action function to execute
452
- */
453
- createAction(name, action) {
454
- if (typeof name === "string" && typeof action === "function") {
455
- this.actions[name] = action;
456
- } else {
457
- throw new Error(
458
- `\u2718 Cradova err : can't create action, ${name} is not a function`
459
- );
460
- }
461
- }
462
- /**
463
- * Cradova Signal
464
- * ----
465
- * creates man y actions at a time
466
- * @param name - name of the action
467
- * @param action function to execute
468
- */
469
- createActions(Actions) {
470
- for (const [name, action] of Object.entries(Actions)) {
471
- if (typeof name === "string" && typeof action === "function") {
472
- this.actions[name] = action;
473
- } else {
474
- throw new Error(
475
- `\u2718 Cradova err : can't create action, ${name} is not a function`
476
- );
477
- }
478
- }
479
- }
480
- /**
481
- * Cradova Signal
482
- * ----
483
- * fires an action if available
484
- * @param key - string key of the action
485
- * @param data - data for the action
486
- */
487
- fireAction(key, data2) {
488
- this._updateState(key, data2);
489
- if (typeof this.actions[key] === "function") {
490
- return this.actions[key].call(this, data2);
491
- } else {
492
- throw Error("\u2718 Cradova err : action " + key + " does not exist!");
493
- }
494
- }
495
- /**
496
- * Cradova
497
- * ---
498
- * is used to bind signal data to elements and Refs
499
- *
500
- * @param prop
501
- * @returns something
502
- */
503
- bind(prop) {
504
- if (typeof this.value === "object" && typeof this.value[prop] !== "undefined") {
505
- return [this, prop];
506
- } else {
507
- throw new Error(
508
- "\u2718 Cradova err : can't bind an unavailable property! " + prop
509
- );
510
- }
511
- }
512
- _updateState(name, data2) {
513
- if (name && data2) {
514
- this.ref.map((ent) => {
515
- if (ent._event === name) {
516
- if (ent._element_property && ent._signalProperty) {
517
- ent.ref?.updateState({
518
- [ent._element_property]: data2[ent._signalProperty]
519
- });
520
- return;
521
- }
522
- if (ent._element_property) {
523
- ent.ref.updateState({
524
- [ent._element_property]: data2
525
- });
526
- return;
527
- }
528
- if (ent._signalProperty) {
529
- ent.ref.updateState(data2[ent._signalProperty]);
530
- return;
531
- }
532
- }
533
- });
534
- } else {
535
- for (let i2 = 0; i2 < this.ref.length; i2++) {
536
- const ent = this.ref[i2];
537
- if (ent._element_property && ent._signalProperty) {
538
- ent.ref.updateState({
539
- [ent._element_property]: this.value[ent._signalProperty]
540
- });
541
- continue;
542
- }
543
- if (ent._element_property) {
544
- ent.ref.updateState({
545
- [ent._element_property]: this.value
546
- });
547
- continue;
548
- }
549
- if (ent._signalProperty) {
550
- ent.ref.updateState(this.value[ent._signalProperty]);
551
- continue;
552
- }
553
- if (!ent._element_property && !ent._signalProperty) {
554
- ent.ref.updateState(this.value);
555
- continue;
556
- }
557
- }
558
- }
559
- }
560
- /**
561
- * Cradova Signal
562
- * ----
563
- * set a auto - rendering component for this store
564
- *
565
- * @param Ref component to bind to.
566
- * @param path a property in the object to send to attached ref
567
- */
568
- bindRef(ref, binding = { signalProperty: "", _element_property: "" }) {
569
- if (ref.render) {
570
- ref.render = ref.render.bind(ref, this.value);
571
- }
572
- if (ref._setExtra) {
573
- ref._setExtra(this);
574
- }
575
- if (ref && ref.updateState) {
576
- this.ref.push({
577
- ref,
578
- _signalProperty: binding.signalProperty,
579
- _element_property: binding._element_property,
580
- _event: binding.event
581
- });
582
- return;
583
- }
584
- throw new Error(
585
- "\u2718 Cradova err : Invalid parameters for binding ref to Signal"
586
- );
587
- }
588
- /**
589
- * Cradova Signal
590
- * ----
591
- * set a update listener on value changes
592
- * @param callback
593
- */
594
- listen(callback) {
595
- this.callback = callback;
596
- }
597
- /**
598
- * Cradova Signal
599
- * ----
600
- * clear the history on local storage
601
- *
602
- */
603
- clearPersist() {
604
- if (this.persistName) {
605
- localStorage.removeItem(this.persistName);
606
- }
607
- }
608
- };
609
-
610
- // lib/parts/Screen.ts
611
- var localTree = new reference();
612
- var Screen = class {
613
- constructor(cradova_screen_initials) {
614
- /**
615
- * this is a set of added html to the screen
616
- */
617
- // private _secondaryChildren: VJSType<HTMLElement>[] = [];
618
- /**
619
- * error handler for the screen
620
- */
621
- this._errorHandler = null;
622
- this._packed = false;
623
- this._template = document.createElement("div");
624
- this._persist = true;
625
- this._delegatedRoutesCount = -1;
626
- this._dropped = false;
627
- const { template: template2, name, persist, renderInParallel, transition } = cradova_screen_initials;
628
- this._html = template2;
629
- this._name = name;
630
- this._transition = transition;
631
- this._template.setAttribute("id", "cradova-screen-set");
632
- if (renderInParallel === true) {
633
- this._delegatedRoutesCount = 0;
634
- this._persist = false;
635
- } else {
636
- if (typeof persist === "boolean") {
637
- this._persist = persist;
638
- }
639
- }
640
- }
641
- get _delegatedRoutes() {
642
- if (this._delegatedRoutesCount > 100) {
643
- return -1;
644
- }
645
- return this._delegatedRoutesCount;
646
- }
647
- set _delegatedRoutes(count) {
648
- if (count) {
649
- this._delegatedRoutesCount += 1;
650
- }
651
- }
652
- setErrorHandler(errorHandler) {
653
- this._errorHandler = errorHandler;
654
- }
655
- async _package() {
656
- if (typeof this._html === "function") {
657
- let fuc = await this._html.apply(this);
658
- if (typeof fuc === "function") {
659
- fuc = fuc();
660
- if (isNode(fuc)) {
661
- this._template.innerHTML = "";
662
- this._template.appendChild(fuc);
663
- }
664
- } else {
665
- if (isNode(fuc)) {
666
- this._template.innerHTML = "";
667
- this._template.appendChild(fuc);
668
- } else {
669
- throw new Error(
670
- ` \u2718 Cradova err: template function for the screen with name '${this._name}' returned ${fuc} instead of html`
671
- );
672
- }
673
- }
674
- }
675
- }
676
- onActivate(cb) {
677
- this._callBack = cb;
678
- }
679
- onDeactivate(cb) {
680
- this._deCallBack = cb;
681
- }
682
- // addChildren(...addOns: VJSType<HTMLElement>[]) {
683
- // this._secondaryChildren.push(...addOns);
684
- // }
685
- async _deActivate() {
686
- if (this._deCallBack) {
687
- await this._deCallBack();
688
- }
689
- }
690
- drop(state) {
691
- if (typeof state === "boolean") {
692
- this._dropped = state;
693
- return void 0;
694
- } else
695
- return this._dropped;
696
- }
697
- async _Activate(force = false) {
698
- if (this._dropped) {
699
- history.go(-1);
700
- return;
701
- }
702
- if (!this._persist || force || !this._packed) {
703
- await this._package();
704
- this._packed = true;
705
- }
706
- if (this._transition) {
707
- this._template.classList.add(this._transition);
708
- }
709
- document.title = this._name;
710
- localTree.doc.innerHTML = "";
711
- localTree.doc.appendChild(this._template);
712
- CradovaEvent.dispatchEvent("onmountEvent");
713
- window.scrollTo({
714
- top: 0,
715
- left: 0,
716
- // @ts-ignore
717
- behavior: "instant"
718
- });
719
- if (this._callBack) {
720
- await this._callBack();
721
- }
722
- }
723
- };
724
-
725
- // lib/parts/Router.ts
726
- var RouterBox = {};
727
- RouterBox["lastNavigatedRouteController"] = null;
728
- RouterBox["nextRouteController"] = null;
729
- RouterBox["lastNavigatedRoute"] = null;
730
- RouterBox["pageShow"] = null;
731
- RouterBox["pageHide"] = null;
732
- RouterBox["errorHandler"] = null;
733
- RouterBox["loadingScreen"] = null;
734
- RouterBox["params"] = {};
735
- RouterBox["routes"] = {};
736
- RouterBox["pageevents"] = [];
737
- RouterBox["paused"] = false;
738
- RouterBox["start_pageevents"] = async function(url) {
739
- setTimeout(() => {
740
- for (let ci = 0; ci < RouterBox["pageevents"].length; ci++) {
741
- RouterBox["pageevents"][ci](url);
742
- }
743
- }, 100);
744
- };
745
- var checker = (url) => {
746
- if (RouterBox.routes[url]) {
747
- return [RouterBox.routes[url], { path: url }];
748
- }
749
- if (RouterBox.routes[url + "/"]) {
750
- return [RouterBox.routes[url], { path: url }];
751
- }
752
- for (const path in RouterBox.routes) {
753
- if (!path.includes(":")) {
754
- continue;
755
- }
756
- const urlFixtures = url.split("/");
757
- const pathFixtures = path.split("/");
758
- if (url.endsWith("/")) {
759
- urlFixtures.pop();
760
- }
761
- let fixturesX = 0;
762
- let fixturesY = 0;
763
- urlFixtures.shift();
764
- pathFixtures.shift();
765
- if (pathFixtures.length === urlFixtures.length) {
766
- const routesParams = { _path: "" };
767
- for (let i2 = 0; i2 < pathFixtures.length; i2++) {
768
- if (pathFixtures[i2].includes(":")) {
769
- fixturesY++;
770
- continue;
771
- }
772
- if (urlFixtures[i2] === pathFixtures[i2]) {
773
- fixturesX++;
774
- }
775
- }
776
- if (fixturesX + fixturesY === pathFixtures.length) {
777
- for (let i2 = 0; i2 < pathFixtures.length; i2++) {
778
- if (pathFixtures[i2].includes(":")) {
779
- routesParams[pathFixtures[i2].split(":")[1]] = urlFixtures[i2];
780
- }
781
- }
782
- routesParams._path = path;
783
- return [RouterBox.routes[path], routesParams];
784
- }
785
- }
786
- }
787
- return [];
788
- };
789
- RouterBox.route = (path, screen) => {
790
- if (typeof screen !== "undefined") {
791
- if (screen && !screen._Activate) {
792
- console.error(" \u2718 Cradova err: not a valid screen ", screen);
793
- throw new Error(" \u2718 Cradova err: Not a valid cradova screen component");
794
- }
795
- return RouterBox.routes[path] = screen;
796
- }
797
- return void 0;
798
- };
799
- RouterBox.router = async function(_e, _force) {
800
- let url = window.location.pathname, route, params;
801
- if (RouterBox["paused"]) {
802
- window.location.hash = "paused";
803
- return;
804
- }
805
- if (url === RouterBox["lastNavigatedRoute"]) {
806
- return;
807
- }
808
- if (RouterBox["nextRouteController"]) {
809
- route = RouterBox["nextRouteController"];
810
- RouterBox["nextRouteController"] = null;
811
- } else {
812
- [route, params] = checker(url);
813
- }
814
- if (typeof route !== "undefined") {
815
- try {
816
- if (typeof route === "function") {
817
- if (RouterBox["LoadingScreen"] && RouterBox["LoadingScreen"]._Activate) {
818
- await RouterBox["LoadingScreen"]._Activate();
819
- }
820
- route = await route();
821
- if (!route) {
822
- if (RouterBox["lastNavigatedRoute"]) {
823
- history.pushState({}, url, RouterBox["lastNavigatedRoute"]);
824
- }
825
- return;
826
- }
827
- }
828
- if (route._delegatedRoutes !== -1) {
829
- route._delegatedRoutes = true;
830
- route = new Screen({
831
- name: route._name,
832
- template: route._html
833
- });
834
- RouterBox.routes[url] = route;
835
- }
836
- if (params) {
837
- RouterBox.params.params = params;
838
- }
839
- await route._Activate(_force);
840
- RouterBox["start_pageevents"](url);
841
- RouterBox["lastNavigatedRouteController"] && RouterBox["lastNavigatedRouteController"]._deActivate();
842
- RouterBox["lastNavigatedRoute"] = url;
843
- RouterBox["lastNavigatedRouteController"] = route;
844
- } catch (error) {
845
- if (route && route["_errorHandler"]) {
846
- route._errorHandler(error);
847
- } else {
848
- if (RouterBox["errorHandler"]) {
849
- RouterBox["errorHandler"](error);
850
- } else {
851
- console.error(error);
852
- throw new Error(
853
- " \u2718 Cradova err: consider adding error boundary to the specific screen "
854
- );
855
- }
856
- }
857
- }
858
- } else {
859
- if (RouterBox.routes["/*"]) {
860
- await RouterBox.routes["/*"]._Activate(_force);
861
- }
862
- }
863
- };
864
- var Router = class {
865
- /** cradova router
866
- * ---
867
- * Registers a route.
868
- *
869
- * accepts an object containing
870
- *
871
- * @param {string} path Route path.
872
- * @param screen the cradova screen.
873
- */
874
- static BrowserRoutes(obj) {
875
- for (const path in obj) {
876
- let screen = obj[path];
877
- if (typeof screen === "object" && typeof screen.then === "function" || typeof screen === "function") {
878
- RouterBox["routes"][path] = async () => {
879
- screen = await (typeof screen === "function" ? await screen() : await screen);
880
- return RouterBox.route(path, screen?.default || screen);
881
- };
882
- } else {
883
- RouterBox.route(path, screen);
884
- }
885
- }
886
- Router._mount();
887
- }
888
- /**
889
- Go back in Navigation history
890
- */
891
- static back() {
892
- history.go(-1);
893
- }
894
- /**
895
- Go forward in Navigation history
896
- */
897
- static forward() {
898
- history.go(1);
899
- }
900
- /**
901
- Pause navigation
902
- */
903
- static pauseNaviagtion() {
904
- RouterBox["paused"] = true;
905
- window.location.hash = "paused";
906
- }
907
- /**
908
- resume navigation
909
- */
910
- static resumeNaviagtion() {
911
- RouterBox["paused"] = false;
912
- window.location.replace(window.location.pathname + window.location.search);
913
- history.go(-1);
914
- }
915
- /**
916
- * Cradova Router
917
- * ------
918
- *
919
- * Navigates to a designated screen in your app
920
- *
921
- * @param href string
922
- * @param data object
923
- * @param force boolean
924
- */
925
- static navigate(href, data2 = null, force = false) {
926
- if (typeof href !== "string") {
927
- throw new TypeError(
928
- " \u2718 Cradova err: href must be a defined path but got " + href + " instead"
929
- );
930
- }
931
- let route = null, params;
932
- if (href.includes("://")) {
933
- window.location.href = href;
934
- } else {
935
- if (href === window.location.pathname) {
936
- return;
937
- }
938
- [route, params] = checker(href);
939
- if (route) {
940
- RouterBox["nextRouteController"] = route;
941
- window.history.pushState({}, "", href);
942
- }
943
- RouterBox.params.params = params;
944
- RouterBox.params.data = data2;
945
- RouterBox.router(null, force);
946
- }
947
- }
948
- /**
949
- * Cradova Router
950
- * ------
951
- *
952
- * Navigates to a designated screen in your app by loading a seperate page;
953
- *
954
- * @param pathname string
955
- */
956
- static navigateNauturally(pathname) {
957
- if (typeof pathname !== "string") {
958
- throw new TypeError(
959
- " \u2718 Cradova err: pathname must be a defined path but got " + pathname + " instead"
960
- );
961
- }
962
- window.location.pathname = pathname;
963
- }
964
- /**
965
- * Cradova
966
- * ---
967
- * Loading screen for your app
968
- *
969
- * lazy loaded loading use
970
- *
971
- * @param screen
972
- */
973
- static setLoadingScreen(screen) {
974
- if (screen instanceof Screen) {
975
- RouterBox["LoadingScreen"] = screen;
976
- } else {
977
- throw new Error(
978
- " \u2718 Cradova err: Loading Screen should be a cradova screen class"
979
- );
980
- }
981
- }
982
- /** cradova router
983
- * ---
984
- * Listen for navigation events
985
- *
986
- * @param callback () => void
987
- */
988
- static onPageEvent(callback) {
989
- if (typeof callback === "function") {
990
- RouterBox["pageevents"].push(callback);
991
- } else {
992
- throw new Error(
993
- " \u2718 Cradova err: callback for pageShow event is not a function"
994
- );
995
- }
996
- }
997
- /** cradova router
998
- * ---
999
- * get a screen ready before time.
1000
- *
1001
- * @param {string} path Route path.
1002
- * @param data data for the screen.
1003
- */
1004
- static async packageScreen(path, data2 = {}) {
1005
- if (!RouterBox.routes[path]) {
1006
- console.error(" \u2718 Cradova err: no screen with path " + path);
1007
- throw new Error(
1008
- " \u2718 Cradova err: cradova err: Not a defined screen path"
1009
- );
1010
- }
1011
- let [route, params] = checker(path);
1012
- if (!route._Activate && typeof route === "function") {
1013
- route = await route();
1014
- }
1015
- if (route._delegatedRoutes !== -1) {
1016
- route._delegatedRoutes = true;
1017
- route = new Screen({
1018
- name: route._name,
1019
- template: route._html
1020
- });
1021
- RouterBox.routes[path] = route;
1022
- }
1023
- route._package(Object.assign(data2, params || {}));
1024
- route._packed = true;
1025
- }
1026
- /**
1027
- * Cradova Router
1028
- * ------
1029
- *
1030
- * return last set router params
1031
- *
1032
- * .
1033
- */
1034
- static getParams() {
1035
- return RouterBox["params"];
1036
- }
1037
- /**
1038
- * Cradova
1039
- * ---
1040
- * Error Handler for your app
1041
- *
1042
- * @param callback
1043
- * @param path? page path
1044
- */
1045
- static addErrorHandler(callback) {
1046
- if (typeof callback === "function") {
1047
- RouterBox["errorHandler"] = callback;
1048
- } else {
1049
- throw new Error(
1050
- " \u2718 Cradova err: callback for error event is not a function"
1051
- );
1052
- }
1053
- }
1054
- static _mount() {
1055
- let doc = document.querySelector("[data-wrapper=app]");
1056
- if (!doc) {
1057
- doc = document.createElement("div");
1058
- doc.setAttribute("data-wrapper", "app");
1059
- document.body.appendChild(doc);
1060
- localTree._appendDomForce("doc", doc);
1061
- } else {
1062
- localTree._appendDomForce("doc", doc);
1063
- }
1064
- window.addEventListener("pageshow", RouterBox.router);
1065
- window.addEventListener("popstate", (_e) => {
1066
- _e.preventDefault();
1067
- RouterBox.router();
1068
- });
1069
- }
1070
- };
1071
-
1072
- // lib/parts/elements.ts
1073
- var makeElement = (element, ElementChildrenAndPropertyList) => {
1074
- let props = {}, text = null;
1075
- if (ElementChildrenAndPropertyList.length !== 0) {
1076
- for (let i2 = 0; i2 < ElementChildrenAndPropertyList.length; i2++) {
1077
- let child = ElementChildrenAndPropertyList[i2];
1078
- if (typeof child === "function") {
1079
- child = child();
1080
- }
1081
- if (child instanceof Ref) {
1082
- child = child.render();
1083
- }
1084
- if (isNode(child)) {
1085
- element.appendChild(child);
1086
- continue;
1087
- }
1088
- if (Array.isArray(child)) {
1089
- element.appendChild(Rhoda(child));
1090
- continue;
1091
- }
1092
- if (typeof child === "string" || typeof child === "number") {
1093
- text = child;
1094
- continue;
1095
- }
1096
- if (typeof child === "object") {
1097
- props = Object.assign(props, child);
1098
- continue;
1099
- }
1100
- }
1101
- } else {
1102
- return element;
1103
- }
1104
- if (typeof props === "object" && element) {
1105
- for (const [prop, value] of Object.entries(props)) {
1106
- if (prop === "style" && typeof value === "object") {
1107
- Object.assign(element.style, value);
1108
- continue;
1109
- }
1110
- if (Array.isArray(value)) {
1111
- if (value[0] instanceof createSignal) {
1112
- element["updateState"] = dispatch.bind(null, element);
1113
- value[0].bindRef(
1114
- element,
1115
- {
1116
- _element_property: prop,
1117
- signalProperty: value[1]
1118
- }
1119
- );
1120
- continue;
1121
- }
1122
- if (prop == "reference" && value[0] instanceof reference) {
1123
- element["updateState"] = dispatch.bind(null, element);
1124
- value[0]._appendDomForce(
1125
- value[1],
1126
- element
1127
- );
1128
- continue;
1129
- }
1130
- }
1131
- if (prop === "onmount" && typeof props["onmount"] === "function") {
1132
- const ev = () => {
1133
- props.onmount?.apply(element);
1134
- props["onmount"] = void 0;
1135
- };
1136
- CradovaEvent.addEventListener("onmountEvent", ev);
1137
- continue;
1138
- }
1139
- if (prop.includes("data-")) {
1140
- element.setAttribute(prop, value);
1141
- continue;
1142
- }
1143
- if (prop.includes("aria-")) {
1144
- element.setAttribute(prop, value);
1145
- continue;
1146
- }
1147
- if (prop === "href" && typeof value === "string") {
1148
- const href = value || "";
1149
- if (!href.includes("://")) {
1150
- element.addEventListener(
1151
- "click",
1152
- (e) => {
1153
- e.preventDefault();
1154
- Router.navigate(
1155
- element.pathname
1156
- );
1157
- if (href.includes("#")) {
1158
- const l = href.split("#").at(-1);
1159
- document.getElementById("#" + l)?.scrollIntoView();
1160
- }
1161
- }
1162
- );
1163
- }
1164
- element.setAttribute(prop, value);
1165
- continue;
1166
- }
1167
- if (typeof element.style[prop] !== "undefined" && prop !== "src") {
1168
- element.style[prop] = value;
1169
- continue;
1170
- }
1171
- element[prop] = value;
1172
- }
1173
- }
1174
- if (text) {
1175
- element.appendChild(document.createTextNode(text));
1176
- }
1177
- return element;
1178
- };
1179
- var make = function(descriptor) {
1180
- if (typeof descriptor !== "string") {
1181
- return [];
1182
- }
1183
- if (Array.isArray(descriptor)) {
1184
- descriptor = descriptor[0];
1185
- }
1186
- let innerValue = "";
1187
- if (descriptor.includes("|")) {
1188
- [descriptor, innerValue] = descriptor.split("|");
1189
- if (!descriptor) {
1190
- return ["P", void 0, void 0, innerValue];
1191
- }
1192
- }
1193
- let tag;
1194
- if (!descriptor.includes("#")) {
1195
- descriptor = descriptor.split(".");
1196
- tag = descriptor.shift();
1197
- if (!tag) {
1198
- tag = "DIV";
1199
- }
1200
- return [tag, void 0, descriptor.join(" "), innerValue];
1201
- } else {
1202
- if (!descriptor.includes(".")) {
1203
- descriptor = descriptor.split("#");
1204
- tag = descriptor.shift();
1205
- if (!tag) {
1206
- tag = "DIV";
1207
- }
1208
- if (descriptor[0].includes(" ")) {
1209
- descriptor = [descriptor[0].split(" ")[1]];
1210
- }
1211
- return [tag, descriptor[0], void 0, innerValue];
1212
- }
1213
- }
1214
- descriptor = descriptor.split(".");
1215
- const classes = [];
1216
- const IDs = [];
1217
- tag = !descriptor[0].includes("#") && descriptor.shift();
1218
- if (!tag) {
1219
- tag = "DIV";
1220
- }
1221
- for (let i2 = 0; i2 < descriptor.length; i2++) {
1222
- if (descriptor[i2].includes("#")) {
1223
- const item = descriptor[i2].split("#");
1224
- IDs.push(item[1]);
1225
- if (i2 === 0) {
1226
- tag = item[0];
1227
- continue;
1228
- }
1229
- classes.push(item[0]);
1230
- continue;
1231
- }
1232
- classes.push(descriptor[i2]);
1233
- }
1234
- return [tag, IDs[0], classes.join(" "), innerValue];
1235
- };
1236
- var cra = (tag) => {
1237
- const extend = (...Children_and_Properties) => makeElement(document.createElement(tag), Children_and_Properties);
1238
- return extend;
1239
- };
1240
- var a = cra("a");
1241
- var area = cra("area");
1242
- var article = cra("article");
1243
- var aside = cra("aside");
1244
- var audio = cra("audio");
1245
- var b = cra("b");
1246
- var base = cra("base");
1247
- var blockquote = cra("blockquote");
1248
- var br = cra("br");
1249
- var button = cra("button");
1250
- var canvas = cra("canvas");
1251
- var caption = cra("caption");
1252
- var code = cra("code");
1253
- var col = cra("col");
1254
- var colgroup = cra("colgroup");
1255
- var data = cra("data");
1256
- var datalist = cra("datalist");
1257
- var details = cra("details");
1258
- var dialog = cra("dialog");
1259
- var div = cra("div");
1260
- var em = cra("em");
1261
- var embed = cra("embed");
1262
- var figure = cra("figure");
1263
- var footer = cra("footer");
1264
- var form = cra("form");
1265
- var h1 = cra("h1");
1266
- var h2 = cra("h2");
1267
- var h3 = cra("h3");
1268
- var h4 = cra("h4");
1269
- var h5 = cra("h5");
1270
- var h6 = cra("h6");
1271
- var head = cra("head");
1272
- var header = cra("header");
1273
- var hr = cra("hr");
1274
- var i = cra("i");
1275
- var iframe = cra("iframe");
1276
- var img = cra("img");
1277
- var input = cra("input");
1278
- var label = cra("label");
1279
- var legend = cra("legend");
1280
- var li = cra("li");
1281
- var link = cra("link");
1282
- var main = cra("main");
1283
- var menu = cra("menu");
1284
- var nav = cra("nav");
1285
- var object = cra("object");
1286
- var ol = cra("ol");
1287
- var optgroup = cra("optgroup");
1288
- var option = cra("option");
1289
- var p = cra("p");
1290
- var pre = cra("pre");
1291
- var progress = cra("progress");
1292
- var q = cra("q");
1293
- var section = cra("section");
1294
- var select = cra("select");
1295
- var source = cra("source");
1296
- var span = cra("span");
1297
- var strong = cra("strong");
1298
- var summary = cra("summary");
1299
- var table = cra("table");
1300
- var tbody = cra("tbody");
1301
- var td = cra("td");
1302
- var template = cra("template");
1303
- var textarea = cra("textarea");
1304
- var th = cra("th");
1305
- var title = cra("title");
1306
- var tr = cra("tr");
1307
- var track = cra("track");
1308
- var u = cra("u");
1309
- var ul = cra("ul");
1310
- var video = cra("video");
1311
-
1312
- // lib/parts/ajax.ts
1313
- function Ajax(url, opts = {}) {
1314
- const { method, data: data2, header: header2, callbacks } = opts;
1315
- if (typeof url !== "string") {
1316
- throw new Error("\u2718 Cradova err : Ajax invalid url " + url);
1317
- }
1318
- return new Promise(function(resolve, reject) {
1319
- const ajax = new XMLHttpRequest();
1320
- const formData = new FormData();
1321
- if (callbacks && typeof callbacks === "object") {
1322
- for (const [k, v] of Object.entries(callbacks)) {
1323
- ajax.addEventListener(k, v);
1324
- }
1325
- }
1326
- ajax.addEventListener("load", function() {
1327
- resolve(ajax.response);
1328
- });
1329
- if (data2 && typeof data2 === "object") {
1330
- for (const [k, v] of Object.entries(data2)) {
1331
- let value = v;
1332
- if (typeof value === "object" && value && !value.name) {
1333
- value = JSON.stringify(value);
1334
- formData.set(k, value);
1335
- }
1336
- if (typeof value === "string") {
1337
- formData.set(k, value);
1338
- }
1339
- }
1340
- }
1341
- ajax.addEventListener("error", () => {
1342
- if (!navigator.onLine) {
1343
- reject(
1344
- JSON.stringify({
1345
- message: `this device is offline!`
1346
- })
1347
- );
1348
- } else {
1349
- reject(
1350
- JSON.stringify({
1351
- message: `problem with the action, please try again!`
1352
- })
1353
- );
1354
- }
1355
- });
1356
- if (!method) {
1357
- ajax.open(data2 && typeof data2 === "object" ? "POST" : "GET", url, true);
1358
- } else {
1359
- ajax.open(method, url, true);
1360
- }
1361
- if (header2 && typeof header2 === "object") {
1362
- Object.keys(header2).forEach(function(key) {
1363
- ajax.setRequestHeader(key, header2[key]);
1364
- });
1365
- }
1366
- ajax.send(formData);
1367
- });
1368
- }
1369
-
1370
- // lib/index.ts
1371
- var _2 = (...element_initials) => {
1372
- const {
1373
- 0: tag,
1374
- 1: id,
1375
- 2: className,
1376
- 3: innerText
1377
- } = make(element_initials[0]);
1378
- const element = tag ? document.createElement(tag) : new DocumentFragment();
1379
- if (tag) {
1380
- if (className) {
1381
- element.className = className;
1382
- }
1383
- if (id) {
1384
- element.id = id;
1385
- }
1386
- if (innerText) {
1387
- element.innerText = innerText;
1388
- }
1389
- element_initials.shift();
1390
- }
1391
- return makeElement(element, element_initials);
1392
- };
1393
- var lib_default = _2;
1394
- //! get url hash here and scroll into view
1395
-
1396
- 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 };