@domql/router 2.2.5 → 2.3.0

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 +22 -24
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,40 +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
- return
11
- }
12
-
13
- const routes = splitRoute(route)
14
- let currentRoute = routes[level + 1]
15
-
16
- const hasHash = currentRoute.split('#')
17
- 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('#')
18
18
 
19
- const content = rootElement.routes[`/${currentRoute}`]
19
+ let route = getActiveRoute(pathname, level)
20
+ const content = element.routes[route]
20
21
 
21
22
  if (content) {
22
- currentRoute = `/${currentRoute}`
23
- if (hasHash[1]) currentRoute += `#${hasHash[1]}`
24
- if (pushState) window.history.pushState(state, null, currentRoute)
23
+ if (pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
25
24
 
26
- const rootNode = rootElement.node
25
+ element.set({ extend: content })
26
+ element.state.update({ route, hash })
27
+
28
+ const rootNode = element.node
27
29
  rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
28
- if (hasHash[1]) {
29
- const activeNode = document.getElementById(hasHash[1])
30
+ if (hash) {
31
+ const activeNode = document.getElementById(hash)
30
32
  if (activeNode) {
31
33
  const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 140
32
34
  rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
33
35
  }
34
36
  }
35
-
36
- rootElement.set({ proto: content })
37
37
  }
38
-
39
- if (level === 0) rootElement.state.update({ currentRoute })
40
38
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@domql/router",
3
- "version": "2.2.5",
3
+ "version": "2.3.0",
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": "c275ad11c795e7abd68cce00d0c758905c4e4f18",
9
+ "gitHead": "02944ad228d2cf5f5727b08e9378d221b2f7205b",
10
10
  "source": "index.js"
11
11
  }