forked from gartz/pushStateTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-state-tree.min.js
More file actions
6 lines (5 loc) · 11.5 KB
/
push-state-tree.min.js
File metadata and controls
6 lines (5 loc) · 11.5 KB
1
2
3
4
5
6
//! push-state-tree - v0.14.0 - 2016-02-26
//* https://github.com/gartz/pushStateTree/
//* Copyright (c) 2016 Gabriel Reitz Giannattasio <gabriel@gartz.com.br>; Licensed
var PushStateTree = {options: {VERSION: '0.14.0'}};
var PushStateTree={options:{VERSION:"0.14.0"}};!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(a){for(var b=a.split("/"),c=1;c<b.length;)".."===b[c]&&c>0&&".."!==b[c-1]&&(b.splice(c-1,2),c-=2),c++;return b.join("/").replace(/\/\.\/|\.\/|\.\.\//g,"/").replace(/^\/$/,"")}function g(b){function d(a){if("function"==typeof g.prototype[a])e[a]=function(){return g.prototype[a].apply(this,arguments)};else{if("undefined"!=typeof e[a])return;Object.defineProperty(e,a,{get:function(){return g.prototype[a]},set:function(b){g.prototype[a]=b}})}}if(b=b||{},b[o]=b[o]!==!1,!(this instanceof B))return g.apply(i.createElement("pushstatetree-route"),arguments);var e=this;this.VERSION=A;for(var f in b)b.hasOwnProperty(f)&&(e[f]=b[f]);if(g.prototype[p]){var h=b[o];Object.defineProperty(e,o,{get:function(){return h},set:function(a){h=a!==!1}})}else c(e,o,!1);Object.defineProperty(e,"beautifyLocation",{get:function(){return g.prototype.beautifyLocation&&h},set:function(a){g.prototype.beautifyLocation=a===!0}}),e.beautifyLocation=b.beautifyLocation&&e.usePushState;var j;Object.defineProperty(e,"basePath",{get:function(){return j},set:function(a){j=a||"","/"!==j[0]&&(j="/"+j)}}),e.basePath=b.basePath;for(var n in g.prototype)g.prototype.hasOwnProperty(n)&&d(n);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[k.href.length-1]){b=a.location.hash.match(/^(#*)?(.*\/?)/)[2];var c=e[o];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(r,function(){var a=e.uri,b=e.state;e.rulesDispatcher(),D=a,C=b,F&&this.dispatch()}.bind(e));var t=!1,u=function(){if(!t&&D!==e.uri){var a=e.uri,b=e.state;e.rulesDispatcher(),D=a,C=b,F&&this.dispatch()}}.bind(e);e.avoidHashchangeHandler=function(){a.removeEventListener(q,u),t=!0},a.addEventListener(q,u);var v=function(){a.dispatchEvent(new m(q))};return i.addEventListener("DOMContentLoaded",v),a.addEventListener("readystatechange",v),a.addEventListener("load",function(){v(),l&&a.setInterval(function(){return e.uri!==D?void v():void(t&&(t=!1,D=e.uri,a.addEventListener(q,u)))}.bind(e),50)}.bind(e)),this}function h(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 g=this.uri.match(/^([^?#]*)\//);g=g?g[1]+"/":"",b[2]=g+b[2]}else b[2]=b[2].match(/^([#\/]*)?(.*)/)[2];this[o]?b[2]=this.basePath+b[2]:b[2]=k.pathname+"#"+f(b[2])}return a.history[c].apply(a.history,b),this}}var i=a.document,j=a.window,k=a.location,l=function(){var a=j.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 m=a.HashChangeEvent,n=a.Event;!function(){function b(a,b){b=b||{bubbles:!1,cancelable:!1,detail:void 0};var c=i.createEvent("CustomEvent");return c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail),c}if(Element.prototype.addEventListener){b.prototype=n.prototype,(!a.CustomEvent||l)&&(a.CustomEvent=b),(!m||a.opera&&a.opera.version()<15||l)&&(m=a.CustomEvent),l&&(n=b);try{new m("hashchange")}catch(c){m=b}try{new n("popstate")}catch(c){n=b}}}(),function(){if(!Element.prototype.addEventListener&&Object.defineProperty){var b=i.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,i.documentElement[d])};if(this.__bindedFunctions.push({original:c,binded:f}),i.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,j.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&&i.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,j.removeEventListener=d,n=function(b,c){var d=i.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=n,m=CustomEvent;var e=function(a){if(!a.customEvent)return void this.fireEvent(a.type,a);if(this.__elemetIEid){var b=a.name+this.__elemetIEid;i.documentElement[b]=a}};HTMLDocument.prototype.dispatchEvent=e,Element.prototype.dispatchEvent=e,j.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 o="usePushState",p="hasPushState",q="hashchange",r="popstate",s="leave",t="update",u="enter",v="change",w="match",x="oldMatch",y=a.PushStateTree&&a.PushStateTree.options||{},z=a.DEBUG||y.DEBUG,A=y.VERSION||"development",B="undefined"!=typeof HTMLElement?HTMLElement:Element,C=null,D=null,E=[],F=!1,G=!1;g.prototype={beautifyLocation:!0,createRule:function(a){var c=i.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,w,{get:function(){return f},set:function(a){f=a instanceof Array?a:[]}});var g=[];return Object.defineProperty(c,x,{get:function(){return g},set:function(a){g=a instanceof Array?a:[]}}),c[w]=[],c[x]=[],["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 G?(F=!0,this):(F=!1,a.dispatchEvent(new n(r)),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[w]=k||[],c.detail[x]=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[w].length>f.parentGroup&&(i=h[w][f.parentGroup]||""),j="",h[x].length>f.parentGroup&&(j=h[x][f.parentGroup]||"")),f[w]=i[w](f.rule),"string"==typeof j?f[x]=j[w](f.rule):f[x]=[];var k=f[w],l=f[x],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(t,{detail:{type:s}})),void f.dispatchEvent(new g(s))}this.eventStack[w].push({element:f,events:[new g(w)]});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[u].push({element:f,events:[new g(t,{detail:{type:u}}),new g(u)]}),void m.forEach(c.bind(this,b,e));k[0]!==l[0]&&this.eventStack[v].push({element:f,events:[new g(t,{detail:{type:v}}),new g(v)]}),m.forEach(c.bind(this,b,e))}}var d=this.debug===!0||z;if(this.isPathValid&&(E.push(b.bind(this,this.uri)),!(E.length>1))){for(var e=D;E.length>0;)e=E[0].call(null,e),E.shift();G=!0;var f=this.eventStack;[v,u,w].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()}}),G=!1}}};for(var H in a.history)"function"==typeof a.history[H]&&h.call(g.prototype,H);g.prototype[p]=a.history&&!!a.history.pushState,g.prototype[p]||(g.prototype[o]=!1);var I=null;g.prototype.pushState||(g.prototype.pushState=function(b,c,g){var h=i.title||"";return g=g||"",null!==I&&(i.title=I),this.avoidHashchangeHandler(),d(g)&&(a.location.href=g),"#"===g[0]&&(g=g.slice(1)),e(g)&&(g=a.location.hash.slice(1,a.location.hash.lastIndexOf("/")+1)+g,g=f(g)),a.location.hash=g,i.title=h,I=c,this}),g.prototype.replaceState||(g.prototype.replaceState=function(b,c,g){var h=i.title||"";if(g=g||"",null!==I&&(i.title=I),this.avoidHashchangeHandler(),d(g))throw new Error("Invalid url replace.");if("#"===g[0]&&(g=g.slice(1)),e(g)){var j=a.location.hash.lastIndexOf("/")+1;g=a.location.hash.slice(1,j)+g,g=f(g)}return g="#"+g,a.location.replace(g),i.title=h,I=c,this}),a.PushStateTree=g,"undefined"!=typeof module&&(module.exports=g)}(function(){return this}());