TPMmodal.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. function tpm_alert(msg,callback,title){
  2. title=title||'系统信息';
  3. var $modal=$('<div><div class="tpm_modal_head"><h3>'+title+'</h3></div><div class="tpm_modal_body">'+msg+'</div><div class="tpm_modal_foot"><button class="tpm_modal_ok" type="button">确认</button></div></div>');
  4. $modal.find('.tpm_modal_foot>button').on('click',function(){
  5. tpm_close_float_box();
  6. });
  7. var id=Modernizr.mq("(max-width:767px)")?'tpm_modal_phone':'tpm_modal';
  8. tpm_show_float_box($modal,id);
  9. if($.isFunction(callback)){
  10. $('#'+id).on('end',function(){
  11. callback();
  12. $('#'+id).off('end');
  13. });
  14. }
  15. }
  16. function tpm_info(msg,callback){
  17. var id=Modernizr.mq("(max-width:767px)")?'tpm_info_phone':'tpm_info';
  18. if(0==$('#'+id).size()){
  19. $('<div id="'+id+'"></div>').appendTo('body').on('webkitTransitionEnd oTransitionEnd otransitionend transitionend',function(){
  20. if(!$(this).is('.in')){
  21. $(this).hide();
  22. if($.isFunction(callback)) callback();
  23. }
  24. });
  25. }
  26. //显示
  27. $('#'+id).show();
  28. $('#'+id).html(msg);
  29. $('#'+id).offset();//强制回流
  30. $('#'+id).addClass('in');
  31. //3秒后隐藏
  32. setTimeout(function(){
  33. $('#'+id).removeClass('in');
  34. if(!Modernizr.csstransitions){
  35. $('#'+id).hide();
  36. if($.isFunction(callback)) callback();
  37. }
  38. },1000)
  39. }
  40. function tpm_confirm(msg,callback,title){
  41. title=title||'请确认';
  42. var $modal=$('<div><div class="tpm_modal_head"><h3>'+title+'</h3></div><div class="tpm_modal_body">'+msg+'</div><div class="tpm_modal_foot"><button class="tpm_modal_cancel" type="button">取消</button><button class="tpm_modal_ok" type="button">确认</button></div></div>');
  43. var id=Modernizr.mq("(max-width:767px)")?'tpm_modal_phone':'tpm_modal';
  44. $modal.find('.tpm_modal_foot>button').on('click',function(){
  45. if($(this).is('.tpm_modal_ok')){
  46. $('#'+id).on('end',function(){
  47. if($.isFunction(callback)) callback();
  48. $('#'+id).off('end');
  49. })
  50. }
  51. tpm_close_float_box();
  52. });
  53. tpm_show_float_box($modal,id);
  54. }
  55. function tpm_popurl(url,callback,title){
  56. var text='<div><div class="tpm_modal_head"><h3>'+title+'</h3> <a class="tpm_modal_close" href="javascript:tpm_close_float_box();">×</a></div><div id="tpm_modal_body" class="tpm_modal_body">loading</div></div>';
  57. tpm_show_float_box(text,'tpm_modal');
  58. if($.isFunction(callback)){
  59. $('#tpm_modal').on('end',function(){
  60. callback();
  61. $('#tpm_modal').off('end');
  62. });
  63. }
  64. //增加随机数,防止缓存
  65. url+=-1==url.indexOf('?')?'?tpm_r='+Math.random():'&tpm_r='+Math.random();
  66. //判断是否为http开头。
  67. if('http'==url.substr(0,4).toLowerCase()){
  68. $(window).off('message.tpm');
  69. //以http开头用iframe显示。
  70. $(window).on('message.tpm',function(e){
  71. if('tpm_close_float_box'==e.originalEvent.data){
  72. tpm_close_float_box();
  73. }
  74. });
  75. $('#tpm_modal_body').html('<iframe src="'+url+'" id="tpm_modal_iframe" name="tpm_modal_iframe"></iframe>');
  76. }else{
  77. //判断是否加载tpm类库。
  78. if(!window.TPM){
  79. $('#tpm_modal_body').load(url);
  80. }else{
  81. window.TPMshowAjaxWait=false;
  82. TPM.http(url,'tpm_modal_body');
  83. }
  84. }
  85. }
  86. function tpm_show_float_box(text,id){
  87. tpm_show_backdrop();
  88. //创建modal层
  89. if(0==$('#'+id).size()){
  90. $('<div id="'+id+'"></div>').appendTo('body').on('webkitTransitionEnd oTransitionEnd otransitionend transitionend',function(){
  91. if(!$(this).is('.in')){
  92. $(this).trigger('end');
  93. $(this).hide();
  94. }
  95. });
  96. }
  97. $('#'+id).empty();
  98. //加入弹出框内容
  99. $(text).appendTo('#'+id);
  100. //显示modal层
  101. $('#'+id).show()
  102. $('#'+id).offset();
  103. //添加modal层in样式
  104. $('#'+id).addClass('in');
  105. }
  106. function tpm_close_float_box(){
  107. //如果iframe中发送postMessage给父窗口
  108. if(parent!=window){
  109. parent.postMessage('tpm_close_float_box','*');
  110. return ;
  111. }
  112. tpm_hide_backdrop();
  113. //删除modal层in样式
  114. $('#tpm_modal,#tpm_modal_phone').removeClass('in');
  115. if(!Modernizr.csstransitions){
  116. $('#tpm_modal,#tpm_modal_phone').hide();
  117. $('#tpm_modal,#tpm_modal_phone').trigger('end');
  118. }
  119. }
  120. //显示笼罩层
  121. function tpm_show_backdrop(){
  122. if(0==$('#tpm_backdrop').size()){
  123. $('<div id="tpm_backdrop"></div>').appendTo('body').on('webkitTransitionEnd oTransitionEnd otransitionend transitionend',function(){
  124. if(!$(this).is('.in')) $(this).hide();
  125. });
  126. }
  127. $('#tpm_backdrop').show();
  128. $('#tpm_backdrop').offset();//强制回流
  129. $('#tpm_backdrop').addClass('in');
  130. }
  131. //隐藏笼罩层
  132. function tpm_hide_backdrop(){
  133. $('#tpm_backdrop').removeClass('in');
  134. if(!Modernizr.csstransitions) $('#tpm_backdrop').hide();
  135. }