-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpush-state-tree.min.js
6 lines (5 loc) · 11.3 KB
/
push-state-tree.min.js
1
2
3
4
5
6
//! push-state-tree - v0.19.0 - 2025-01-16
//* https://github.com/gartz/pushStateTree/
//* Copyright (c) 2025 Gabriel Reitz Giannattasio <[email protected]>; Licensed
var PushStateTree = {options: {VERSION: '0.19.0'}};
var PushStateTree={options:{VERSION:"0.19.0"}};(l=>{var t,i,u=l.document,e=l.window,p=l.location,d=0<=e.navigator.userAgent.indexOf("Trident"),f=(l.location.origin||(l.location.origin=l.location.protocol+"//"+l.location.host),Function.prototype.bind||(Function.prototype.bind=function(e){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");function t(){var t=e;return this instanceof r&&e&&(t=this),i.apply(t,n.concat(Array.prototype.slice.call(arguments)))}var n=Array.prototype.slice.call(arguments,1),i=this,r=function(){};return r.prototype=this.prototype,t.prototype=new r,t}),l.HashChangeEvent),n=l.Event;if(Element.prototype.addEventListener){r.prototype=n.prototype,l.CustomEvent&&!d||(l.CustomEvent=r),(!f||l.opera&&l.opera.version()<15||d)&&(f=l.CustomEvent),d&&(n=r);try{new f("hashchange")}catch(t){f=r}try{new n("popstate")}catch(t){n=r}}function r(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var n=u.createEvent("CustomEvent");return n.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),n}!Element.prototype.addEventListener&&Object.defineProperty&&(t=u.createEventObject().constructor.prototype,Object.defineProperty(t,"target",{get:function(){return this.srcElement}}),t=function(t,e){this.__bindedFunctions||(this.__bindedFunctions=[]);var n=e;if(!("on"+t in this)||"hashchange"===t){this.__elemetIEid=this.__elemetIEid||"__ie__"+Math.random();var i=t+this.__elemetIEid,r=this,e=function(t){t.propertyName===i&&n.call(r,u.documentElement[i])};if(this.__bindedFunctions.push({original:n,binded:e}),u.documentElement.attachEvent("onpropertychange",e),"hashchange"!==t)return}e=n.bind(this);this.__bindedFunctions.push({original:n,binded:e}),this.attachEvent("on"+t,e)},HTMLDocument.prototype.addEventListener=t,Element.prototype.addEventListener=t,e.addEventListener=t,HTMLDocument.prototype.removeEventListener=t=function(t,e){this.__bindedFunctions||(this.__bindedFunctions=[]);var n,i=e;if(!("on"+t in this)||"hashchange"===t){for(var r=0;r<this.__bindedFunctions.length;r++)this.__bindedFunctions[r].original===i&&(n=this.__bindedFunctions[r].binded,this.__bindedFunctions=this.__bindedFunctions.splice(r,1),r=this.__bindedFunctions.length);if(n&&u.documentElement.detachEvent("onpropertychange",n),"hashchange"!==t)return}for(var o=0;o<this.__bindedFunctions.length;o++)this.__bindedFunctions[o].original===i&&(n=this.__bindedFunctions[o].binded,this.__bindedFunctions=this.__bindedFunctions.splice(o,1),o=this.__bindedFunctions.length);n&&this.detachEvent("on"+t,n)},Element.prototype.removeEventListener=t,e.removeEventListener=t,CustomEvent=n=function(t,e){var n=u.createEventObject();return e=e||{},n.type=t,n.detail=e.detail,"on"+t in l&&"hashchange"!==t||(n.name=t,n.customEvent=!0),n},f=CustomEvent,HTMLDocument.prototype.dispatchEvent=t=function(t){var e;t.customEvent?this.__elemetIEid&&(e=t.name+this.__elemetIEid,u.documentElement[e]=t):this.fireEvent(t.type,t)},Element.prototype.dispatchEvent=t,e.dispatchEvent=t),"forEach"in Array.prototype||(i=Array.prototype.slice,Array.prototype.slice=function(){for(var t=[],e=0,n=this.length;e<n;e++)e in this&&t.push(this[e]);return i.apply(t,arguments)}),"forEach"in Array.prototype||(Array.prototype.forEach=function(t,e){for(var n=0;n<this.length;n++)n in this&&t.call(e,this[n],n)}),"function"!=typeof String.prototype.trim&&(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),Array.prototype.filter||(Array.prototype.filter=function(t){if(null==this)throw new TypeError;var e=Object(this),n=e.length>>>0;if("function"!=typeof t)throw new TypeError;for(var i,r=[],o=2<=arguments.length?arguments[1]:void 0,a=0;a<n;a++)a in e&&(i=e[a],t.call(o,i,a,e))&&r.push(i);return r});var o=l.PushStateTree&&l.PushStateTree.options||{},y="usePushState",v="hasPushState",b="ignoreHash",a=l.DEBUG||o.DEBUG,g=o.VERSION||"development",E="hashchange",m="popstate",_="leave",P="update",w="enter",S="change",L="match",O="oldMatch";function s(t){return!isNaN(parseFloat(t))&&t%1==0&&isFinite(t)}function A(t,e,n){Object.defineProperty(t,e,{get:function(){return n},set:function(){}})}function c(t){return/^[a-z0-9]+:\/\//i.test(t)}function h(t){return/^[^#/]/.test(t)}function F(t){for(var e=t.split("/"),n=1;n<e.length;)".."===e[n]&&0<n&&".."!==e[n-1]&&(e.splice(n-1,2),n-=2),n++;return e.join("/").replace(/\/\.\/|\.\/|\.\.\//g,"/").replace(/^\/$/,"")}var x="undefined"!=typeof HTMLElement?HTMLElement:Element;function j(n){if((n=n||{})[y]=!1!==n[y],n[b]=!0===n[b],!(this instanceof x))return j.apply(u.createElement("pushstatetree-route"),arguments);var t,e,i,r,o=this;for(t in this.VERSION=g,n)n.hasOwnProperty(t)&&(o[t]=n[t]);for(r in j.prototype[v]?(e=n[y],Object.defineProperty(o,y,{get:function(){return e},set:function(t){e=!1!==t}})):A(o,y,!1),Object.defineProperty(o,"beautifyLocation",{get:function(){return j.prototype.beautifyLocation&&e},set:function(t){j.prototype.beautifyLocation=!0===t}}),o.beautifyLocation=n.beautifyLocation&&o.usePushState,Object.defineProperty(o,"basePath",{get:function(){return i},set:function(t){"/"!==(i=t||"")[0]&&(i="/"+i)}}),o.basePath=n.basePath,j.prototype)j.prototype.hasOwnProperty(r)&&(e=>{"function"==typeof j.prototype[e]?o[e]=function(){return j.prototype[e].apply(this,arguments)}:void 0===o[e]&&Object.defineProperty(o,e,{get:function(){return j.prototype[e]},set:function(t){j.prototype[e]=t}})})(r);A(o,"length",l.history.length),A(o,"state",l.history.state);function a(){l.dispatchEvent(new f(E))}var s={url:"",uri:""},c=(Object.defineProperty(o,"uri",{get:function(){var t,e;return s.url===l.location.href?s.uri:(n[b]||!l.location.hash.length&&"#"!==l.location.href[p.href.length-1]?(t=l.location.pathname+l.location.search,this.isPathValid&&(t=t.slice(this.basePath.length))):(t=l.location.hash.match(/^(#*)?(.*\/?)/)[2],e=o[y],o.beautifyLocation&&o.isPathValid&&e&&o.replaceState(o.state,o.title,"/"===t[0]?t:"/"+t)),t=t.replace(/^[\/]+/,""),o.getAttribute("uri")!==t&&o.setAttribute("uri",t),s.url=l.location.href,s.uri=t)},configurable:!0}),Object.defineProperty(o,"isPathValid",{get:function(){var t=l.location.pathname+l.location.search;return!this.basePath||0===t.indexOf(this.basePath)}}),o.eventStack={leave:[],change:[],enter:[],match:[]},l.addEventListener(m,function(){var t=o.uri,e=o.state;o.rulesDispatcher(),T=t,R&&this.dispatch()}.bind(o)),!1),h=function(){var t,e;c||T!==o.uri&&(t=o.uri,e=o.state,o.rulesDispatcher(),T=t,R)&&this.dispatch()}.bind(o);o.avoidHashchangeHandler=function(){l.removeEventListener(E,h),c=!0},l.addEventListener(E,h);return u.addEventListener("DOMContentLoaded",a),l.addEventListener("readystatechange",a),l.addEventListener("load",function(){a(),d&&l.setInterval(function(){o.uri!==T?a():c&&(c=!1,T=o.uri,l.addEventListener(E,h))}.bind(o),50)}.bind(o)),this}var I,T=null,H=[],R=!1,C=!1;function G(t){var n=t;this[t]=function(){var t,e=Array.prototype.slice.call(arguments);return this[y]&&this[b]?l.history[n].apply(l.history,e):("pushState"!==n&&"replaceState"!==n||c(e[2])||(h(e[2])?(t=(t=this.uri.match(/^([^?#]*)\//))?t[1]+"/":"",e[2]=t+e[2]):e[2]=e[2].match(/^([#/]*)?(.*)/)[2],this[y]?e[2]=this.basePath+e[2]:e[2]=p.pathname+"#"+F(e[2])),l.history[n].apply(l.history,e)),this}}for(I in j.prototype={beautifyLocation:!0,createRule:function(t){var e,n=u.createElement("pushstatetree-rule"),i=new RegExp("");for(e in Object.defineProperty(n,"rule",{get:function(){return i},set:function(t){if(t instanceof RegExp)i=t;else{if(t===i.toString())return;t=(t+"").match(/^\/(.+)\/([gmi]*)|(.*)/);i=new RegExp(t[1]||t[3],t[2])}n.setAttribute("rule",i.toString())}}),Object.defineProperty(n,"parentGroup",{get:function(){var t=n.getAttribute("parent-group");return t&&s(t)?+t:null},set:function(t){s(t)?n.setAttribute("parent-group",t):n.removeAttribute("parent-group")}}),t)t.hasOwnProperty(e)&&(n[e]=t[e]);var r=[],o=(Object.defineProperty(n,L,{get:function(){return r},set:function(t){r=t instanceof Array?t:[]}}),[]);return Object.defineProperty(n,O,{get:function(){return o},set:function(t){o=t instanceof Array?t:[]}}),n[L]=[],n[O]=[],["assign","navigate","replace","dispatch","pushState","replaceState"].forEach(function(t){n[t]=function(){this.parentElement[t].apply(this.parentElement,arguments)}}),n},add:function(t){return this.appendChild(this.createRule(t)),this},remove:function(t){var e=t;if((e="string"==typeof t?this.querySelector(t):e)&&e.parentElement)return e.parentElement.removeChild(e),e},dispatch:function(){return C?R=!0:(R=!1,l.dispatchEvent(new n(m))),this},assign:function(t){return this.pushState(null,null,t).dispatch()},replace:function(t){return this.replaceState(null,null,t).dispatch()},navigate:function(){this.assign.apply(this,arguments)},rulesDispatcher:function(){var u=!0===this.debug||a;if(this.isPathValid&&(H.push(function(t,e){return Array.prototype.slice.call(this.children||this.childNodes).forEach(p.bind(this,t,e)),t}.bind(this,this.uri)),!(1<H.length))){for(var t=T;0<H.length;)t=H[0].call(null,t),H.shift();C=!0;var i=this.eventStack;[S,w,L].forEach(function(t){for(;0<i[t].length;){for(var e=i[t][0].events,n=i[t][0].element;0<e.length;)n.dispatchEvent(e[0]),e.shift();i[t].shift()}}),C=!1}function p(t,e,n){if(n.rule){var i=t,r=e,o=("number"==typeof n.parentGroup&&(i="",(s=n.parentElement)[L].length>n.parentGroup&&(i=s[L][n.parentGroup]||""),r="",s[O].length>n.parentGroup)&&(r=s[O][n.parentGroup]||""),n[L]=i[L](n.rule),n[O]="string"==typeof r?r[L](n.rule):[],n[L]),a=n[O],s=Array.prototype.slice.call(n.children);if(0===o.length)return 0===a.length||n.routerURI!==e?void 0:(n.uri=null,n.removeAttribute("uri"),s.forEach(p.bind(this,t,e)),n.dispatchEvent(new h(P,{detail:{type:_}})),void n.dispatchEvent(new h(_)));this.eventStack[L].push({element:n,events:[new h(L)]});var c=n.routerURI!==e;n.routerURI=this.uri,n.uri=o[0],n.setAttribute("uri",o[0]),0===a.length||c?this.eventStack[w].push({element:n,events:[new h(P,{detail:{type:w}}),new h(w)]}):o[0]!==a[0]&&this.eventStack[S].push({element:n,events:[new h(P,{detail:{type:S}}),new h(S)]}),s.forEach(p.bind(this,t,e))}function h(t,e){return(e=e||{}).detail=e.detail||{},e.detail[L]=o||[],e.detail[O]=a||[],e.cancelable=!0,u&&"object"==typeof console&&(console.log({name:t,ruleElement:n,params:e,useURI:i,useOldURI:r}),console.trace)&&console.trace(),new l.CustomEvent(t,e)}}}},l.history)"function"==typeof l.history[I]&&G.call(j.prototype,I);j.prototype[v]=l.history&&!!l.history.pushState,j.prototype[v]||(j.prototype[y]=!1);var D=null;j.prototype.pushState||(j.prototype.pushState=function(t,e,n){var i=u.title||"";return n=n||"",null!==D&&(u.title=D),this.avoidHashchangeHandler(),c(n)&&(l.location.href=n),h(n=o[b]||"#"!==n[0]?n:n.slice(1))&&(n=F(n=l.location.hash.slice(1,l.location.hash.lastIndexOf("/")+1)+n)),l.location.hash=n,u.title=i,D=e,this}),j.prototype.replaceState||(j.prototype.replaceState=function(t,e,n){var i,r=u.title||"";if(n=n||"",null!==D&&(u.title=D),this.avoidHashchangeHandler(),c(n))throw new Error("Invalid url replace.");return h(n=o[b]||"#"!==n[0]?n:n.slice(1))&&(i=l.location.hash.lastIndexOf("/")+1,n=F(n=l.location.hash.slice(1,i)+n)),l.location.replace(n="#"+n),u.title=r,D=e,this}),l.PushStateTree=j,"undefined"!=typeof module&&(module.exports=j)})("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:void 0);