-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathmobilebone.min.js
7 lines (7 loc) · 17.1 KB
/
mobilebone.min.js
1
2
3
4
5
6
7
/**!
* @author @copyright zhangxinxu(.com)
* @since 2014-09-26
* @description mobilebone.js(v2.8.1) - bone of switch for mobile web app - https://github.com/zhangxinxu/mobilebone
* @license MIT
**/
(function(t,a){if(document.MBLOADED){t.console&&console.warn("Don't repeat load Mobilebone!");return}if(typeof define==="function"&&(define.amd||define.cmd)){define("mobilebone",function(e){return a(t,e)})}else if(typeof module==="object"&&typeof module.exports==="object"){module.exports=a(t,{})}else{t.Mobilebone=a(t,{})}})(typeof global!=="undefined"?global:typeof window!=="undefined"?window:typeof self!=="undefined"?self:this,function(e,q){var T={};var t=[];var L=t.slice;var s=/^#?\w+(?:[\-_]\w+)*$/i;var S="WebkitAppearance"in document.documentElement.style||typeof document.webkitHidden!="undefined";var w="pushState"in history&&"replaceState"in history;q.support=w;var l=false;q.VERSION="2.8.1";q.autoInit=true;q.captureLink=true;q.captureForm=true;q.rootTransition=e;q.mergeCallback=true;q.classAnimation="slide";q.classPage="page";q.classMask="mask";q.pushStateEnabled=true;q.evalScript=false;if(navigator.userAgent.indexOf("Firefox")>=0&&window.top!==window){q.pushStateEnabled=false}if(w==false){return q}history.popstate=false;q.transition=function(e,t,a,s){if(arguments.length==0||e==t)return;if(arguments.length==3&&isNaN(a*1)==true){s=a;a=s.back}t=t||null,a=a||false,s=s||{};var n={root:this.rootTransition,form:this.form||this.classAnimation,onpagefirstinto:this.onpagefirstinto,animationstart:this.animationstart,animationend:this.animationend,preventdefault:this.preventdefault,fallback:this.fallback,callback:this.callback},l=function(a){if(!a||!a.getAttribute)return{};var i={},r=B(a.getAttribute("data-params")||"");["title","root","form"].forEach(function(e){i[e]=a.getAttribute("data-"+e)||r[e]||s[e]||n[e]});if(typeof i.root=="string"){i.root=q.getFunction(i.root)}["onpagefirstinto","callback","fallback","animationstart","animationend","preventdefault"].forEach(function(e){if(q.mergeCallback==true&&typeof n[e]=="function"){var t=a.getAttribute("data-"+e)||r[e];if(typeof i.root[t]=="function"){i[e]=function(){n[e].apply(this,arguments);i.root[t].apply(this,arguments)}}else if(typeof s[e]=="function"){i[e]=function(){n[e].apply(this,arguments);s[e].apply(this,arguments)}}else{i[e]=n[e]}}else{i[e]=a.getAttribute("data-"+e)||r[e]||s[e]||n[e]}});return i};var i=l(t),f=l(e);if(t!=null&&t.classList){var r=i.preventdefault,o=false;if(typeof r=="string"){r=i.root[r]}}if(e!=null&&e.classList){var u=f.preventdefault,c=false;if(typeof u=="string"){u=f.root[u]}}if(typeof r=="function"){o=r.call(i.root,e,t,s)}if(o==true&&r===u){return false}if(typeof u=="function"){c=u.call(f.root,e,t,s)}if(c==true){return false}var d=function(n,o){if(n.flagAniBind==true)return;["animationstart","animationend"].forEach(function(e,t){var a=f[e],i="webkit"+e.replace(/^a|s|e/g,function(e){return e.toUpperCase()});var r=S?i:e;if(t){n.addEventListener(r,function(){if(this.classList.contains("in")==false){this.style.display="none"}this.classList.remove(l(this).form)})}if(typeof a=="string"&&f.root[a]){n.addEventListener(r,function(){o.root[a].call(o.root,this,this.classList.contains("in")?"into":"out",s)})}else if(typeof a=="function"){n.addEventListener(r,function(){a.call(o.root,this,this.classList.contains("in")?"into":"out",s)})}n.flagAniBind=true})};if(t!=null&&t.classList){if(o!=true){t.classList.add(i.form);t.offsetWidth;t.style.display="block";t.classList.add("out");t.classList.remove("in");t.classList[a?"add":"remove"]("reverse");t.removeSelf=t.removeSelf||null;d(t,i);var p=i.fallback;if(typeof p=="string"){p=i.root[p]}if(typeof p=="function"){p.call(i.root,e,t,s)}}}if(e!=null&&e.classList){var y=f.title,m=document.querySelector("h1"),h=document.querySelector("."+this.classPage);if(y&&s.title!==false){document.title=y;if(m){m.innerHTML=y;m.title=y}}else if(h==e&&!t&&document.title){e.setAttribute("data-title",document.title)}var g=s.id||e.id;var v=s.id||e.id;if(t){e.classList.add(f.form)}e.offsetWidth;e.style.display="block";e.classList.remove("out");e.classList.add("in");e.classList[a?"add":"remove"]("reverse");var b=f.onpagefirstinto;if(!e.firstintoBind){if(typeof b=="string"&&f.root[b]){f.root[b].call(f.root,e,t,s)}else if(typeof b=="function"){b.call(f.root,e,t,s)}L.call(e.querySelectorAll("form")).forEach(function(e){q.submit(e)});e.firstintoBind=true}d(e,f);var k=v,E="";if(k&&/#/.test(k)==false){k="#"+k}E=k.replace(/#/,"#&");if(w&&this.pushStateEnabled&&s.history!==false&&k&&E!=location.hash){history.popstate=false;history[t?"pushState":"replaceState"](null,document.title,k.replace(/#/,"#&"))}if(!T[g]){T[g]=e}var A=f.callback;if(typeof A=="string"){A=f.root[A]}if(typeof A=="function"){A.call(f.root,e,t,s)}setTimeout(function(){history.popstate=true},17);if(a==false&&history.popstate==false){T.lastShip=[e,t]}else{T.lastShip=null}}};q.remove=function(e){if(!e){return this}var t=e;var a=e;if(typeof a=="string"){t=T[a]}else if(t.tagName&&t.tagName.toLowerCase()=="a"){a=this.getCleanUrl(t);t=T[a]}if(t&&t.parentElement){for(var i in T){if(T[i]==t){delete T[i]}}t.parentElement.removeChild(t)}};q.getCleanUrl=function(e,t,a){var i="",r="",n="";if(e){if(e.nodeType==1){if(e.action){i=e.getAttribute("action");if(e.method&&e.method.toUpperCase()=="POST"){return i}else if(window.$&&$.fn&&$.fn.serialize){r=$(e).serialize()}else{r={};L.call(e.querySelectorAll("input,select,textarea")).forEach(function(e){if(e.name&&!e.disabled){var t=e.value.trim(),a=e.name;if(/^radio|checkbox/i.test(e.type)){if(e.checked){if(r[a]){r[a].push(t)}else{r[a]=[t]}}}else{r[a]=[t]}}})}}else{i=e.getAttribute("href");if(/^javascript/.test(i)){i=""}r=e.getAttribute("data-formdata")||e.getAttribute("data-params")||"";var o="container",s=e.getAttribute("data-"+o);if(r.indexOf(o)==-1&&s){var l=o+"="+s;r=r?r+"&"+l:l}}}else if(e.url){i=e.url;r=e.data}}if(!(i=i||t)){return""}r=r||a||"";if(typeof r=="object"){var f=[];for(key in r){if(!r[key].forEach){r[key]=[r[key]]}r[key].forEach(function(e){f.push(key+"="+encodeURIComponent(e))})}if(f.length>0){r=f.join("&")}else{r=""}}n=i.split("#")[0].replace(/&+$/,"").replace(/^\.\/+/,"");if(n.slice(-1)=="?"){n=n.split("?")[0]}if(r!=""){if(/\?/.test(n)){r=r.replace(/^&|\?/,"");n=n+"&"+r}else if(r!=""){r=r.replace("?","");n=n+"?"+r}}return n};q.createPage=function(e,t,a){var i=null,r=null,n=this.classPage,o=null;if(!e)return;if(typeof a=="undefined"&&typeof t=="object"){a=t}a=a||{};var s={};var l,f,u;if(t){if(t.nodeType==1){if(t.href||t.action){l=t.getAttribute("data-title")||a.title}i=a.response;f=t.getAttribute("data-container");r=document.getElementById(f);u=t.getAttribute("data-classpage");s.target=t;o=t.getAttribute("data-reload");if(t.tagName.toLowerCase()=="form"||o!==null&&o!="false"){s.reload=true}s.back=t.getAttribute("data-rel")=="back";if(t.getAttribute("data-history")=="false"){s.history=false}}else{i=t.response||a.response;l=t.title||a.title;r=t.container||a.container;u=t.classPage||a.classPage;s.target=t.target;s.back=t.back||a.back}if(r&&u){n=u}}var c=(n==u?r:document).querySelector(".in."+n);var d=null;var p=document.createElement("div");if(typeof e=="string"){p.innerHTML=e}else{p.appendChild(e)}if(q.evalScript==true&&e.firstintoBind!=true){L.call(p.getElementsByTagName("script")).forEach(function(e){var t=e.innerHTML.trim();var a=e.type||e.getAttribute("type");var i=document.getElementsByTagName("head")[0]||document.documentElement;var r=document.createElement("script");if(a){r.type=a}if(t){r.appendChild(document.createTextNode(t))}else if(e.src&&!document.querySelector('script[src="'+e.src+'"]')){r.src=e.src}setTimeout(function(){i.insertBefore(r,i.firstChild);i.removeChild(r);r=null},17);e=null})}var y=p.getElementsByTagName("title")[0];if(!(d=p.querySelector("."+n))){p.className=n+" out";d=p}if(typeof l=="string"){d.setAttribute("data-title",l)}else if(y&&y.innerText){d.setAttribute("data-title",y.innerText)}s.response=i||e;s.id=a.id||this.getCleanUrl(t)||d.id||a.url||"unique"+Date.now();if(typeof a=="object"){if(typeof a.history!="undefined"){s.history=a.history}if(typeof a.remove!="undefined"){s.remove=a.remove}if(typeof a.target!="undefined"){s.target=a.target}if(typeof a.title!="undefined"){s.title=a.title}if(typeof a.query!="undefined"){s.query=a.query}}if(n==u){s.history=false;s.classPage=n}r=r||document.body;if(s.remove==true&&s.id){this.remove(s.id);if(a.id){L.call(document.querySelectorAll('a[data-reload="'+a.id+'"]')).forEach(function(e){q.remove(e)})}}var m=s.id.split("?")[0];if(m&&T[m]&&r.contains(T[m])){r.insertBefore(d,T[m])}else{r.appendChild(d)}p=null;this.transition(d,c,s);var h=T.backShip||[];h.push({pageIn:d,pageOut:c,isBack:!!s.back});T.backShip=h};q.getFunction=function(e){if(typeof e!="string")return;var t=q.rootTransition,a=e.split(".");for(var i=0;i<a.length;i+=1){if(!(t=t[a[i]])){break}}return t};q.ajax=function(a){if(!a){return}var e=a;var t={url:"",type:"",dataType:"",data:{},timeout:1e4,success:function(){},error:function(){},complete:function(){}};var i={},r=null,n=null;var o=this.classMask;var s={},l;if(a.nodeType==1){s=B(e.getAttribute("data-params")||"");for(c in t){i[c]=e.getAttribute("data-"+c)||s[c]||t[c];if(typeof t[c]=="function"&&typeof i[c]=="string"){i[c]=this.getFunction(i[c]);if(typeof i[c]!="function"){i[c]=t[c]}}}var f=this.getCleanUrl(e,i.url);i.url=f;var u=B(f.split("?")[1]);for(var c in u){if(typeof s[c]=="undefined"){s[c]=u[c]}}i.query=s;i.target=e;i.back=e.getAttribute("data-rel")=="back";var d=e.tagName.toLowerCase();if(d=="form"){i.type=e.method;n=new FormData(e)}else if(d=="a"){var p=e.getAttribute("data-container"),y=e.getAttribute("data-classpage"),m=p&&document.getElementById(p);if(m&&y&&y!=q.classPage){i.history=false;i.title=false}var h=e.getAttribute("data-reload");if(typeof h=="string"&&h!="false"){if(h!=""&&h!="true"){L.call(document.querySelectorAll('a[data-reload="'+h+'"]')).forEach(function(e){q.remove(e)});q.remove(T[h])}else{q.remove(T[f])}}}l=a.getAttribute("data-mask");if(l=="true"||l==""){r=a.querySelector("."+o)}}else if(a.url){i=a;for(key2 in t){i[key2]=a[key2]||t[key2]}i.url=this.getCleanUrl(null,i.url,i.data);i.title=a.title;i.back=a.back;i.container=a.container;i.query=B(a.url.split("?")[1])}else{return}var g=m||document.body;if(typeof l!="string"){r=[].slice.call(g.children).filter(function(e){return e.classList.contains(o)})[0]}if(r==null){r=document.createElement("div");r.className=o;r.innerHTML='<s class="loading"></s>';if(typeof l=="string"){a.appendChild(r)}else{g.appendChild(r)}}r.style.display="inline";if(this.showLoading){this.showLoading()}var v=new XMLHttpRequest;v.open(i.type||"GET",i.url+(/\?/.test(i.url)?"&":"?")+"r="+Date.now());v.timeout=i.timeout;v.onload=function(){var t=null;if(i.dataType=="json"||i.dataType=="JSON"){try{t=JSON.parse(v.response);i.response=t;q.createPage(q.jsonHandle(t,i),a,i)}catch(e){i.message="JSON parse error:"+e.message;i.error.call(i,v,v.status)}}else if(i.dataType=="unknown"){i.history=false;try{t=JSON.parse(v.response);i.response=t;q.createPage(q.jsonHandle(t,i),a,i)}catch(e){t=v.response;q.createPage(t,a,i)}}else{t=v.response;q.createPage(t,a,i)}i.success.call(i,t,v.status)};v.onerror=function(e){i.message="Illegal request address or an unexpected network error!";i.error.call(i,v,v.status)};v.onloadend=function(){r.style.display="none";if(this.hideLoading){this.hideLoading()}i.complete.call(i,v,v.status)};v.ontimeout=function(){i.message="The request timeout!";i.error.call(i,v,v.status);r.style.display="none";if(this.hideLoading){this.hideLoading()}};v.setRequestHeader("Type","ajax");v.setRequestHeader("From","mobilebone");v.send(n)};q.submit=function(e){if(!e||typeof e.action!="string"){return}var t=e.getAttribute("data-ajax");if(t=="false"||q.captureForm==false&&t!="true")return;e.addEventListener("submit",function(e){var t=this.getAttribute("data-preventdefault");var a=q.getFunction(t);if(typeof a=="function"&&a(this)==true){e.preventDefault();return false}q.ajax(this);e.preventDefault()})};q.isBack=function(t,a){if(history.tempBack==true){history.tempBack=null;return true}if(history.tempGo==true){history.tempGo=null;return false}if(typeof t=="undefined"){return false}if(!a){return false}var e=T.backShip||[];var i=null;e.forEach(function(e){if(e.pageIn==t&&e.pageOut==a){i=e.isBack}else if(e.pageIn==a&&e.pageOut==t){i=!e.isBack}});if(i===null){i=!!(t.compareDocumentPosition(a)&Node.DOCUMENT_POSITION_FOLLOWING)}return i};q.jsonHandle=function(e,t){return'<p style="text-align:center;">Dear master, if you see me, show that JSON parsing function is undefined!</p>'},q.init=function(){if(l==true)return"Don't repeat initialization!";var e=location.hash.replace("#&","#"),t=null,a=null;var i=e.split("?")[0];var r=e.split("?")[1];var n={query:{}};if(r){n.query=B(r)}if(i==""||i=="#"){this.transition(document.querySelector("."+this.classPage))}else if(s.test(i)==true&&(t=document.querySelector(i))&&t.classList.contains(this.classPage)){n.id=e.replace(/^#/,"");this.transition(t,null,n)}else{if(e.split("container=").length==2){a=document.getElementById(e.split("container=")[1].split("&")[0])}this.ajax({url:e.replace("#",""),dataType:"unknown",container:a,error:function(){n.id=e.replace(/^#/,"");t=document.querySelector("."+q.classPage);q.transition(t,null,n)}})}document.addEventListener("click",this.handleTapEvent);var o=!!navigator.userAgent.match(/safari/i)&&!navigator.userAgent.match(/chrome/i)&&typeof document.hidden!=="undefined"&&!window.chrome;if("ontouchstart"in window==true&&o){document.addEventListener("touchmove",function(){history.popstateswipe=true});document.addEventListener("touchend",function(){history.popstateswipe=false})}l=true};q.handleTapEvent=function(e){var t=null;if(e&&e.nodeType==1){t=e;t.preventDefault=function(){}}t=(t||e.target)&&(t||e.target).closest("a");if(!t){return}var a=t.href;var i=document.querySelector(".in."+q.classPage);if(i==null){return}var r={target:t};var n=t.getAttribute("data-preventdefault")||B(t.getAttribute("data-params")||"").preventdefault;var o=q.getFunction(n);if(typeof o=="function"&&o(t)==true){e.preventDefault();return false}var s=t.getElementsByClassName(q.classMask)[0];if(s&&s.style.display!="none"){e.preventDefault();return false}var l=t.getAttribute("data-container");var f=t.getAttribute("data-classpage");var u=l&&document.getElementById(l);if(u&&f&&f!=q.classPage){i=u.querySelector(".in."+f)||u.querySelector(f);r.history=false;r.title=false;r.classPage=f}if(t.getAttribute("data-history")=="false"){r.history=false}var c=q.captureLink==true;var d=t.getAttribute("data-rel");if(!d){d="auto"}var p=false;if(d=="back"){p=true}var y;if(d=="go"){y=true}var m=d=="external";if(!a){return}var h=t.getAttribute("href");a=a.replace("#&","#");if(h.replace(/#/g,"")===""){e.preventDefault();return}if(/^javascript/.test(a)){if(p==false&&!y){return}}else{m=m||a.replace("://","").split("/")[0]!==location.href.replace("://","").split("/")[0];if((m==true||c==false)&&t.getAttribute("data-ajax")!="true"){return}}if(/^#/.test(h)==true){e.preventDefault();var g=a.split("#")[1];var v=g.split("?")[0];var b=g.split("?")[1];var k=t.getAttribute("data-formdata")||t.getAttribute("data-params");if(k){b=b+"&"+k}r.query=B(b);r.id=g;var E=v&&document.getElementById(v);if(p==false&&d=="auto"){p=q.isBack(E,i)}if(E){var A=T.lastShip;if(A&&E==A[1]&&i==A[0]&&!y){history.tempBack=true;history.back()}else{q.transition(E,i,p,r)}}}else if(/^javascript/.test(a)){if(p){history.tempBack=true;history.back()}else if(y){history.tempGo=true;history.go(1)}}else if(t.getAttribute("data-ajax")!="false"){e.preventDefault();var L=q.getCleanUrl(t);r.query=B(L.split("?")[1]);var S=t.getAttribute("data-reload");if((S==null||S=="false")&&T[L]){if(p==false&&d=="auto"){p=q.isBack(T[L],i)}r.id=L;var w=u||document.body;if(w.contains(T[L])==false){w.appendChild(T[L])}q.transition(T[L],i,p,r)}else{q.ajax(t)}}};if(!Element.prototype.matches){Element.prototype.matches=Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector}if(!Element.prototype.closest){Element.prototype.closest=function(e){var t=this;do{if(t.matches(e))return t;t=t.parentElement||t.parentNode}while(t!==null&&t.nodeType===1);return null}}var B=function(e){var a={};if(typeof e=="string"){e.split("&").forEach(function(e){var t=e.split("=");if(t.length>1){a[t[0]]=e.replace(t[0]+"=","")}})}return a};window.addEventListener("DOMContentLoaded",function(){if(l==false&&q.autoInit==true){q.init()}});window.addEventListener("popstate",function(){if(history.popstateswipe==true){location.reload();history.popstateswipe=false;return}if(history.popstate==false){history.popstate=true;return}var e=location.hash.replace("#&","#").replace(/^#/,"");var t=e.split("?")[0];var a=null;var i=document.querySelector(".in."+q.classPage);var r=null;if(e==""){a=document.querySelector("."+q.classPage);if(a.id)return}else{a=T[t]||T[e];if(e.split("container=").length==2){r=document.getElementById(e.split("container=")[1].split("&")[0])}if(a&&s.test(t)==false){q.transition(a,i,q.isBack(a,i),{id:e,history:false,container:r,target:document.activeElement,query:B(e.split("?")[1])});return}}if(!a){if(s.test(e)==false){q.ajax({url:e,dataType:"unknown",back:q.isBack(),container:r});return}a=document.querySelector("#"+t)||document.querySelector("#"+e)}if(a&&a==i||q.pushStateEnabled==false)return;if(a){q.transition(a,i,q.isBack(a,i),{id:e,history:false,target:document.activeElement,query:B(e.split("?")[1])})}});document.MBLOADED=true;return q});