mobilebone_wev8.js
16.5 KB
!function(e,a){document.MBLOADED||("function"==typeof define&&(define.amd||define.cmd)?define("mobilebone",function(t){return a(e,t)}):e.Mobilebone=a(e,{}))}(this,function(c,D){if(document.MBLOADED)return"Don't repeat load Mobilebone!";var C={},q=[].slice,u=/^#?\w+(?:[\-_]\w+)*$/i,j="WebkitAppearance"in document.documentElement.style||void 0!==document.webkitHidden,N="pushState"in history&&"replaceState"in history;D.support=N;var d=!1;if(D.VERSION="2.6.2",D.captureLink=!0,D.captureForm=!0,D.rootTransition=c,D.mergeCallback=!0,D.classAnimation="slide",D.classPage="page",D.classMask="mask",D.pushStateEnabled=!0,D.evalScript=!1,0<=window.navigator.userAgent.indexOf("Firefox")&&window.top!==window&&(D.pushStateEnabled=!1),0==N)return D;history.popstate=!1,D.transition=function(t,e,a,c){if(0!=arguments.length){3==arguments.length&&1==isNaN(1*a)&&(a=(c=a).back),e=e||null,a=a||!1,c=c||{};var n=D.isDulicatePage(t,e);if(n&&!a){var i=D.createDuplicatePage(t,e,c.formdata);t=i[0],e=i[1]}if(t!=e){var r={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},u=function(a){if(!a||!a.getAttribute)return{};var n={},i=M(a.getAttribute("data-params")||"");return["title","root","form"].forEach(function(t){n[t]=a.getAttribute("data-"+t)||i[t]||c[t]||r[t]}),"string"==typeof n.root&&(n.root=D.getFunction(n.root)),["onpagefirstinto","callback","fallback","animationstart","animationend","preventdefault"].forEach(function(t){if(1==D.mergeCallback&&"function"==typeof r[t]){var e=a.getAttribute("data-"+t)||i[t];"function"==typeof n.root[e]?n[t]=function(){r[t].apply(this,arguments),n.root[e].apply(this,arguments)}:"function"==typeof c[t]?n[t]=function(){r[t].apply(this,arguments),c[t].apply(this,arguments)}:n[t]=r[t]}else n[t]=a.getAttribute("data-"+t)||i[t]||c[t]||r[t]}),n},o=u(e),d=u(t);if(null!=e&&e.classList){var s=o.preventdefault,l=!1;"string"==typeof s&&(s=o.root[s])}if(null!=t&&t.classList){var f=d.preventdefault,p=!1;"string"==typeof f&&(f=d.root[f])}if("function"==typeof s&&(l=s.call(o.root,t,e,c)),1==l&&s===f)return!1;if("function"==typeof f&&(p=f.call(d.root,t,e,c)),1==p)return!1;var m=function(s,l){1!=s.flagAniBind&&["animationstart","animationend"].forEach(function(t,e){var a=d[t]||s[t],n="webkit"+t.replace(/^a|s|e/g,function(t){return t.toUpperCase()}),i=j?n:t;if(e&&!s._hide){s._hide=!0,s.addEventListener(i,function(){0==this.classList.contains("in")&&(this.style.display="none",1==this.removeSelf&&(this.parentElement.removeChild(this),this.removeSelf=null)),this.classList.remove(u(this).form)})}var r=null,o="_"+i;"string"==typeof a&&d.root[a]?r=function(){l.root[a].call(l.root,this,this.classList.contains("in")?"into":"out",c)}:"function"==typeof a&&(r=function(){a.call(l.root,this,this.classList.contains("in")?"into":"out",c)}),s[o]&&s.removeEventListener(i,s[o]),r&&(s.addEventListener(i,r),s[o]=r),s.flagAniBind=!0})},y=c.reverse,h=a||y?"add":"remove";if(null!=e&&e.classList&&1!=l){e.offsetWidth=e.offsetWidth,e.style.display="block",e.classList.add("out"),e.classList.remove("in"),e.classList[h]("reverse"),e.classList.add(o.form),e.removeSelf=e.removeSelf||n&&O(e.id)&&a,m(e,o);var g=o.fallback;"string"==typeof g&&(g=o.root[g]),"function"==typeof g&&g.call(o.root,t,e,c)}if(null!=t&&t.classList){var v=d.title,b=document.querySelector("h1"),k=document.querySelector("."+this.classPage);v&&!1!==c.title?(document.title=v,b&&(b.innerHTML=v,b.title=v)):k==t&&!e&&document.title&&t.setAttribute("data-title",document.title);var A=t.id||c.id,E=c.id||t.id,L=C["_"+A];!1!==c.remove&&C[A]&&C[A]!=t&&(L&&C[L]&&1==c.reload&&(delete C[L],delete C["_"+A]),C[A]!=e?C[A].parentElement&&C[A].parentElement.removeChild(C[A]):e.removeSelf=!0,delete C[A]),t.offsetWidth=t.offsetWidth,t.style.display="block",t.classList.remove("out"),t.classList.add("in"),t.classList[h]("reverse"),e&&t.classList.add(d.form);var S=d.onpagefirstinto;(!t.firstintoBind||c.reload&&!a)&&("string"==typeof S&&d.root[S]?d.root[S].call(d.root,t,e,c):"function"==typeof S&&S.call(d.root,t,e,c),q.call(t.querySelectorAll("form")).forEach(function(t){D.submit(t)}),t.firstintoBind=!0),m(t,d);var w,T=E;if(T&&0==/^#/.test(T)&&(T="#"+T),w=T.replace(/^#/,"#&"),N&&this.pushStateEnabled&&!1!==c.history&&T&&w!=location.hash){history.popstate=!1;var B=c.formdata?"?"+c.formdata:"",P=e&&"true"!=e.getAttribute("data-replaceState");history[e&&!c.replaceState&&P?"pushState":"replaceState"](null,document.title,T.replace(/^#/,"#&")+B)}C[A]||(C[A]=t,E!==A&&(C[E]=t,C["_"+A]=E));var x=d.callback;"string"==typeof x&&(x=d.root[x]),"function"==typeof x&&x.call(d.root,t,e,c,a),setTimeout(function(){history.popstate=!0},17)}}}},D.getCleanUrl=function(t,e,a){var n="",i="",r="";if(t)if(1==t.nodeType)if(t.action){if(n=t.getAttribute("action"),t.method&&"POST"==t.method.toUpperCase())return n;window.$&&$.fn&&$.fn.serialize?i=$(t).serialize():(i={},q.call(t.querySelectorAll("input,select,textarea")).forEach(function(t){if(t.name&&!t.disabled){var e=t.value.trim(),a=t.name;/^radio|checkbox/i.test(t.type)?t.checked&&(i[a]?i[a].push(e):i[a]=[e]):i[a]=[e]}}))}else{n=t.getAttribute("href"),i=t.getAttribute("data-formdata")||t.getAttribute("data-data")||"";var o="container",s=t.getAttribute("data-"+o);if(-1==i.indexOf(o)&&s){var l=o+"="+s;i=i?i+"&"+l:l}}else t.url&&(n=t.url,i=t.data);if(!(n=n||e))return"";if("object"==typeof(i=i||a||"")){var c=[];for(key in i)i[key].forEach||(i[key]=[i[key]]),i[key].forEach(function(t){c.push(key+"="+encodeURIComponent(t))});i=0<c.length?c.join("&"):""}return"?"==(r=n.split("#")[0].replace(/&+$/,"")).slice(-1)&&(r=r.split("?")[0]),""!=i&&(/\?/.test(r)?r=r+"&"+(i=i.replace(/^&|\?/,"")):""!=i&&(r=r+"?"+(i=i.replace("?","")))),r},D.createPage=function(t,e,a){var n=null,i=null,r=this.classPage,o=null;if(t){void 0===a&&"object"==typeof e&&(a=e),a=a||{};var s,l,c,u={};e&&(1==e.nodeType?((e.href||e.action)&&(s=e.getAttribute("data-title")||a.title),n=a.response,l=e.getAttribute("data-container"),i=document.getElementById(l),c=e.getAttribute("data-classpage"),o=(u.target=e).getAttribute("data-reload"),("form"==e.tagName.toLowerCase()||null!==o&&"false"!=o)&&(u.reload=!0),u.back="back"==e.getAttribute("data-rel"),"false"==e.getAttribute("data-history")&&(u.history=!1)):(n=e.response||a.response,s=e.title||a.title,i=e.container||a.container,c=e.classPage||a.classPage,u.target=e.target,"history"in e&&(u.history=e.history),u.back=e.back||a.back),i&&c&&(r=c));var d=(r==c?i:document).querySelector(".in."+r),f=null,p=document.createElement("div");"string"==typeof t?p.innerHTML=t:p.appendChild(t),1==D.evalScript&&1!=t.firstintoBind&&q.call(p.getElementsByTagName("script")).forEach(function(t){var e=t.innerHTML.trim(),a=t.getAttribute("type");if(""!=e.trim()&&!t.src){var n=document.getElementsByTagName("head")[0]||document.documentElement,i=document.createElement("script");a&&(i.type=a),i.appendChild(document.createTextNode(e)),setTimeout(function(){n.insertBefore(i,n.firstChild),n.removeChild(i),i=null},17),t=null}});var m=p.getElementsByTagName("title")[0];if((f=p.querySelector("."+r))||(p.className=r+" out",f=p),"string"==typeof s?f.setAttribute("data-title",s):m&&m.innerText&&f.setAttribute("data-title",m.innerText),u.response=n||t,u.id=this.getCleanUrl(e)||f.id||"unique"+Date.now(),~u.id.indexOf("?")){var y=u.id.split("?");u.id=y[0],u.formdata=y[1]}"object"==typeof a&&(void 0!==a.history&&(u.history=a.history),void 0!==a.remove&&(u.remove=a.remove),void 0!==a.target&&(u.target=a.target),void 0!==a.title&&(u.title=a.title)),r==c&&(u.history=!1,u.classPage=r),i=i||document.body;var h=f.id;h&&C[h]&&i.contains(C[h])?i.insertBefore(f,C[h]):i.appendChild(f),p=null,this.transition(f,d,u)}};var f=function(t){return(t||"").replace(/_sub.*$/g,"")},O=function(t){return!!t&&f(t)!==t};D.isDulicatePage=function(t,e){return!(!t||!e)&&(t==e||f(t.id)===f(e.id))},D.createDuplicatePage=function(a,n,i){if(!D.isDulicatePage(a,n)||!i)return[a,n];var r=f(a.id),t=[].slice.call(document.querySelectorAll("body > [id^="+r+"]")),o=t.length,s=t.map(function(t){return t.id}),l=function(t,e){return~s.indexOf(t+e)?l(t,e+1):t+e};if(1===o){var e=new RegExp("#&"+r+"(&|\\?)?","g");n.formdata=location.hash.replace(e,"")}return t.every(function(t,e){return t.formdata===i?(a=t,!1):(e===o-1&&((a=n.cloneNode(!0)).id=l(r+"_sub",e),a.classList.remove("in"),a.classList.add("out"),a.removeAttribute("style"),a.formdata=i,document.body.appendChild(a)),!0)}),[a,n]},D.getFunction=function(t){if("string"==typeof t){for(var e=c,a=t.split("."),n=0;n<a.length&&(e=e[a[n]]);n+=1);return e}},D.ajax=function(a){if(a){var t,e={url:"",type:"",dataType:"",data:{},timeout:1e4,async:!0,username:"",password:"",success:function(){},error:function(){},complete:function(){}},n={},i=null,r=null,o={};if(1==a.nodeType){for(key in o=M(a.getAttribute("data-params")||""),e)n[key]=a.getAttribute("data-"+key)||o[key]||e[key],"function"==typeof e[key]&&"string"==typeof n[key]&&(n[key]=this.getFunction(n[key]),"function"!=typeof n[key]&&(n[key]=e[key]));n.url=this.getCleanUrl(a,n.url),n.target=a,n.back="back"==a.getAttribute("data-rel");var s=a.tagName.toLowerCase();if("form"==s)n.type=a.method,r=new FormData(a);else if("a"==s){var l=a.getAttribute("data-container"),c=a.getAttribute("data-classpage"),u=l&&document.getElementById(l);u&&c&&c!=D.classPage&&(n.history=!1,n.title=!1)}"true"!=(t=a.getAttribute("data-mask"))&&""!=t||(i=a.querySelector("."+this.classMask))}else{if(!a.url)return;for(key2 in e)n[key2]=a[key2]||e[key2];n.url=this.getCleanUrl(null,n.url,n.data),n.title=a.title,n.back=a.back,n.container=a.container}var d=u||document.body;"string"!=typeof t&&(i=d.querySelector("."+this.classMask)),null==i&&((i=document.createElement("div")).className=this.classMask,i.innerHTML='<i class="loading"></i>',"string"==typeof t?a.appendChild(i):d.appendChild(i)),i.style.display="block";var f=new XMLHttpRequest;f.open(n.type||"GET",n.url+(/\?/.test(n.url)?"&":"?")+"r="+Date.now(),n.async,n.username,n.password),f.timeout=n.timeout,f.onload=function(){var e=null;if(200==f.status){if("json"==n.dataType||"JSON"==n.dataType)try{e=JSON.parse(f.response),n.response=e,setTimeout(function(){D.createPage(D.jsonHandle(e),a,n)},20)}catch(t){n.message="JSON parse error:"+t.message,n.error.call(n,f,f.status)}else if("unknown"==n.dataType){n.history=!1;try{e=JSON.parse(f.response),n.response=e,setTimeout(function(){D.createPage(D.jsonHandle(e),a,n)},20)}catch(t){e=f.response,setTimeout(function(){D.createPage(e,a,n)},20)}}else e=f.response,setTimeout(function(){D.createPage(e,a,n)},20);n.success.call(n,e,f.status)}else n.message="The status code exception!",n.error.call(n,f,f.status);n.complete.call(n,f,f.status),i.style.display="none"},f.onerror=function(t){n.message="Illegal request address or an unexpected network error!",n.error.call(n,f,f.status),i.style.display="none"},f.ontimeout=function(){n.message="The request timeout!",n.error.call(n,f,f.status),i.style.display="none"},f.setRequestHeader("Type","ajax"),f.setRequestHeader("From","mobilebone"),f.send(r)}},D.submit=function(t){if(t&&"string"==typeof t.action){var e=t.getAttribute("data-ajax");"false"==e||0==D.captureForm&&"true"!=e||t.addEventListener("submit",function(t){var e=this.getAttribute("data-preventdefault"),a=D.getFunction(e);if("function"==typeof a&&1==a(this))return t.preventDefault(),!1;D.ajax(this),t.preventDefault()})}},D.isBack=function(t,e){return 1==history.tempBack?!(history.tempBack=null):void 0===t||!!e},D.jsonHandle=function(t){return'<p style="text-align:center;">Dear master, if you see me, show that JSON parsing function is undefined!</p>'},D.init=function(t,e){if(1==d)return"Don't repeat initialization!";var a=location.hash.replace("#&","#"),n=null,i=null,r=null;if(e=e||{},~a.indexOf("?")&&(a=(r=a.split("?"))[0],e.formdata=r[1]),""==a||"#"==a){var o=t&&"#"+t||"."+this.classPage;this.transition(document.querySelector(o),null,e)}else 1==u.test(a)&&(n=document.querySelector(f(a)))&&n.classList.contains(this.classPage)?(O(a)&&((n=this.createDuplicatePage(n,n,e.formdata)[0]).formdata=e.formdata,n.id=a.replace("#","")),this.transition(n,null,e)):(2==a.split("container=").length&&(i=document.getElementById(a.split("container=")[1].split("&")[0])),this.ajax({url:location.hash.replace("#&","#").replace("#",""),dataType:"unknown",container:i,error:function(){n=document.querySelector("."+D.classPage),D.transition(n)}}));var s=c.$||c.jQuery||c.Zepto;s&&s.fn&&s.fn.tap&&"ontouchstart"in window==1?(s(document).tap(this.handleTapEvent),document.addEventListener("click",function(t){var e=t.target;if(e&&("a"==e.tagName.toLowerCase()||(e=e.getParentElementByTag("a")))){var a=e.getAttribute("data-ajax"),n=e.href;"external"==e.getAttribute("data-rel")||"false"==a||n.replace("://","").split("/")[0]!==location.href.replace("://","").split("/")[0]&&"true"!=a||0==D.captureLink&&"true"!=a?/^http/i.test(n)&&(location.href=n):t.preventDefault()}})):document.addEventListener("click",this.handleTapEvent);var l=!!navigator.userAgent.match(/safari/i)&&!navigator.userAgent.match(/chrome/i)&&void 0!==document.hidden&&!window.chrome;"ontouchstart"in window==1&&l&&(document.addEventListener("touchmove",function(){history.popstateswipe=!0}),document.addEventListener("touchend",function(){history.popstateswipe=!1})),d=!0},D.handleTapEvent=function(t){var e,a=null;t&&1==t.nodeType&&((a=t).preventDefault=function(){}),(e=(a=a||t.target||t.touches[0]).href)&&0!=/a/i.test(a.tagName)||!(a=a.getParentElementByTag("a"))||(e=a.href);var n=document.querySelector(".in."+D.classPage);if(null!=n&&a){var i={target:a},r=a.getAttribute("data-preventdefault")||M(a.getAttribute("data-params")||"").preventdefault,o=D.getFunction(r);if("function"==typeof o&&1==o(a))return t.preventDefault(),!1;var s=a.getAttribute("data-formdata");s&&(i.formdata=s);var l=a.getElementsByClassName(D.classMask)[0];if(l&&"none"!=l.style.display)return t.preventDefault(),!1;var c=a.getAttribute("data-container"),u=a.getAttribute("data-classpage"),d=c&&document.getElementById(c);d&&u&&u!=D.classPage&&(n=d.querySelector(".in."+u)||d.querySelector(u),i.history=!1,i.title=!1,i.classPage=u);var f=1==D.captureLink,p=a.getAttribute("data-rel"),m=!1;"back"==p&&(m=!0);var y="external"==p;if(e)if(e=e.replace("#&","#"),""!==a.getAttribute("href").replace(/#/g,"")){if(/^javascript/.test(e)){if(0==m)return}else if((1==(y=y||e.replace("://","").split("/")[0]!==location.href.replace("://","").split("/")[0])||0==f)&&"true"!=a.getAttribute("data-ajax"))return;if(1==/^#/.test(a.getAttribute("href"))){var h,g,v=e.split("#")[1];v&&~v.indexOf("?")&&(v=(g=v.split("?"))[0],i.formdata=g[1]),h=v&&document.getElementById(v),0==m&&"auto"==p&&(m=D.isBack(h,n)),h&&(i.reload="true"==a.getAttribute("data-reload"),D.transition(h,n,m,i)),t.preventDefault()}else if(/^javascript/.test(e))history.tempBack=!0,history.back();else if("false"!=a.getAttribute("data-ajax")){var b=D.getCleanUrl(a).split("?")[0],k=a.getAttribute("data-reload");a.getAttribute("href");if(null!=k&&"false"!=k||!C[b])D.ajax(a);else{0==m&&"auto"==p&&(m=D.isBack(C[b],n)),i.id=b;var A=d||document.body;0==A.contains(C[b])&&A.appendChild(C[b]),D.transition(C[b],n,m,i)}t.preventDefault()}}else t.preventDefault()}},Element.prototype.getParentElementByTag=function(e){if(!e)return null;var a=null,n=this,i=function(){if(!(n=n.parentElement))return null;var t=n.tagName.toLowerCase();t===e?a=n:"body"==t?a=null:i()};return i(),a};var M=function(t){var a={};return"string"==typeof t&&t.split("&").forEach(function(t){var e=t.split("=");1<e.length&&(a[e[0]]=t.replace(e[0]+"=",""))}),a};return window.addEventListener("popstate",function(){if(1==history.popstateswipe)return location.reload(),void(history.popstateswipe=!1);if(0!=history.popstate){var t,e=location.hash.replace("#&","").replace(/^#/,""),a=null,n=null,i="";if(~e.indexOf("?")&&(e=(t=e.split("?"))[0],i=t[1]),""==e){if((a=document.querySelector("."+D.classPage)).id)return}else{if(a=C[e],O(e)&&(!a||!document.getElementById(a.id))){var r=document.getElementById(f(e));(a=D.createDuplicatePage(r,r,i)[0]).formdata=i,a.id=e.replace("#","")}if(2==e.split("container=").length&&(n=document.getElementById(e.split("container=")[1].split("&")[0])),a&&0==u.test(e))return void D.transition(a,(n||document).querySelector(".in."+D.classPage),!0,{id:e,history:!1,container:n,formdata:i})}if(!a){if(0==u.test(e))return void D.ajax({url:location.hash.replace("#&","").replace(/^#/,""),dataType:"unknown",back:D.isBack(),container:n});a=document.querySelector("#"+e)}var o=document.querySelector(".in."+D.classPage);a&&a==o||0==D.pushStateEnabled||a&&D.transition(a,o,D.isBack(a,o),{id:e,history:!1,remove:!1,formdata:i})}else history.popstate=!0}),document.MBLOADED=!0,D});