-
Notifications
You must be signed in to change notification settings - Fork 8
/
es-router.min.js
1 lines (1 loc) · 5.97 KB
/
es-router.min.js
1
"use strict";function _toConsumableArray(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function isNotDefined(t){return"undefined"==typeof t||null===t}function clone(t){return JSON.parse(JSON.stringify(t))}var _typeof="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},_createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),EsRouter=function(){function t(e){var r=this,n=e.useHash,a=e.routes,i=e.notStrictRouting,s=e.home,o=e.base,u=e.routeOnLoad,h=void 0===u?!0:u;if(_classCallCheck(this,t),this.events={startRouteChange:[],finishRouteChange:[],paramChange:[]},this.useHash=n,this.routes=a,this.base=o,this.notStrictRouting=i,this.queryParams=this.getParamsFromUrl(),o&&"/"===o[o.length-1]&&(this.base=this.base.substring(0,this.base.length-1)),!o&&!n){var c=document.getElementsByTagName("base")[0]&&document.getElementsByTagName("base")[0].href||"";this.base=c.split(window.location.origin)[1]}this.allRoutes=Object.assign(a,{}).reduce(function(t,e){if(e.name===s&&(r.home=e),e.variablePath)throw new Error("route objects cannot be initialized with a key of variablePath");var n=e.route.split("/").filter(function(t){return t}),a=n.length,i=n.reduce(function(t,e,r){var n={id:e.split(":")[1],index:r};return e.includes(":")?[].concat(_toConsumableArray(t),[n]):[].concat(_toConsumableArray(t))},[]);return e.variablePath=i,t[a]=t[a]?[].concat(_toConsumableArray(t[a]),[e]):[e],t},{}),n?(-1===window.location.href.indexOf("#")&&(window.location.hash="/"),window.addEventListener("hashchange",function(t){return r.wasChangedByUser?(r.wasChangedByUser=!1,void 0):(r.eventChangeListener.call(r,t),void 0)})):window.onpopstate=this.eventChangeListener.bind(this),h&&this.path(this.getPathFromUrl())}return _createClass(t,[{key:"getState",value:function(){return this.currentPathObject}},{key:"getParamsFromUrl",value:function(){var t=this.useHash?window.location.hash.split("?")[1]:window.location.search.split("?")[1];return t&&t.split("&").reduce(function(t,e){var r=e.split("=");return t[decodeURIComponent(r[0])]=decodeURIComponent(r[1])||"",t},{})||{}}},{key:"getPathFromUrl",value:function(){return this.useHash?window.location.hash.split("?")[0].substring(1):window.location.pathname.split(this.base)[1]||"/"}},{key:"eventChangeListener",value:function(){var t=this,e=this.getParamsFromUrl(),r=this.getPathFromUrl(),n=this.createParamString(e).join(""),a=this.createParamString(this.queryParams).join("");if(n!==a&&(this.queryParams=this.getParamsFromUrl(),this.events.paramChange.forEach(function(e){e(t.queryParams)})),r!==this.currentPath){this.currentPath=r;var i=this.getPreDefinedRoute(this.currentPath),s=clone(this.currentPathObject);this.currentPathObject=i,this.startRouteChange(s,i),this.finishRouteChange(s,i)}}},{key:"subscribe",value:function(t,e){var r=this;if(!t||!e)return{};if(!this.events.hasOwnProperty.call(this.events,t)||"string"!=typeof t||"function"!=typeof e)return{};var n=this.events[t].push(e)-1;return{remove:function(){delete r.events[t][n]}}}},{key:"unsubscribe",value:function(t){var e=this,r=function(e){return e!==t};Object.keys(this.events).forEach(function(t){e.events[t]=e.events[t].filter(r)})}},{key:"search",value:function(t,e){var r=this;if(isNotDefined(t))return this.queryParams;"object"===("undefined"==typeof t?"undefined":_typeof(t))?this.queryParams=Object.assign(this.queryParams,t):isNotDefined(e)?this.queryParams[t]&&delete this.queryParams[t]:this.queryParams[t]=e,Object.keys(this.queryParams).forEach(function(t){isNotDefined(r.queryParams[t])&&"number"!=typeof r.queryParams[t]&&delete r.queryParams[t]});var n=this.getParamsFromUrl(),a=this.createParamString(n).join(""),i=this.createParamString(this.queryParams).join("");return a===i?void 0:(this.path(this.currentPath,!0),this.events.paramChange.forEach(function(t){t(r.queryParams)}),this.queryParams)}},{key:"getPreDefinedRoute",value:function(t){var e=t.split("/").filter(function(t){return t}),r=clone(this.allRoutes);return r[e.length]&&r[e.length].reduce(function(t,r){if(t)return t;var n=[].concat(_toConsumableArray(e)),a=r.route.split("/").filter(function(t){return t}),i=void 0;if(r.variablePath&&r.variablePath.length&&(i=r.variablePath.reduce(function(t,e,r){var i=n.splice(e.index-r,1)[0];return a.splice(e.index-r,1),t[e.id]=i,t},{})),n.join("")===a.join("")){var s=Object.assign({},r);return s.variablePath=i,s}return!1},0)}},{key:"createParamString",value:function(t){return Object.keys(t).reduce(function(e,r){var n=r.toString(),a=t[r].toString();return isNotDefined(a)||Array.isArray(a)&&!a.length?e:[].concat(_toConsumableArray(e),[encodeURIComponent(n)+"="+encodeURIComponent(a)])},[])}},{key:"path",value:function(t,e){if(t){var r=t,n=this.getPreDefinedRoute(r);n||this.notStrictRouting||(r=this.home.route,n=this.home),e||this.startRouteChange(this.currentPathObject,n);var a=this.createParamString(this.queryParams),i=a.length?"?"+a.join("&"):"",s=""+(this.base||"")+r+i;this.useHash?(this.wasChangedByUser=!0,window.location.hash=s):window.history.pushState(null,null,s);var o=this.currentPathObject&&Object.keys(this.currentPathObject).length&&clone(this.currentPathObject);this.currentPathObject=n,this.currentPath=t,e||this.finishRouteChange(o,this.currentPathObject)}}},{key:"startRouteChange",value:function(t,e){this.events.startRouteChange.forEach(function(r){r(t,e)})}},{key:"finishRouteChange",value:function(t,e){this.events.finishRouteChange.forEach(function(r){r(t,e)}),this.previousQueryParam=clone(this.queryParams)}}]),t}();"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=EsRouter:window.EsRouter=EsRouter;