@ajces/router
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -1,2 +0,2 @@ | ||
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?b(exports,require('hyperapp')):'function'==typeof define&&define.amd?define(['exports','hyperapp'],b):b(a.router={},a.hyperapp)})(this,function(a,b){'use strict';function c(a){var b={};return a.attributes.forEach(function(a){'name'===a.name&&(b.name=a.value),'content'===a.name&&(b.content=a.value)}),b}function d(a,b){return{match:function(c){for(var d,e,f,g,h={},k=0;k<a.length&&!d;k++){var i=a[k].path,l=[];c.replace(RegExp('*'===i?'.*':'^'+i.replace(/\//g,'\\/').replace(/:([\w]+)/g,function(a,b){return l.push(b),'([-\\.%\\w\\(\\)]+)'})+'/?$','g'),function(){for(var c,m=1;m<arguments.length-2;){c=arguments[m++];try{c=decodeURIComponent(c)}catch(a){}h[l.shift()]=c}d=i,f=a[k].component;var j;j=void 0!==a[k].meta&&'function'==typeof a[k].meta?a[k].meta(h):{},g=a[k].props,e=Object.assign({},b,j)})}return{component:f,match:d,meta:e,params:h,props:g}}}}var e,f=['','origin','referrer','viewport'];a.router=function(){return function(){return{state:{router:{}},actions:{router:{set:function(a,b,c){return{router:c}},go:function(a,b,c){location.pathname+location.search!==c&&(history.pushState({},'',c),b.router.set({path:c}))}}},events:{load:function(a,b){addEventListener('popstate',function(){b.router.set({path:location.pathname+location.search})})}}}}},a.updateMeta=function(a){document.title=a.title;var b=[].filter.call(document.getElementsByTagName('meta'),function(a){var b=c(a);return!(-1<f.indexOf(b.name))}),d=Object.keys(a).filter(function(a){return'title'!==a&&-1===f.indexOf(a)}),e=[];d.forEach(function(d){var f=b.filter(function(a){var b=c(a);return b.name===d})[0];if(f===void 0){var g=document.createElement('meta');g.setAttribute('name',d),g.setAttribute('content',a[d]),document.head.appendChild(g)}else f.setAttribute('content',a[d]);e.push(d)}),b.forEach(function(a){var b=c(a);-1===e.indexOf(b.name)&&-1===f.indexOf(b.name)&&document.head.removeChild(a)})},a.Matcher=d,a.Router=function(a,b){var c=a.meta,f=a.pathname,g=a.updateMeta;void 0==e&&(e=d(b,c));var h=e.match(f);return void 0!==h.meta&&{}!==h.meta&&void 0!==g&&g(h.meta),h.component(Object.assign({},h.props,{params:h.params}))},a.Route=function(a){var b=a.path,c=a.props,d=a.component,e=a.meta;return{path:b,props:c,component:d,meta:e}},a.Link=function(a,c){return a.href=a.to,a.to=null,a.onclick=function(b){0!==b.button||b.metaKey||b.altKey||b.ctrlKey||b.shiftKey||'_blank'===a.target||b.currentTarget.origin!==window.location.origin||(b.preventDefault(),a.go(a.href))},b.h('a',a,c)},Object.defineProperty(a,'__esModule',{value:!0})}); | ||
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?b(exports,require('hyperapp')):'function'==typeof define&&define.amd?define(['exports','hyperapp'],b):b(a.router={},a.hyperapp)})(this,function(a,b){'use strict';function c(a){var b={};return a.attributes.forEach(function(a){'name'===a.name&&(b.name=a.value),'content'===a.name&&(b.content=a.value)}),b}function d(a,b){return{match:function(c){for(var d,e,f,g,h,k={},l=0;l<a.length&&!d;l++){var i=a[l].path,m=[];c.replace(RegExp('*'===i?'.*':'^'+i.replace(/\//g,'\\/').replace(/:([\w]+)/g,function(a,b){return m.push(b),'([-\\.%\\w\\(\\)]+)'})+'/?$','g'),function(){for(var c,n=1;n<arguments.length-2;){c=arguments[n++];try{c=decodeURIComponent(c)}catch(a){}k[m.shift()]=c}d=i,f=a[l].component;var j;j=void 0!==a[l].meta&&'function'==typeof a[l].meta?a[l].meta(k):{},h=a[l].props,g=a[l].onroute,e=Object.assign({},b,j)})}return{component:f,match:d,meta:e,onroute:g,params:k,props:h}}}}var e=['','origin','referrer','viewport'];a.router=function(){return function(){return{state:{router:{}},actions:{router:{set:function(a,b,c){return{router:c}},go:function(a,b,c){location.pathname+location.search!==c&&(history.pushState({},'',c),b.router.set({path:c}))}}},events:{load:function(a,b){addEventListener('popstate',function(){b.router.set({path:location.pathname+location.search})})}}}}},a.updateMeta=function(a){document.title=a.title;var b=[].filter.call(document.getElementsByTagName('meta'),function(a){var b=c(a);return!(-1<e.indexOf(b.name))}),d=Object.keys(a).filter(function(a){return'title'!==a&&-1===e.indexOf(a)}),f=[];d.forEach(function(d){var e=b.filter(function(a){var b=c(a);return b.name===d})[0];if(e===void 0){var g=document.createElement('meta');g.setAttribute('name',d),g.setAttribute('content',a[d]),document.head.appendChild(g)}else e.setAttribute('content',a[d]);f.push(d)}),b.forEach(function(a){var b=c(a);-1===f.indexOf(b.name)&&-1===e.indexOf(b.name)&&document.head.removeChild(a)})},a.Matcher=d,a.Router=function(a,b){var c=a.meta,e=a.pathname,f=a.updateMeta,g=b.reduce(function(c,a){return c.concat(a)},[]),h=d(g,c).match(e);void 0!==h.meta&&{}!==h.meta&&void 0!==f&&f(h.meta);var i=Object.assign({},h);return delete i.onroute,h.onroute&&h.onroute(i),h.component(Object.assign({},h.props,{params:h.params}))},a.Route=function(a,b){var c=a.path,d=a.props,e=a.component,f=a.meta,g=a.onroute,h=[];if(e&&h.unshift({path:c,props:d,component:e,meta:f,onroute:g}),b)for(var j,k=b.reduce(function(c,a){return c.concat(a)},[]),l=k.length-1;-1<l;l--)j=k[l],j.path=c+j.path,h.unshift(j);return h},a.Link=function(a,c){return a.href=a.to,a.to=null,a.onclick=function(b){0!==b.button||b.metaKey||b.altKey||b.ctrlKey||b.shiftKey||'_blank'===a.target||b.currentTarget.origin!==window.location.origin||(b.preventDefault(),a.go(a.href))},b.h('a',a,c)},Object.defineProperty(a,'__esModule',{value:!0})}); | ||
//# sourceMappingURL=router.js.map |
{ | ||
"name": "@ajces/router", | ||
"description": "Utilities for routing client-side pages.", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"main": "dist/router.js", | ||
@@ -6,0 +6,0 @@ "jsnext:main": "src/index.js", |
import { h } from "hyperapp"; | ||
import { Matcher } from "./matcher"; | ||
import { Matcher } from "./matcher"; | ||
// <Router meta={defaultMeta} pathname={router.path}, updateMeta={updateMeta}}>...</Router> | ||
let router; // simplify and just have 1 router for now... | ||
export function Router({ meta, pathname, updateMeta }, children) { | ||
if (router === undefined) { | ||
router = Matcher(children, meta); | ||
} | ||
const match = router.match(pathname); | ||
const flatChildren = children.reduce((a, b) => a.concat(b), []); | ||
const match = Matcher(flatChildren, meta).match(pathname); | ||
if ( | ||
@@ -18,2 +15,9 @@ match.meta !== undefined && | ||
} | ||
let prunedMatch = Object.assign({}, match); | ||
delete prunedMatch.onroute; | ||
if (match.onroute) { | ||
match.onroute(prunedMatch); | ||
} | ||
// props for component should be {...match.props, params: match.params} | ||
@@ -26,4 +30,16 @@ return match.component( | ||
// <Route path="/" component={Home} meta={HomeMeta} /> | ||
export function Route({ path, props, component, meta }) { | ||
return { path, props, component, meta }; | ||
export function Route({ path, props, component, meta, onroute }, children) { | ||
const result = []; | ||
if (component) { | ||
result.unshift({ path, props, component, meta, onroute }); | ||
} | ||
if (children) { | ||
const flatChildren = children.reduce((a, b) => a.concat(b), []); | ||
for (let i = flatChildren.length - 1; i > -1; i--) { | ||
const child = flatChildren[i]; | ||
child.path = path + child.path; | ||
result.unshift(child); | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -30,0 +46,0 @@ |
@@ -63,7 +63,2 @@ function getNameAndContent(el) { | ||
export function Matcher(config, defaultMeta) { | ||
/* | ||
config.forEach(route => { | ||
route.meta = Object.assign({}, defaultMeta, route.meta); | ||
}); | ||
*/ | ||
return { | ||
@@ -75,2 +70,3 @@ match: pathname => { | ||
var params = {}; | ||
var onroute; | ||
var props; | ||
@@ -114,2 +110,3 @@ | ||
props = config[i].props; | ||
onroute = config[i].onroute; | ||
meta = Object.assign({}, defaultMeta, routeMeta); | ||
@@ -124,2 +121,3 @@ } | ||
meta, | ||
onroute, | ||
params, | ||
@@ -126,0 +124,0 @@ props |
Sorry, the diff of this file is not supported yet
23769
226