1 Star 0 Fork 4

leader20 / PathJS

forked from 山哥 / PathJS 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
path.min.js 6.93 KB
一键复制 编辑 原始数据 按行查看 历史
山哥 提交于 2015-08-26 17:08 . 修改 version: 1.0.1
/*!
* 山哥(wangys0927@gmail.com) at:
* 2015/08/26 -> 1.0.1 `this.params` supported querystring parameters.
* 2015/08/25 -> 1.0 See README.md
* 2015/01/16 -> 0.9 Use new route path parser, now it's powerful!
* 2015/01/04 -> 0.8.6 Upgrade route path parser.
* 2014/12/31 -> 0.8.5 Supported legacy IE(6,7).
*/
;(function(a,b){if(typeof define==="function"&&define.amd){define(function(){return(a.Path=b(a));});}else{if(typeof exports!=="undefined"){}else{a.Path=b(a);}}})(typeof window!=="undefined"?window:this,function(b){var a=function(h){return("[object Array]"===Object.prototype.toString.call(h));};var g=function(h){return("function"===typeof h);};var e=function(h){try{return decodeURIComponent(h.replace(/\+/g," "));}catch(i){return h;}};var f=function(h){if(!h){return{};}var m=h.slice(h.indexOf("?")+1),q=m.split("&");if(q.length===0){return{};}var j={},p,l,r;for(var n=0,o=q.length;n<o;n++){p=q[n].split("=");l=e(p[0]).replace(/^\s+|\s+$/g,"");if(!l){continue;}r=p.length>1?e(p[1]):"";if(!j.hasOwnProperty(l)){j[l]=r;}else{if(a(j[l])){j[l].push(r);}else{j[l]=[j[l],r];}}}return j;};var d={version:"1.0.1",map:function(h){var i=d._pathToString(h);if(d.routes.defined.hasOwnProperty(i)){return d.routes.defined[i];}else{return new d.core.Route(h);}},root:function(h){d.routes.root=h;},rescue:function(h){d.routes.rescue=h;},to:function(i){if(!i){return;}if(i==d.routes.current){var h=d.match(d.routes.current);if(!h){return;}g(h.do_exit)&&h.do_exit.call(h);d.routes.current=null;d.routes.previous=null;d.dispatch(i);}else{if(d.history.supported){d.history.pushState({},document.title,i);}else{window.location.hash=d._fillHashPath(i);}}},match:function(j,i){var h=null;for(var k in d.routes.defined){h=d.routes.defined[k];if(h.match(j,i)){return h;}}return null;},dispatch:function(h){if(!h){return;}var i,j;if(d.routes.current!==h){d.routes.previous=d.routes.current;d.routes.current=h;j=d.match(h,true);if(d.routes.previous){i=d.match(d.routes.previous);if(i!==null&&g(i.do_exit)){i.do_exit.call(i);}}if(j!==null){j.run();return true;}else{if(d.routes.rescue!==null){d.routes.rescue();}}}},listen:function(h){h=h||{};if(false===h.hashbang){d.routes.options.hashbang=false;}d._hackLegacyIE();var i=function(){d.dispatch(window.location.hash);};if(window.location.hash===""){if(d.routes.root!==null){window.location.hash=d._fillHashPath(d.routes.root);}}if("onhashchange" in window&&(!document.documentMode||document.documentMode>=8)){window.onhashchange=i;}else{setInterval(i,50);}if(window.location.hash!==""){d.dispatch(window.location.hash);}},history:{initial:{},pushState:function(h,j,i){if(d.history.supported){if(d.dispatch(i)){window.history.pushState(h,j,i);}}else{if(d.history.fallback){window.location.hash=d._fillHashPath(i);}}},popState:function(i){var h=!d.history.initial.popped&&window.location.href==d.history.initial.URL;d.history.initial.popped=true;if(h){return;}d.dispatch(document.location.pathname);},listen:function(h){h=h||{};d.history.supported=!!(window.history&&window.history.pushState);d.history.fallback=h.fallback;if(d.history.supported){d.history.initial.popped=("state" in window.history);d.history.initial.URL=window.location.href;window.onpopstate=d.history.popState;}else{if(d.history.fallback){d.listen(h);}}}},core:{Route:function(j){this.path=d._pathToString(j);this.pathKeys=[];this.pathRegExp=[];if(a(j)){for(var h=0;h<j.length;h++){this.pathRegExp.push(d._pathRegExp(j[h],this.pathKeys,false,false));}}else{this.pathRegExp.push(d._pathRegExp(j,this.pathKeys,false,false));}this.do_action=function(){};this.do_enter=[];this.do_exit=function(){};this.params={};d.routes.defined[this.path]=this;}},routes:{current:null,root:null,rescue:null,previous:null,options:{hashbang:true},defined:{}},_getHashPath:function(i){var h=i||window.location.hash;if(h.indexOf("#!")===0){h=h.slice(2);}else{if("#"===h.charAt(0)){h=h.slice(1);}}var j=h.indexOf("?");if(j>=0){h=h.slice(0,j);}return h;},_fillHashPath:function(h){if("#"===h.charAt(0)){return h;}return(d.routes.options.hashbang?"#!":"#")+h;},_pathToString:function(h){if(a(h)){return"["+h.join(",")+"]";}return h.toString();},_pathRegExp:function(w,v,n,r){if(Object.prototype.toString.call(w)==="[object RegExp]"){return w;}if(a(w)){w="("+w.join("|")+")";}var p=[];w=w.concat(r?"":"/?").replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g,function(x,z,B,y,i,j,A){p.push({name:y,optional:!!j});z=z||"";return""+(j?"":z)+"(?:"+(j?z:"")+(B||"")+(i||(B&&"([^/.]+?)"||"([^/]+?)"))+")"+(j||"")+(A?"(/*)?":"");}).replace(/([\/.])/g,"\\$1").replace(/\*/g,"(.*)");if(a(v)&&p.length>0){var q=[],t=[],m=[];for(var k=0,l=w.length;k<l;++k){var o=w.charAt(k),u=(w.charAt(k-1)!=="\\");if(o==="("&&u){q.push(o);t.push(o);}else{if(o===")"&&u){q.pop();t.push(o);if(q.length==0){m.push(t.join("").indexOf("?:")!=-1?1:0);t=[];}}else{if(q.length>0){t.push(o);}}}}if(q.length>0){throw new Error("The Path regexp <"+w+"> has unmatched group!");}var s=p.slice(0),o=0;for(var h=0,l=m.length;h<l;++h){v.push((m[h]==1&&o<s.length)?s[o++]:false);}}return new RegExp("^"+w+"$",n?"":"i");},_ieFrame:null,_hackLegacyIE:function(){if(d._ieFrame){return;}var i=(!+"\v1")&&!("onhashchange" in window&&(!document.documentMode||document.documentMode>=8));if(!i){return;}var h=document.createElement("iframe");h.src="about:blank";h.style.display="none";h.setAttribute("tabindex","-1");h.attachEvent("onload",function(){if(d._ieFrame.frameHash&&d._ieFrame.frameHash!=window.location.hash){window.location.hash=d._ieFrame.frameHash;}});document.body.appendChild(h);d._ieFrame=h.contentWindow;h=null;}};d.core.Route.prototype={to:function(h){this.do_action=h;return this;},enter:function(h){if(h instanceof Array){this.do_enter=this.do_enter.concat(h);}else{this.do_enter.push(h);}return this;},exit:function(h){this.do_exit=h;return this;},run:function(){var h=false,m;if(this.do_enter.length>0){for(var j=0;j<this.do_enter.length;j++){m=this.do_enter[j];if(!m||!g(m)){continue;}if(false===m.call(this)){h=true;break;}}}if(!h&&g(this.do_action)){this.do_action.call(this);}if(d._ieFrame){var l=window.location.hash;if(l!=d._ieFrame.frameHash){l=l.replace(/"/g,'\\"');var k=d._ieFrame.document;k.open();k.write("<html><head><title>"+document.title+'</title><script type="text/javascript">var frameHash="'+l+'";<\/script></head><body>&nbsp;</body></html>');k.close();}}},match:function(x,v){if(this.pathRegExp.length==0){return false;}var s=d._getHashPath(x);for(var o=0;o<this.pathRegExp.length;o++){var l=this.pathRegExp[o].exec(s);if(l){if(v){var w=this.pathKeys,j=w.length,t,k,n=[];for(var o=1,r=l.length;o<r;++o){t=j>=o?w[o-1]:null;k="string"==typeof l[o]?e(l[o]):l[o];if(t&&t.name){if(!n.hasOwnProperty(t.name)){n[t.name]=k;}else{if(a(n[t.name])){n[t.name].push(k);}else{n[t.name]=[n[t.name],k];}}}else{n.push(k);}}var u=f(x);for(var h in u){var q=u[h];if(!n.hasOwnProperty(h)){n[h]=q;}else{if(a(n[h])){if(a(q)){n[h]=n[h].concat(q);}else{n[h].push(q);}}else{if(a(q)){n[h]=[n[h]].concat(q);}else{n[h]=[n[h],q];}}}}this.params=n;}return true;}}return false;}};var c=b.Path;d.noConflict=function(){b.Path=c;return d;};return d;});
JavaScript
1
https://gitee.com/lixuda/PathJS.git
git@gitee.com:lixuda/PathJS.git
lixuda
PathJS
PathJS
master

搜索帮助