@lexriver/dome 1.4.2 → 1.5.0

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,11 +7,13 @@ 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?
@@ -26,7 +28,7 @@ export var DomeRouter;
26
28
  function navigate(url) {
27
29
  window.history.pushState(null, '', url);
28
30
  addUrlToHistory(url);
29
- executeAsync();
31
+ executeAsync(url, 0);
30
32
  DomeManipulator.scrollToTop();
31
33
  }
32
34
  DomeRouter.navigate = navigate;
@@ -72,15 +74,16 @@ export var DomeRouter;
72
74
  }
73
75
  DomeRouter.getPreviousPageUrl = getPreviousPageUrl;
74
76
  function reloadCurrentPage(addToHistory = false) {
77
+ const url = window.location.pathname;
75
78
  if (addToHistory)
76
- addUrlToHistory(window.location.pathname);
77
- executeAsync();
79
+ addUrlToHistory(url);
80
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
78
81
  }
79
82
  DomeRouter.reloadCurrentPage = reloadCurrentPage;
80
83
  function resolveUrl(url = window.location.pathname, addToHistory = true) {
81
84
  if (addToHistory)
82
85
  addUrlToHistory(window.location.pathname);
83
- executeAsync(url);
86
+ executeAsync(url, getScrollPositionForUrl(url)); // TODO: check
84
87
  }
85
88
  DomeRouter.resolveUrl = resolveUrl;
86
89
  function onRoute(route, exactMatch, action) {
@@ -177,7 +180,7 @@ export var DomeRouter;
177
180
  return [name.substring(1), value];
178
181
  }
179
182
  }
180
- async function executeAsync(url = window.location.pathname) {
183
+ async function executeAsync(url = window.location.pathname, scrollToPosition) {
181
184
  //const url = window.location.pathname
182
185
  const urlSlices = getRouteSlices(url);
183
186
  // urlSlices = show, category, 345
@@ -216,7 +219,11 @@ export var DomeRouter;
216
219
  if (matched) {
217
220
  countOfFoundRoutes++;
218
221
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
219
- await route.action(extractedParameters, url);
222
+ await route.action(extractedParameters, url, async () => {
223
+ await DomeManipulator.scrollToAsync({
224
+ pxFromTop: scrollToPosition
225
+ });
226
+ });
220
227
  }
221
228
  }
222
229
  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.0",
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,11 +26,13 @@ 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){
@@ -43,7 +49,7 @@ export module DomeRouter {
43
49
  export function navigate(url:string){
44
50
  window.history.pushState(null, '', url)
45
51
  addUrlToHistory(url)
46
- executeAsync()
52
+ executeAsync(url, 0)
47
53
  DomeManipulator.scrollToTop()
48
54
  }
49
55
 
@@ -91,13 +97,14 @@ export module DomeRouter {
91
97
  }
92
98
 
93
99
  export function reloadCurrentPage(addToHistory:boolean = false){
94
- if(addToHistory) addUrlToHistory(window.location.pathname)
95
- executeAsync()
100
+ const url = window.location.pathname
101
+ if(addToHistory) addUrlToHistory(url)
102
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
96
103
  }
97
104
 
98
105
  export function resolveUrl(url:string = window.location.pathname, addToHistory:boolean = true){
99
106
  if(addToHistory) addUrlToHistory(window.location.pathname)
100
- executeAsync(url)
107
+ executeAsync(url, getScrollPositionForUrl(url)) // TODO: check
101
108
  }
102
109
 
103
110
  export function onRoute(route:string, exactMatch:boolean, action:RouteAction){
@@ -198,7 +205,7 @@ export module DomeRouter {
198
205
 
199
206
  }
200
207
 
201
- async function executeAsync(url:string = window.location.pathname){
208
+ async function executeAsync(url:string = window.location.pathname, scrollToPosition:number){
202
209
  //const url = window.location.pathname
203
210
  const urlSlices = getRouteSlices(url)
204
211
  // urlSlices = show, category, 345
@@ -238,7 +245,11 @@ export module DomeRouter {
238
245
  if(matched){
239
246
  countOfFoundRoutes++
240
247
  //console.log('DomeRouter', 'executeAsync', url, route, extractedParameters)
241
- await route.action(extractedParameters, url)
248
+ await route.action(extractedParameters, url, async () => {
249
+ await DomeManipulator.scrollToAsync({
250
+ pxFromTop: scrollToPosition
251
+ })
252
+ })
242
253
  }
243
254
  }
244
255
  if(countOfFoundRoutes==0 && onNotFoundAction){