UNPKG

8.45 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).Curi={})}(this,function(e){"use strict";function w(e){return"externalURL"in e}function R(e,t,n,r,i){var o=n||{},a=o.resolved,u=void 0===a?null:a,c=o.error,l=void 0===c?null:c,p={data:void 0,body:void 0,meta:void 0};for(var f in t)p[f]=t[f];if(!e.methods.respond)return p;var s=e.methods.respond({resolved:u,error:l,match:t,external:i});return s&&(p.meta=s.meta,p.body=s.body,p.data=s.data,s.redirect&&(p.redirect=function(e,t){if(w(e))return e;var n=e.name,r=e.params,i=e.query,o=e.hash,a=e.state,u=t.url({name:n,params:r,query:i,hash:o});return{name:n,params:r,query:i,hash:o,state:a,url:u}}(s.redirect,r))),p}function t(e,t){return a(o(e,t))}var y=c,n=o,r=a,i=u,T="/",$="./",j=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g");function o(e,t){for(var n,r,i=[],o=0,a=0,u="",c=t&&t.delimiter||T,l=t&&t.delimiters||$,p=!1;null!==(n=j.exec(e));){var f=n[0],s=n[1],d=n.index;if(u+=e.slice(a,d),a=d+f.length,s)u+=s[1],p=!0;else{var h="",v=e[a],m=n[2],g=n[3],y=n[4],x=n[5];if(!p&&u.length){var b=u.length-1;-1<l.indexOf(u[b])&&(h=u[b],u=u.slice(0,b))}u&&(i.push(u),u="",p=!1);var E=""!==h&&void 0!==v&&v!==h,w="+"===x||"*"===x,R="?"===x||"*"===x,k=h||c,A=g||y;i.push({name:m||o++,prefix:h,delimiter:k,optional:R,repeat:w,partial:E,pattern:A?(r=A,r.replace(/([=!:$/()])/g,"\\$1")):"[^"+q(k)+"]+?"})}}return(u||a<e.length)&&i.push(u+e.substr(a)),i}function a(l){for(var p=new Array(l.length),e=0;e<l.length;e++)"object"==typeof l[e]&&(p[e]=new RegExp("^(?:"+l[e].pattern+")$"));return function(e,t){for(var n="",r=t&&t.encode||encodeURIComponent,i=0;i<l.length;i++){var o=l[i];if("string"!=typeof o){var a,u=e?e[o.name]:void 0;if(Array.isArray(u)){if(!o.repeat)throw new TypeError('Expected "'+o.name+'" to not repeat, but got array');if(0===u.length){if(o.optional)continue;throw new TypeError('Expected "'+o.name+'" to not be empty')}for(var c=0;c<u.length;c++){if(a=r(u[c],o),!p[i].test(a))throw new TypeError('Expected all "'+o.name+'" to match "'+o.pattern+'"');n+=(0===c?o.prefix:o.delimiter)+a}}else if("string"!=typeof u&&"number"!=typeof u&&"boolean"!=typeof u){if(!o.optional)throw new TypeError('Expected "'+o.name+'" to be '+(o.repeat?"an array":"a string"));o.partial&&(n+=o.prefix)}else{if(a=r(String(u),o),!p[i].test(a))throw new TypeError('Expected "'+o.name+'" to match "'+o.pattern+'", but got "'+a+'"');n+=o.prefix+a}}else n+=o}return n}}function q(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function h(e){return e&&e.sensitive?"":"i"}function u(e,t,n){for(var r=(n=n||{}).strict,i=!1!==n.start,o=!1!==n.end,a=q(n.delimiter||T),u=n.delimiters||$,c=[].concat(n.endsWith||[]).map(q).concat("$").join("|"),l=i?"^":"",p=0===e.length,f=0;f<e.length;f++){var s=e[f];if("string"==typeof s)l+=q(s),p=f===e.length-1&&-1<u.indexOf(s[s.length-1]);else{var d=s.repeat?"(?:"+s.pattern+")(?:"+q(s.delimiter)+"(?:"+s.pattern+"))*":s.pattern;t&&t.push(s),s.optional?s.partial?l+=q(s.prefix)+"("+d+")?":l+="(?:"+q(s.prefix)+"("+d+"))?":l+=q(s.prefix)+"("+d+")"}}return o?(r||(l+="(?:"+a+")?"),l+="$"===c?"$":"(?="+c+")"):(r||(l+="(?:"+a+"(?="+c+"))?"),p||(l+="(?="+a+"|"+c+")")),new RegExp(l,h(n))}function c(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;var n=e.source.match(/\((?!\?)/g);if(n)for(var r=0;r<n.length;r++)t.push({name:r,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,pattern:null});return e}(e,t):Array.isArray(e)?function(e,t,n){for(var r=[],i=0;i<e.length;i++)r.push(c(e[i],t,n).source);return new RegExp("(?:"+r.join("|")+")",h(n))}(e,t,n):function(e,t,n){return u(o(e,n),t,n)}(e,t,n)}y.parse=n,y.compile=t,y.tokensToFunction=r,y.tokensToRegExp=i;var x=function(e){return"/"===e.charAt(0)?e:"/"+e},b=function(e,t){return function(e){return"/"===e.charAt(e.length-1)?e:e+"/"}(e)+t};function v(e,t){var n=e.matching,r=n.re,i=n.children,o=n.exact,a=r.exec(t);if(!a)return[];var u=a[0],c=[{route:e,parsed:a.slice(1)}],l=t.slice(u.length);if(!i.length||""===l)return c;for(var p=x(l),f=0,s=i.length;f<s;f++){var d=v(i[f],p);if(d.length)return c.concat(d)}return o?[]:c}e.announce=function(t,e){void 0===e&&(e="assertive");var n=document.createElement("div");return n.setAttribute("aria-live",e),n.setAttribute("style",["border: 0 !important;","clip: rect(1px, 1px, 1px, 1px) !important;","-webkit-clip-path: inset(50%) !important;","clip-path: inset(50%) !important;","height: 1px !important;","overflow: hidden !important;","padding: 0 !important;","position: absolute !important;","width: 1px !important;","white-space: nowrap !important;","top: 0;"].join(" ")),document.body.appendChild(n),function(e){n.textContent=t(e)}},e.createRouter=function(e,o,a){var u,c;void 0===a&&(a={});var l=e(function(t){var n={action:t.action,previous:u},e=o.match(t.location);if(!e)return t.finish(),void g();var r=e.route,i=e.match;!function(e){return void 0!==e.methods.resolve}(r)?p(r,i,t,n,null):(x.length&&void 0===s&&(s=function(){l.cancel(),b(),m()},x.forEach(function(e){e(s)})),r.methods.resolve(i,a.external).then(function(e){return{resolved:e,error:null}},function(e){return{error:e,resolved:null}}).then(function(e){t.cancelled||p(r,i,t,n,e)}))},a.history||{});function p(e,t,n,r,i){b(),n.finish();var o=R(e,t,i,E,a.external);g(),function(e,t){if(!e.redirect||!d||w(e.redirect)){var n={response:u=e,navigation:c=t,router:E};!function(t){h.forEach(function(e){e(t)})}(n),function(t){v.splice(0).forEach(function(e){e(t)}),a.sideEffects&&a.sideEffects.forEach(function(e){e(t)})}(n)}void 0===e.redirect||w(e.redirect)||l.navigate(e.redirect,"replace")}(o,r)}var i,f,s,t=a.invisibleRedirects,d=void 0!==t&&t,h=[],v=[];function m(){i&&i(),y()}function g(){f&&f(),y()}function y(){f=i=void 0}var x=[];function b(){s&&(s=void 0,x.forEach(function(e){e()}))}var E={route:o.route,history:l,external:a.external,observe:function(t,e){var n=(e||{}).initial,r=void 0===n||n;return h.push(t),u&&r&&t({response:u,navigation:c,router:E}),function(){h=h.filter(function(e){return e!==t})}},once:function(e,t){var n=(t||{}).initial;u&&(void 0===n||n)?e({response:u,navigation:c,router:E}):v.push(e)},cancel:function(t){return x.push(t),function(){x=x.filter(function(e){return e!==t})}},url:function(e){var t,n=e.name,r=e.params,i=e.hash,o=e.query;if(n){var a=E.route(n);a&&(t=function(e,t){return e.methods.pathname(t)}(a,r))}return l.url({pathname:t,hash:i,query:o})},navigate:function(e){m();var t=e.url,n=e.state,r=e.method;if(l.navigate({url:t,state:n},r),e.cancelled||e.finished)return i=e.cancelled,f=e.finished,y},current:function(){return{response:u,navigation:c}},destroy:function(){l.destroy()}};return l.current(),E},e.prepareRoutes=function(e){var t={},n=e.map(function(e){return function t(e,n,r){void 0===r&&(r={path:"",keys:[]});var i=x(b(r.path,e.path)),o=e.pathOptions||{},a=o.match,u=void 0===a?{}:a,c=o.compile,l=void 0===c?{}:c,p=null==u.end||u.end;e.children&&e.children.length&&(u.end=!1);var f=[],s=y(x(e.path),f,u),d=f.map(function(e){return e.name});r.keys.length&&(d=r.keys.concat(d));var h=[],v=[];e.children&&e.children.length&&(v=(h=e.children.map(function(e){return t(e,n,{path:i,keys:d})})).map(function(e){return e.public}));var m=y.compile(i),g={public:{name:e.name,keys:d,parent:void 0,children:v,methods:{resolve:e.resolve,respond:e.respond,pathname:function(e){return m(e,l)}},extra:e.extra},matching:{re:s,keys:f,exact:p,parsers:e.params||{},children:h}};return n[e.name]=g.public,h.length&&h.forEach(function(e){e.public.parent=g.public}),g}(e,t)});return{match:function(e){return function(e,t){for(var n=0,r=t.length;n<r;n++){var i=v(t[n],e.pathname);if(i.length)return a=e,void 0,{route:u=(o=i)[o.length-1].route.public,match:{location:a,name:u.name,params:o.reduce(function(i,e){var o=e.route,t=e.parsed;return t.forEach(function(e,t){var n=o.matching.keys[t].name,r=o.matching.parsers[n]||decodeURIComponent;i[n]=r(e)}),i},{})}}}var o,a,u}(e,n)},route:function(e){return t[e]}}},e.scroll=function(){return function(e){var n=e.response;"pop"!==e.navigation.action&&setTimeout(function(){var e=n.location.hash;if(""!==e){var t=document.getElementById(e);if(t&&t.scrollIntoView)return void t.scrollIntoView()}window.scrollTo(0,0)},0)}},e.title=function(t){return function(e){document.title=t(e)}},Object.defineProperty(e,"__esModule",{value:!0})});