2 lines
7.6 KiB
JavaScript
2 lines
7.6 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Navigo",[],e):"object"==typeof exports?exports.Navigo=e():t.Navigo=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){"use strict";function n(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}function o(){return!("undefined"==typeof window||!window.history||!window.history.pushState)}function r(t,e,n){this.root=null,this._routes=[],this._useHash=e,this._hash="undefined"==typeof n?"#":n,this._paused=!1,this._destroyed=!1,this._lastRouteResolved=null,this._notFoundHandler=null,this._defaultHandler=null,this._usePushState=!e&&o(),this._onLocationChange=this._onLocationChange.bind(this),t?this.root=e?t.replace(/\/$/,"/"+this._hash):t.replace(/\/$/,""):e&&(this.root=this._cLoc().split(this._hash)[0].replace(/\/$/,"/"+this._hash)),this._listen(),this.updatePageLinks()}function i(t){return t instanceof RegExp?t:t.replace(/\/+$/,"").replace(/^\/+/,"/")}function s(t,e){return 0===e.length?null:t?t.slice(1,t.length).reduce(function(t,n,o){return null===t&&(t={}),t[e[o]]=n,t},null):null}function u(t){var e,n=[];return e=t instanceof RegExp?t:new RegExp(i(t).replace(r.PARAMETER_REGEXP,function(t,e,o){return n.push(o),r.REPLACE_VARIABLE_REGEXP}).replace(r.WILDCARD_REGEXP,r.REPLACE_WILDCARD)+r.FOLLOWED_BY_SLASH_REGEXP,r.MATCH_REGEXP_FLAGS),{regexp:e,paramNames:n}}function a(t){return t.replace(/\/$/,"").split("/").length}function h(t,e){return a(e)-a(t)}function l(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return e.map(function(e){var n=u(e.route),o=n.regexp,r=n.paramNames,i=t.match(o),a=s(i,r);return!!i&&{match:i,route:e,params:a}}).filter(function(t){return t})}function d(t,e){return l(t,e)[0]||!1}function c(t,e){var n=l(t,e.filter(function(t){var e=i(t.route);return""!==e&&"*"!==e})),o=i(t);return n.length>0?n.map(function(e){return i(t.substr(0,e.match.index))}).reduce(function(t,e){return e.length<t.length?e:t},o):o}function f(){return!!("undefined"!=typeof window&&"onhashchange"in window)}function _(t){return t.split(/\?(.*)?$/).slice(1).join("")}function p(t,e,n){var r=t.split(/\?(.*)?$/)[0];return"undefined"==typeof n&&(n="#"),o()&&!e&&(r=r.split(n)[0]),r}function v(t,e,n){return e&&e.hooks&&"object"===R(e.hooks)?void(e.hooks.before?e.hooks.before(function(){var o=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];o&&(t(),e.hooks.after&&e.hooks.after(n))},n):e.hooks.after&&(t(),e.hooks.after&&e.hooks.after(n))):void t()}function g(t,e,n){if(o()&&!e)return!1;if(!t.match(n))return!1;var r=t.split(n);return r.length<2||""===r[1]}Object.defineProperty(e,"__esModule",{value:!0});var R="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};r.prototype={helpers:{match:d,root:c,clean:i},navigate:function(t,e){var n;return t=t||"",this._usePushState?(n=(e?"":this._getRoot()+"/")+t.replace(/^\/+/,"/"),n=n.replace(/([^:])(\/{2,})/g,"$1/"),history[this._paused?"replaceState":"pushState"]({},"",n),this.resolve()):"undefined"!=typeof window&&(t=t.replace(new RegExp("^"+this._hash),""),window.location.href=window.location.href.replace(/#$/,"").replace(new RegExp(this._hash+".*$"),"")+this._hash+t),this},on:function(){for(var t=this,e=arguments.length,n=Array(e),o=0;o<e;o++)n[o]=arguments[o];if("function"==typeof n[0])this._defaultHandler={handler:n[0],hooks:n[1]};else if(n.length>=2)if("/"===n[0]){var r=n[1];"object"===R(n[1])&&(r=n[1].uses),this._defaultHandler={handler:r,hooks:n[2]}}else this._add(n[0],n[1],n[2]);else if("object"===R(n[0])){var i=Object.keys(n[0]).sort(h);i.forEach(function(e){t.on(e,n[0][e])})}return this},off:function(t){return null!==this._defaultHandler&&t===this._defaultHandler.handler?this._defaultHandler=null:null!==this._notFoundHandler&&t===this._notFoundHandler.handler&&(this._notFoundHandler=null),this._routes=this._routes.reduce(function(e,n){return n.handler!==t&&e.push(n),e},[]),this},notFound:function(t,e){return this._notFoundHandler={handler:t,hooks:e},this},resolve:function(t){var e,o,r=this,i=(t||this._cLoc()).replace(this._getRoot(),"");this._useHash&&(i=i.replace(new RegExp("^/"+this._hash),"/"));var s=_(t||this._cLoc()),u=p(i,this._useHash,this._hash);return!(this._paused||this._lastRouteResolved&&u===this._lastRouteResolved.url&&s===this._lastRouteResolved.query)&&((o=d(u,this._routes))?(this._lastRouteResolved={url:u,query:s},e=o.route.handler,v(function(){o.route.route instanceof RegExp?e.apply(void 0,n(o.match.slice(1,o.match.length))):e(o.params,s)},o.route,o.params),o):this._defaultHandler&&(""===u||"/"===u||u===this._hash||g(u,this._useHash,this._hash))?(v(function(){r._lastRouteResolved={url:u,query:s},r._defaultHandler.handler(s)},this._defaultHandler),!0):(this._notFoundHandler&&v(function(){r._lastRouteResolved={url:u,query:s},r._notFoundHandler.handler(s)},this._notFoundHandler),!1))},destroy:function(){this._routes=[],this._destroyed=!0,clearTimeout(this._listenningInterval),"undefined"!=typeof window&&(window.removeEventListener("popstate",this._onLocationChange),window.removeEventListener("hashchange",this._onLocationChange))},updatePageLinks:function(){var t=this;"undefined"!=typeof document&&this._findLinks().forEach(function(e){e.hasListenerAttached||(e.addEventListener("click",function(n){var o=t.getLinkPath(e);t._destroyed||(n.preventDefault(),t.navigate(i(o)))}),e.hasListenerAttached=!0)})},generate:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=this._routes.reduce(function(n,o){var r;if(o.name===t){n=o.route;for(r in e)n=n.replace(":"+r,e[r])}return n},"");return this._useHash?this._hash+n:n},link:function(t){return this._getRoot()+t},pause:function(){var t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];this._paused=t},resume:function(){this.pause(!1)},disableIfAPINotAvailable:function(){o()||this.destroy()},lastRouteResolved:function(){return this._lastRouteResolved},getLinkPath:function(t){return t.pathname||t.getAttribute("href")},_add:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;return"string"==typeof t&&(t=encodeURI(t)),"object"===("undefined"==typeof e?"undefined":R(e))?this._routes.push({route:t,handler:e.uses,name:e.as,hooks:n||e.hooks}):this._routes.push({route:t,handler:e,hooks:n}),this._add},_getRoot:function(){return null!==this.root?this.root:(this.root=c(this._cLoc().split("?")[0],this._routes),this.root)},_listen:function(){var t=this;if(this._usePushState)window.addEventListener("popstate",this._onLocationChange);else if(f())window.addEventListener("hashchange",this._onLocationChange);else{var e=this._cLoc(),n=void 0,o=void 0;o=function(){n=t._cLoc(),e!==n&&(e=n,t.resolve()),t._listenningInterval=setTimeout(o,200)},o()}},_cLoc:function(){return"undefined"!=typeof window?"undefined"!=typeof window.__NAVIGO_WINDOW_LOCATION_MOCK__?window.__NAVIGO_WINDOW_LOCATION_MOCK__:i(window.location.href):""},_findLinks:function(){return[].slice.call(document.querySelectorAll("[data-navigo]"))},_onLocationChange:function(){this.resolve()}},r.PARAMETER_REGEXP=/([:*])(\w+)/g,r.WILDCARD_REGEXP=/\*/g,r.REPLACE_VARIABLE_REGEXP="([^/]+)",r.REPLACE_WILDCARD="(?:.*)",r.FOLLOWED_BY_SLASH_REGEXP="(?:/$|$)",r.MATCH_REGEXP_FLAGS="",e["default"]=r,t.exports=e["default"]}])});
|
|
//# sourceMappingURL=navigo.min.js.map
|