@symbo.ls/router 2.12.111 → 2.12.113

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