Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
feat: add link factory
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 9, 2015
1 parent c336218 commit ce7c420
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# router5-deku
Helpers for using router5 with Deku

See: [router5](http://router5.github.io)
34 changes: 34 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "router5-deku",
"version": "0.1.0-alpha.1",
"homepage": "http://router5.github.io",
"authors": [
"Thomas Roch <thomas.c.roch@gmail.com>"
],
"description": "router5 helpers for Deku",
"main": "dist/browser/router5-deku.js",
"moduleType": [
"amd",
"es6",
"globals",
"node"
],
"keywords": [
"router",
"html5",
"history",
"tree",
"deku",
"functional"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"scripts",
"npm-debug.log",
"bower.json",
"README.md",
"package.json"
]
}
85 changes: 85 additions & 0 deletions modules/link-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {element} from 'deku'

// Use of a factory rather than source as having nested routers
// are not excluded
export default linkFactory

var listeners = {}

function linkFactory(router) {
let propTypes = {
label: {type: 'string', optional: false},
button: {type: 'boolean'},
routeName: {type: 'string', optional: false},
routeParams: {type: 'object'},
routeOptions: {type: 'object'},
activeClassName: {type: 'string'},
activeStrict: {type: 'function'},
onClick: {type: 'function'}
}

let defaultProps = {
activeClassName: 'active',
button: false,
activeStrict: false,
routeParams: {},
routeOptions: {}
}

let getInitialState = (props) {
return {
active: router.isActive(props.routeName, props.routeParams, props.activeStrict)
};
}

// Is it overkill?
let shouldUpdate = (component, nextProps, nextState) => {
let {state, props} = component

return !router.areStatesEqual(
{name: nextProps.routeName, params: nextProps.routeParams},
{name: props.routeName, params: props.routeParams}
) || state.active !== nextState.active;
}

let render = (component) => {
let {props,state} = component

}

let clickHandler = (evt, component, updateState) => {
evt.preventDefault()

let {props} = component
router.navigate(props.routeName, props.routeParams, props.routeOptions)
}

let afterMount = (component, el, setState) => {
listeners[component.id] = () => {
let {props} = component.props
setState({active: router.isActive(props.routeName, props.routeParams)})
}

router.addListener(listeners[component.id])
}

let beforeUnmout = (component, el) => {
router.removeListener(listeners[component.id])
delete listeners[component.id]
}

let render = (component, el) => {
let {props, state} = component
let href = router.buildUrl(props.routeName, props.routeParams)
let className = (props.className ? props.className.split(' ') : [])
.concat(state.active ? [props.activeClassName] : []).join(' ')
let onClick = props.onClick || clickHandler

if (button) {
return element('button', {type: 'button', className, onClick}, [props.label])
}
return element('a', {href, className, onClick}, [props.label])
}

return {propTypes, defaultProps, shouldUpdate, afterMount, beforeUnmout, render}
}
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "router5-deku",
"version": "0.1.0-alpha.1",
"description": "router5 helpers for Deku",
"main": "dist/commonjs/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/router5/router5-deku.git"
},
"keywords": [
"router",
"html5",
"history",
"tree",
"deku",
"functional"
],
"author": "Thomas Roch",
"license": "MIT",
"bugs": {
"url": "https://github.com/router5/router5-deku/issues"
},
"homepage": "http://router5.github.io",
"devDependencies": {
"router5": "0.1.0-rc.7",
"async": "^1.3.0",
"uglify-js2": "^2.1.11",
"babel": "^5.6.14"
},
"peerDependencies": {
"router5": ">=0.1.0-rc.7"
}
}

0 comments on commit ce7c420

Please sign in to comment.