@domql/router 2.5.190 → 3.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/dist/cjs/index.js +26 -16
- package/dist/esm/index.js +18 -12
- package/index.js +38 -19
- package/package.json +4 -4
package/dist/cjs/index.js
CHANGED
|
@@ -16,22 +16,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
21
|
-
default: () =>
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
default: () => index_default,
|
|
22
22
|
getActiveRoute: () => getActiveRoute,
|
|
23
23
|
lastLevel: () => lastLevel,
|
|
24
24
|
lastPathname: () => lastPathname,
|
|
25
25
|
router: () => router
|
|
26
26
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
var import_event = require("@domql/event");
|
|
29
29
|
var import_utils = require("@domql/utils");
|
|
30
|
+
var import_set = require("@domql/element/set");
|
|
30
31
|
const getActiveRoute = (level = 0, route = import_utils.window.location.pathname) => {
|
|
31
32
|
const routeArray = route.split("/");
|
|
32
33
|
const activeRoute = routeArray[level + 1];
|
|
33
|
-
if (activeRoute)
|
|
34
|
-
return `/${activeRoute}`;
|
|
34
|
+
if (activeRoute) return `/${activeRoute}`;
|
|
35
35
|
};
|
|
36
36
|
let lastPathname;
|
|
37
37
|
let lastLevel = 0;
|
|
@@ -49,17 +49,21 @@ const defaultOptions = {
|
|
|
49
49
|
contentElementKey: "content",
|
|
50
50
|
scrollToOptions: { behavior: "smooth" }
|
|
51
51
|
};
|
|
52
|
-
const router = (path, el, state = {}, options = {}) => {
|
|
52
|
+
const router = async (path, el, state = {}, options = {}) => {
|
|
53
53
|
const element = el || void 0;
|
|
54
54
|
const win = element.context.window || import_utils.window;
|
|
55
55
|
const doc = element.context.document || import_utils.document;
|
|
56
|
-
const opts = {
|
|
56
|
+
const opts = {
|
|
57
|
+
...defaultOptions,
|
|
58
|
+
...element.context.routerOptions,
|
|
59
|
+
...options
|
|
60
|
+
};
|
|
57
61
|
lastLevel = opts.lastLevel;
|
|
58
62
|
const ref = element.__ref;
|
|
59
63
|
if (opts.contentElementKey !== "content" && opts.contentElementKey !== ref.contentElementKey || !ref.contentElementKey) {
|
|
60
64
|
ref.contentElementKey = opts.contentElementKey || "content";
|
|
61
65
|
}
|
|
62
|
-
const contentElementKey = (0,
|
|
66
|
+
const contentElementKey = (0, import_set.setContentKey)(element, opts);
|
|
63
67
|
const urlObj = new win.URL(win.location.origin + path);
|
|
64
68
|
const { pathname, search, hash } = urlObj;
|
|
65
69
|
const rootNode = element.node;
|
|
@@ -78,15 +82,21 @@ const router = (path, el, state = {}, options = {}) => {
|
|
|
78
82
|
}
|
|
79
83
|
if (pathChanged || !hashChanged) {
|
|
80
84
|
if (opts.updateState) {
|
|
81
|
-
element.state.update(
|
|
85
|
+
await element.state.update(
|
|
86
|
+
{ route, hash, debugging: false },
|
|
87
|
+
{ preventContentUpdate: true }
|
|
88
|
+
);
|
|
82
89
|
}
|
|
83
90
|
if (contentElementKey && opts.removeOldElement) {
|
|
84
91
|
element[contentElementKey].remove();
|
|
85
92
|
}
|
|
86
|
-
element.set(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
await element.set(
|
|
94
|
+
{
|
|
95
|
+
tag: opts.useFragment && "fragment",
|
|
96
|
+
extends: content
|
|
97
|
+
},
|
|
98
|
+
{ contentElementKey }
|
|
99
|
+
);
|
|
90
100
|
}
|
|
91
101
|
if (opts.scrollToTop) {
|
|
92
102
|
scrollNode.scrollTo({
|
|
@@ -113,6 +123,6 @@ const router = (path, el, state = {}, options = {}) => {
|
|
|
113
123
|
});
|
|
114
124
|
}
|
|
115
125
|
}
|
|
116
|
-
(0, import_event.triggerEventOn)("routeChanged", element, opts);
|
|
126
|
+
await (0, import_event.triggerEventOn)("routeChanged", element, opts);
|
|
117
127
|
};
|
|
118
|
-
var
|
|
128
|
+
var index_default = router;
|
package/dist/esm/index.js
CHANGED
|
@@ -18,12 +18,12 @@ var __spreadValues = (a, b) => {
|
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
20
|
import { triggerEventOn } from "@domql/event";
|
|
21
|
-
import {
|
|
21
|
+
import { document, window } from "@domql/utils";
|
|
22
|
+
import { setContentKey } from "@domql/element/set";
|
|
22
23
|
const getActiveRoute = (level = 0, route = window.location.pathname) => {
|
|
23
24
|
const routeArray = route.split("/");
|
|
24
25
|
const activeRoute = routeArray[level + 1];
|
|
25
|
-
if (activeRoute)
|
|
26
|
-
return `/${activeRoute}`;
|
|
26
|
+
if (activeRoute) return `/${activeRoute}`;
|
|
27
27
|
};
|
|
28
28
|
let lastPathname;
|
|
29
29
|
let lastLevel = 0;
|
|
@@ -41,7 +41,7 @@ const defaultOptions = {
|
|
|
41
41
|
contentElementKey: "content",
|
|
42
42
|
scrollToOptions: { behavior: "smooth" }
|
|
43
43
|
};
|
|
44
|
-
const router = (path, el, state = {}, options = {}) => {
|
|
44
|
+
const router = async (path, el, state = {}, options = {}) => {
|
|
45
45
|
const element = el || void 0;
|
|
46
46
|
const win = element.context.window || window;
|
|
47
47
|
const doc = element.context.document || document;
|
|
@@ -70,15 +70,21 @@ const router = (path, el, state = {}, options = {}) => {
|
|
|
70
70
|
}
|
|
71
71
|
if (pathChanged || !hashChanged) {
|
|
72
72
|
if (opts.updateState) {
|
|
73
|
-
element.state.update(
|
|
73
|
+
await element.state.update(
|
|
74
|
+
{ route, hash, debugging: false },
|
|
75
|
+
{ preventContentUpdate: true }
|
|
76
|
+
);
|
|
74
77
|
}
|
|
75
78
|
if (contentElementKey && opts.removeOldElement) {
|
|
76
79
|
element[contentElementKey].remove();
|
|
77
80
|
}
|
|
78
|
-
element.set(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
await element.set(
|
|
82
|
+
{
|
|
83
|
+
tag: opts.useFragment && "fragment",
|
|
84
|
+
extends: content
|
|
85
|
+
},
|
|
86
|
+
{ contentElementKey }
|
|
87
|
+
);
|
|
82
88
|
}
|
|
83
89
|
if (opts.scrollToTop) {
|
|
84
90
|
scrollNode.scrollTo(__spreadProps(__spreadValues({}, opts.scrollToOptions || {}), {
|
|
@@ -102,11 +108,11 @@ const router = (path, el, state = {}, options = {}) => {
|
|
|
102
108
|
}));
|
|
103
109
|
}
|
|
104
110
|
}
|
|
105
|
-
triggerEventOn("routeChanged", element, opts);
|
|
111
|
+
await triggerEventOn("routeChanged", element, opts);
|
|
106
112
|
};
|
|
107
|
-
var
|
|
113
|
+
var index_default = router;
|
|
108
114
|
export {
|
|
109
|
-
|
|
115
|
+
index_default as default,
|
|
110
116
|
getActiveRoute,
|
|
111
117
|
lastLevel,
|
|
112
118
|
lastPathname,
|
package/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
import { triggerEventOn } from '@domql/event'
|
|
4
|
-
import {
|
|
4
|
+
import { document, window } from '@domql/utils'
|
|
5
|
+
import { setContentKey } from '@domql/element/set'
|
|
5
6
|
|
|
6
7
|
export const getActiveRoute = (level = 0, route = window.location.pathname) => {
|
|
7
8
|
const routeArray = route.split('/')
|
|
@@ -27,20 +28,23 @@ const defaultOptions = {
|
|
|
27
28
|
scrollToOptions: { behavior: 'smooth' }
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
export const router = (
|
|
31
|
-
path,
|
|
32
|
-
el,
|
|
33
|
-
state = {},
|
|
34
|
-
options = {}
|
|
35
|
-
) => {
|
|
31
|
+
export const router = async (path, el, state = {}, options = {}) => {
|
|
36
32
|
const element = el || this
|
|
37
33
|
const win = element.context.window || window
|
|
38
34
|
const doc = element.context.document || document
|
|
39
|
-
const opts = {
|
|
35
|
+
const opts = {
|
|
36
|
+
...defaultOptions,
|
|
37
|
+
...element.context.routerOptions,
|
|
38
|
+
...options
|
|
39
|
+
}
|
|
40
40
|
lastLevel = opts.lastLevel
|
|
41
41
|
const ref = element.__ref
|
|
42
42
|
|
|
43
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
(opts.contentElementKey !== 'content' &&
|
|
45
|
+
opts.contentElementKey !== ref.contentElementKey) ||
|
|
46
|
+
!ref.contentElementKey
|
|
47
|
+
) {
|
|
44
48
|
ref.contentElementKey = opts.contentElementKey || 'content'
|
|
45
49
|
}
|
|
46
50
|
|
|
@@ -68,42 +72,57 @@ export const router = (
|
|
|
68
72
|
|
|
69
73
|
if (pathChanged || !hashChanged) {
|
|
70
74
|
if (opts.updateState) {
|
|
71
|
-
element.state.update(
|
|
75
|
+
await element.state.update(
|
|
76
|
+
{ route, hash, debugging: false },
|
|
77
|
+
{ preventContentUpdate: true }
|
|
78
|
+
)
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
if (contentElementKey && opts.removeOldElement) {
|
|
75
82
|
element[contentElementKey].remove()
|
|
76
83
|
}
|
|
77
84
|
|
|
78
|
-
element.set(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
await element.set(
|
|
86
|
+
{
|
|
87
|
+
tag: opts.useFragment && 'fragment',
|
|
88
|
+
extends: content
|
|
89
|
+
},
|
|
90
|
+
{ contentElementKey }
|
|
91
|
+
)
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
if (opts.scrollToTop) {
|
|
85
95
|
scrollNode.scrollTo({
|
|
86
|
-
...(opts.scrollToOptions || {}),
|
|
96
|
+
...(opts.scrollToOptions || {}),
|
|
97
|
+
top: 0,
|
|
98
|
+
left: 0
|
|
87
99
|
})
|
|
88
100
|
}
|
|
89
101
|
if (opts.scrollToNode) {
|
|
90
102
|
content[contentElementKey].node.scrollTo({
|
|
91
|
-
...(opts.scrollToOptions || {}),
|
|
103
|
+
...(opts.scrollToOptions || {}),
|
|
104
|
+
top: 0,
|
|
105
|
+
left: 0
|
|
92
106
|
})
|
|
93
107
|
}
|
|
94
108
|
|
|
95
109
|
if (hash) {
|
|
96
110
|
const activeNode = doc.getElementById(hash)
|
|
97
111
|
if (activeNode) {
|
|
98
|
-
const top =
|
|
112
|
+
const top =
|
|
113
|
+
activeNode.getBoundingClientRect().top +
|
|
114
|
+
rootNode.scrollTop -
|
|
115
|
+
opts.scrollToOffset || 0
|
|
99
116
|
scrollNode.scrollTo({
|
|
100
|
-
...(opts.scrollToOptions || {}),
|
|
117
|
+
...(opts.scrollToOptions || {}),
|
|
118
|
+
top,
|
|
119
|
+
left: 0
|
|
101
120
|
})
|
|
102
121
|
}
|
|
103
122
|
}
|
|
104
123
|
|
|
105
124
|
// trigger `on.routeChanged`
|
|
106
|
-
triggerEventOn('routeChanged', element, opts)
|
|
125
|
+
await triggerEventOn('routeChanged', element, opts)
|
|
107
126
|
}
|
|
108
127
|
|
|
109
128
|
export default router
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"prepublish": "rimraf -I dist && npm run build && npm run copy:package:cjs"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@domql/event": "^
|
|
26
|
-
"@domql/utils": "^
|
|
25
|
+
"@domql/event": "^3.0.0",
|
|
26
|
+
"@domql/utils": "^3.0.0"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "bcbdc271a602b958de6a60ab387ea7715a935dc1",
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@babel/core": "^7.12.0"
|
|
31
31
|
}
|