From ce7c420eab019cfa1933b02fc83372dfd9f505f9 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Thu, 9 Jul 2015 17:08:50 +0100 Subject: [PATCH] feat: add link factory --- .gitignore | 2 + README.md | 2 + bower.json | 34 +++++++++++++++++ modules/link-factory.js | 85 +++++++++++++++++++++++++++++++++++++++++ package.json | 36 +++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 .gitignore create mode 100644 bower.json create mode 100644 modules/link-factory.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ca9571 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log diff --git a/README.md b/README.md index ad287cc..16e413c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # router5-deku Helpers for using router5 with Deku + +See: [router5](http://router5.github.io) diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..77ef000 --- /dev/null +++ b/bower.json @@ -0,0 +1,34 @@ +{ + "name": "router5-deku", + "version": "0.1.0-alpha.1", + "homepage": "http://router5.github.io", + "authors": [ + "Thomas Roch " + ], + "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" + ] +} diff --git a/modules/link-factory.js b/modules/link-factory.js new file mode 100644 index 0000000..49755a0 --- /dev/null +++ b/modules/link-factory.js @@ -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} +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..036f4b6 --- /dev/null +++ b/package.json @@ -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" + } +}