loading.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Created with Sublime Text 3.
  3. * license: http://www.lovewebgames.com/jsmodule/index.html
  4. * github: https://github.com/tianxiangbing/loading
  5. * User: 田想兵
  6. * Date: 2015-08-05
  7. * Time: 11:27:55
  8. * Contact: 55342775@qq.com
  9. * desc:请尽量使用github上的代码,会修复一些问题,关注https://github.com/tianxiangbing/loading
  10. */
  11. ;
  12. (function (root, factory) {
  13. //amd
  14. if (typeof define === 'function' && define.amd) {
  15. define(['jquery'], factory);
  16. } else if (typeof exports === 'object') { //umd
  17. module.exports = factory($);
  18. } else {
  19. root.Loading = factory(window.Zepto || window.jQuery || $);
  20. }
  21. })(window, function ($) {
  22. var Loading = function () { };
  23. Loading.prototype = {
  24. loadingTpl: '<div class="ui-loading"><div class="ui-loading-mask"></div><i></i></div>',
  25. stop: function () {
  26. this.loading.remove();
  27. this.loading = null;
  28. },
  29. start: function () {
  30. var _this = this;
  31. var loading = this.loading;
  32. if (!loading) {
  33. loading = $(_this.loadingTpl);
  34. $('body').append(loading);
  35. }
  36. this.loading = loading;
  37. //console.log(cw,ch)
  38. this.setPosition();
  39. },
  40. setPosition: function () {
  41. var _this = this;
  42. var loading = this.loading;
  43. var target = _this.target;
  44. var content = $(target);
  45. var ch = $(content).outerHeight();
  46. var cw = $(content).outerWidth();
  47. if ($(target)[0].tagName == "HTML") {
  48. ch = Math.max($(target).height(), $(window).height());
  49. cw = Math.max($(target).width(), $(window).width());
  50. }
  51. loading.height(ch).width(cw);
  52. loading.find('div').height(ch).width(cw);
  53. if (ch < 100) {
  54. loading.find('i').height(ch).width(ch);
  55. }
  56. var offset = $(content).offset();
  57. loading.css({
  58. top: offset.top,
  59. left: offset.left
  60. });
  61. var icon = loading.find('i');
  62. var h = ch,
  63. w = cw,
  64. top = 0,
  65. left = 0;
  66. if ($(target)[0].tagName == "HTML") {
  67. h = $(window).height();
  68. w = $(window).width();
  69. top = (h - icon.height()) / 2 + $(window).scrollTop();
  70. left = (w - icon.width()) / 2 + $(window).scrollLeft();
  71. } else {
  72. top = (h - icon.height()) / 2;
  73. left = (w - icon.width()) / 2;
  74. }
  75. icon.css({
  76. top: top,
  77. left: left
  78. })
  79. },
  80. init: function (settings) {
  81. settings = settings || {};
  82. this.loadingTpl = settings.loadingTpl || this.loadingTpl;
  83. this.target = settings.target || 'html';
  84. this.bindEvent();
  85. },
  86. bindEvent: function () {
  87. var _this = this;
  88. $(this.target).on('stop', function () {
  89. _this.stop();
  90. });
  91. $(window).on('resize', function () {
  92. _this.loading && _this.setPosition();
  93. });
  94. }
  95. }
  96. return Loading;
  97. });
  98. function loadingxmsl(xmslid,reload){
  99. var load = new Loading();
  100. load.init({
  101. target: this.document.documentElement
  102. });
  103. var start=false;
  104. var od=setInterval(function() {
  105. $.ajax({
  106. url:"/wd/js/loading/loadXmsl.jsp",
  107. data:{"xmslid":xmslid},
  108. type:'post',
  109. async:false,
  110. success:function(data){
  111. data=JSON.parse(data.trim());
  112. if(data.status=='none'){
  113. clearInterval(od);
  114. if(start&&reload){
  115. load.stop();
  116. window.location.reload();
  117. }
  118. }else{
  119. if(!start){
  120. load.start();
  121. start=true;
  122. }else{
  123. }
  124. var msg=$(".ui-loading>div.msg");
  125. if(msg.length==0){
  126. msg=$("<div class='msg' align='center' style='background:white;position:absolute;bottom:2em;left:0;right:0'>");
  127. $(".ui-loading").append(msg);
  128. }
  129. msg.html("正在:<strong>"+data.desc+"</strong>");
  130. }
  131. }
  132. })
  133. }, 1000);
  134. }
  135. function loadXmslStatus(xmslid){
  136. loadingxmsl(xmslid,true)
  137. $("form[name='xmslframe']").each(function(){
  138. var fr=this;
  139. fr.addEventListener("submit",function(){
  140. loadingxmsl(xmslid);
  141. });
  142. })
  143. }