preact-router
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -182,12 +182,11 @@ (function (global, factory) { | ||
function Router() { | ||
function Router(props) { | ||
babelHelpers.classCallCheck(this, Router); | ||
_Component.apply(this, arguments); | ||
_Component.call(this); | ||
this.state = { | ||
url: props.url || getCurrentUrl() | ||
}; | ||
} | ||
Router.prototype.getInitialState = function getInitialState() { | ||
return { url: getCurrentUrl() }; | ||
}; | ||
Router.prototype.routeTo = function routeTo(url) { | ||
@@ -194,0 +193,0 @@ this.setState({ url: url }); |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(t){"use strict";function e(t,e){var n=arguments.length<=2||void 0===arguments[2]?l:arguments[2],o=/(?:\?([^#]*))?(#.*)?$/,i=t.match(o),u={},a=void 0;if(i&&i[1])for(var c=i[1].split("&"),p=0;p<c.length;p++){var f=c[p].split("=");u[decodeURIComponent(f[0])]=decodeURIComponent(f.slice(1).join("="))}t=r(t.replace(o,"")),e=r(e||"");for(var s=Math.max(t.length,e.length),p=0;s>p;p++)if(e[p]&&":"===e[p].charAt(0)){var h=e[p].replace(/(^\:|[+*?]+$)/g,""),d=(e[p].match(/[+*?]+$/)||l)[0]||"",v=~d.indexOf("+"),y=~d.indexOf("*"),m=t[p]||"";if(!m&&!y&&(d.indexOf("?")<0||v)){a=!1;break}if(u[h]=decodeURIComponent(m),v||y){u[h]=t.slice(p).map(decodeURIComponent).join("/");break}}else if(e[p]!==t[p]){a=!1;break}return n["default"]!==!0&&a===!1?!1:u}function n(t,e){var n=t.attributes||l,r=e.attributes||l;if(n["default"])return 1;if(r["default"])return-1;var i=o(n.path)-o(r.path);return i||n.path.length-r.path.length}function r(t){return i(t).split("/")}function o(t){return(i(t).match(/\/+/g)||"").length}function i(t){return t.replace(/(^\/+|\/+$)/g,"")}function u(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),history&&(e===!0?history.replaceState(null,null,t):history.pushState(null,null,t)),a(t)}function a(t){s.forEach(function(e){return e.routeTo(t)})}function c(){var t="undefined"!=typeof location?location:h;return""+(t.pathname||"")+(t.search||"")}function p(t){return u(this.getAttribute("href")),t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation(),t.preventDefault(),!1}var f={};f.inherits=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},f.objectWithoutProperties=function(t,e){var n={};for(var r in t)e.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r]);return n},f._extends=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},f.a=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};var l={},s=[],h={};"function"==typeof addEventListener&&addEventListener("popstate",function(){return a(c())});var d=function(e){var n=e.children,r=f.objectWithoutProperties(e,["children"]);return t.h("a",f._extends({},r,{onClick:p}),n)},v=function(t){function r(){f.a(this,r),t.apply(this,arguments)}return f.inherits(r,t),r.prototype.getInitialState=function(){return{url:c()}},r.prototype.routeTo=function(t){this.setState({url:t})},r.prototype.componentWillMount=function(){s.push(this)},r.prototype.componentWillUnmount=function(){s.splice(s.indexOf(this),1)},r.prototype.render=function(t,r){var o=t.children,i=t.onChange,u=r.url,a=o.slice().sort(n).filter(function(t){var n=t.attributes,r=n.path,o=e(u,r,n);if(o){n.url=u,n.matches=o;for(var i in o)o.hasOwnProperty(i)&&(n[i]=o[i]);return!0}}),c=this.previousUrl;return u!==c&&(this.previousUrl=u,"function"==typeof i&&i({router:this,url:u,previous:c,active:a,current:a[0]})),a[0]||null},r}(t.Component),y=function(e){var n=e.component,r=e.url,o=e.matches;return t.h(n,{url:r,matches:o})};return v.route=u,v.Router=v,v.Route=y,v.Link=d,v}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(t){"use strict";function e(t,e){var n=arguments.length<=2||void 0===arguments[2]?l:arguments[2],o=/(?:\?([^#]*))?(#.*)?$/,i=t.match(o),u={},a=void 0;if(i&&i[1])for(var c=i[1].split("&"),p=0;p<c.length;p++){var f=c[p].split("=");u[decodeURIComponent(f[0])]=decodeURIComponent(f.slice(1).join("="))}t=r(t.replace(o,"")),e=r(e||"");for(var s=Math.max(t.length,e.length),p=0;s>p;p++)if(e[p]&&":"===e[p].charAt(0)){var h=e[p].replace(/(^\:|[+*?]+$)/g,""),d=(e[p].match(/[+*?]+$/)||l)[0]||"",m=~d.indexOf("+"),v=~d.indexOf("*"),g=t[p]||"";if(!g&&!v&&(d.indexOf("?")<0||m)){a=!1;break}if(u[h]=decodeURIComponent(g),m||v){u[h]=t.slice(p).map(decodeURIComponent).join("/");break}}else if(e[p]!==t[p]){a=!1;break}return n["default"]!==!0&&a===!1?!1:u}function n(t,e){var n=t.attributes||l,r=e.attributes||l;if(n["default"])return 1;if(r["default"])return-1;var i=o(n.path)-o(r.path);return i||n.path.length-r.path.length}function r(t){return i(t).split("/")}function o(t){return(i(t).match(/\/+/g)||"").length}function i(t){return t.replace(/(^\/+|\/+$)/g,"")}function u(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),history&&(e===!0?history.replaceState(null,null,t):history.pushState(null,null,t)),a(t)}function a(t){s.forEach(function(e){return e.routeTo(t)})}function c(){var t="undefined"!=typeof location?location:h;return""+(t.pathname||"")+(t.search||"")}function p(t){return u(this.getAttribute("href")),t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation(),t.preventDefault(),!1}var f={};f.inherits=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},f.objectWithoutProperties=function(t,e){var n={};for(var r in t)e.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r]);return n},f._extends=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},f.a=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};var l={},s=[],h={};"function"==typeof addEventListener&&addEventListener("popstate",function(){return a(c())});var d=function(e){var n=e.children,r=f.objectWithoutProperties(e,["children"]);return t.h("a",f._extends({},r,{onClick:p}),n)},m=function(t){function r(e){f.a(this,r),t.call(this),this.state={url:e.url||c()}}return f.inherits(r,t),r.prototype.routeTo=function(t){this.setState({url:t})},r.prototype.componentWillMount=function(){s.push(this)},r.prototype.componentWillUnmount=function(){s.splice(s.indexOf(this),1)},r.prototype.render=function(t,r){var o=t.children,i=t.onChange,u=r.url,a=o.slice().sort(n).filter(function(t){var n=t.attributes,r=n.path,o=e(u,r,n);if(o){n.url=u,n.matches=o;for(var i in o)o.hasOwnProperty(i)&&(n[i]=o[i]);return!0}}),c=this.previousUrl;return u!==c&&(this.previousUrl=u,"function"==typeof i&&i({router:this,url:u,previous:c,active:a,current:a[0]})),a[0]||null},r}(t.Component),v=function(e){var n=e.component,r=e.url,o=e.matches;return t.h(n,{url:r,matches:o})};return m.route=u,m.Router=m,m.Route=v,m.Link=d,m}); | ||
//# sourceMappingURL=preact-router.min.js.map |
{ | ||
"name": "preact-router", | ||
"amdName": "preactRouter", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Connect your components up to that address bar.", | ||
@@ -38,3 +38,3 @@ "main": "dist/preact-router.js", | ||
"babel": "^5.8.23", | ||
"babel-eslint": "^5.0.0", | ||
"babel-eslint": "^6.0.0", | ||
"chai": "^3.5.0", | ||
@@ -41,0 +41,0 @@ "eslint": "^2.2.0", |
@@ -53,4 +53,7 @@ import { h, Component } from 'preact'; | ||
class Router extends Component { | ||
getInitialState() { | ||
return { url: getCurrentUrl() }; | ||
constructor(props) { | ||
super(); | ||
this.state = { | ||
url: props.url || getCurrentUrl() | ||
}; | ||
} | ||
@@ -57,0 +60,0 @@ |
@@ -18,3 +18,3 @@ import { Router, Link, route } from '../src'; | ||
it('should filter children based on URL', () => { | ||
let router = new Router(); | ||
let router = new Router({}); | ||
let children = [ | ||
@@ -40,3 +40,3 @@ <foo path="/" />, | ||
it('should support nested parameterized routes', () => { | ||
let router = new Router(); | ||
let router = new Router({}); | ||
let children = [ | ||
@@ -63,3 +63,3 @@ <foo path="/foo" />, | ||
it('should support default routes', () => { | ||
let router = new Router(); | ||
let router = new Router({}); | ||
let children = [ | ||
@@ -83,3 +83,18 @@ <foo default />, | ||
}); | ||
it('should support initial route prop', () => { | ||
let router = new Router({ url:'/foo' }); | ||
let children = [ | ||
<foo default />, | ||
<foo path="/" />, | ||
<foo path="/foo" /> | ||
]; | ||
expect( | ||
router.render({ children }, router.state) | ||
).to.equal(children[2]); | ||
expect(new Router({})).to.have.deep.property('state.url', ''); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51267
599