@lexriver/dome 1.4.2 → 1.5.2

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,6 +1,6 @@
1
1
  export declare type RouteAction = (params: {
2
2
  [key: string]: string;
3
- }, url: string) => void | Promise<void>;
3
+ }, url: string, scrollToPreviousPositionFunctionAsync: () => Promise<void>) => void | Promise<void>;
4
4
  interface HistoryUrl {
5
5
  url: string;
6
6
  scroll: number;
package/out/DomeRouter.js CHANGED
@@ -7,26 +7,36 @@ export var DomeRouter;
7
7
  const allRoutes = [];
8
8
  let onNotFoundAction = undefined;
9
9
  window.addEventListener('popstate', async (e) => {
10
+ // on go back
10
11
  //console.log(filename, 'window.popstate event', e, 'historyUrl=', historyUrls)
11
- await executeAsync();
12
- await DomeManipulator.scrollToAsync({
13
- pxFromTop: getScrollPositionForUrl(window.location.pathname)
14
- });
12
+ const url = window.location.pathname;
13
+ await executeAsync(url, getScrollPositionForUrl(url));
14
+ // await DomeManipulator.scrollToAsync({
15
+ // pxFromTop: getScrollPositionForUrl(window.location.pathname)
16
+ // })
15
17
  });
16
18
  function getScrollPositionForUrl(url) {
17
19
  // well, let's find the last url? or which index?
18
- let result = 0;
19
- for (let item of historyUrls) {
20
+ //let result = 0
21
+ for (let i = historyUrls.length - 1; i >= 0; i--) {
22
+ const item = historyUrls[i];
20
23
  if (item.url === url) {
21
- result = item.scroll;
24
+ //result = item.scroll
25
+ return item.scroll;
22
26
  }
23
27
  }
24
- return result;
28
+ return 0;
29
+ // for(let item of historyUrls){
30
+ // if(item.url === url){
31
+ // result = item.scroll
32
+ // }
33
+ // }
34
+ // return result
25
35
  }
26
36
  function navigate(url) {
27
37
  window.history.pushState(null, '', url);
28
38
  addUrlToHistory(url);
29
- executeAsync();
39
+ executeAsync(url, 0);
30
40
  DomeManipulator.scrollToTop();
31
41
  }
32
42
  DomeRouter.navigate = navigate;
@@ -54,6 +64,8 @@ export var DomeRouter;
54
64
  if (historyUrls.length > 1) {
55
65
  historyUrls[historyUrls.length - 2].scroll = DomeManipulator.getCurrentScrollPosition();
56
66
  }
67
+ console.log('adding url to history', url);
68
+ console.log('historyUrls=', historyUrls);
57
69
  }
58
70
  function getCurrentUrl() {
59
71
  return historyUrls.length > 0 ? historyUrls[historyUrls.length - 1] : window.location.pathname;
@@ -72,15 +84,16 @@ export var DomeRouter;
72
84
  }
73
85
  DomeRouter.getPreviousPageUrl = getPreviousPageUrl;
74
86
  function reloadCurrentPage(addToHistory = false) {
87
+ const url = window.location.pathname;
75
88
  if (addToHistory)
76
- addUrlToHistory(window.location.pathname);
77
- executeAsync();
89
+ addUrlToHistory(url);
90
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
78
91
  }
79
92
  DomeRouter.reloadCurrentPage = reloadCurrentPage;
80
93
  function resolveUrl(url = window.location.pathname, addToHistory = true) {
81
94
  if (addToHistory)
82
95
  addUrlToHistory(window.location.pathname);
83
- executeAsync(url);
96
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
84
97
  }
85
98
  DomeRouter.resolveUrl = resolveUrl;
86
99
  function onRoute(route, exactMatch, action) {
@@ -177,7 +190,7 @@ export var DomeRouter;
177
190
  return [name.substring(1), value];
178
191
  }
179
192
  }
