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

Commit 446431e

Browse files
committed
Hyperapp#2 support
1 parent 97b0a07 commit 446431e

File tree

8 files changed

+68
-134
lines changed

8 files changed

+68
-134
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
"babel-jest": "^22.4.3",
4949
"babel-plugin-transform-react-jsx": "^6.24.1",
5050
"babel-preset-env": "^1.6.1",
51-
"hyperapp": "^1.2.5",
51+
"hyperapp": "^2.0.0",
5252
"jest": "^22.4.3",
5353
"prettier": "^1.11.1",
5454
"rollup": "^0.57.1",
5555
"uglify-js": "^3.3.16",
5656
"typescript": "2.8.1"
5757
},
5858
"peerDependencies": {
59-
"hyperapp": "1.2.5"
59+
"hyperapp": "2.0.0"
6060
}
6161
}

src/Link.js

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
1-
import { h } from "hyperapp"
1+
import { h } from 'hyperapp'
2+
import { location } from './location'
23

3-
function getOrigin(loc) {
4-
return loc.protocol + "//" + loc.hostname + (loc.port ? ":" + loc.port : "")
5-
}
6-
7-
function isExternal(anchorElement) {
8-
// Location.origin and HTMLAnchorElement.origin are not
9-
// supported by IE and Safari.
10-
return getOrigin(location) !== getOrigin(anchorElement)
4+
function onClick(state, link, e){
5+
if(
6+
e.defaultPrevented ||
7+
e.button !== 0 ||
8+
e.altKey ||
9+
e.metaKey ||
10+
e.ctrlKey ||
11+
e.shiftKey ||
12+
e.target.getAttribute("target") === "_blank" ||
13+
link.href === state.location.pathname
14+
){
15+
return state
16+
} else {
17+
e.preventDefault()
18+
return location(state, link.href)
19+
}
1120
}
1221

13-
export function Link(props, children) {
14-
return function(state, actions) {
15-
var to = props.to
16-
var location = state.location
17-
var onclick = props.onclick
18-
delete props.to
19-
delete props.location
20-
21-
props.href = to
22-
props.onclick = function(e) {
23-
if (onclick) {
24-
onclick(e)
25-
}
26-
if (
27-
e.defaultPrevented ||
28-
e.button !== 0 ||
29-
e.altKey ||
30-
e.metaKey ||
31-
e.ctrlKey ||
32-
e.shiftKey ||
33-
props.target === "_blank" ||
34-
isExternal(e.currentTarget)
35-
) {
36-
} else {
37-
e.preventDefault()
38-
39-
if (to !== location.pathname) {
40-
history.pushState(location.pathname, "", to)
41-
}
42-
}
43-
}
44-
45-
return h("a", props, children)
46-
}
22+
export function Link(props, childrens){
23+
props.onClick = props.onClick || [ onClick, props ]
24+
props.href = props.to
25+
delete props.to
26+
return h('a', props, childrens)
4727
}

src/Redirect.js

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

src/Route.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import { parseRoute } from "./parseRoute"
1+
import { Switch } from './Switch'
2+
import { parseRoute } from './parseRoute'
23

3-
export function Route(props) {
4-
return function(state, actions) {
5-
var location = state.location
6-
var match = parseRoute(props.path, location.pathname, {
7-
exact: !props.parent
8-
})
9-
10-
return (
11-
match &&
12-
props.render({
13-
match: match,
14-
location: location
4+
export function Route(props, childrens){
5+
var location = props.location
6+
var path = props.path || ''
7+
var render = props.render || Switch
8+
var match = parseRoute(path, location.pathname, {
9+
exact: !props.parent
10+
})
11+
return (match && render(
12+
function(){
13+
var args = Array.prototype.slice.call(arguments, 0)
14+
args[0] = Object.assign({}, args[0], {
15+
location: location,
16+
path: path + args[0].path || '',
1517
})
16-
)
17-
}
18-
}
18+
return Route.apply(this, args)
19+
},
20+
Object.assign({}, props, { match: match, location: location } ),
21+
childrens)
22+
)
23+
}

src/Switch.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
export function Switch(props, children) {
2-
return function(state, actions) {
3-
var child,
4-
i = 0
5-
while (
6-
!(child = children[i] && children[i](state, actions)) &&
7-
i < children.length
8-
)
9-
i++
10-
return child
11-
}
1+
export function Switch(props, childrens){
2+
return childrens[0]
123
}

src/history.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function history(location){
2+
if(location && window.location.pathname !== location.pathname){
3+
window.history.pushState(location, '', location.pathname)
4+
}
5+
}

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { Link } from "./Link"
2-
export { Route } from "./Route"
3-
export { Switch } from "./Switch"
4-
export { Redirect } from "./Redirect"
5-
export { location } from "./location"
1+
export { location } from './location'
2+
export { history } from './history'
3+
export { Switch } from './Switch'
4+
export { Route } from './Route'
5+
export { Link } from './Link'

src/location.js

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

0 commit comments

Comments
 (0)