@domql/router 2.3.131 → 2.3.134
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/dist/cjs/index.js +35 -25
- package/index.js +29 -20
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -21,20 +21,22 @@ __export(router_exports, {
|
|
|
21
21
|
default: () => router_default,
|
|
22
22
|
getActiveRoute: () => getActiveRoute,
|
|
23
23
|
lastLevel: () => lastLevel,
|
|
24
|
+
lastPathname: () => lastPathname,
|
|
24
25
|
router: () => router
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(router_exports);
|
|
27
28
|
var import_globals = require("@domql/globals");
|
|
28
29
|
var import_utils = require("@domql/utils");
|
|
29
|
-
var _a;
|
|
30
30
|
const getActiveRoute = (route = import_globals.window.location.pathname, level) => `/${route.split("/")[level + 1]}`;
|
|
31
|
+
let lastPathname;
|
|
31
32
|
let lastLevel = 0;
|
|
32
33
|
const defaultOptions = {
|
|
33
34
|
level: lastLevel,
|
|
34
35
|
pushState: true,
|
|
36
|
+
initialRender: false,
|
|
35
37
|
scrollToTop: true,
|
|
36
38
|
scrollToNode: false,
|
|
37
|
-
scrollNode:
|
|
39
|
+
scrollNode: import_globals.document && import_globals.document.documentElement,
|
|
38
40
|
scrollBody: false,
|
|
39
41
|
scrollDocument: true,
|
|
40
42
|
useFragment: false,
|
|
@@ -46,34 +48,42 @@ const router = (path, element, state = {}, options = defaultOptions) => {
|
|
|
46
48
|
(0, import_utils.merge)(options, defaultOptions);
|
|
47
49
|
lastLevel = options.lastLevel;
|
|
48
50
|
const [pathname, hash] = path.split("#");
|
|
51
|
+
const rootNode = element.node;
|
|
49
52
|
const route = getActiveRoute(pathname, options.level);
|
|
50
53
|
const content = element.routes[route] || element.routes["/*"];
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
)
|
|
54
|
+
const scrollNode = options.scrollDocument ? import_globals.document.documentElement : rootNode;
|
|
55
|
+
const hashChanged = hash && hash !== import_globals.window.location.hash.slice(1);
|
|
56
|
+
const pathChanged = pathname !== lastPathname;
|
|
57
|
+
lastPathname = pathname;
|
|
58
|
+
if (content) {
|
|
59
|
+
if (options.pushState)
|
|
60
|
+
import_globals.window.history.pushState(state, null, route + (hash ? `#${hash}` : ""));
|
|
61
|
+
if (pathChanged || !hashChanged) {
|
|
62
|
+
element.set({ tag: options.useFragment && "fragment", extend: content });
|
|
63
|
+
if (options.updateState) {
|
|
64
|
+
element.state.update({ route, hash }, {
|
|
65
|
+
preventContentUpdate: !options.stateContentUpdate
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (options.scrollToTop) {
|
|
70
|
+
scrollNode.scrollTo({
|
|
71
|
+
...options.scrollToOptions || {},
|
|
72
|
+
top: 0,
|
|
73
|
+
left: 0
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (options.scrollToNode) {
|
|
77
|
+
content.content.node.scrollTo({
|
|
78
|
+
...options.scrollToOptions || {},
|
|
79
|
+
top: 0,
|
|
80
|
+
left: 0
|
|
81
|
+
});
|
|
82
|
+
}
|
|
74
83
|
}
|
|
75
84
|
if (hash) {
|
|
76
85
|
const activeNode = import_globals.document.getElementById(hash);
|
|
86
|
+
console.log(hash, activeNode);
|
|
77
87
|
if (activeNode) {
|
|
78
88
|
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0;
|
|
79
89
|
scrollNode.scrollTo({
|
package/index.js
CHANGED
|
@@ -7,14 +7,16 @@ export const getActiveRoute = (
|
|
|
7
7
|
route = window.location.pathname, level
|
|
8
8
|
) => `/${route.split('/')[level + 1]}`
|
|
9
9
|
|
|
10
|
+
export let lastPathname
|
|
10
11
|
export let lastLevel = 0
|
|
11
12
|
|
|
12
13
|
const defaultOptions = {
|
|
13
14
|
level: lastLevel,
|
|
14
15
|
pushState: true,
|
|
16
|
+
initialRender: false,
|
|
15
17
|
scrollToTop: true,
|
|
16
18
|
scrollToNode: false,
|
|
17
|
-
scrollNode: document
|
|
19
|
+
scrollNode: document && document.documentElement,
|
|
18
20
|
scrollBody: false,
|
|
19
21
|
scrollDocument: true,
|
|
20
22
|
useFragment: false,
|
|
@@ -34,34 +36,41 @@ export const router = (
|
|
|
34
36
|
|
|
35
37
|
const [pathname, hash] = path.split('#')
|
|
36
38
|
|
|
39
|
+
const rootNode = element.node
|
|
37
40
|
const route = getActiveRoute(pathname, options.level)
|
|
38
41
|
const content = element.routes[route] || element.routes['/*']
|
|
42
|
+
const scrollNode = options.scrollDocument ? document.documentElement : rootNode
|
|
43
|
+
const hashChanged = hash && hash !== window.location.hash.slice(1)
|
|
44
|
+
const pathChanged = pathname !== lastPathname
|
|
45
|
+
lastPathname = pathname
|
|
39
46
|
|
|
40
|
-
if (
|
|
41
|
-
|
|
47
|
+
if (content) {
|
|
48
|
+
if (options.pushState) window.history.pushState(state, null, route + (hash ? `#${hash}` : ''))
|
|
42
49
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
if (pathChanged || !hashChanged) {
|
|
51
|
+
element.set({ tag: options.useFragment && 'fragment', extend: content })
|
|
52
|
+
if (options.updateState) {
|
|
53
|
+
element.state.update({ route, hash }, {
|
|
54
|
+
preventContentUpdate: !options.stateContentUpdate
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
49
58
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
)
|
|
59
|
+
if (options.scrollToTop) {
|
|
60
|
+
scrollNode.scrollTo({
|
|
61
|
+
...(options.scrollToOptions || {}), top: 0, left: 0
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
if (options.scrollToNode) {
|
|
65
|
+
content.content.node.scrollTo({
|
|
66
|
+
...(options.scrollToOptions || {}), top: 0, left: 0
|
|
67
|
+
})
|
|
68
|
+
}
|
|
61
69
|
}
|
|
62
70
|
|
|
63
71
|
if (hash) {
|
|
64
72
|
const activeNode = document.getElementById(hash)
|
|
73
|
+
console.log(hash, activeNode)
|
|
65
74
|
if (activeNode) {
|
|
66
75
|
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0
|
|
67
76
|
scrollNode.scrollTo({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/router",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.134",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@domql/globals": "latest",
|
|
26
26
|
"@domql/utils": "latest"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "51e57aa34b812e2624c94ccc80bfd5095c1609fb",
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@babel/core": "^7.12.0"
|
|
31
31
|
}
|