-
Notifications
You must be signed in to change notification settings - Fork 0
/
kahe.min.js
2 lines (2 loc) · 4.37 KB
/
kahe.min.js
1
2
/*! kahe 0.7.4 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.kahe=t()}(this,function(){"use strict";function e(e){for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];return Object.assign?Object.assign.apply(Object,[e].concat(t)):(t.forEach(function(t){Object.keys(t).forEach(function(n,r){t.hasOwnProperty(r)&&(e[r]=n)})}),e)}function t(e){return Array.isArray(e)}function n(e){return"function"==typeof e}function r(e){return"object"==typeof e&&!t(e)}function i(e){return void 0===e}function o(){}function u(e,t){return"/"!==e[0]&&(e="/"+e),e=e.concat("/?").replace(/\/\(/g,"(?:/").replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?|\*/g,function(e,n,r,i,o,u){return"*"===e?(t.push(void 0),e):(t.push(i),n=n||"",(u?"":n)+"(?:"+(u?n:"")+(r||"")+(o||"([^/]+?)")+")"+(u||""))}).replace(/([\/.])/g,"\\$1").replace(/\*/g,"(.*)"),new RegExp("^"+e+"$","i")}function a(e){return"true"===e?e=!0:"false"===e?e=!1:"null"===e?e=null:"undefined"===e?e=void 0:!1===isNaN(e)&&(e=Number(e)),e}function c(e){if(!(e.defaultPrevented||e.ctrlKey||e.metaKey||e.shiftKey||0!==e.button)){for(var t=e.target;t&&"A"!==t.nodeName.toUpperCase();)t=t.parentNode;t&&t.href&&(t.target||-1===t.href.indexOf(v)||"external"===t.getAttribute("rel")||t.hasAttribute("download")||(e.preventDefault(),h(t.href)))}}function f(){h(window.location.href,{replace:!0})}function s(){var e=window.innerWidth,t=window.innerHeight;g&&g.resize(e,t),b&&b.resize(e,t)}function h(e,t){void 0===t&&(t={}),"/"!==(e=e.replace(v,"")).charAt(0)&&(e="/"+e);for(var n,r,o=0;o<x.length&&(n=x[o],!(r=n.match(e)));o++);i(r)||y&&y.path===r.path||(window.history[t.replace?"replaceState":"pushState"]({},"",e),p(n,r))}function p(e,t){if(E)m={route:e,request:t};else{i(b)&&(b=new d,t.initial=!0),E={from:y,to:t},k.forEach(function(e){return e(E)}),g=new d(e.view),y=t;var n=function(){return new Promise(function(e){return g.init(t,e)})},r=function(){return new Promise(function(e){return g.animateIn(t,e)})},o=function(){return new Promise(function(e){return b.animateOut(t,e)})},u=function(){O.forEach(function(e){return e(E)}),b=g,g=null,E=null,m&&(p(m.route,m.request),m=null)};switch(E.type){case"out-in":return o().then(n).then(r).then(u);case"in-out":return n().then(r).then(o).then(u);default:return n().then(function(){return Promise.all([r(),o()])}).then(u)}}}var d=function(){for(var e=[],r=arguments.length;r--;)e[r]=arguments[r];t(e)||(e=[e]),this.views=e.map(function(e){return n(e)?new e:e})};d.prototype.init=function(e,t){this.execute("init",e,t)},d.prototype.resize=function(e,t){this.views.forEach(function(r){n(r.resize)&&r.resize(e,t)})},d.prototype.animateIn=function(e,t){this.resize(window.innerWidth,window.innerHeight),this.execute("animateIn",e,t)},d.prototype.animateOut=function(e,t){var n=this;this.execute("animateOut",e,function(){n.destroy(e,o),t()})},d.prototype.destroy=function(e,t){this.execute("destroy",e,t)},d.prototype.execute=function(e,t,r){function i(){++u===o&&r()}var o=0,u=0;this.views.forEach(function(t){n(t[e])&&o++}),o?this.views.forEach(function(r){n(r[e])&&r[e].call(r,t,i)}):r()};var l=/^(hash|initial|keys|path|params|query|regex|view)$/,w=function(e,n){var r=this;t(n)&&(n={view:n}),this.keys=[],this.view=n.view||n,this.regex=u(e,this.keys),Object.keys(n).forEach(function(e){l.test(e)||(r[e]=n[e])})};w.prototype.match=function(t){var n=this,r=t.split(/[?#]/)[0],i=r.match(this.regex);if(i){var o=[];i.forEach(function(e,t){var r=n.keys[t],u=decodeURIComponent(i[t+1]);r?o[r]=a(u):"undefined"!==u&&o.push(a(u))});var u=e({url:t,path:r,params:o},this);return delete u.regex,delete u.keys,delete u.view,u}};var v,y,m,g,b,E,x=[],k=[],O=[],j={before:function(e){n(e)&&(e=[e]),k.push.apply(k,e)},after:function(e){n(e)&&(e=[e]),O.push.apply(O,e)},route:function(e,t){i(t)?h(e):x.push(new w(e,t))},start:function(e){void 0===e&&(e={}),v=window.location.protocol+"//"+window.location.host+(e.base||"/"),t(e.routes)?e.routes.forEach(function(e){e.path&&j.route(e.path,e)}):r(e.routes)&&Object.keys(e.routes).forEach(function(t){j.route(t,e.routes[t])}),e.before&&j.before(e.before),e.after&&j.after(e.after),window.addEventListener("click",c),window.addEventListener("touchstart",c),window.addEventListener("popstate",f),window.addEventListener("resize",s);var n="/"+window.location.href.replace(v,"");h(n=x.some(function(e){return e.match(n)})?n:e.fallback||"/",{replace:!0})}};return j});