@zyrab/domo 1.0.4 → 1.1.0
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 +1 -1
- package/src/core/Domo.js +49 -10
- package/src/core/Router.js +11 -13
package/package.json
CHANGED
package/src/core/Domo.js
CHANGED
|
@@ -63,9 +63,7 @@ class Domo {
|
|
|
63
63
|
* @returns {string[]}
|
|
64
64
|
*/
|
|
65
65
|
_parseClassList(input) {
|
|
66
|
-
return Array.isArray(input)
|
|
67
|
-
? input.filter(Boolean)
|
|
68
|
-
: String(input).split(" ").filter(Boolean);
|
|
66
|
+
return Array.isArray(input) ? input.filter(Boolean) : String(input).split(" ").filter(Boolean);
|
|
69
67
|
}
|
|
70
68
|
|
|
71
69
|
/**
|
|
@@ -264,7 +262,49 @@ class Domo {
|
|
|
264
262
|
});
|
|
265
263
|
return this;
|
|
266
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Appends children to the element.
|
|
267
|
+
* Alias for `child`.
|
|
268
|
+
* Accepts:
|
|
269
|
+
* - DOM nodes
|
|
270
|
+
* - Domo instances
|
|
271
|
+
* - DocumentFragments
|
|
272
|
+
* - Primitive strings/numbers (as text nodes)
|
|
273
|
+
* - Nested arrays of above
|
|
274
|
+
* @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} children
|
|
275
|
+
* @returns {Domo}
|
|
276
|
+
*/
|
|
277
|
+
append(children = []) {
|
|
278
|
+
return this.child(children);
|
|
279
|
+
}
|
|
267
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Appends the current element to a target parent.
|
|
283
|
+
* Accepts:
|
|
284
|
+
* - HTMLElement
|
|
285
|
+
* - Domo instance
|
|
286
|
+
* - DocumentFragment
|
|
287
|
+
* @param {HTMLElement|Domo|DocumentFragment} target
|
|
288
|
+
* @returns {Domo}
|
|
289
|
+
*/
|
|
290
|
+
appendTo(target) {
|
|
291
|
+
if (_handleElementInstance(target)) parent.appendChild(this.element);
|
|
292
|
+
return this;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Appends the current element to a target parent.
|
|
297
|
+
* Alias for `appendTo`.
|
|
298
|
+
* @param {HTMLElement|Domo|DocumentFragment} target
|
|
299
|
+
* @returns {Domo}
|
|
300
|
+
*/
|
|
301
|
+
parent(target) {
|
|
302
|
+
return this.appendTo(target);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
parent(parent) {
|
|
306
|
+
parent.appendChild(this.element);
|
|
307
|
+
}
|
|
268
308
|
/**
|
|
269
309
|
* Removes all children.
|
|
270
310
|
*/
|
|
@@ -286,13 +326,14 @@ class Domo {
|
|
|
286
326
|
* @param {(Node|string|number|Domo|DocumentFragment|Array<any>)[]} newChild
|
|
287
327
|
*/
|
|
288
328
|
replace(child, newChild) {
|
|
289
|
-
const
|
|
329
|
+
const newChild = this._handleElementInstance(newChild);
|
|
330
|
+
const child = this._handleElementInstance(child);
|
|
290
331
|
|
|
291
332
|
if (child === this.element) {
|
|
292
|
-
this.element.replaceWith(
|
|
293
|
-
this.element =
|
|
333
|
+
this.element.replaceWith(newChild);
|
|
334
|
+
this.element = newChild;
|
|
294
335
|
} else if (this.element.contains(child)) {
|
|
295
|
-
child.replaceWith(
|
|
336
|
+
child.replaceWith(newChild);
|
|
296
337
|
}
|
|
297
338
|
|
|
298
339
|
return this;
|
|
@@ -315,9 +356,7 @@ class Domo {
|
|
|
315
356
|
*/
|
|
316
357
|
if(condition) {
|
|
317
358
|
if (!condition) {
|
|
318
|
-
return new Domo("if")
|
|
319
|
-
.attr({ hidden: true })
|
|
320
|
-
.data({ if: this.element.tagName.toLowerCase() });
|
|
359
|
+
return new Domo("if").attr({ hidden: true }).data({ if: this.element.tagName.toLowerCase() });
|
|
321
360
|
}
|
|
322
361
|
return this;
|
|
323
362
|
}
|
package/src/core/Router.js
CHANGED
|
@@ -8,6 +8,10 @@ let _root;
|
|
|
8
8
|
function init() {
|
|
9
9
|
["DOMContentLoaded", "popstate"].forEach((event) =>
|
|
10
10
|
window.addEventListener(event, async () => {
|
|
11
|
+
if (!_root) {
|
|
12
|
+
_root = document.createElement("main");
|
|
13
|
+
_root.id = "main";
|
|
14
|
+
}
|
|
11
15
|
saveScroll(_previousUrl);
|
|
12
16
|
const url = path();
|
|
13
17
|
load(url);
|
|
@@ -20,7 +24,7 @@ function init() {
|
|
|
20
24
|
async function render({ component, meta }, params) {
|
|
21
25
|
try {
|
|
22
26
|
const content = await component(params);
|
|
23
|
-
_root
|
|
27
|
+
_root?.replaceChildren();
|
|
24
28
|
|
|
25
29
|
if (content instanceof HTMLElement) {
|
|
26
30
|
_root.appendChild(content);
|
|
@@ -33,9 +37,7 @@ async function render({ component, meta }, params) {
|
|
|
33
37
|
}
|
|
34
38
|
if (meta) {
|
|
35
39
|
document.title = meta?.title;
|
|
36
|
-
document
|
|
37
|
-
.querySelector("meta[name='description']")
|
|
38
|
-
.setAttribute("content", meta?.description);
|
|
40
|
+
document.querySelector("meta[name='description']").setAttribute("content", meta?.description);
|
|
39
41
|
}
|
|
40
42
|
} catch (error) {
|
|
41
43
|
console.error("Rendering error:", error);
|
|
@@ -115,13 +117,13 @@ async function load(url) {
|
|
|
115
117
|
function notify(info) {
|
|
116
118
|
_listeners.forEach((cb) => cb(info));
|
|
117
119
|
}
|
|
118
|
-
function saveScroll(path = window
|
|
119
|
-
_scrollPositions[path] = window
|
|
120
|
+
function saveScroll(path = window?.location?.pathname) {
|
|
121
|
+
_scrollPositions[path] = window?.scrollY;
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
function restoreScroll() {
|
|
123
|
-
const pos = _scrollPositions[window
|
|
124
|
-
window
|
|
125
|
+
const pos = _scrollPositions[window?.location?.pathname];
|
|
126
|
+
window?.scrollTo(0, pos || 0);
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
const Router = {
|
|
@@ -129,11 +131,7 @@ const Router = {
|
|
|
129
131
|
* Mount point of the router, where components will be rendered.
|
|
130
132
|
* @returns {HTMLElement} The root DOM element used by the router.
|
|
131
133
|
*/
|
|
132
|
-
mount: () =>
|
|
133
|
-
_root = document.createElement("main");
|
|
134
|
-
_root.id = "main";
|
|
135
|
-
return _root;
|
|
136
|
-
},
|
|
134
|
+
mount: () => _root,
|
|
137
135
|
|
|
138
136
|
/**
|
|
139
137
|
* Initializes the router by setting up event listeners and loading the initial route.
|