common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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='pop-in';
  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');
  37. }else{
  38. ws.close('auto');
  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. * @param {JSON} ws : Webview窗口属性
  54. */
  55. w.clicked=function(id,wa,ns,ws){
  56. if(openw){//避免多次打开同一个页面
  57. openw.show();
  58. return null;
  59. }
  60. if(w.plus){
  61. wa&&(waiting=plus.nativeUI.showWaiting());
  62. ws=ws||{};
  63. ws.scrollIndicator||(ws.scrollIndicator='none');
  64. ws.scalable||(ws.scalable=false);
  65. var pre='';//'http://192.168.1.178:8080/h5/';
  66. openw=plus.webview.create(pre+id,id,ws);
  67. ns||openw.addEventListener('loaded',function(){//页面加载完成后才显示
  68. // setTimeout(function(){//延后显示可避免低端机上动画时白屏
  69. openw.show(as);
  70. closeWaiting();
  71. // },200);
  72. },false);
  73. openw.addEventListener('close',function(){//页面关闭后可再次打开
  74. openw=null;
  75. },false);
  76. return openw;
  77. }else{
  78. w.open(id);
  79. }
  80. return null;
  81. };
  82. w.openDoc=function(t,c){
  83. var d=plus.webview.getWebviewById('document');
  84. if(d){
  85. d.evalJS('updateDoc("'+t+'","'+c+'")');
  86. }else{
  87. d=plus.webview.create('/plus/doc.html','document',{zindex:9999,popGesture:'hide'},{preate:true});
  88. d.addEventListener('loaded',function(){
  89. d.evalJS('updateDoc("'+t+'","'+c+'")');
  90. },false);
  91. }
  92. }
  93. /**
  94. * 关闭等待框
  95. */
  96. w.closeWaiting=function(){
  97. waiting&&waiting.close();
  98. waiting=null;
  99. }
  100. // 兼容性样式调整
  101. var adjust=false;
  102. function compatibleAdjust(){
  103. if(adjust||!w.plus||!domready){
  104. return;
  105. } // iOS平台使用滚动的div
  106. if('iOS'==plus.os.name){
  107. var t=document.getElementById("dcontent");
  108. t&&(t.className="sdcontent");
  109. t=document.getElementById("content");
  110. t&&(t.className="scontent");
  111. //iOS8横竖屏切换div不更新滚动问题
  112. var lasto=window.orientation;
  113. window.addEventListener("orientationchange",function(){
  114. var nowo=window.orientation;
  115. if(lasto!=nowo&&(90==nowo||-90==nowo)){
  116. dcontent&&(0==dcontent.scrollTop)&&(dcontent.scrollTop=1);
  117. content&&(0==content.scrollTop)&&(content.scrollTop=1);
  118. }
  119. lasto=nowo;
  120. },false);
  121. }
  122. adjust=true;
  123. };
  124. w.compatibleConfirm=function(){
  125. plus.nativeUI.confirm('本OS原生层面不提供该控件,需使用mui框架实现类似效果。请点击“确定”下载Hello mui示例',function(e){
  126. if(0==e.index){
  127. plus.runtime.openURL("http://www.dcloud.io/hellomui/");
  128. }
  129. },"",["确定","取消"]);
  130. }
  131. // 通用元素对象
  132. var _dout_=null,_dcontent_=null;
  133. w.gInit=function(){
  134. _dout_=document.getElementById("output");
  135. _dcontent_=document.getElementById("dcontent");
  136. };
  137. // 清空输出内容
  138. w.outClean=function(){
  139. _dout_.innerText="";
  140. _dout_.scrollTop=0;//在iOS8存在不滚动的现象
  141. };
  142. // 输出内容
  143. w.outSet=function(s){
  144. _dout_.innerText=s+"\n";
  145. (0==_dout_.scrollTop)&&(_dout_.scrollTop=1);//在iOS8存在不滚动的现象
  146. };
  147. // 输出行内容
  148. w.outLine=function(s){
  149. _dout_.innerText+=s+"\n";
  150. (0==_dout_.scrollTop)&&(_dout_.scrollTop=1);//在iOS8存在不滚动的现象
  151. };
  152. // 格式化时长字符串,格式为"HH:MM:SS"
  153. w.timeToStr=function(ts){
  154. if(isNaN(ts)){
  155. return "--:--:--";
  156. }
  157. var h=parseInt(ts/3600);
  158. var m=parseInt((ts%3600)/60);
  159. var s=parseInt(ts%60);
  160. return (ultZeroize(h)+":"+ultZeroize(m)+":"+ultZeroize(s));
  161. };
  162. // 格式化日期时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
  163. w.dateToStr=function(d){
  164. return (d.getFullYear()+"-"+ultZeroize(d.getMonth()+1)+"-"+ultZeroize(d.getDate())+" "+ultZeroize(d.getHours())+":"+ultZeroize(d.getMinutes())+":"+ultZeroize(d.getSeconds()));
  165. };
  166. /**
  167. * zeroize value with length(default is 2).
  168. * @param {Object} v
  169. * @param {Number} l
  170. * @return {String}
  171. */
  172. w.ultZeroize=function(v,l){
  173. var z="";
  174. l=l||2;
  175. v=String(v);
  176. for(var i=0;i<l-v.length;i++){
  177. z+="0";
  178. }
  179. return z+v;
  180. };
  181. })(window);