@domql/router 2.0.5 → 2.2.3
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/index.js +23 -6
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
export const splitRoute = (route = window.location.pathname) => route.
|
|
3
|
+
export const splitRoute = (route = window.location.pathname) => route.split('/')
|
|
4
4
|
|
|
5
5
|
export const router = (rootElement, path, state = {}, level = 0, pushState = true) => {
|
|
6
6
|
const route = path || window.location.pathname
|
|
7
|
+
|
|
8
|
+
if (path.slice(0, 1) === '#') {
|
|
9
|
+
window.location.hash = path
|
|
10
|
+
}
|
|
11
|
+
|
|
7
12
|
const routes = splitRoute(route)
|
|
13
|
+
let currentRoute = routes[level + 1]
|
|
8
14
|
|
|
9
|
-
const
|
|
15
|
+
const hasHash = currentRoute.split('#')
|
|
16
|
+
if (hasHash[1]) currentRoute = hasHash[0]
|
|
17
|
+
|
|
18
|
+
const content = rootElement.routes[`/${currentRoute}`]
|
|
10
19
|
|
|
11
20
|
if (content) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
21
|
+
currentRoute = `/${currentRoute}`
|
|
22
|
+
if (hasHash[1]) currentRoute += `#${hasHash[1]}`
|
|
23
|
+
if (pushState) window.history.pushState(state, null, currentRoute)
|
|
24
|
+
|
|
25
|
+
const newContent = rootElement.set({ proto: content }).content.node
|
|
26
|
+
newContent.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
|
27
|
+
console.log(newContent)
|
|
28
|
+
if (hasHash[1]) {
|
|
29
|
+
// window.location.hash = hasHash[1]
|
|
30
|
+
document.getElementById(hasHash[1]).scrollIntoView({ behavior: 'smooth', block: 'start' })
|
|
31
|
+
}
|
|
15
32
|
}
|
|
16
33
|
|
|
17
|
-
if (level === 0) rootElement.state.update({
|
|
34
|
+
if (level === 0) rootElement.state.update({ currentRoute })
|
|
18
35
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"vpatch": "npm version patch && npm publish --access public"
|
|
8
8
|
},
|
|
9
|
-
"gitHead": "
|
|
9
|
+
"gitHead": "140b4437092d6a5efaf99cc32f04fedd55faac0c",
|
|
10
10
|
"source": "index.js"
|
|
11
11
|
}
|