@domql/router 2.2.4 → 2.2.7

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 +28 -24
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,34 +1,38 @@
1
1
  'use strict'
2
2
 
3
- export const splitRoute = (route = window.location.pathname) => route.split('/')
3
+ export const getActiveRoute = (
4
+ route = window.location.pathname, level
5
+ ) => `/${route.split('/')[level + 1]}`
4
6
 
5
- export const router = (rootElement, path, state = {}, level = 0, pushState = true) => {
6
- const route = path || window.location.pathname
7
+ export let lastLevel = 0
7
8
 
8
- if (path.slice(0, 1) === '#') {
9
- window.location.hash = path
10
- }
11
-
12
- const routes = splitRoute(route)
13
- let currentRoute = routes[level + 1]
14
-
15
- const hasHash = currentRoute.split('#')
16
- if (hasHash[1]) currentRoute = hasHash[0]
9
+ export const router = (
10
+ element,
11
+ path,
12
+ state = {},
13
+ level = 0,
14
+ pushState = true
15
+ ) => {
16
+ const lastLevel = level
17
+ const [ pathname, hash ] = (path).split('#')
17
18
 
18
- const content = rootElement.routes[`/${currentRoute}`]
19
+ let route = getActiveRoute(pathname, level)
20
+ const content = element.routes[route]
19
21
 
20
22
  if (content) {
21
- currentRoute = `/${currentRoute}`
22
- if (hasHash[1]) currentRoute += `#${hasHash[1]}`
23
- if (pushState) window.history.pushState(state, null, currentRoute)
24
-
25
- const newContent = rootElement.set({ proto: content }).content.node
26
- newContent.scrollIntoView({ behavior: 'smooth', block: 'start' })
27
- if (hasHash[1]) {
28
- // window.location.hash = hasHash[1]
29
- document.getElementById(hasHash[1]).scrollIntoView({ behavior: 'smooth', block: 'start' })
23
+ if (pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
24
+
25
+ element.set({ proto: content })
26
+ element.state.update({ route, hash })
27
+
28
+ const rootNode = element.node
29
+ rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
30
+ if (hash) {
31
+ const activeNode = document.getElementById(hash)
32
+ if (activeNode) {
33
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 140
34
+ rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
35
+ }
30
36
  }
31
37
  }
32
-
33
- if (level === 0) rootElement.state.update({ currentRoute })
34
38
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@domql/router",
3
- "version": "2.2.4",
3
+ "version": "2.2.7",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "vpatch": "npm version patch && npm publish --access public"
8
8
  },
9
- "gitHead": "a105ac3a4a89d120ec7199d9a4b9706e76ce128f",
9
+ "gitHead": "9952e1abd4466080e0f8d5031fe4962694746591",
10
10
  "source": "index.js"
11
11
  }