@lexriver/dome 1.5.5 → 1.5.8

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