Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

Commit 18f51cd

Browse files
author
Jorge Bucaran
committed
Introduce new router API.
- location module - Route - Redirect - Link - Switch
1 parent abe55f3 commit 18f51cd

File tree

12 files changed

+166
-124
lines changed

12 files changed

+166
-124
lines changed

.editorconfig

Lines changed: 0 additions & 9 deletions
This file was deleted.

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
node_js:
3+
- "8"
34
- "7"
4-
- "6"
55

66
env:
77
- NODE_ENV=development
@@ -15,3 +15,6 @@ install:
1515
script:
1616
- npm test
1717
- codecov
18+
19+
notifications:
20+
email: false

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright © 2017-present [Jorge Bucaran](https://github.com/jbucaran)
1+
Copyright © 2017-present [Jorge Bucaran](https://github.com/JorgeBucaran)
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hyperapp/router",
33
"description": "Utilities for routing client-side pages with Hyperapp.",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"main": "dist/router.js",
66
"jsnext:main": "src/index.js",
77
"module": "src/index.js",
@@ -26,24 +26,24 @@
2626
"scripts": {
2727
"test": "jest --coverage --no-cache",
2828
"build": "npm run bundle && npm run minify",
29-
"bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router",
30-
"minify": "uglifyjs dist/router.js -o dist/router.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --source-map dist/router.js.map",
29+
"bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router -g hyperapp:hyperapp",
30+
"minify": "uglifyjs dist/router.js -o dist/router.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --in-source-map dist/router.js.map --source-map dist/router.js.map",
3131
"prepublish": "npm run build",
3232
"format": "prettier --semi false --write 'src/**/*.js'",
3333
"release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
3434
},
3535
"babel": {
36-
"presets": "es2015"
36+
"presets": "env"
3737
},
3838
"devDependencies": {
39-
"babel-preset-es2015": "^6.24.1",
40-
"hyperapp": "^0.12.0",
41-
"jest": "^20.0.4",
42-
"prettier": "~1.5.3",
43-
"rollup": "^0.47.6",
39+
"babel-preset-env": "^1.6.1",
40+
"hyperapp": "*",
41+
"jest": "^21.2.1",
42+
"prettier": "^1.8.2",
43+
"rollup": "^0.52.0",
4444
"uglify-js": "^2.7.5"
4545
},
4646
"peerDependencies": {
47-
"hyperapp": "^0.12.0"
47+
"hyperapp": "*"
4848
}
4949
}

src/Link.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import { h } from "hyperapp"
22

33
export function Link(props, children) {
4-
props.href = props.to
5-
props.to = null
4+
var href = props.to
5+
var location = props.location || window.location
66

7-
props.onclick = function(event) {
8-
if (
9-
event.button !== 0 ||
10-
event.metaKey ||
11-
event.altKey ||
12-
event.ctrlKey ||
13-
event.shiftKey ||
14-
props.target === "_blank" ||
15-
event.currentTarget.origin !== window.location.origin
16-
) {
17-
return
18-
}
7+
return h(
8+
"a",
9+
{
10+
href: href,
11+
onclick: function(e) {
12+
if (
13+
e.button !== 0 ||
14+
e.altKey ||
15+
e.metaKey ||
16+
e.ctrlKey ||
17+
e.shiftKey ||
18+
props.target === "_blank" ||
19+
e.currentTarget.origin !== location.origin
20+
) {
21+
} else {
22+
e.preventDefault()
1923

20-
event.preventDefault()
21-
props.go(props.href)
22-
}
23-
24-
return h("a", props, children)
24+
if (location.pathname !== href) {
25+
history.pushState(location.pathname, "", href)
26+
}
27+
}
28+
}
29+
},
30+
children
31+
)
2532
}

src/Redirect.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function Redirect(props) {
2+
var location = props.location || window.location
3+
history.replaceState(props.from || location.pathname, "", props.to)
4+
}

src/Route.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { parseRoute } from "./parseRoute"
2+
3+
export function Route(props) {
4+
var location = props.location || window.location
5+
var path = props.path
6+
var match =
7+
!path ||
8+
parseRoute(path, location.pathname, {
9+
exact: !props.parent
10+
})
11+
12+
return (
13+
match &&
14+
props.view({
15+
location: location,
16+
match: match
17+
})
18+
)
19+
}

src/Switch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function Switch(props, children) {
2+
return children[0]
3+
}

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
export { router } from "./router"
21
export { Link } from "./Link"
2+
export { Route } from "./Route"
3+
export { Switch } from "./Switch"
4+
export { Redirect } from "./Redirect"
5+
export { default as location } from "./location"

src/location.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var pathname = location.pathname
2+
var PUSHSTATE = "pushstate"
3+
var POPSTATE = "popstate"
4+
5+
export default {
6+
state: {
7+
pathname: pathname,
8+
previous: pathname
9+
},
10+
actions: {
11+
go: function(pathname) {
12+
history.pushState(null, "", pathname)
13+
},
14+
set: function(data) {
15+
return data
16+
}
17+
},
18+
subscribe: function(actions) {
19+
var unwrap = wrapHistory(["pushState", "replaceState"])
20+
21+
addEventListener(PUSHSTATE, handleLocationChange)
22+
addEventListener(POPSTATE, handleLocationChange)
23+
24+
return function() {
25+
removeEventListener(PUSHSTATE, handleLocationChange)
26+
removeEventListener(POPSTATE, handleLocationChange)
27+
unwrap()
28+
}
29+
30+
function handleLocationChange(e) {
31+
actions.set({
32+
pathname: location.pathname,
33+
previous: e.detail ? (location.previous = e.detail) : location.previous
34+
})
35+
}
36+
}
37+
}
38+
39+
function wrapHistory(keys) {
40+
return keys.reduce(function(next, key) {
41+
var method = history[key]
42+
43+
history[key] = function(data, title, url) {
44+
method.call(this, data, title, url)
45+
dispatchEvent(
46+
new CustomEvent("pushstate", {
47+
detail: data
48+
})
49+
)
50+
}
51+
52+
return function() {
53+
history[key] = method
54+
next && next()
55+
}
56+
}, null)
57+
}

0 commit comments

Comments
 (0)