olova-router 1.0.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/README.md +18 -0
- package/dist/router.d.ts +1 -0
- package/dist/router.js +40 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# olova
|
|
2
|
+
|
|
3
|
+
```html
|
|
4
|
+
<div id="app">
|
|
5
|
+
<template>
|
|
6
|
+
<div i-data="{count : 0}">
|
|
7
|
+
<div>{ count }</div>
|
|
8
|
+
<button @click="count++">Increment</button>
|
|
9
|
+
</div>
|
|
10
|
+
</template>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
<script type="module">
|
|
14
|
+
import { createApp } from "//unpkg.com/olova";
|
|
15
|
+
const app = createApp();
|
|
16
|
+
app.mount("#app");
|
|
17
|
+
</script>
|
|
18
|
+
```
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module "router";
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createApp } from "olova";
|
|
2
|
+
class Router {
|
|
3
|
+
constructor() {
|
|
4
|
+
(this.routes = new Map()), (this.currentApp = null), this.init();
|
|
5
|
+
}
|
|
6
|
+
init() {
|
|
7
|
+
window.addEventListener("popstate", () => this.handleRoute()),
|
|
8
|
+
document.addEventListener("click", (t) => {
|
|
9
|
+
if (t.target.matches("a")) {
|
|
10
|
+
t.preventDefault();
|
|
11
|
+
const e = t.target.getAttribute("href");
|
|
12
|
+
this.navigate(e);
|
|
13
|
+
}
|
|
14
|
+
}),
|
|
15
|
+
window.addEventListener("load", () => {
|
|
16
|
+
this.handleRoute();
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
add(t, e) {
|
|
20
|
+
return this.routes.set(t, e), this;
|
|
21
|
+
}
|
|
22
|
+
navigate(t) {
|
|
23
|
+
window.history.pushState(null, "", t), this.handleRoute();
|
|
24
|
+
}
|
|
25
|
+
handleRoute() {
|
|
26
|
+
const t = window.location.pathname,
|
|
27
|
+
e = this.routes.get(t),
|
|
28
|
+
n = document.querySelector("[i-view]");
|
|
29
|
+
if (n)
|
|
30
|
+
if (e) {
|
|
31
|
+
this.currentApp && (n.innerHTML = "");
|
|
32
|
+
const t = document.createElement("div");
|
|
33
|
+
(t.id = "route-container"),
|
|
34
|
+
n.appendChild(t),
|
|
35
|
+
(this.currentApp = createApp({ ...e })),
|
|
36
|
+
this.currentApp.mount("#route-container");
|
|
37
|
+
} else n.innerHTML = "<h1>404 Not Found</h1>";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export const router = new Router();
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "olova-router",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": " olova-router for olovaJs",
|
|
5
|
+
"main": "dist/router.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"javascript",
|
|
8
|
+
"framework",
|
|
9
|
+
"reactive",
|
|
10
|
+
"olova",
|
|
11
|
+
"olovajs"
|
|
12
|
+
],
|
|
13
|
+
"author": "Nazmul Hossain",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"olova": "latest"
|
|
17
|
+
}
|
|
18
|
+
}
|