@lexriver/dome 1.5.2 → 1.5.5

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,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 | HistoryUrl;
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,14 +1,19 @@
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, 'historyUrl=', historyUrls)
15
+ // on go back/forward
16
+ //console.log(filename, 'window.popstate event', e, 'historyUrls=', historyUrls)
12
17
  const url = window.location.pathname;
13
18
  await executeAsync(url, getScrollPositionForUrl(url));
14
19
  // await DomeManipulator.scrollToAsync({
@@ -16,26 +21,16 @@ export var DomeRouter;
16
21
  // })
17
22
  });
18
23
  function getScrollPositionForUrl(url) {
19
- // well, let's find the last url? or which index?
20
- //let result = 0
21
- for (let i = historyUrls.length - 1; i >= 0; i--) {
22
- const item = historyUrls[i];
23
- if (item.url === url) {
24
- //result = item.scroll
25
- return item.scroll;
26
- }
27
- }
28
- return 0;
29
- // for(let item of historyUrls){
30
- // if(item.url === url){
31
- // result = item.scroll
32
- // }
33
- // }
34
- // 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());
35
31
  }
36
32
  function navigate(url) {
37
33
  window.history.pushState(null, '', url);
38
- addUrlToHistory(url);
39
34
  executeAsync(url, 0);
40
35
  DomeManipulator.scrollToTop();
41
36
  }
@@ -50,49 +45,45 @@ export var DomeRouter;
50
45
  DomeRouter.goForward = goForward;
51
46
  function changeUrl(url) {
52
47
  window.history.replaceState(null, '', url);
53
- if (historyUrls.length > 0) {
54
- historyUrls[historyUrls.length - 1].url = url;
55
- }
48
+ // if(historyUrls.length>0){
49
+ // historyUrls[historyUrls.length-1].url = url
50
+ // }
56
51
  }
57
52
  DomeRouter.changeUrl = changeUrl;
58
- function addUrlToHistory(url) {
59
- historyUrls.push({ url, scroll: 0 });
60
- while (historyUrls.length > DomeRouter.maxHistoryUrlsCount) {
61
- historyUrls.shift();
62
- }
63
- // save scroll position to previous url
64
- if (historyUrls.length > 1) {
65
- historyUrls[historyUrls.length - 2].scroll = DomeManipulator.getCurrentScrollPosition();
66
- }
67
- console.log('adding url to history', url);
68
- console.log('historyUrls=', historyUrls);
69
- }
53
+ // function addUrlToHistory(url:string){
54
+ // historyUrls.push({url, scroll: 0})
55
+ // while(historyUrls.length>maxHistoryUrlsCount){
56
+ // historyUrls.shift()
57
+ // }
58
+ // // save scroll position to previous url
59
+ // if(historyUrls.length>1){
60
+ // historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
61
+ // }
62
+ // console.log('adding url to history', url)
63
+ // console.log('historyUrls=', historyUrls)
64
+ // }
70
65
  function getCurrentUrl() {
71
- return historyUrls.length > 0 ? historyUrls[historyUrls.length - 1] : window.location.pathname;
66
+ return window.location.pathname;
67
+ // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
72
68
  }
73
69
  DomeRouter.getCurrentUrl = getCurrentUrl;
74
- /**
75
- * get previous page url navigated by router
76
- * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
77
- */
78
- function getPreviousPageUrl(previousPageIndex = 0) {
79
- //if(historyUrls.length==0) return undefined
80
- let index = historyUrls.length - 2 - previousPageIndex;
81
- if (index >= 0 && index < historyUrls.length)
82
- return historyUrls[index].url;
83
- return undefined;
84
- }
85
- DomeRouter.getPreviousPageUrl = getPreviousPageUrl;
86
- function reloadCurrentPage(addToHistory = false) {
87
- const url = window.location.pathname;
88
- if (addToHistory)
89
- addUrlToHistory(url);
90
- executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
91
- }
92
- DomeRouter.reloadCurrentPage = reloadCurrentPage;
93
- function resolveUrl(url = window.location.pathname, addToHistory = true) {
94
- if (addToHistory)
95
- addUrlToHistory(window.location.pathname);
70
+ // /**
71
+ // * get previous page url navigated by router
72
+ // * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
73
+ // */
74
+ // export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
75
+ // //if(historyUrls.length==0) return undefined
76
+ // let index = historyUrls.length-2-previousPageIndex
77
+ // if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
78
+ // return undefined
79
+ // }
80
+ // export function reloadCurrentPage(addToHistory:boolean = false){
81
+ // const url = window.location.pathname
82
+ // if(addToHistory) addUrlToHistory(url)
83
+ // executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
84
+ // }
85
+ function resolveUrl(url = window.location.pathname) {
86
+ // if(addToHistory) addUrlToHistory(window.location.pathname)
96
87
  executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
97
88
  }
98
89
  DomeRouter.resolveUrl = resolveUrl;
@@ -191,6 +182,7 @@ export var DomeRouter;
191
182
  }
192
183
  }
