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