@domql/router 2.5.139 → 2.5.140
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 +22 -18
- package/index.js +25 -19
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -49,18 +49,22 @@ const defaultOptions = {
|
|
|
49
49
|
contentElementKey: "content",
|
|
50
50
|
scrollToOptions: { behavior: "smooth" }
|
|
51
51
|
};
|
|
52
|
-
const router = (path, element, state = {},
|
|
52
|
+
const router = (path, element, state = {}, options = {}) => {
|
|
53
53
|
const win = element.context.window || import_utils.window;
|
|
54
54
|
const doc = element.context.document || import_utils.document;
|
|
55
|
-
const
|
|
56
|
-
lastLevel =
|
|
57
|
-
const
|
|
55
|
+
const opts = { ...defaultOptions, ...element.context.routerOptions, ...options };
|
|
56
|
+
lastLevel = opts.lastLevel;
|
|
57
|
+
const ref = element.__ref;
|
|
58
|
+
if (opts.contentElementKey !== "content" && opts.contentElementKey !== ref.contentElementKey || !ref.contentElementKey) {
|
|
59
|
+
ref.contentElementKey = opts.contentElementKey || "content";
|
|
60
|
+
}
|
|
61
|
+
const contentElementKey = (0, import_utils.setContentKey)(element, opts);
|
|
58
62
|
const urlObj = new win.URL(win.location.origin + path);
|
|
59
63
|
const { pathname, search, hash } = urlObj;
|
|
60
64
|
const rootNode = element.node;
|
|
61
|
-
const route = getActiveRoute(
|
|
65
|
+
const route = getActiveRoute(opts.level, pathname);
|
|
62
66
|
const content = element.routes[route || "/"] || element.routes["/*"];
|
|
63
|
-
const scrollNode =
|
|
67
|
+
const scrollNode = opts.scrollToNode ? rootNode : opts.scrollNode;
|
|
64
68
|
const hashChanged = hash && hash !== win.location.hash.slice(1);
|
|
65
69
|
const pathChanged = pathname !== lastPathname;
|
|
66
70
|
lastPathname = pathname;
|
|
@@ -68,31 +72,31 @@ const router = (path, element, state = {}, passedOptions = {}) => {
|
|
|
68
72
|
element.state.root.debugging = false;
|
|
69
73
|
return;
|
|
70
74
|
}
|
|
71
|
-
if (
|
|
75
|
+
if (opts.pushState) {
|
|
72
76
|
win.history.pushState(state, null, pathname + (search || "") + (hash || ""));
|
|
73
77
|
}
|
|
74
78
|
if (pathChanged || !hashChanged) {
|
|
75
|
-
if (
|
|
79
|
+
if (opts.updateState) {
|
|
76
80
|
element.state.update({ route, hash, debugging: false }, { preventContentUpdate: true });
|
|
77
81
|
}
|
|
78
|
-
if (contentElementKey &&
|
|
82
|
+
if (contentElementKey && opts.removeOldElement) {
|
|
79
83
|
element[contentElementKey].remove();
|
|
80
84
|
}
|
|
81
85
|
element.set({
|
|
82
|
-
tag:
|
|
86
|
+
tag: opts.useFragment && "fragment",
|
|
83
87
|
extend: content
|
|
84
88
|
}, { contentElementKey });
|
|
85
89
|
}
|
|
86
|
-
if (
|
|
90
|
+
if (opts.scrollToTop) {
|
|
87
91
|
scrollNode.scrollTo({
|
|
88
|
-
...
|
|
92
|
+
...opts.scrollToOptions || {},
|
|
89
93
|
top: 0,
|
|
90
94
|
left: 0
|
|
91
95
|
});
|
|
92
96
|
}
|
|
93
|
-
if (
|
|
94
|
-
content.
|
|
95
|
-
...
|
|
97
|
+
if (opts.scrollToNode) {
|
|
98
|
+
content[contentElementKey].node.scrollTo({
|
|
99
|
+
...opts.scrollToOptions || {},
|
|
96
100
|
top: 0,
|
|
97
101
|
left: 0
|
|
98
102
|
});
|
|
@@ -100,14 +104,14 @@ const router = (path, element, state = {}, passedOptions = {}) => {
|
|
|
100
104
|
if (hash) {
|
|
101
105
|
const activeNode = doc.getElementById(hash);
|
|
102
106
|
if (activeNode) {
|
|
103
|
-
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop -
|
|
107
|
+
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - opts.scrollToOffset || 0;
|
|
104
108
|
scrollNode.scrollTo({
|
|
105
|
-
...
|
|
109
|
+
...opts.scrollToOptions || {},
|
|
106
110
|
top,
|
|
107
111
|
left: 0
|
|
108
112
|
});
|
|
109
113
|
}
|
|
110
114
|
}
|
|
111
|
-
(0, import_event.triggerEventOn)("routeChanged", element,
|
|
115
|
+
(0, import_event.triggerEventOn)("routeChanged", element, opts);
|
|
112
116
|
};
|
|
113
117
|
var router_default = router;
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
import { triggerEventOn } from '@domql/event'
|
|
4
|
-
import { document, window } from '@domql/utils'
|
|
4
|
+
import { setContentKey, document, window } from '@domql/utils'
|
|
5
5
|
|
|
6
6
|
export const getActiveRoute = (level = 0, route = window.location.pathname) => {
|
|
7
7
|
const routeArray = route.split('/')
|
|
@@ -31,21 +31,27 @@ export const router = (
|
|
|
31
31
|
path,
|
|
32
32
|
element,
|
|
33
33
|
state = {},
|
|
34
|
-
|
|
34
|
+
options = {}
|
|
35
35
|
) => {
|
|
36
36
|
const win = element.context.window || window
|
|
37
37
|
const doc = element.context.document || document
|
|
38
|
-
const
|
|
39
|
-
lastLevel =
|
|
40
|
-
const
|
|
38
|
+
const opts = { ...defaultOptions, ...element.context.routerOptions, ...options }
|
|
39
|
+
lastLevel = opts.lastLevel
|
|
40
|
+
const ref = element.__ref
|
|
41
|
+
|
|
42
|
+
if ((opts.contentElementKey !== 'content' && opts.contentElementKey !== ref.contentElementKey) || !ref.contentElementKey) {
|
|
43
|
+
ref.contentElementKey = opts.contentElementKey || 'content'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const contentElementKey = setContentKey(element, opts)
|
|
41
47
|
|
|
42
48
|
const urlObj = new win.URL(win.location.origin + path)
|
|
43
49
|
const { pathname, search, hash } = urlObj
|
|
44
50
|
|
|
45
51
|
const rootNode = element.node
|
|
46
|
-
const route = getActiveRoute(
|
|
52
|
+
const route = getActiveRoute(opts.level, pathname)
|
|
47
53
|
const content = element.routes[route || '/'] || element.routes['/*']
|
|
48
|
-
const scrollNode =
|
|
54
|
+
const scrollNode = opts.scrollToNode ? rootNode : opts.scrollNode
|
|
49
55
|
const hashChanged = hash && hash !== win.location.hash.slice(1)
|
|
50
56
|
const pathChanged = pathname !== lastPathname
|
|
51
57
|
lastPathname = pathname
|
|
@@ -55,48 +61,48 @@ export const router = (
|
|
|
55
61
|
return
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
if (
|
|
64
|
+
if (opts.pushState) {
|
|
59
65
|
win.history.pushState(state, null, pathname + (search || '') + (hash || ''))
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
if (pathChanged || !hashChanged) {
|
|
63
|
-
if (
|
|
69
|
+
if (opts.updateState) {
|
|
64
70
|
element.state.update({ route, hash, debugging: false }, { preventContentUpdate: true })
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
if (contentElementKey &&
|
|
73
|
+
if (contentElementKey && opts.removeOldElement) {
|
|
68
74
|
element[contentElementKey].remove()
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
element.set({
|
|
72
|
-
tag:
|
|
78
|
+
tag: opts.useFragment && 'fragment',
|
|
73
79
|
extend: content
|
|
74
80
|
}, { contentElementKey })
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
if (
|
|
83
|
+
if (opts.scrollToTop) {
|
|
78
84
|
scrollNode.scrollTo({
|
|
79
|
-
...(
|
|
85
|
+
...(opts.scrollToOptions || {}), top: 0, left: 0
|
|
80
86
|
})
|
|
81
87
|
}
|
|
82
|
-
if (
|
|
83
|
-
content.
|
|
84
|
-
...(
|
|
88
|
+
if (opts.scrollToNode) {
|
|
89
|
+
content[contentElementKey].node.scrollTo({
|
|
90
|
+
...(opts.scrollToOptions || {}), top: 0, left: 0
|
|
85
91
|
})
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
if (hash) {
|
|
89
95
|
const activeNode = doc.getElementById(hash)
|
|
90
96
|
if (activeNode) {
|
|
91
|
-
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop -
|
|
97
|
+
const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - opts.scrollToOffset || 0
|
|
92
98
|
scrollNode.scrollTo({
|
|
93
|
-
...(
|
|
99
|
+
...(opts.scrollToOptions || {}), top, left: 0
|
|
94
100
|
})
|
|
95
101
|
}
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
// trigger `on.routeChanged`
|
|
99
|
-
triggerEventOn('routeChanged', element,
|
|
105
|
+
triggerEventOn('routeChanged', element, opts)
|
|
100
106
|
}
|
|
101
107
|
|
|
102
108
|
export default router
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/router",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.140",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@domql/utils": "^2.5.
|
|
25
|
+
"@domql/utils": "^2.5.140"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "2fc2e5d07523014581b0747b4201cc6afea32860",
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@babel/core": "^7.12.0"
|
|
30
30
|
}
|