193
184
  async function executeAsync(url = window.location.pathname, scrollToPosition) {
185
+ saveScrollPositionForUrl(url);
194
186
  //const url = window.location.pathname
195
187
  const urlSlices = getRouteSlices(url);
196
188
  // urlSlices = show, category, 345
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.2",
5
+ "version": "1.5.5",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
package/src/DomeRouter.ts CHANGED
@@ -14,50 +14,43 @@ interface Route{
14
14
  action:RouteAction
15
15
  }
16
16
 
17
- interface HistoryUrl{
18
- url:string
19
- scroll:number
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, 'historyUrl=', 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) )
34
+
33
35
  // await DomeManipulator.scrollToAsync({
34
36
  // pxFromTop: getScrollPositionForUrl(window.location.pathname)
35
37
  // })
36
38
  })
37
39
 
38
40
  function getScrollPositionForUrl(url:string){
39
- // well, let's find the last url? or which index?
40
- //let result = 0
41
- for(let i=historyUrls.length-1;i>=0;i--){
42
- const item = historyUrls[i]
43
- if(item.url === url){
44
- //result = item.scroll
45
- return item.scroll
41
+ return scrollPositionByUrl.get(url) ?? 0
42
+ }
46
43
 
47
- }
48
- }
49
- return 0
50
- // for(let item of historyUrls){
51
- // if(item.url === url){
52
- // result = item.scroll
53
- // }
54
- // }
55
- // return result
44
+ function saveScrollPositionForUrl(url:string){
45
+ scrollPositionByUrl.set(url, DomeManipulator.getCurrentScrollPosition())
46
+ }
47
+
48
+ function saveScrollPositionForCurrentUrl(){
49
+ scrollPositionByUrl.set(getCurrentUrl(), DomeManipulator.getCurrentScrollPosition())
56
50
  }
57
51
 
58
52
  export function navigate(url:string){
59
53
  window.history.pushState(null, '', url)
60
- addUrlToHistory(url)
61
54
  executeAsync(url, 0)
62
55
  DomeManipulator.scrollToTop()
63
56
  }
@@ -72,50 +65,52 @@ export module DomeRouter {
72
65
 
73
66
  export function changeUrl(url:string){
74
67
  window.history.replaceState(null, '', url)
75
- if(historyUrls.length>0){
76
- historyUrls[historyUrls.length-1].url = url
77
- }
68
+ // if(historyUrls.length>0){
69
+ // historyUrls[historyUrls.length-1].url = url
70
+ // }
78
71
  }
79
72
 
80
- function addUrlToHistory(url:string){
81
- historyUrls.push({url, scroll: 0})
82
- while(historyUrls.length>maxHistoryUrlsCount){
83
- historyUrls.shift()
84
- }
73
+ // function addUrlToHistory(url:string){
74
+ // historyUrls.push({url, scroll: 0})
75
+ // while(historyUrls.length>maxHistoryUrlsCount){
76
+ // historyUrls.shift()
77
+ // }
85
78
 
86
- // save scroll position to previous url
87
- if(historyUrls.length>1){
88
- historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
89
- }
79
+ // // save scroll position to previous url
80
+ // if(historyUrls.length>1){
81
+ // historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
82
+ // }
90
83
 
91
- console.log('adding url to history', url)
92
- console.log('historyUrls=', historyUrls)
93
- }
84
+ // console.log('adding url to history', url)
85
+ // console.log('historyUrls=', historyUrls)
86
+ // }
94
87
 
95
88
 
96
89
 
97
90
  export function getCurrentUrl(){
98
- return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
99
- }
100
- /**
101
- * get previous page url navigated by router
102
- * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
103
- */
104
- export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
105
- //if(historyUrls.length==0) return undefined
106
- let index = historyUrls.length-2-previousPageIndex
107
- if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
108
- return undefined
109
- }
110
-
111
- export function reloadCurrentPage(addToHistory:boolean = false){
112
- const url = window.location.pathname
113
- if(addToHistory) addUrlToHistory(url)
114
- executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
91
+ return window.location.pathname
92
+ // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
115
93
  }
116
94
 
117
- export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
118
- if(addToHistory) addUrlToHistory(window.location.pathname)
95
+ // /**
96
+ // * get previous page url navigated by router
97
+ // * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
98
+ // */
99
+ // export function getPreviousPageUrl(previousPageIndex:number=0):string|undefined{
100
+ // //if(historyUrls.length==0) return undefined
101
+ // let index = historyUrls.length-2-previousPageIndex
102
+ // if(index >= 0 && index < historyUrls.length) return historyUrls[index].url
103
+ // return undefined
104
+ // }
105
+
106
+ // export function reloadCurrentPage(addToHistory:boolean = false){
107
+ // const url = window.location.pathname
108
+ // if(addToHistory) addUrlToHistory(url)
109
+ // executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
110
+ // }
111
+
112
+ export function resolveUrl(url:string = window.location.pathname){
113
+ // if(addToHistory) addUrlToHistory(window.location.pathname)
119
114
  executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
120
115
  }
121
116
 
@@ -218,6 +213,7 @@ export module DomeRouter {
218
213
  }
219
214
 
220
215
  async function executeAsync(url:string = window.location.pathname, scrollToPosition:number){
216
+ saveScrollPositionForUrl(url)
221
217
  //const url = window.location.pathname
222
218
  const urlSlices = getRouteSlices(url)
223
219
  // urlSlices = show, category, 345