twister HTML + Javascript User Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

10 lines
6.7 KiB

/*!
* jQueryMobile-router v20130527
* http://github.com/azicchetti/jquerymobile-router
*
* Copyright 2011-2013 (c) Andrea Zicchetti
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://github.com/azicchetti/jquerymobile-router/blob/master/MIT-LICENSE.txt
* http://github.com/azicchetti/jquerymobile-router/blob/master/GPL-LICENSE.txt
*/
(function(a,b){if(typeof define==="function"&&define.amd){define(["jquery"],b)}else{b(jQuery)}}(this,function(a){a(document).on("mobileinit",function(){var c=a.extend({fixFirstPageDataUrl:false,firstPageDataUrl:"index.html",ajaxApp:false,firstMatchOnly:false,defaultArgsRe:false},a.mobile.jqmRouter||{});var d=null,e=null,b=false;a(document).on("pagebeforechange",function(j,i){if(i.options.hasOwnProperty("_jqmrouter_handled")){return}var h;if(typeof i.toPage==="string"){h=i.toPage}else{h=i.toPage.jqmData("url")||"";if(i.toPage.attr("id")==h){h="#"+h}}i.options._jqmrouter_handled=true;if(i.options.data&&(i.options.type+"").toLowerCase()=="get"){h+="?"+i.options.data}var g=a.mobile.path.parseUrl(h);d=e;e=g;if(g.hash.indexOf("?")!=-1){i.options.dataUrl=g.hash.replace(/^#/,"")}var f=/^#|\?.*$/g;if(d&&g.hash!=d.hash&&g.hash.replace(f,"")==d.hash.replace(f,"")){i.options.allowSamePageTransition=true&&!b}b=false;if(window.location.hash.indexOf("&ui-state=dialog")!=-1){b=true}});if(c.fixFirstPageDataUrl){a(document).ready(function(){if(!window.location.pathname.match("/$")){return}var g=a(":jqmData(role='page')").first();var h=g.jqmData("url"),f=window.location.pathname+c.firstPageDataUrl+window.location.search+window.location.hash;if(h!=f){g.attr("data-url",f).jqmData("url",f)}})}a.mobile.Router=function(j,g,h){this.routes={pagebeforecreate:null,pagecreate:null,pagebeforeshow:null,pageshow:null,pagebeforehide:null,pagehide:null,pageinit:null,pageremove:null,pagebeforechange:null,pagebeforeload:null,pageload:null,popupbeforeposition:null,popupafteropen:null,popupafterclose:null};this.evtLookup={bC:"pagebeforechange",bl:"pagebeforeload",l:"pageload",bc:"pagebeforecreate",c:"pagecreate",bs:"pagebeforeshow",s:"pageshow",bh:"pagebeforehide",h:"pagehide",i:"pageinit",rm:"pageremove",pbp:"popupbeforeposition",pao:"popupafteropen",pac:"popupafterclose"};this.routesRex={};this.conf=a.extend({},c,h||{});this.defaultHandlerEvents={};if(this.conf.defaultHandlerEvents){var f=this.conf.defaultHandlerEvents.split(",");for(var k=0;k<f.length;k++){this.defaultHandlerEvents[this.evtLookup[f[k]]]=f[k]}}this.add(j,g)};a.extend(a.mobile.Router.prototype,{documentEvts:{pagebeforechange:1,pagebeforeload:1,pageload:1},debug:function(g){var f=this.conf;if(f.debugHandler){f.debugHandler.apply(this,arguments)}else{if(f.debugHandler!==false&&typeof(console)!="undefined"&&console.log){console.log.apply(console,arguments);if(g.stack){console.log(g.stack)}}}},add:function(i,h,k){if(!i){return}var f=this,g=[],l=[];if(i instanceof Array){a.each(i,a.proxy(function(n,m){this.add(m,h,true)},this))}else{a.each(i,function(q,o){if(typeof(o)=="string"||typeof(o)=="function"){if(f.routes.pagebeforeshow===null){f.routes.pagebeforeshow={}}if(f.conf.defaultArgsRe){q+="(?:[?](.*))?$"}f.routes.pagebeforeshow[q]={handler:o,events:"bs"};if(!f.routesRex.hasOwnProperty(q)){f.routesRex[q]=new RegExp(q)}}else{var n,p=o.events.split(","),m;if(o.argsre!==false&&(o.argsre||f.conf.defaultArgsRe)){q+="(?:[?](.*))?$"}for(n=0;n<p.length;n++){m=f.evtLookup[p[n]];if(f.routes.hasOwnProperty(m)){if(f.routes[m]===null){f.routes[m]={}}f.routes[m][q]=o;if(!f.routesRex.hasOwnProperty(q)){f.routesRex[q]=new RegExp(q)}}else{f.debug("can't set unsupported route "+p[n])}}}})}if(k===true){return}if(!this.userHandlers){this.userHandlers=h||{}}else{a.extend(this.userHandlers,h||{})}a.each(f.routes,function(m,n){if(n!==null){if(!f.documentEvts[m]){g.push(m)}else{l.push(m)}}});this._detachEvents();var j=function(n,m){f._processRoutes(n,m,this)};if(g.length>0){this._eventData={events:g.join(" "),selectors:":jqmData(role='page'),:jqmData(role='dialog')",handler:j};a(document).on(this._eventData.events,this._eventData.selectors,this._eventData.handler)}if(l.length>0){this._docEventData={events:l.join(" "),handler:j};a(document).on(this._docEventData.events,this._docEventData.handler)}},_processRoutes:function(k,o,l){var m=this,n,h,g=0,f=null;if(k.type=="pagebeforechange"){if(o.options._jqmrouter_bC){return}f={isString:typeof o.toPage==="string",deferred:a.Deferred(),toPage:o.toPage};l=!f.isString?o.toPage:null}if(k.type in {pagebeforehide:true,pagehide:true,pageremove:true}){n=d}else{n=e}h=!this.conf.ajaxApp?n.hash:(n.pathname+n.search+n.hash);if(h.length==0){n=null;if(!this.documentEvts[k.type]&&l){n=a(l).jqmData("url")}else{n=a.mobile.firstPage.jqmData("url")}if(n){n=a.mobile.path.parseUrl("#"+n);h=!this.conf.ajaxApp?n.hash:(n.pathname+n.search+n.hash)}}if(h.length==0){return}var j=false;a.each(this.routes[k.type],function(r,q){var s,u;if(f&&q.step!="all"){q.step=q.step||"page";if((f.isString&&q.step=="page")||(!f.isString&&q.step!="page")){return}}if((s=h.match(m.routesRex[r]))){if(typeof(q.handler)=="function"){u=q.handler}else{if(typeof(m.userHandlers[q.handler])=="function"){u=m.userHandlers[q.handler]}}if(u){try{if(f&&o){o.bCDeferred=f.deferred}u.apply(m.userHandlers,[k.type,s,o,l,k]);j=true}catch(t){m.debug(t)}}}if(j&&m.conf.firstMatchOnly){return false}});if(!j&&this.conf.defaultHandler&&this.defaultHandlerEvents[k.type]){var p;if(typeof(this.conf.defaultHandler)=="function"){p=this.conf.defaultHandler}else{if(typeof(this.userHandlers[this.conf.defaultHandler])=="function"){p=this.userHandlers[this.conf.defaultHandler]}}try{p.apply(this.userHandlers,[k.type,o,l,k])}catch(i){this.debug(i)}}if(f&&k.isDefaultPrevented()){f.deferred.done(function(){var q=(f.toPage===o.toPage?{_jqmrouter_handled:true,_jqmrouter_bC:true}:null);a.mobile.changePage(o.toPage,a.extend({allowSamePageTransition:o.options.allowSamePageTransition,changeHash:o.options.changeHash,data:o.options.data,dataUrl:o.options.dataUrl,pageContainer:o.options.pageContainer,reloadPage:o.options.reloadPage,reverse:o.options.reverse,role:o.options.role,showLoadMsg:o.options.showLoadMsg,transition:o.options.transition,type:o.options.type},q))})}},_detachEvents:function(){if(this._eventData){a(document).off(this._eventData.events,this._eventData.selectors,this._eventData.handler)}if(this._docEventData){a(document).off(this._docEventData.events,this._docEventData.handler)}},destroy:function(){this._detachEvents();this.routes=this.routesRex=null},getParams:function(f){if(!f){return null}var i={},g;var h=f.slice(f.indexOf("?")+1).split("&");a.each(h,function(l,j){g=j.split("=");g[0]=decodeURIComponent(g[0]);if(i[g[0]]){if(!(i[g[0]] instanceof Array)){i[g[0]]=[i[g[0]]]}i[g[0]].push(decodeURIComponent(g[1]))}else{i[g[0]]=decodeURIComponent(g[1])}});if(a.isEmptyObject(i)){return null}return i}})});return{}}));