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