@lexriver/dome 1.5.6 → 1.5.9

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.
package/README.md CHANGED
@@ -1506,7 +1506,11 @@ Parameters:
1506
1506
 
1507
1507
  Where `RouteAction` type is:
1508
1508
  ```typescript
1509
- export type RouteAction = (params:{[key:string]:string}, url:string) => void|Promise<void>
1509
+ export type RouteAction = (
1510
+ params:{[key:string]:string}, // parameters from url
1511
+ url:string,
1512
+ scrollToPreviousPositionAsync:()=>Promise<void> // this function can be called to scroll to previous page position
1513
+ ) => void|Promise<void>
1510
1514
  ```
1511
1515
 
1512
1516
  example:
@@ -1,6 +1,6 @@
1
1
  export declare type RouteAction = (params: {
2
2
  [key: string]: string;
3
- }, url: string, scrollToPreviousPositionFunctionAsync: () => Promise<void>) => void | Promise<void>;
3
+ }, url: string, scrollToPreviousPositionAsync: () => Promise<void>) => void | Promise<void>;
4
4
  export declare module DomeRouter {
5
5
  let maxHistoryUrlsCount: number;
6
6
  function navigate(url: string): void;
@@ -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 = [];
@@ -30,10 +26,12 @@ export var DomeRouter;
30
26
  scrollPositionByUrl.set(getCurrentUrl(), DomeManipulator.getCurrentScrollPosition());
31
27
  }
32
28
  function navigate(url) {
29
+ addUrlToHistory(getCurrentUrl());
33
30
  saveScrollPositionForCurrentUrl();
34
31
  window.history.pushState(null, '', url);
35
32
  executeAsync(url, 0);
36
33
  DomeManipulator.scrollToTop();
34
+ // historyUrls.push()
37
35
  }
38
36
  DomeRouter.navigate = navigate;
39
37
  function goBack() {
@@ -51,41 +49,44 @@ export var DomeRouter;
51
49
  // }
52
50
  }
53
51
  DomeRouter.changeUrl = changeUrl;
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
- // }
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
+ }
66
64
  function getCurrentUrl() {
67
65
  return window.location.pathname;
68
66
  // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
69
67
  }
70
68
  DomeRouter.getCurrentUrl = getCurrentUrl;
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
- // }
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;
81
81
  // export function reloadCurrentPage(addToHistory:boolean = false){
82
82
  // const url = window.location.pathname
83
83
  // if(addToHistory) addUrlToHistory(url)
84
84
  // executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
85
85
  // }
86
- function resolveUrl(url = window.location.pathname) {
87
- // if(addToHistory) addUrlToHistory(window.location.pathname)
88
- 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));
89
90
  }
90
91
  DomeRouter.resolveUrl = resolveUrl;
91
92
  function onRoute(route, exactMatch, action) {
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.6",
5
+ "version": "1.5.9",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
package/src/DomeRouter.ts CHANGED
@@ -5,7 +5,7 @@ const filename = '[DomeRouter]'
5
5
  export type RouteAction = (
6
6
  params:{[key:string]:string},
7
7
  url:string,
8
- scrollToPreviousPositionFunctionAsync:()=>Promise<void>
8
+ scrollToPreviousPositionAsync:()=>Promise<void>
9
9
  ) => void|Promise<void>
10
10
 
11
11
  interface Route{
@@ -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[] = []
@@ -50,10 +50,12 @@ export module DomeRouter {
50
50
  }
51
51
 
52
52
  export function navigate(url:string){
53
+ addUrlToHistory(getCurrentUrl())
53
54
  saveScrollPositionForCurrentUrl()
54
55
  window.history.pushState(null, '', url)
55
56
  executeAsync(url, 0)
56
57
  DomeManipulator.scrollToTop()
58
+ // historyUrls.push()
57
59
  }
58
60
 
59
61
  export function goBack(){
@@ -71,20 +73,20 @@ export module DomeRouter {
71
73
  // }
72
74
  }
73
75
 
74
- // function addUrlToHistory(url:string){
75
- // historyUrls.push({url, scroll: 0})
76
- // while(historyUrls.length>maxHistoryUrlsCount){
77
- // historyUrls.shift()
78
- // }
76
+ function addUrlToHistory(url:string){
77
+ historyUrls.push({url, scroll: 0})
78
+ while(historyUrls.length>maxHistoryUrlsCount){
79
+ historyUrls.shift()
80
+ }
79
81
 
80
- // // save scroll position to previous url
81
- // if(historyUrls.length>1){
82
- // historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
83
- // }
82
+ // save scroll position to previous url
83
+ if(historyUrls.length>1){
84
+ historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
85
+ }
84
86
 
85
- // console.log('adding url to history', url)
86
- // console.log('historyUrls=', historyUrls)
87
- // }
87
+ console.log('adding url to history', url)
88
+ console.log('historyUrls=', historyUrls)
89
+ }
88
90
 
89
91
 
90
92
 
@@ -93,16 +95,16 @@ export module DomeRouter {
93
95
  // return historyUrls.length>0?historyUrls[historyUrls.length-1]:window.location.pathname
94
96
  }
95
97
 
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
- // }
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
+ }
106
108
 
107
109
  // export function reloadCurrentPage(addToHistory:boolean = false){
108
110
  // const url = window.location.pathname
@@ -110,9 +112,9 @@ export module DomeRouter {
110
112
  // executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
111
113
  // }
112
114
 
113
- export function resolveUrl(url:string = window.location.pathname){
114
- // if(addToHistory) addUrlToHistory(window.location.pathname)
115
- 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))
116
118
  }
117
119
 
118
120
  export function onRoute(route:string, exactMatch:boolean, action:RouteAction){