ziko 2.0.0-beta.2 → 2.0.0-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ziko",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.4",
4
4
  "description": "A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...",
5
5
  "keywords": [
6
6
  "front-end",
@@ -1,11 +1,11 @@
1
1
  import { text } from "../text/index.js";
2
2
  import { isStateGetter } from "../../hooks/use-state.js";
3
3
  export function append(...ele) {
4
- __addItem__.call(this, "append", "push", ...ele);
4
+ __addItems__.call(this, "append", "push", ...ele);
5
5
  return this;
6
6
  }
7
7
  export function prepend(...ele) {
8
- this.__addItem__.call(this, "prepend", "unshift", ...ele);
8
+ this.__addItems__.call(this, "prepend", "unshift", ...ele);
9
9
  return this;
10
10
  }
11
11
  export function insertAt(index, ...ele) {
@@ -47,58 +47,46 @@ export function replaceElementWith(new_element){
47
47
  return this
48
48
  }
49
49
  export function after(ui){
50
- if(ui?.isUIElement) ui=ui.element;
50
+ if(ui?.isUIElement) ui = ui.element;
51
51
  this.itemsTarget.element?.after(ui)
52
52
  return this;
53
53
  }
54
54
  export function before(ui){
55
- if(ui?.isUIElement) ui=ui.element;
55
+ if(ui?.isUIElement) ui = ui.element;
56
56
  this.itemsTarget.element?.before(ui)
57
57
  return this;
58
58
  }
59
- export async function __addItem__(adder, pusher, ...ele) {
60
- const itemsTarget_el = this.itemsTarget.element;
61
- const itemsTarget = this.itemsTarget
62
- if (this.cache.isFrozzen) {
63
- console.warn("You can't append new item to frozzen element");
64
- return this;
59
+
60
+ export async function __addItem__(adder, pusher, item, referenceNode = null, index = null) {
61
+ const { element: itemsTargetEl, items } = this.itemsTarget;
62
+ if (["number", "string"].includes(typeof item)) item = text(item);
63
+ if (typeof item === "function" && isStateGetter(item)) {
64
+ const getter = item();
65
+ item = text(getter.value);
66
+ getter._subscribe(
67
+ (newValue) => { item.element.textContent = newValue; },
68
+ item
69
+ );
65
70
  }
66
- for (let i = 0; i < ele.length; i++) {
67
- if (["number", "string"].includes(typeof ele[i])) ele[i] = text(ele[i]);
68
- // Fix Items Latter
69
- if (ele[i] instanceof Function) {
70
- if (isStateGetter(ele[i])) {
71
- const getter = ele[i]();
72
- ele[i] = text(getter.value);
73
- getter._subscribe(
74
- (newValue) => (ele[i].element.textContent = newValue),
75
- ele[i]
76
- );
77
- // this.itemsTarget.element.appendChild(textNode);
78
- }
79
- }
80
- if (typeof globalThis?.Node === "function" && ele[i] instanceof globalThis?.Node) ele[i] = new this.constructor(ele[i]);
81
- if (ele[i]?.isUINode) {
82
- ele[i].cache.parent = this;
83
- itemsTarget_el?.[adder](ele[i].element);
84
- ele[i].target = this.itemsTarget.element;
85
- itemsTarget.items[pusher](ele[i]);
86
- }
87
- else if(ele[i] instanceof Promise){
88
- const UIEle = await ele[i]
89
- UIEle.cache.parent = this;
90
- itemsTarget_el?.[adder](UIEle.element);
91
- UIEle.target = this.itemsTarget.element;
92
- itemsTarget.items[pusher](UIEle)
93
- }
94
- else if (ele[i] instanceof Object) {
95
- if (ele[i]?.style) this.style(ele[i]?.style);
96
- if (ele[i]?.attr) {
97
- Object.entries(ele[i].attr).forEach((n) =>
98
- this.setAttr("" + n[0], n[1]),
99
- );
100
- }
101
- }
71
+ if (typeof globalThis?.Node === "function" && item instanceof globalThis.Node)
72
+ item = new this.constructor(item);
73
+ if (item instanceof Promise) item = await item;
74
+ if (item?.isUINode) {
75
+ item.cache.parent = this;
76
+ item.target = itemsTargetEl;
77
+ if (adder === "insertBefore" && itemsTargetEl)
78
+ itemsTargetEl.insertBefore(item.element, referenceNode);
79
+ else if (typeof itemsTargetEl?.[adder] === "function")
80
+ itemsTargetEl[adder](item.element);
81
+ if (pusher === "splice" && index !== null) items.splice(index, 0, item);
82
+ else if (typeof items?.[pusher] === "function") items[pusher](item);
83
+ return;
84
+ }
85
+ }
86
+
87
+ export async function __addItems__(adder, pusher, ...elements) {
88
+ for (const item of elements) {
89
+ await this.__addItem__(adder, pusher, item);
102
90
  }
103
91
  this.maintain();
104
92
  return this;
@@ -3,12 +3,9 @@ export function mount(target = this.target, delay = 0) {
3
3
  setTimeout(() => this.mount(target, 0), delay);
4
4
  return this;
5
5
  }
6
-
7
6
  if (this.isBody) return this;
8
-
9
7
  if (target?.isUIElement) target = target.element;
10
8
  this.target = target;
11
-
12
9
  this.target?.appendChild(this.element);
13
10
  return this;
14
11
  }
@@ -18,7 +15,6 @@ export function unmount(delay = 0) {
18
15
  setTimeout(() => this.unmount(0), delay);
19
16
  return this;
20
17
  }
21
-
22
18
  if (this.cache.parent) {
23
19
  this.cache.parent.remove(this);
24
20
  } else if (
@@ -1,5 +1,3 @@
1
- // file-based-router/index.js
2
-
3
1
  import {
4
2
  get_root,
5
3
  normalize_path,
@@ -10,17 +8,21 @@ import {
10
8
  renderer as ziko_renderer
11
9
  } from "../internal-utils/index.js";
12
10
 
13
- /**
14
- * Environment-independent file-based router core
15
- */
16
11
  export async function createFileBasedRouter({
17
12
  pages = {},
18
13
  url = typeof location !== 'undefined' ? location.pathname : '/',
19
14
  target = typeof document !== 'undefined' ? document.body : null,
20
15
  extensions = ['js', 'ts'],
16
+ base = '/',
21
17
  renderer = ziko_renderer,
22
18
  wrapper,
23
- base = '/',
19
+ namedExportHandler = {
20
+ Get: async (exportedFn, context) => {
21
+ if (typeof exportedFn === 'function') {
22
+ return await exportedFn(context);
23
+ }
24
+ }
25
+ }
24
26
  } = {}) {
25
27
  // Normalize target element safely for UI frameworks/DOM wrapper objects
26
28
  let mountTarget = target;
@@ -44,16 +46,20 @@ export async function createFileBasedRouter({
44
46
 
45
47
  let currentPath = rawPath.startsWith('/') ? rawPath : '/' + rawPath;
46
48
 
47
- // 3. Normalize route masks
49
+ // 3. Normalize route masks and collect modules
48
50
  const routes = Object.keys(pages);
49
51
  const root = get_root(routes);
50
52
 
51
53
  const pairs = {};
54
+ const modules = {};
55
+
52
56
  for (const route of routes) {
53
57
  const module = await pages[route]();
54
58
  const modComponent = await module.default;
55
59
  const normalizedKey = normalize_path(route, root, extensions);
60
+
56
61
  pairs[normalizedKey] = modComponent;
62
+ modules[normalizedKey] = { module, rawRoute: route };
57
63
  }
58
64
 
59
65
  // 4. Sort routes by precedence (Static -> Dynamic -> Catch-All -> Optional Catch-All)
@@ -76,7 +82,21 @@ export async function createFileBasedRouter({
76
82
 
77
83
  const params = is_dynamic(mask) ? dynamic_routes_parser(mask, currentPath) : {};
78
84
 
79
- // Execute renderer if a valid mount target is available
85
+ if (mask in modules) {
86
+ const { module, rawRoute } = modules[mask];
87
+ for (const exportName in namedExportHandler) {
88
+ if (exportName in module && typeof namedExportHandler[exportName] === 'function') {
89
+ await namedExportHandler[exportName](module[exportName], {
90
+ route: rawRoute,
91
+ mask,
92
+ module,
93
+ currentPath,
94
+ params
95
+ });
96
+ }
97
+ }
98
+ }
99
+
80
100
  if (mountTarget && typeof renderer === 'function') {
81
101
  await renderer(mountTarget, component, params, wrapper);
82
102
  }