@hyperapp/router
Version:
Declarative routing for Hyperapp V1 using the History API.
2 lines • 2.34 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("hyperapp")):"function"==typeof define&&define.amd?define(["exports","hyperapp"],e):e(t.hyperappRouter={},t.hyperapp)}(this,function(t,u){"use strict";function c(t){return t.protocol+"//"+t.hostname+(t.port?":"+t.port:"")}function p(t,e,n,r){return{isExact:t,path:e,url:n,params:r}}function s(t){for(var e=t.length;"/"===t[--e];);return t.slice(0,e+1)}function l(e){try{return decodeURIComponent(e)}catch(t){return e}}var e={state:{pathname:window.location.pathname,previous:window.location.pathname},actions:{go:function(t){history.pushState(null,"",t)},set:function(t){return t}},subscribe:function(e){function t(t){e.set({pathname:window.location.pathname,previous:t.detail?window.location.previous=t.detail:window.location.previous})}var n=function(t){return t.reduce(function(t,e){var r=history[e];return history[e]=function(t,e,n){r.call(this,t,e,n),dispatchEvent(new CustomEvent("pushstate",{detail:t}))},function(){history[e]=r,t&&t()}},null)}(["pushState","replaceState"]);return addEventListener("pushstate",t),addEventListener("popstate",t),function(){removeEventListener("pushstate",t),removeEventListener("popstate",t),n()}}};t.Link=function(i,a){return function(t,e){var n=i.to,r=t.location,o=i.onclick;return delete i.to,delete i.location,i.href=n,i.onclick=function(t){o&&o(t),t.defaultPrevented||0!==t.button||t.altKey||t.metaKey||t.ctrlKey||t.shiftKey||"_blank"===i.target||function(t){return c(location)!==c(t)}(t.currentTarget)||(t.preventDefault(),n!==r.pathname&&history.pushState(r.pathname,"",n))},u.h("a",i,a)}},t.Route=function(o){return function(t,e){var n=t.location,r=function(t,e,n){if(t===e||!t)return p(t===e,t,e);var r=n&&n.exact,o=s(t).split("/"),i=s(e).split("/");if(!(o.length>i.length||r&&o.length<i.length)){var a=0,u={},c=o.length;for(e="";a<c;a++){if(":"===o[a][0])u[o[a].slice(1)]=i[a]=l(i[a]);else if(o[a]!==i[a])return;e+=i[a]+"/"}return p(!1,t,e.slice(0,-1),u)}}(o.path,n.pathname,{exact:!o.parent});return r&&o.render({match:r,location:n})}},t.Switch=function(t,o){return function(t,e){for(var n,r=0;!(n=o[r]&&o[r](t,e))&&r<o.length;)r++;return n}},t.Redirect=function(r){return function(t,e){var n=t.location;history.replaceState(r.from||n.pathname,"",r.to)}},t.location=e});
//# sourceMappingURL=router.js.map