180
- async function executeAsync(url = window.location.pathname) {
193
+ async function executeAsync(url = window.location.pathname, scrollToPosition) {
181
194
  //const url = window.location.pathname
182
195
  const urlSlices = getRouteSlices(url);
183
196
  // urlSlices = show, category, 345
@@ -216,7 +229,11 @@ export var DomeRouter;
216
229
  if (matched) {
217
230
  countOfFoundRoutes++;
218
231
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
219
- await route.action(extractedParameters, url);
232
+ await route.action(extractedParameters, url, async () => {
233
+ await DomeManipulator.scrollToAsync({
234
+ pxFromTop: scrollToPosition
235
+ });
236
+ });
220
237
  }
221
238
  }
222
239
  if (countOfFoundRoutes == 0 && onNotFoundAction) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "sideEffects": false,
4
4
  "name": "@lexriver/dome",
5
- "version": "1.4.2",
5
+ "version": "1.5.2",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
package/src/DomeRouter.ts CHANGED
@@ -2,7 +2,11 @@ import { DomeManipulator } from "./DomeManipulator"
2
2
 
3
3
  const filename = '[DomeRouter]'
4
4
 
5
- export type RouteAction = (params:{[key:string]:string}, url:string) => void|Promise<void>
5
+ export type RouteAction = (
6
+ params:{[key:string]:string},
7
+ url:string,
8
+ scrollToPreviousPositionFunctionAsync:()=>Promise<void>
9
+ ) => void|Promise<void>
6
10
 
7
11
  interface Route{
8
12
  routeSlices:string[]
@@ -22,28 +26,39 @@ export module DomeRouter {
22
26
  let onNotFoundAction:(()=>void)|undefined = undefined
23
27
 
24
28
  window.addEventListener('popstate', async (e) => {
29
+ // on go back
25
30
  //console.log(filename, 'window.popstate event', e, 'historyUrl=', historyUrls)
26
- await executeAsync()
27
- await DomeManipulator.scrollToAsync({
28
- pxFromTop: getScrollPositionForUrl(window.location.pathname)
29
- })
31
+ const url = window.location.pathname
32
+ await executeAsync(url, getScrollPositionForUrl(url) )
33
+ // await DomeManipulator.scrollToAsync({
34
+ // pxFromTop: getScrollPositionForUrl(window.location.pathname)
35
+ // })
30
36
  })
31
37
 
32
38
  function getScrollPositionForUrl(url:string){
33
39
  // well, let's find the last url? or which index?
34
- let result = 0
35
- for(let item of historyUrls){
40
+ //let result = 0
41
+ for(let i=historyUrls.length-1;i>=0;i--){
42
+ const item = historyUrls[i]
36
43
  if(item.url === url){
37
- result = item.scroll
44
+ //result = item.scroll
45
+ return item.scroll
46
+
38
47
  }
39
48
  }
40
- return result
49
+ return 0
50
+ // for(let item of historyUrls){
51
+ // if(item.url === url){
52
+ // result = item.scroll
53
+ // }
54
+ // }
55
+ // return result
41
56
  }
42
57
 
43
58
  export function navigate(url:string){
44
59
  window.history.pushState(null, '', url)
45
60
  addUrlToHistory(url)
46
- executeAsync()
61
+ executeAsync(url, 0)
47
62
  DomeManipulator.scrollToTop()
48
63
  }
49
64
 
@@ -72,6 +87,9 @@ export module DomeRouter {
72
87
  if(historyUrls.length>1){
73
88
  historyUrls[historyUrls.length-2].scroll = DomeManipulator.getCurrentScrollPosition()
74
89
  }
90
+
91
+ console.log('adding url to history', url)
92
+ console.log('historyUrls=', historyUrls)
75
93
  }
76
94
 
77
95
 
@@ -91,13 +109,14 @@ export module DomeRouter {
91
109
  }
92
110
 
93
111
  export function reloadCurrentPage(addToHistory:boolean = false){
94
- if(addToHistory) addUrlToHistory(window.location.pathname)
95
- executeAsync()
112
+ const url = window.location.pathname
113
+ if(addToHistory) addUrlToHistory(url)
114
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
96
115
  }
97
116
 
98
117
  export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
99
118
  if(addToHistory) addUrlToHistory(window.location.pathname)
100
- executeAsync(url)
119
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
101
120
  }
102
121
 
103
122
  export function onRoute(route:string, exactMatch:boolean, action:RouteAction){
@@ -198,7 +217,7 @@ export module DomeRouter {
198
217
 
199
218
  }
200
219
 
201
- async function executeAsync(url:string = window.location.pathname){
220
+ async function executeAsync(url:string = window.location.pathname, scrollToPosition:number){
202
221
  //const url = window.location.pathname
203
222
  const urlSlices = getRouteSlices(url)
204
223
  // urlSlices = show, category, 345
@@ -238,7 +257,11 @@ export module DomeRouter {
238
257
  if(matched){
239
258
  countOfFoundRoutes++
240
259
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
241
- await route.action(extractedParameters, url)
260
+ await route.action(extractedParameters, url, async () => {
261
+ await DomeManipulator.scrollToAsync({
262
+ pxFromTop: scrollToPosition
263
+ })
264
+ })
242
265
  }
243
266
  }
244
267
  if(countOfFoundRoutes==0 && onNotFoundAction){