@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.
@@ -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,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
- // await DomeManipulator.scrollToAsync({
16
- // pxFromTop: getScrollPositionForUrl(window.location.pathname)
17
- // })
19
+ await DomeManipulator.scrollToAsync({
20
+ pxFromTop: getScrollPositionForUrl(window.location.pathname)
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) {
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 (historyUrls.length > 0) {
55
- historyUrls[historyUrls.length - 1].url = url;
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
- 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
- }
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 historyUrls.length > 0 ? historyUrls[historyUrls.length - 1] : window.location.pathname;
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
- * 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);
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
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "sideEffects": false,
4
4
  "name": "@lexriver/dome",
5
- "version": "1.5.3",
5
+ "version": "1.5.6",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
package/src/DomeRouter.ts CHANGED
@@ -14,51 +14,44 @@ 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
- addUrlToHistory(url)
33
33
  await executeAsync(url, getScrollPositionForUrl(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
+ 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
- historyUrls[historyUrls.length-1].url = url
78
- }
69
+ // if(historyUrls.length>0){
70
+ // historyUrls[historyUrls.length-1].url = url
71
+ // }
79
72
  }
80
73
 
81
- function addUrlToHistory(url:string){
82
- historyUrls.push({url, scroll: 0})
83
- while(historyUrls.length>maxHistoryUrlsCount){
84
- historyUrls.shift()
85
- }
74
+ // function addUrlToHistory(url:string){
75
+ // historyUrls.push({url, scroll: 0})
76
+ // while(historyUrls.length>maxHistoryUrlsCount){
77
+ // historyUrls.shift()
78
+ // }
86
79
 
87
- // save scroll position to previous url
88
- if(historyUrls.length>1){
89
- historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
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
- console.log('adding url to history', url)
93
- console.log('historyUrls=', historyUrls)
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 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
92
+ return window.location.pathname
93
+ // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
116
94
  }
117
95
 
118
- export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
119
- if(addToHistory) addUrlToHistory(window.location.pathname)
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