common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. (function(w){
  2. // 空函数
  3. function shield(){
  4. return false;
  5. }
  6. document.addEventListener('touchstart',shield,false);//取消浏览器的所有事件,使得active的样式在手机上正常生效
  7. document.oncontextmenu=shield;//屏蔽选择函数
  8. // H5 plus事件处理
  9. var ws=null,as='slide-in-right',at=200;
  10. function plusReady(){
  11. ws=plus.webview.currentWebview();
  12. // Android处理返回键
  13. // plus.key.addEventListener('backbutton',function(){
  14. // back();
  15. // },false);
  16. compatibleAdjust();
  17. }
  18. if(w.plus){
  19. plusReady();
  20. }else{
  21. document.addEventListener('plusready',plusReady,false);
  22. }
  23. // DOMContentLoaded事件处理
  24. var domready=false;
  25. document.addEventListener('DOMContentLoaded',function(){
  26. domready=true;
  27. gInit();
  28. document.body.onselectstart=shield;
  29. compatibleAdjust();
  30. },false);
  31. // 处理返回事件
  32. w.back=function(hide){
  33. if(w.plus){
  34. ws||(ws=plus.webview.currentWebview());
  35. if(hide||ws.preate){
  36. ws.hide('auto',at);
  37. }else{
  38. ws.close('auto',at);
  39. }
  40. }else if(history.length>1){
  41. history.back();
  42. }else{
  43. w.close();
  44. }
  45. };
  46. // 处理点击事件
  47. var openw=null,waiting=null;
  48. /**
  49. * 打开新窗口
  50. * @param {URIString} id : 要打开页面url
  51. * @param {boolean} wa : 是否显示等待框
  52. * @param {boolean} ns : 是否不自动显示
  53. */
  54. w.clicked=function(id,wa,ns){
  55. if(openw){//避免多次打开同一个页面
  56. return null;
  57. }
  58. if(w.plus){
  59. wa&&(waiting=plus.nativeUI.showWaiting());
  60. var pre='';//'http://192.168.1.178:8080/h5/';
  61. openw=plus.webview.create(pre+id,id,{scrollIndicator:'none',scalable:false});
  62. ns||openw.addEventListener('loaded',function(){//页面加载完成后才显示
  63. // setTimeout(function(){//延后显示可避免低端机上动画时白屏
  64. openw.show(as,at);
  65. closeWaiting();
  66. // },200);
  67. },false);
  68. openw.addEventListener('close',function(){//页面关闭后可再次打开
  69. openw=null;
  70. },false);
  71. return openw;
  72. }else{
  73. w.open(id);
  74. }
  75. return null;
  76. };
  77. w.openDoc=function(t,c){
  78. var d=plus.webview.getWebviewById('document');
  79. if(d){
  80. d.evalJS('updateDoc("'+t+'","'+c+'")');
  81. }else{
  82. d=plus.webview.create('/plus/doc.html','document',{zindex:9999,popGesture:'hide'},{preate:true});
  83. d.addEventListener('loaded',function(){
  84. d.evalJS('updateDoc("'+t+'","'+c+'")');
  85. },false);
  86. }
  87. // d.show(as,at);
  88. }
  89. /**
  90. * 关闭等待框
  91. */
  92. w.closeWaiting=function(){
  93. waiting&&waiting.close();
  94. waiting=null;
  95. }
  96. // 兼容性样式调整
  97. var adjust=false;
  98. function compatibleAdjust(){
  99. if(adjust||!w.plus||!domready){
  100. return;
  101. } // iOS平台使用滚动的div
  102. if('iOS'==plus.os.name){
  103. as='pop-in';
  104. at=300;
  105. var t=document.getElementById("dcontent");
  106. t&&(t.className="sdcontent");
  107. t=document.getElementById("content");
  108. t&&(t.className="scontent");
  109. //iOS8横竖屏切换div不更新滚动问题
  110. var lasto=window.orientation;
  111. window.addEventListener("orientationchange",function(){
  112. var nowo=window.orientation;
  113. if(lasto!=nowo&&(90==nowo||-90==nowo)){
  114. dcontent&&(0==dcontent.scrollTop)&&(dcontent.scrollTop=1);
  115. content&&(0==content.scrollTop)&&(content.scrollTop=1);
  116. }
  117. lasto=nowo;
  118. },false);
  119. }
  120. adjust=true;
  121. };
  122. w.compatibleConfirm=function(){
  123. plus.nativeUI.confirm('本OS原生层面不提供该控件,需使用mui框架实现类似效果。请点击“确定”下载Hello mui示例',function(e){
  124. if(0==e.index){
  125. plus.runtime.openURL("http://www.dcloud.io/hellomui/");
  126. }
  127. },"",["确定","取消"]);
  128. }
  129. // 通用元素对象
  130. var _dout_=null,_dcontent_=null;
  131. w.gInit=function(){
  132. _dout_=document.getElementById("output");
  133. _dcontent_=document.getElementById("dcontent");
  134. };
  135. // 清空输出内容
  136. w.outClean=function(){
  137. _dout_.innerHTML="";
  138. _dout_.scrollTop=0;//在iOS8存在不滚动的现象
  139. };
  140. // 输出内容
  141. w.outSet=function(s){
  142. _dout_.innerHTML=s+"<br/>";
  143. (0==_dout_.scrollTop)&&(_dout_.scrollTop=1);//在iOS8存在不滚动的现象
  144. };
  145. // 输出行内容
  146. w.outLine=function(s){
  147. _dout_.innerHTML+=s+"<br/>";
  148. (0==_dout_.scrollTop)&&(_dout_.scrollTop=1);//在iOS8存在不滚动的现象
  149. };
  150. // 格式化时长字符串,格式为"HH:MM:SS"
  151. w.timeToStr=function(ts){
  152. if(isNaN(ts)){
  153. return "--:--:--";
  154. }
  155. var h=parseInt(ts/3600);
  156. var m=parseInt((ts%3600)/60);
  157. var s=parseInt(ts%60);
  158. return (ultZeroize(h)+":"+ultZeroize(m)+":"+ultZeroize(s));
  159. };
  160. // 格式化日期时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
  161. w.dateToStr=function(d){
  162. return (d.getFullYear()+"-"+ultZeroize(d.getMonth()+1)+"-"+ultZeroize(d.getDate())+" "+ultZeroize(d.getHours())+":"+ultZeroize(d.getMinutes())+":"+ultZeroize(d.getSeconds()));
  163. };
  164. /**
  165. * zeroize value with length(default is 2).
  166. * @param {Object} v
  167. * @param {Number} l
  168. * @return {String}
  169. */
  170. w.ultZeroize=function(v,l){
  171. var z="";
  172. l=l||2;
  173. v=String(v);
  174. for(var i=0;i<l-v.length;i++){
  175. z+="0";
  176. }
  177. return z+v;
  178. };
  179. })(window);