@domql/router 2.2.4 → 2.2.7
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 +28 -24
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,34 +1,38 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const getActiveRoute = (
|
|
4
|
+
route = window.location.pathname, level
|
|
5
|
+
) => `/${route.split('/')[level + 1]}`
|
|
4
6
|
|
|
5
|
-
export
|
|
6
|
-
const route = path || window.location.pathname
|
|
7
|
+
export let lastLevel = 0
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
9
|
+
export const router = (
|
|
10
|
+
element,
|
|
11
|
+
path,
|
|
12
|
+
state = {},
|
|
13
|
+
level = 0,
|
|
14
|
+
pushState = true
|
|
15
|
+
) => {
|
|
16
|
+
const lastLevel = level
|
|
17
|
+
const [ pathname, hash ] = (path).split('#')
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
let route = getActiveRoute(pathname, level)
|
|
20
|
+
const content = element.routes[route]
|
|
19
21
|
|
|
20
22
|
if (content) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
document.getElementById(
|
|
23
|
+
if (pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
|
|
24
|
+
|
|
25
|
+
element.set({ proto: content })
|
|
26
|
+
element.state.update({ route, hash })
|
|
27
|
+
|
|
28
|
+
const rootNode = element.node
|
|
29
|
+
rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
|
|
30
|
+
if (hash) {
|
|
31
|
+
const activeNode = document.getElementById(hash)
|
|
32
|
+
if (activeNode) {
|
|
33
|
+
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 140
|
|
34
|
+
rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
|
|
35
|
+
}
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
|
-
|
|
33
|
-
if (level === 0) rootElement.state.update({ currentRoute })
|
|
34
38
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/router",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.7",
|
|
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": "9952e1abd4466080e0f8d5031fe4962694746591",
|
|
10
10
|
"source": "index.js"
|
|
11
11
|
}
|