UNPKG

dop-router

Version:
3 lines (2 loc) 3.23 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("dop")):"function"==typeof define&&define.amd?define(["exports","dop"],t):t(e.doprouter_location={},e.dop)}(this,function(e,t){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n=encodeURIComponent;function o(e,t,r){window.history.pushState(t,r,e)}function a(){return window.location.href}function i(e){return e.pathname+e.search+e.hash}function p(e){var t=/((.*):\/\/([^/#?]+))?([^?#]*)([^#]*)(.*)?/.exec(decodeURIComponent(e)),r={url:e,origin:t[1],protocol:t[2],host:t[3],pathname:""===t[4]?"/":t[4],path:t[4].split("/").filter(function(e){return e.length>0}),search:t[5],query:{},hash:t[6]||""};return r.href=i(r),r.search.length>1&&r.search.substr(1).split("&").forEach(function(e){if(e.length>0){var t=e.indexOf("=");t>-1?r.query[e.substr(0,t)]=e.substr(t+1):r.query[e]=""}}),r}e.createLocation=function(e,u){var h=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"location",s=!1,f=void 0,l=p(e);function c(e,r){var n=p(e);n.href=i(n);var o=t.collect();void 0!==r&&o.mutations.push(r),s=!0,t.set(f,"href",n.href),t.set(f,"pathname",n.pathname),t.set(f,"search",n.search),t.set(f,"hash",n.hash),n.path.forEach(function(e,r){return t.set(f.path,r,e)}),t.set(f.path,"length",n.path.length);var a=void 0,u=n.query,h=f.query;for(a in u)t.set(h,a,u[a]);for(a in h)u.hasOwnProperty(a)||t.del(h,a);s=!1,o.emit()}return null!==u&&"object"==(void 0===u?"undefined":r(u))?(t.isRegistered(u)?t.set(u,h,l):(u[h]=l,u=t.register(u)),f=u[h]):f=t.register(l),f.toString=function(){return f.href},t.intercept(f,function(e,t){if(!s)if("href"===e.prop)t.href=e.oldValue,o(e.value),c(a());else if("pathname"===e.prop){var r=e.value.split("/").map(n).join("/");"/"!==e.value[0]&&(r="/"+r),r=r+f.search+f.hash,t.pathname=e.oldValue,o(r),c(a())}else if("search"===e.prop){var i="?"===e.value[0]?e.value.substr(1):e.value;i=i.split("&").map(function(e){var t=e.split("=");return e=n(t[0]||""),t.hasOwnProperty(1)&&(e+="="+n(t[1])),e}).join("&"),i=f.pathname+"?"+i+f.hash,t.search=e.oldValue,o(i),c(a())}else if("hash"===e.prop){var p="#"===e.value[0]?e.value:"#"+e.value;p=f.pathname+f.search+p,t.hash=e.oldValue,o(p),c(a())}else if("path"===e.prop)o("/"+e.value.map(n).join("/")+f.search+f.hash),c(a(),e);else if("query"===e.prop){var u=void 0,h=e.value,l=[];for(u in h)l.push(n(u)+"="+n(h[u]));o(f.pathname+"?"+l.join("&")+f.hash),c(a())}else t[e.prop]=e.oldValue;return s}),t.intercept(f.path,function(e,t){if(!s){var r=f.path;t[e.prop]=n(r[e.prop]);var i="/"+r.filter(function(e){return void 0!==e}).join("/")+f.search+f.hash;i!==f.pathname&&(o(i),c(a(),e))}return s}),t.intercept(f.query,function(e,t){if(!s){var r=f.query,i=[],p=e.prop;if(e.hasOwnProperty("value")){var u=n(e.prop),h=n(e.value);delete t[e.prop],t[u]=h}for(p in r)i.push(p+"="+r[p]);o(f.pathname+"?"+i.join("&")+f.hash),c(a(),e)}return s}),"undefined"!=typeof window&&window.addEventListener("popstate",function(){c(a())}),f},Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=index.umd.js.map