forked from devote/HTML5-History-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.min.js
30 lines (30 loc) · 7.6 KB
/
history.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*!
* History API JavaScript Library v4.2.4
*
* Support: IE8+, FF3+, Opera 9+, Safari, Chrome and other
*
* Copyright 2011-2015, Dmitrii Pakhtinov ( spb.piksel@gmail.com )
*
* http://spb-piksel.ru/
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Update: 2015-10-16 22:16
*/
(function(m){if("function"===typeof define&&define.amd){var w="[history"+(new Date).getTime()+"]",j=requirejs.onError;m.toString=function(){return w};requirejs.onError=function(m){-1===m.message.indexOf(w)&&j.call(requirejs,m)};define([],m)}if("object"===typeof exports&&"undefined"!==typeof module)module.exports=m();else return m()})(function(){var h=!0,i=null,p=!1;function m(a,b){var c=e.history!==n;c&&(e.history=n);a.apply(n,b);c&&(e.history=k)}function w(){}function j(a,b,c){if(a!=i&&""!==a&&!b){var b=j(),d=g.getElementsByTagName("base")[0];
!c&&d&&d.getAttribute("href")&&(d.href=d.href,b=j(d.href,i,h));c=b.d;d=b.h;a=""+a;a=/^(?:\w+\:)?\/\//.test(a)?0===a.indexOf("/")?d+a:a:d+"//"+b.g+(0===a.indexOf("/")?a:0===a.indexOf("?")?c+a:0===a.indexOf("#")?c+b.e+a:c.replace(/[^\/]+$/g,"")+a)}else if(a=b?a:f.href,!o||c)a=a.replace(/^[^#]*/,"")||"#",a=f.protocol.replace(/:.*$|$/,":")+"//"+f.host+l.basepath+a.replace(RegExp("^#[/]?(?:"+l.type+")?"),"");N.href=a;var a=/(?:(\w+\:))?(?:\/\/(?:[^@]*@)?([^\/:\?#]+)(?::([0-9]+))?)?([^\?#]*)(?:(\?[^#]+)|\?)?(?:(#.*))?/.exec(N.href),
b=a[2]+(a[3]?":"+a[3]:""),c=a[4]||"/",d=a[5]||"",e="#"===a[6]?"":a[6]||"",k=c+d+e,m=c.replace(RegExp("^"+l.basepath,"i"),l.type)+d;return{b:a[1]+"//"+b+k,h:a[1],g:b,i:a[2],k:a[3]||"",d:c,e:d,a:e,c:k,j:m,f:m+e}}function Z(){var a;try{a=e.sessionStorage,a.setItem(A+"t","1"),a.removeItem(A+"t")}catch(b){a={getItem:function(a){a=g.cookie.split(a+"=");return 1<a.length&&a.pop().split(";").shift()||"null"},setItem:function(a){var b={};if(b[f.href]=k.state)g.cookie=a+"="+B.stringify(b)}}}try{q=B.parse(a.getItem(A))||
{}}catch(c){q={}}s(t+"unload",function(){a.setItem(A,B.stringify(q))},p)}function C(a,b,c,d){var f=0;c||(c={set:w},f=1);var g=!c.set,j=!c.get,k={configurable:h,set:function(){g=1},get:function(){j=1}};try{x(a,b,k),a[b]=a[b],x(a,b,c)}catch(l){}if(!g||!j)if(a.__defineGetter__&&(a.__defineGetter__(b,k.get),a.__defineSetter__(b,k.set),a[b]=a[b],c.get&&a.__defineGetter__(b,c.get),c.set&&a.__defineSetter__(b,c.set)),!g||!j){if(f)return p;if(a===e){try{var m=a[b];a[b]=i}catch(o){}if("execScript"in e)e.execScript("Public "+
b,"VBScript"),e.execScript("var "+b+";","JavaScript");else try{x(a,b,{value:w})}catch(r){"onpopstate"===b&&(s("popstate",c=function(){H("popstate",c,p);var b=a.onpopstate;a.onpopstate=i;setTimeout(function(){a.onpopstate=b},1)},p),O=0)}a[b]=m}else try{try{var n=I.create(a);x(I.getPrototypeOf(n)===a?n:a,b,c);for(var q in a)"function"===typeof a[q]&&(n[q]=a[q].bind(a));try{d.call(n,n,a)}catch(t){}a=n}catch(u){x(a.constructor.prototype,b,c)}}catch(v){return p}}return a}function $(a,b,c){c=c||{};a=a===
P?f:a;c.set=c.set||function(c){a[b]=c};c.get=c.get||function(){return a[b]};return c}function D(a,b){var c=(""+("string"===typeof a?a:a.type)).replace(/^on/,""),d=u[c];if(d){b="string"===typeof a?b:a;if(b.target==i)for(var f=["target","currentTarget","srcElement","type"];a=f.pop();)b=C(b,a,{get:"type"===a?function(){return c}:function(){return e}});O&&(("popstate"===c?e.onpopstate:e.onhashchange)||w).call(e,b);for(var f=0,g=d.length;f<g;f++)d[f].call(e,b);return h}return aa(a,b)}function Q(){var a=
g.createEvent?g.createEvent("Event"):g.createEventObject();a.initEvent?a.initEvent("popstate",p,p):a.type="popstate";a.state=k.state;D(a)}function y(a,b,c,d){o?z=f.href:(0===r&&(r=2),b=j(b,2===r&&-1!==(""+b).indexOf("#")),b.c!==j().c&&(z=d,c?f.replace("#"+b.f):f.hash=b.f));!E&&a&&(q[f.href]=a);F=p}function R(a){var b=z;z=f.href;if(b){S!==f.href&&Q();var a=a||e.event,b=j(b,h),c=j();a.oldURL||(a.oldURL=b.b,a.newURL=c.b);b.a!==c.a&&D(a)}}function T(a){setTimeout(function(){s("popstate",function(a){S=
f.href;E||(a=C(a,"state",{get:function(){return k.state}}));D(a)},p)},0);!o&&a!==h&&"location"in k&&(U(G.hash),F&&(F=p,Q()))}function ba(a){var a=a||e.event,b;a:{for(b=a.target||a.srcElement;b;){if("A"===b.nodeName)break a;b=b.parentNode}b=void 0}var c="defaultPrevented"in a?a.defaultPrevented:a.returnValue===p;b&&"A"===b.nodeName&&!c&&(c=j(),b=j(b.getAttribute("href",2)),c.b.split("#").shift()===b.b.split("#").shift()&&b.a&&(c.a!==b.a&&(G.hash=b.a),U(b.a),a.preventDefault?a.preventDefault():a.returnValue=
p))}function U(a){var b=g.getElementById(a=(a||"").replace(/^#/,""));b&&b.id===a&&"A"===b.nodeName&&(a=b.getBoundingClientRect(),e.scrollTo(J.scrollLeft||0,a.top+(J.scrollTop||0)-(J.clientTop||0)))}var e=("object"===typeof window?window:this)||{};if(!e.history||"emulate"in e.history)return e.history;var g=e.document,J=g.documentElement,I=e.Object,B=e.JSON,f=e.location,n=e.history,k=n,K=n.pushState,V=n.replaceState,o=!!K,E="state"in n,x=I.defineProperty,G=C({},"t")?{}:g.createElement("a"),t="",L=e.addEventListener?
"addEventListener":(t="on")&&"attachEvent",W=e.removeEventListener?"removeEventListener":"detachEvent",X=e.dispatchEvent?"dispatchEvent":"fireEvent",s=e[L],H=e[W],aa=e[X],l={basepath:"/",redirect:0,type:"/",init:0},A="__historyAPI__",N=g.createElement("a"),z=f.href,S="",O=1,F=p,r=0,q={},u={},v=g.title,M,ca={onhashchange:i,onpopstate:i},Y={setup:function(a,b,c){l.basepath=(""+(a==i?l.basepath:a)).replace(/(?:^|\/)[^\/]*$/,"/");l.type=b==i?l.type:b;l.redirect=c==i?l.redirect:!!c},redirect:function(a,
b){k.setup(b,a);b=l.basepath;if(e.top==e.self){var c=j(i,p,h).c,d=f.pathname+f.search;o?(d=d.replace(/([^\/])$/,"$1/"),c!=b&&RegExp("^"+b+"$","i").test(d)&&f.replace(c)):d!=b&&(d=d.replace(/([^\/])\?/,"$1/?"),RegExp("^"+b,"i").test(d)&&f.replace(b+"#"+d.replace(RegExp("^"+b,"i"),l.type)+f.hash))}},pushState:function(a,b,c){var d=g.title;v!=i&&(g.title=v);K&&m(K,arguments);y(a,c);g.title=d;v=b},replaceState:function(a,b,c){var d=g.title;v!=i&&(g.title=v);delete q[f.href];V&&m(V,arguments);y(a,c,h);
g.title=d;v=b},location:{set:function(a){0===r&&(r=1);e.location=a},get:function(){0===r&&(r=1);return G}},state:{get:function(){return q[f.href]||i}}},P={assign:function(a){!o&&0===(""+a).indexOf("#")?y(i,a):f.assign(a)},reload:function(a){f.reload(a)},replace:function(a){!o&&0===(""+a).indexOf("#")?y(i,a,h):f.replace(a)},toString:function(){return this.href},origin:{get:function(){return void 0!==M?M:!f.origin?f.protocol+"//"+f.hostname+(f.port?":"+f.port:""):f.origin},set:function(a){M=a}},href:o?
i:{get:function(){return j().b}},protocol:i,host:i,hostname:i,port:i,pathname:o?i:{get:function(){return j().d}},search:o?i:{get:function(){return j().e}},hash:o?i:{set:function(a){y(i,(""+a).replace(/^(#|)/,"#"),p,z)},get:function(){return j().a}}};if(function(){var a=g.getElementsByTagName("script"),a=(a[a.length-1]||{}).src||"";(-1!==a.indexOf("?")?a.split("?").pop():"").replace(/(\w+)(?:=([^&]*))?/g,function(a,b,c){l[b]=(c||"").replace(/^(0|false)$/,"")});s(t+"hashchange",R,p);var b=[P,G,ca,e,
Y,k];E&&delete Y.state;for(var c=0;c<b.length;c+=2)for(var d in b[c])if(b[c].hasOwnProperty(d))if("object"!==typeof b[c][d])b[c+1][d]=b[c][d];else{a=$(b[c],d,b[c][d]);if(!C(b[c+1],d,a,function(a,d){if(d===k)e.history=k=b[c+1]=a}))return H(t+"hashchange",R,p),p;b[c+1]===e&&(u[d]=u[d.substr(2)]=[])}k.setup();l.redirect&&k.redirect();l.init&&(r=1);!E&&B&&Z();if(!o)g[L](t+"click",ba,p);"complete"===g.readyState?T(h):(!o&&j().c!==l.basepath&&(F=h),s(t+"load",T,p));return h}())return k.emulate=!o,e[L]=
function(a,b,c){a in u?u[a].push(b):3<arguments.length?s(a,b,c,arguments[3]):s(a,b,c)},e[W]=function(a,b,c){var d=u[a];if(d)for(a=d.length;a--;){if(d[a]===b){d.splice(a,1);break}}else H(a,b,c)},e[X]=D,k});