@hickory/browser
Version:
Hickory's browser history
2 lines (1 loc) • 4.86 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n=n||self).HickoryBrowser={})}(this,function(n){"use strict";function k(n,t){return n?0===n.indexOf(t)?n:t+n:""}function b(n){return n||""}function x(n){return n||""}function E(){}function A(){try{return window.history.state||{}}catch(n){return{}}}function O(){}n.createBase=function(e,n){if("string"!=typeof e||"/"!==e.charAt(0)||"/"===e.charAt(e.length-1))throw new Error('The base segment "'+e+'" is not valid. The "base" option must begin with a forward slash and end with a non-forward slash character.');var t=n||{},r=t.emptyRoot,i=void 0!==r&&r,o=t.strict,a=void 0!==o&&o;return{add:function(n){if(i){if("/"===n)return e;if(n.startsWith("/?")||n.startsWith("/#"))return""+e+n.substr(1)}else if("?"===n.charAt(0)||"#"===n.charAt(0))return n;return""+e+n},remove:function(n){if(""===n)return"";var t;if(t=n,!new RegExp("^"+e+"(\\/|\\?|#|$)","i").test(t)){if(a)throw new Error('Expected a string that begins with "'+e+'", but received "'+n+'".');return n}if(n!==e)return n.substr(e.length);if(a&&!i)throw new Error('Received string "'+e+'", which is the same as the base, but "emptyRoot" is not true.');return"/"}}},n.browser=function(n,t){if(void 0===t&&(t={}),!window||!window.location)throw new Error("Cannot use @hickory/browser without a DOM");var e,r,c=function(n){void 0===n&&(n={});var t=n.query,e=void 0===t?{}:t,r=e.parse,d=void 0===r?b:r,i=e.stringify,o=void 0===i?x:i,l=n.base;return{location:function(n,t){var e=n.url,r=n.state;if(""===e||"#"===e.charAt(0)){t||(t={pathname:"/",hash:"",query:d()});var i={pathname:t.pathname,hash:"#"===e.charAt(0)?e.substring(1):t.hash,query:t.query};return r&&(i.state=r),i}var o,a=e.indexOf("#");-1!==a?(o=e.substring(a+1),e=e.substring(0,a)):o="";var c,u=e.indexOf("?");-1!==u&&(c=e.substring(u+1),e=e.substring(0,u));var s=d(c),f=l?l.remove(e):e;""===f&&(f="/");var h={hash:o,query:s,pathname:f};return r&&(h.state=r),h},keyed:function(n,t){return n.key=t,n},stringify:function(n){if("string"!=typeof n)return(void 0!==n.pathname?l?l.add(n.pathname):n.pathname:"")+k(o(n.query),"?")+k(n.hash,"#");var t=n.charAt(0);return"#"===t||"?"===t?n:l?l.add(n):n}}}(t),u=(e=0,{major:function(n){return n&&(e=n[0]+1),[e++,0]},minor:function(n){return[n[0],n[1]+1]}}),i={confirmNavigation:function(n,t,e){r?r(n,t,e||E):t()},confirm:function(n){r=n||null}},o=i.confirm,a=i.confirmNavigation;function s(n){var t=window.location,e=t.pathname+t.search+t.hash,r=n||A(),i=r.key,o=r.state;i||(i=u.major(),window.history.replaceState({key:i,state:o},"",e));var a=c.location({url:e,state:o});return c.keyed(a,i)}function f(n){return c.stringify(n)}var h=void 0!==A().key?"pop":"push",d=function(n){var o,t=n.responseHandler,i=n.utils,e=n.keygen,a=n.current,r=n.push,c=n.replace;function u(n,t,e,r){var i={location:n,action:t,finish:function(){o===i&&(e(),o=void 0)},cancel:function(n){o===i&&(r(n),i.cancelled=!0,o=void 0)},cancelled:!1};return i}function s(n){var t=i.keyed(n,e.minor(a().key));return u(t,"replace",c.finish(t),c.cancel)}function f(n){var t=i.keyed(n,e.major(a().key));return u(t,"push",r.finish(t),r.cancel)}return{prepare:function(n,t){var e=a(),r=i.location(n,e);switch(t){case"anchor":return i.stringify(r)===i.stringify(e)?s(r):f(r);case"push":return f(r);case"replace":return s(r);default:throw new Error("Invalid navigation type: "+t)}},emitNavigation:function(n){t(o=n)},createNavigation:u,cancelPending:function(n){o&&(o.cancel(n),o=void 0)}}}({responseHandler:n,utils:c,keygen:u,current:function(){return m.location},push:{finish:function(r){return function(){var t=f(r),n=r.key,e=r.state;try{window.history.pushState({key:n,state:e},"",t)}catch(n){window.location.assign(t)}m.location=r,h="push"}},cancel:O},replace:{finish:function(r){return function(){var t=f(r),n=r.key,e=r.state;try{window.history.replaceState({key:n,state:e},"",t)}catch(n){window.location.replace(t)}m.location=r,h="replace"}},cancel:O}}),l=d.emitNavigation,p=d.cancelPending,v=d.createNavigation,y=d.prepare,w=!1;function g(n){if(w)w=!1;else if(void 0!==n.state||-1!==navigator.userAgent.indexOf("CriOS")){p("pop");var t=s(n.state),e=m.location.key[0]-t.key[0],r=function(){w=!0,window.history.go(e)};a({to:t,from:m.location,action:"pop"},function(){l(v(t,"pop",function(){m.location=t,h="pop"},function(n){"pop"!==n&&r()}))},r)}}window.addEventListener("popstate",g,!1);var m={location:s(),current:function(){l(v(m.location,h,O,O))},url:f,navigate:function(n,t){void 0===t&&(t="anchor");var e=y(n,t);p(e.action),a({to:e.location,from:m.location,action:e.action},function(){l(e)})},go:function(n){window.history.go(n)},confirm:o,cancel:function(){p()},destroy:function(){window.removeEventListener("popstate",g),l=O}};return m},Object.defineProperty(n,"__esModule",{value:!0})});