@symbo.ls/router 2.12.111 → 2.12.115

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 (3) hide show
  1. package/index.js +88 -44
  2. package/location.js +3 -0
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -1,21 +1,31 @@
1
- 'use strict'
2
-
3
1
  import * as utils from '@domql/utils'
4
- const { setCookie, merge } = (utils.default || utils)
2
+ const { setCookie, merge } = utils.default || utils
3
+
4
+ export * from './location.js'
5
5
 
6
- export const getLocationPath = (index = 0, route = window.location.pathname) => {
6
+ export const getLocationPath = (
7
+ index = 0,
8
+ route = window.location.pathname
9
+ ) => {
7
10
  const routeArray = route.split('/')
8
11
  const activeRoute = routeArray[index + 1]
9
- if (activeRoute) return `/${activeRoute}`
12
+ if (activeRoute) {
13
+ return `/${activeRoute}`
14
+ }
10
15
  }
11
16
 
12
- export const getLastLocationPath = (level = 0, route = window.location.pathname) => {
17
+ export const getLastLocationPath = (
18
+ level = 0,
19
+ route = window.location.pathname
20
+ ) => {
13
21
  const routeArray = route.split('/')
14
22
  const activeRoute = routeArray[routeArray.length - level - 1]
15
- if (activeRoute) return `/${activeRoute}`
23
+ if (activeRoute) {
24
+ return `/${activeRoute}`
25
+ }
16
26
  }
17
27
 
18
- export let lastPathname
28
+ export let lastPathname = null
19
29
  export let lastLevel = 0
20
30
 
21
31
  const defaultOptions = {
@@ -41,12 +51,9 @@ const defaultOptions = {
41
51
  scrollToOptions: { behavior: 'smooth' }
42
52
  }
43
53
 
44
- export function router (
45
- path = '/',
46
- el,
47
- state = {},
48
- passedOptions = {}
49
- ) {
54
+ // eslint-disable-next-line func-style, complexity, default-param-last
55
+ export function router (path = '/', el, state = {}, passedOptions = {}) {
56
+ // eslint-disable-next-line no-invalid-this
50
57
  const element = el || (this && this.getRoot())
51
58
  const options = merge(passedOptions, defaultOptions)
52
59
  lastLevel = options.level
@@ -71,76 +78,113 @@ export function router (
71
78
 
72
79
  const content = element.routes[routeInContent] || element.routes['/*']
73
80
 
74
- const wasModal = element.call('isModalRoute', options.window.location.pathname.split('#')[0])
81
+ const wasModal = element.call(
82
+ 'isModalRoute',
83
+ options.window.location.pathname.split('#')[0]
84
+ )
75
85
  const isInitial = options.initialRender
76
86
  const hashChanged = hash && hash !== options.window.location.hash
77
87
  const pathChanged = pathname !== lastPathname
78
88
  lastPathname = pathname
79
89
 
80
- if (!content || (element.state.root.debugging && !options.allowRouterWhileInspect)) return
90
+ if (
91
+ !content ||
92
+ (element.state.root.debugging && !options.allowRouterWhileInspect)
93
+ ) {
94
+ return
95
+ }
81
96
  if (options.pushState) {
82
- options.window.history.pushState({
83
- prevUrl: options.window.location.href, ...state
84
- }, null, route + (search ? `${search}` : '') + (hash ? `${hash}` : ''))
97
+ options.window.history.pushState(
98
+ {
99
+ prevUrl: options.window.location.href,
100
+ ...state
101
+ },
102
+ null,
103
+ route + (search ? `${search}` : '') + (hash ? `${hash}` : '')
104
+ )
85
105
  }
86
106
 
87
- const isModal = !!element.call('isModalRoute', pathname)
107
+ const isModal = Boolean(element.call('isModalRoute', pathname))
88
108
 
89
109
  if (pathChanged || !hashChanged) {
90
110
  // const rootRouteHasChanged = rootRoute !== element.state.rootRoute
91
111
  // || (!rootRouteHasChanged && { preventUpdateTriggerStateUpdate: true, preventContentUpdate: true })
92
112
 
93
113
  if (options.updateState) {
94
- element.state.update({ route, hash, routeInContent, debugging: false }, {
95
- preventUpdate: options.preventUpdate,
96
- updateByRouter: true,
97
- onlyUpdate: isModal && 'Modal',
98
- ...options.updateStateOptions
99
- })
114
+ element.state.update(
115
+ { route, hash, routeInContent, debugging: false },
116
+ {
117
+ preventUpdate: options.preventUpdate,
118
+ updateByRouter: true,
119
+ onlyUpdate: isModal && 'Modal',
120
+ ...options.updateStateOptions
121
+ }
122
+ )
100
123
  }
101
124
 
102
125
  const hasSet = isInitial || (!isInitial && !isModal)
103
126
  if (hasSet) {
104
127
  if (options.forceContentUpdate || (!isInitial && wasModal)) {
105
- element.content.update({}, {
106
- preventUpdate: options.preventUpdate
107
- })
128
+ element.content.update(
129
+ {},
130
+ {
131
+ preventUpdate: options.preventUpdate
132
+ }
133
+ )
108
134
  } else {
109
- element.set({
110
- tag: options.useFragment && 'fragment',
111
- extend: content
112
- }, {
113
- // ...(!isInitial && wasModal && !options.forceContentUpdate && { preventContentUpdate: true }),
114
- preventUpdate: options.preventUpdate,
115
- ...options.setOptions
116
- })
135
+ element.set(
136
+ {
137
+ tag: options.useFragment && 'fragment',
138
+ extend: content
139
+ },
140
+ {
141
+ // ...(!isInitial && wasModal && !options.forceContentUpdate && { preventContentUpdate: true }),
142
+ preventUpdate: options.preventUpdate,
143
+ ...options.setOptions
144
+ }
145
+ )
146
+ }
147
+ if (element.content) {
148
+ element.content.__ref.__componentKey = route
117
149
  }
118
- element.content.__ref.__componentKey = route
119
150
  }
120
151
  }
121
152
 
122
153
  const rootNode = element.node
123
- const scrollNode = options.scrollDocument ? options.window.document.documentElement : rootNode
154
+ const scrollNode = options.scrollDocument
155
+ ? options.window.document.documentElement
156
+ : rootNode
124
157
  if (options.scrollToTop) {
125
158
  scrollNode.scrollTo({
126
- ...(options.scrollToOptions || {}), top: 0, left: 0
159
+ ...(options.scrollToOptions || {}),
160
+ top: 0,
161
+ left: 0
127
162
  })
128
163
  }
129
164
  if (options.scrollToNode) {
130
165
  content.content.node.scrollTo({
131
- ...(options.scrollToOptions || {}), top: 0, left: 0
166
+ ...(options.scrollToOptions || {}),
167
+ top: 0,
168
+ left: 0
132
169
  })
133
170
  }
134
171
 
135
172
  if (hash) {
136
173
  const activeNode = options.window.document.getElementById(hash.slice(1))
137
174
  if (activeNode) {
138
- const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - options.scrollToOffset || 0
175
+ const top =
176
+ activeNode.getBoundingClientRect().top +
177
+ rootNode.scrollTop -
178
+ options.scrollToOffset || 0
139
179
  scrollNode.scrollTo({
140
- ...(options.scrollToOptions || {}), top, left: 0
180
+ ...(options.scrollToOptions || {}),
181
+ top,
182
+ left: 0
141
183
  })
142
184
  }
143
185
  }
144
186
 
145
- if (route !== '/*') setCookie('route', route)
187
+ if (route !== '/*') {
188
+ setCookie('route', route)
189
+ }
146
190
  }
package/location.js ADDED
@@ -0,0 +1,3 @@
1
+ export const onLeave = msg => {
2
+ window.onbeforeunload = () => msg || 'Are you sure you want to leave?'
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/router",
3
- "version": "2.12.111",
3
+ "version": "2.12.115",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -12,6 +12,6 @@
12
12
  "@domql/utils": "latest",
13
13
  "@symbo.ls/auth-redirect": "latest"
14
14
  },
15
- "gitHead": "ead7b40e059bcf0f3f8afacdf108e90c57ea6a2f",
15
+ "gitHead": "f777ffc758150302112fa1fd4b7b852a15bc939e",
16
16
  "source": "index.js"
17
17
  }