@lexriver/dome 1.4.1 → 1.5.1

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;
@@ -72,15 +82,16 @@ export var DomeRouter;
72
82
  }
73
83
  DomeRouter.getPreviousPageUrl = getPreviousPageUrl;
74
84
  function reloadCurrentPage(addToHistory = false) {
85
+ const url = window.location.pathname;
75
86
  if (addToHistory)
76
- addUrlToHistory(window.location.pathname);
77
- executeAsync();
87
+ addUrlToHistory(url);
88
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
78
89
  }
79
90
  DomeRouter.reloadCurrentPage = reloadCurrentPage;
80
91
  function resolveUrl(url = window.location.pathname, addToHistory = true) {
81
92
  if (addToHistory)
82
93
  addUrlToHistory(window.location.pathname);
83
- executeAsync(url);
94
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
84
95
  }
85
96
  DomeRouter.resolveUrl = resolveUrl;
86
97
  function onRoute(route, exactMatch, action) {
@@ -177,7 +188,7 @@ export var DomeRouter;
177
188
  return [name.substring(1), value];
178
189
  }
179
190
  }
180
- async function executeAsync(url = window.location.pathname) {
191
+ async function executeAsync(url = window.location.pathname, scrollToPosition) {
181
192
  //const url = window.location.pathname
182
193
  const urlSlices = getRouteSlices(url);
183
194
  // urlSlices = show, category, 345
@@ -216,7 +227,11 @@ export var DomeRouter;
216
227
  if (matched) {
217
228
  countOfFoundRoutes++;
218
229
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
219
- await route.action(extractedParameters, url);
230
+ await route.action(extractedParameters, url, async () => {
231
+ await DomeManipulator.scrollToAsync({
232
+ pxFromTop: scrollToPosition
233
+ });
234
+ });
220
235
  }
221
236
  }
222
237
  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.1",
5
+ "version": "1.5.1",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
@@ -30,8 +30,7 @@
30
30
  "@lexriver/async": "^1.0.0",
31
31
  "@lexriver/data-types": "^2.0.0",
32
32
  "@lexriver/observable": "^1.0.0",
33
- "lodash.clonedeep": "^4.5.0",
34
33
  "svg-tag-names": "^2.0.0",
35
34
  "ts-debounce": "^2.0.1"
36
35
  }
37
- }
36
+ }
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
 
@@ -91,13 +106,14 @@ export module DomeRouter {
91
106
  }
92
107
 
93
108
  export function reloadCurrentPage(addToHistory:boolean = false){
94
- if(addToHistory) addUrlToHistory(window.location.pathname)
95
- executeAsync()
109
+ const url = window.location.pathname
110
+ if(addToHistory) addUrlToHistory(url)
111
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
96
112
  }
97
113
 
98
114
  export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
99
115
  if(addToHistory) addUrlToHistory(window.location.pathname)
100
- executeAsync(url)
116
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
101
117
  }
102
118
 
103
119
  export function onRoute(route:string, exactMatch:boolean, action:RouteAction){
@@ -198,7 +214,7 @@ export module DomeRouter {
198
214
 
199
215
  }
200
216
 
201
- async function executeAsync(url:string = window.location.pathname){
217
+ async function executeAsync(url:string = window.location.pathname, scrollToPosition:number){
202
218
  //const url = window.location.pathname
203
219
  const urlSlices = getRouteSlices(url)
204
220
  // urlSlices = show, category, 345
@@ -238,7 +254,11 @@ export module DomeRouter {
238
254
  if(matched){
239
255
  countOfFoundRoutes++
240
256
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
241
- await route.action(extractedParameters, url)
257
+ await route.action(extractedParameters, url, async () => {
258
+ await DomeManipulator.scrollToAsync({
259
+ pxFromTop: scrollToPosition
260
+ })
261
+ })
242
262
  }
243
263
  }
244
264
  if(countOfFoundRoutes==0 && onNotFoundAction){