@lexriver/dome 1.5.4 → 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,42 +1,36 @@
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
18
  await executeAsync(url, getScrollPositionForUrl(url));
14
- addUrlToHistory(url);
15
19
  // await DomeManipulator.scrollToAsync({
16
20
  // pxFromTop: getScrollPositionForUrl(window.location.pathname)
17
21
  // })
18
22
  });
19
23
  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
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) {
38
33
  window.history.pushState(null, '', url);
39
- addUrlToHistory(url);
40
34
  executeAsync(url, 0);
41
35
  DomeManipulator.scrollToTop();
42
36
  }
@@ -51,49 +45,45 @@ export var DomeRouter;
51
45
  DomeRouter.goForward = goForward;
52
46
  function changeUrl(url) {
53
47
  window.history.replaceState(null, '', url);
54
- if (historyUrls.length > 0) {
55
- historyUrls[historyUrls.length - 1].url = url;
56
- }
48
+ // if(historyUrls.length>0){
49
+ // historyUrls[historyUrls.length-1].url = url
50
+ // }
57
51
  }
58
52
  DomeRouter.changeUrl = changeUrl;
59
- function addUrlToHistory(url) {
60
- historyUrls.push({ url, scroll: 0 });
61
- while (historyUrls.length > DomeRouter.maxHistoryUrlsCount) {
62
- historyUrls.shift();
63
- }
64
- // save scroll position to previous url
65
- if (historyUrls.length > 1) {
66
- historyUrls[historyUrls.length - 2].scroll = DomeManipulator.getCurrentScrollPosition();
67
- }
68
- console.log('adding url to history', url);
69
- console.log('historyUrls=', historyUrls);
70
- }
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
+ // }
71
65
  function getCurrentUrl() {
72
- 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
73
68
  }
74
69
  DomeRouter.getCurrentUrl = getCurrentUrl;
75
- /**
76
- * get previous page url navigated by router
77
- * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
78
- */
79
- function getPreviousPageUrl(previousPageIndex = 0) {
80
- //if(historyUrls.length==0) return undefined
81
- let index = historyUrls.length - 2 - previousPageIndex;
82
- if (index >= 0 && index < historyUrls.length)
83
- return historyUrls[index].url;
84
- return undefined;
85
- }
86
- 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);
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)
97
87
  executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
98
88
  }
99
89
  DomeRouter.resolveUrl = resolveUrl;
@@ -192,6 +182,7 @@ export var DomeRouter;
192
182
  }
193
183
  }
194
184
  async function executeAsync(url = window.location.pathname, scrollToPosition) {
185
+ saveScrollPositionForUrl(url);
195
186
  //const url = window.location.pathname
196
187
  const urlSlices = getRouteSlices(url);
197
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.4",
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,51 +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, '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
+
34
35
  // await DomeManipulator.scrollToAsync({
35
36
  // pxFromTop: getScrollPositionForUrl(window.location.pathname)
36
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){
60
53
  window.history.pushState(null, '', url)
61
- addUrlToHistory(url)
62
54
  executeAsync(url, 0)
63
55
  DomeManipulator.scrollToTop()
64
56
  }
@@ -73,50 +65,52 @@ export module DomeRouter {
73
65
 
74
66
  export function changeUrl(url:string){
75
67
  window.history.replaceState(null, '', url)
76
- if(historyUrls.length>0){
77
- historyUrls[historyUrls.length-1].url = url
78
- }
68
+ // if(historyUrls.length>0){
69
+ // historyUrls[historyUrls.length-1].url = url
70
+ // }
79
71
  }
80
72
 
81
- function addUrlToHistory(url:string){
82
- historyUrls.push({url, scroll: 0})
83
- while(historyUrls.length>maxHistoryUrlsCount){
84
- historyUrls.shift()
85
- }
73
+ // function addUrlToHistory(url:string){
74
+ // historyUrls.push({url, scroll: 0})
75
+ // while(historyUrls.length>maxHistoryUrlsCount){
76
+ // historyUrls.shift()
77
+ // }
86
78
 
87
- // save scroll position to previous url
88
- if(historyUrls.length>1){
89
- historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
90
- }
79
+ // // save scroll position to previous url
80
+ // if(historyUrls.length>1){
81
+ // historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
82
+ // }
91
83
 
92
- console.log('adding url to history', url)
93
- console.log('historyUrls=', historyUrls)
94
- }
84
+ // console.log('adding url to history', url)
85
+ // console.log('historyUrls=', historyUrls)
86
+ // }
95
87
 
96
88
 
97
89
 
98
90
  export function getCurrentUrl(){
99
- return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
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
91
+ return window.location.pathname
92
+ // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
116
93
  }
117
94
 
118
- export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
119
- 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)
120
114
  executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
121
115
  }
122
116
 
@@ -219,6 +213,7 @@ export module DomeRouter {
219
213
  }
220
214
 
221
215
  async function executeAsync(url:string = window.location.pathname, scrollToPosition:number){
216
+ saveScrollPositionForUrl(url)
222
217
  //const url = window.location.pathname
223
218
  const urlSlices = getRouteSlices(url)
224
219
  // urlSlices = show, category, 345