@symbo.ls/router 2.11.25 → 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 +16 -11
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,18 +1,20 @@
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] && `/${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,
@@ -24,19 +26,19 @@ const defaultOptions = {
24
26
  }
25
27
 
26
28
  export const router = (
29
+ path = '/',
27
30
  element,
28
- path,
29
31
  state = {},
30
- options = defaultOptions
32
+ passedOptions = {}
31
33
  ) => {
32
- merge(options, defaultOptions)
34
+ const options = { ...defaultOptions, ...passedOptions }
33
35
  lastLevel = options.level
34
36
 
35
37
  const [pathname, hash] = path.split('#')
36
38
  const routeArray = pathname.split('/')
37
- const rootRoute = `/${routeArray[1] || ''}`
39
+ const rootRoute = getActiveRoute(0)
38
40
 
39
- const authRedirect = redirectBasedOnAuth(element, element.state, { route: rootRoute })
41
+ const authRedirect = redirectBasedOnAuth(rootRoute, element, element.state)
40
42
 
41
43
  if (authRedirect && rootRoute !== authRedirect) {
42
44
  routeArray[1] = authRedirect.slice(1)
@@ -46,10 +48,13 @@ export const router = (
46
48
  const routeInContent = `/${routeArray[lastLevel + 1] || ''}`
47
49
  const content = element.routes[routeInContent] || element.routes['/*']
48
50
 
51
+ const isModal = (findModal(element, element.state) || {}).FoundModal
52
+ const isInitial = options.initialRender
53
+
49
54
  if (content) {
50
55
  if (options.pushState) window.history.pushState(state, null, route + (hash ? `#${hash}` : ''))
51
56
 
52
- element.set({ tag: options.useFragment && 'fragment', extend: content })
57
+ if (isInitial || (!isInitial && !isModal)) element.set({ tag: options.useFragment && 'fragment', extend: content })
53
58
  if (options.updateState) element.state.update({ route, hash })
54
59
 
55
60
  const rootNode = element.node
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/router",
3
- "version": "2.11.25",
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": "d1168ce03f34dca3a88f82bcd0336de2c595d7d6",
14
+ "gitHead": "26b6500bc487650001f793925123dc2048065a87",
15
15
  "source": "index.js"
16
16
  }