@symbo.ls/router 2.11.18 → 2.11.28

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.
Files changed (2) hide show
  1. package/index.js +18 -12
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,41 +1,44 @@
1
1
  'use strict'
2
2
 
3
- import { merge } from '@domql/utils'
4
3
  import { setCookie } from '@domql/cookie'
5
4
  import { redirectBasedOnAuth } from '@symbo.ls/auth-redirect'
5
+ import { findModal } from '@symbo.ls/modals/action'
6
6
 
7
- export const getActiveRoute = (
8
- route = window.location.pathname,
9
- level
10
- ) => `/${route.split('/')[level + 1]}`
7
+ export const getActiveRoute = (level = 0, route = window.location.pathname) => {
8
+ const routeArray = route.split('/')
9
+ const activeRoute = routeArray[level + 1]
10
+ if (activeRoute) return `/${activeRoute}`
11
+ }
11
12
 
12
13
  export let lastLevel = 0
13
14
 
14
15
  const defaultOptions = {
15
16
  level: lastLevel,
17
+ initialRender: false,
16
18
  pushState: true,
17
19
  scrollToTop: true,
18
20
  scrollToNode: false,
19
21
  scrollDocument: false,
20
22
  useFragment: false,
21
23
  updateState: true,
24
+ scrollToOffset: 120,
22
25
  scrollToOptions: { behavior: 'smooth' }
23
26
  }
24
27
 
25
28
  export const router = (
29
+ path = '/',
26
30
  element,
27
- path,
28
31
  state = {},
29
- options = defaultOptions
32
+ passedOptions = {}
30
33
  ) => {
31
- merge(options, defaultOptions)
34
+ const options = { ...defaultOptions, ...passedOptions }
32
35
  lastLevel = options.level
33
36
 
34
37
  const [pathname, hash] = path.split('#')
35
38
  const routeArray = pathname.split('/')
36
- const rootRoute = `/${routeArray[1] || ''}`
39
+ const rootRoute = getActiveRoute(0)
37
40
 
38
- const authRedirect = redirectBasedOnAuth(element, element.state, { route: rootRoute })
41
+ const authRedirect = redirectBasedOnAuth(rootRoute, element, element.state)
39
42
 
40
43
  if (authRedirect && rootRoute !== authRedirect) {
41
44
  routeArray[1] = authRedirect.slice(1)
@@ -45,10 +48,13 @@ export const router = (
45
48
  const routeInContent = `/${routeArray[lastLevel + 1] || ''}`
46
49
  const content = element.routes[routeInContent] || element.routes['/*']
47
50
 
51
+ const isModal = (findModal(element, element.state) || {}).FoundModal
52
+ const isInitial = options.initialRender
53
+
48
54
  if (content) {
49
55
  if (options.pushState) window.history.pushState(state, null, route + (hash ? `#${hash}` : ''))
50
56
 
51
- element.set({ tag: options.useFragment && 'fragment', extend: content })
57
+ if (isInitial || (!isInitial && !isModal)) element.set({ tag: options.useFragment && 'fragment', extend: content })
52
58
  if (options.updateState) element.state.update({ route, hash })
53
59
 
54
60
  const rootNode = element.node
@@ -67,7 +73,7 @@ export const router = (
67
73
  if (hash) {
68
74
  const activeNode = document.getElementById(hash)
69
75
  if (activeNode) {
70
- const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 120
76
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0
71
77
  scrollNode.scrollTo({
72
78
  ...(options.scrollToOptions || {}), top, left: 0
73
79
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/router",
3
- "version": "2.11.18",
3
+ "version": "2.11.28",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -11,6 +11,6 @@
11
11
  "@domql/utils": "latest",
12
12
  "@symbo.ls/auth-redirect": "latest"
13
13
  },
14
- "gitHead": "d6d07132f062d77e5a64ba90cdde1d419ed66150",
14
+ "gitHead": "26b6500bc487650001f793925123dc2048065a87",
15
15
  "source": "index.js"
16
16
  }