@lexriver/dome 1.5.3 → 1.5.6
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/out/DomeRouter.d.ts +2 -13
- package/out/DomeRouter.js +53 -62
- package/package.json +1 -1
- package/src/DomeRouter.ts +57 -62
package/out/DomeRouter.d.ts
CHANGED
|
@@ -1,24 +1,14 @@
|
|
|
1
1
|
export declare type RouteAction = (params: {
|
|
2
2
|
[key: string]: string;
|
|
3
3
|
}, url: string, scrollToPreviousPositionFunctionAsync: () => Promise<void>) => void | Promise<void>;
|
|
4
|
-
interface HistoryUrl {
|
|
5
|
-
url: string;
|
|
6
|
-
scroll: number;
|
|
7
|
-
}
|
|
8
4
|
export declare module DomeRouter {
|
|
9
5
|
let maxHistoryUrlsCount: number;
|
|
10
6
|
function navigate(url: string): void;
|
|
11
7
|
function goBack(): void;
|
|
12
8
|
function goForward(): void;
|
|
13
9
|
function changeUrl(url: string): void;
|
|
14
|
-
function getCurrentUrl(): string
|
|
15
|
-
|
|
16
|
-
* get previous page url navigated by router
|
|
17
|
-
* @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
|
|
18
|
-
*/
|
|
19
|
-
function getPreviousPageUrl(previousPageIndex?: number): string | undefined;
|
|
20
|
-
function reloadCurrentPage(addToHistory?: boolean): void;
|
|
21
|
-
function resolveUrl(url?: string, addToHistory?: boolean): void;
|
|
10
|
+
function getCurrentUrl(): string;
|
|
11
|
+
function resolveUrl(url?: string): void;
|
|
22
12
|
function onRoute(route: string, exactMatch: boolean, action: RouteAction): void;
|
|
23
13
|
function onNotFound(action: () => void): void;
|
|
24
14
|
/**
|
|
@@ -33,4 +23,3 @@ export declare module DomeRouter {
|
|
|
33
23
|
*/
|
|
34
24
|
function checkUrlMatchRouteAndGetParameters(url: string, route: string, exactMatch?: boolean): Object | undefined;
|
|
35
25
|
}
|
|
36
|
-
export {};
|
package/out/DomeRouter.js
CHANGED
|
@@ -1,42 +1,37 @@
|
|
|
1
1
|
import { DomeManipulator } from "./DomeManipulator";
|
|
2
2
|
const filename = '[DomeRouter]';
|
|
3
|
+
// interface HistoryUrl{
|
|
4
|
+
// url:string
|
|
5
|
+
// scroll:number
|
|
6
|
+
// }
|
|
3
7
|
export var DomeRouter;
|
|
4
8
|
(function (DomeRouter) {
|
|
5
|
-
const historyUrls = []
|
|
9
|
+
//const historyUrls:HistoryUrl[] = []
|
|
10
|
+
const scrollPositionByUrl = new Map();
|
|
6
11
|
DomeRouter.maxHistoryUrlsCount = 20;
|
|
7
12
|
const allRoutes = [];
|
|
8
13
|
let onNotFoundAction = undefined;
|
|
9
14
|
window.addEventListener('popstate', async (e) => {
|
|
10
|
-
// on go back
|
|
11
|
-
console.log(filename, 'window.popstate event', e, 'historyUrls=', historyUrls)
|
|
15
|
+
// on go back/forward
|
|
16
|
+
//console.log(filename, 'window.popstate event', e, 'historyUrls=', historyUrls)
|
|
12
17
|
const url = window.location.pathname;
|
|
13
|
-
addUrlToHistory(url);
|
|
14
18
|
await executeAsync(url, getScrollPositionForUrl(url));
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
await DomeManipulator.scrollToAsync({
|
|
20
|
+
pxFromTop: getScrollPositionForUrl(window.location.pathname)
|
|
21
|
+
});
|
|
18
22
|
});
|
|
19
23
|
function getScrollPositionForUrl(url) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return 0;
|
|
30
|
-
// for(let item of historyUrls){
|
|
31
|
-
// if(item.url === url){
|
|
32
|
-
// result = item.scroll
|
|
33
|
-
// }
|
|
34
|
-
// }
|
|
35
|
-
// return result
|
|
24
|
+
return scrollPositionByUrl.get(url) ?? 0;
|
|
25
|
+
}
|
|
26
|
+
function saveScrollPositionForUrl(url) {
|
|
27
|
+
scrollPositionByUrl.set(url, DomeManipulator.getCurrentScrollPosition());
|
|
28
|
+
}
|
|
29
|
+
function saveScrollPositionForCurrentUrl() {
|
|
30
|
+
scrollPositionByUrl.set(getCurrentUrl(), DomeManipulator.getCurrentScrollPosition());
|
|
36
31
|
}
|
|
37
32
|
function navigate(url) {
|
|
33
|
+
saveScrollPositionForCurrentUrl();
|
|
38
34
|
window.history.pushState(null, '', url);
|
|
39
|
-
addUrlToHistory(url);
|
|
40
35
|
executeAsync(url, 0);
|
|
41
36
|
DomeManipulator.scrollToTop();
|
|
42
37
|
}
|
|
@@ -51,49 +46,45 @@ export var DomeRouter;
|
|
|
51
46
|
DomeRouter.goForward = goForward;
|
|
52
47
|
function changeUrl(url) {
|
|
53
48
|
window.history.replaceState(null, '', url);
|
|
54
|
-
if
|
|
55
|
-
|
|
56
|
-
}
|
|
49
|
+
// if(historyUrls.length>0){
|
|
50
|
+
// historyUrls[historyUrls.length-1].url = url
|
|
51
|
+
// }
|
|
57
52
|
}
|
|
58
53
|
DomeRouter.changeUrl = changeUrl;
|
|
59
|
-
function addUrlToHistory(url)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
54
|
+
// function addUrlToHistory(url:string){
|
|
55
|
+
// historyUrls.push({url, scroll: 0})
|
|
56
|
+
// while(historyUrls.length>maxHistoryUrlsCount){
|
|
57
|
+
// historyUrls.shift()
|
|
58
|
+
// }
|
|
59
|
+
// // save scroll position to previous url
|
|
60
|
+
// if(historyUrls.length>1){
|
|
61
|
+
// historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
|
|
62
|
+
// }
|
|
63
|
+
// console.log('adding url to history', url)
|
|
64
|
+
// console.log('historyUrls=', historyUrls)
|
|
65
|
+
// }
|
|
71
66
|
function getCurrentUrl() {
|
|
72
|
-
return
|
|
67
|
+
return window.location.pathname;
|
|
68
|
+
// return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
|
|
73
69
|
}
|
|
74
70
|
DomeRouter.getCurrentUrl = getCurrentUrl;
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
function getPreviousPageUrl(previousPageIndex
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
DomeRouter.reloadCurrentPage = reloadCurrentPage;
|
|
94
|
-
function resolveUrl(url = window.location.pathname, addToHistory = true) {
|
|
95
|
-
if (addToHistory)
|
|
96
|
-
addUrlToHistory(window.location.pathname);
|
|
71
|
+
// /**
|
|
72
|
+
// * get previous page url navigated by router
|
|
73
|
+
// * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
|
|
74
|
+
// */
|
|
75
|
+
// export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
|
|
76
|
+
// //if(historyUrls.length==0) return undefined
|
|
77
|
+
// let index = historyUrls.length-2-previousPageIndex
|
|
78
|
+
// if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
|
|
79
|
+
// return undefined
|
|
80
|
+
// }
|
|
81
|
+
// export function reloadCurrentPage(addToHistory:boolean = false){
|
|
82
|
+
// const url = window.location.pathname
|
|
83
|
+
// if(addToHistory) addUrlToHistory(url)
|
|
84
|
+
// executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
|
|
85
|
+
// }
|
|
86
|
+
function resolveUrl(url = window.location.pathname) {
|
|
87
|
+
// if(addToHistory) addUrlToHistory(window.location.pathname)
|
|
97
88
|
executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
|
|
98
89
|
}
|
|
99
90
|
DomeRouter.resolveUrl = resolveUrl;
|
package/package.json
CHANGED
package/src/DomeRouter.ts
CHANGED
|
@@ -14,51 +14,44 @@ interface Route{
|
|
|
14
14
|
action:RouteAction
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
interface HistoryUrl{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
17
|
+
// interface HistoryUrl{
|
|
18
|
+
// url:string
|
|
19
|
+
// scroll:number
|
|
20
|
+
// }
|
|
21
21
|
|
|
22
22
|
export module DomeRouter {
|
|
23
|
-
const historyUrls:HistoryUrl[] = []
|
|
23
|
+
//const historyUrls:HistoryUrl[] = []
|
|
24
|
+
const scrollPositionByUrl = new Map<string, number>()
|
|
24
25
|
export let maxHistoryUrlsCount:number = 20
|
|
25
26
|
const allRoutes:Route[] = []
|
|
26
27
|
let onNotFoundAction:(()=>void)|undefined = undefined
|
|
27
28
|
|
|
28
29
|
window.addEventListener('popstate', async (e) => {
|
|
29
|
-
// on go back
|
|
30
|
-
console.log(filename, 'window.popstate event', e, 'historyUrls=', historyUrls)
|
|
30
|
+
// on go back/forward
|
|
31
|
+
//console.log(filename, 'window.popstate event', e, 'historyUrls=', historyUrls)
|
|
31
32
|
const url = window.location.pathname
|
|
32
|
-
addUrlToHistory(url)
|
|
33
33
|
await executeAsync(url, getScrollPositionForUrl(url) )
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
|
|
35
|
+
await DomeManipulator.scrollToAsync({
|
|
36
|
+
pxFromTop: getScrollPositionForUrl(window.location.pathname)
|
|
37
|
+
})
|
|
37
38
|
})
|
|
38
39
|
|
|
39
40
|
function getScrollPositionForUrl(url:string){
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
for(let i=historyUrls.length-1;i>=0;i--){
|
|
43
|
-
const item = historyUrls[i]
|
|
44
|
-
if(item.url === url){
|
|
45
|
-
//result = item.scroll
|
|
46
|
-
return item.scroll
|
|
41
|
+
return scrollPositionByUrl.get(url) ?? 0
|
|
42
|
+
}
|
|
47
43
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// }
|
|
55
|
-
// }
|
|
56
|
-
// return result
|
|
44
|
+
function saveScrollPositionForUrl(url:string){
|
|
45
|
+
scrollPositionByUrl.set(url, DomeManipulator.getCurrentScrollPosition())
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function saveScrollPositionForCurrentUrl(){
|
|
49
|
+
scrollPositionByUrl.set(getCurrentUrl(), DomeManipulator.getCurrentScrollPosition())
|
|
57
50
|
}
|
|
58
51
|
|
|
59
52
|
export function navigate(url:string){
|
|
53
|
+
saveScrollPositionForCurrentUrl()
|
|
60
54
|
window.history.pushState(null, '', url)
|
|
61
|
-
addUrlToHistory(url)
|
|
62
55
|
executeAsync(url, 0)
|
|
63
56
|
DomeManipulator.scrollToTop()
|
|
64
57
|
}
|
|
@@ -73,50 +66,52 @@ export module DomeRouter {
|
|
|
73
66
|
|
|
74
67
|
export function changeUrl(url:string){
|
|
75
68
|
window.history.replaceState(null, '', url)
|
|
76
|
-
if(historyUrls.length>0){
|
|
77
|
-
|
|
78
|
-
}
|
|
69
|
+
// if(historyUrls.length>0){
|
|
70
|
+
// historyUrls[historyUrls.length-1].url = url
|
|
71
|
+
// }
|
|
79
72
|
}
|
|
80
73
|
|
|
81
|
-
function addUrlToHistory(url:string){
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
74
|
+
// function addUrlToHistory(url:string){
|
|
75
|
+
// historyUrls.push({url, scroll: 0})
|
|
76
|
+
// while(historyUrls.length>maxHistoryUrlsCount){
|
|
77
|
+
// historyUrls.shift()
|
|
78
|
+
// }
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
// // save scroll position to previous url
|
|
81
|
+
// if(historyUrls.length>1){
|
|
82
|
+
// historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
|
|
83
|
+
// }
|
|
91
84
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
85
|
+
// console.log('adding url to history', url)
|
|
86
|
+
// console.log('historyUrls=', historyUrls)
|
|
87
|
+
// }
|
|
95
88
|
|
|
96
89
|
|
|
97
90
|
|
|
98
91
|
export function getCurrentUrl(){
|
|
99
|
-
return
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* get previous page url navigated by router
|
|
103
|
-
* @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
|
|
104
|
-
*/
|
|
105
|
-
export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
|
|
106
|
-
//if(historyUrls.length==0) return undefined
|
|
107
|
-
let index = historyUrls.length-2-previousPageIndex
|
|
108
|
-
if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
|
|
109
|
-
return undefined
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function reloadCurrentPage(addToHistory:boolean = false){
|
|
113
|
-
const url = window.location.pathname
|
|
114
|
-
if(addToHistory) addUrlToHistory(url)
|
|
115
|
-
executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
|
|
92
|
+
return window.location.pathname
|
|
93
|
+
// return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
|
|
116
94
|
}
|
|
117
95
|
|
|
118
|
-
|
|
119
|
-
|
|
96
|
+
// /**
|
|
97
|
+
// * get previous page url navigated by router
|
|
98
|
+
// * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
|
|
99
|
+
// */
|
|
100
|
+
// export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
|
|
101
|
+
// //if(historyUrls.length==0) return undefined
|
|
102
|
+
// let index = historyUrls.length-2-previousPageIndex
|
|
103
|
+
// if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
|
|
104
|
+
// return undefined
|
|
105
|
+
// }
|
|
106
|
+
|
|
107
|
+
// export function reloadCurrentPage(addToHistory:boolean = false){
|
|
108
|
+
// const url = window.location.pathname
|
|
109
|
+
// if(addToHistory) addUrlToHistory(url)
|
|
110
|
+
// executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
export function resolveUrl(url:string = window.location.pathname){
|
|
114
|
+
// if(addToHistory) addUrlToHistory(window.location.pathname)
|
|
120
115
|
executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
|
|
121
116
|
}
|
|
122
117
|
|