Loading.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. let tpl = '<div class="dcat-loading d-flex items-center align-items-center justify-content-center pin" style="{style}">{svg}</div>',
  2. loading = '.dcat-loading',
  3. LOADING_SVG = [
  4. '<svg xmlns="http://www.w3.org/2000/svg" class="mx-auto block" style="width:{width};{svg_style}" viewBox="0 0 120 30" fill="{color}"><circle cx="15" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"/><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite" /></circle><circle cx="60" cy="15" r="9" fill-opacity="0.3"><animate attributeName="r" from="9" to="9" begin="0s" dur="0.8s" values="9;15;9" calcMode="linear" repeatCount="indefinite" /><animate attributeName="fill-opacity" from="0.5" to="0.5" begin="0s" dur="0.8s" values=".5;1;.5" calcMode="linear" repeatCount="indefinite" /></circle><circle cx="105" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite" /><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite" /></circle></svg>',
  5. ];
  6. class Loading {
  7. constructor(Dcat, options) {
  8. options = $.extend({
  9. container: Dcat.config.pjax_container_selector,
  10. zIndex: 100,
  11. width: '52px',
  12. color: Dcat.color.dark60,
  13. background: '#fff',
  14. style: '',
  15. svg: LOADING_SVG[0]
  16. }, options);
  17. let _this = this,
  18. defStyle = 'position:absolute;',
  19. content;
  20. _this.$container = $(options.container);
  21. content = $(
  22. tpl
  23. .replace('{svg}', options.svg)
  24. .replace('{color}', options.color)
  25. .replace('{color}', options.color)
  26. .replace('{width}', options.width)
  27. .replace('{style}', `${defStyle}background:${options.background};z-index:${options.zIndex};${options.style}`)
  28. );
  29. content.appendTo(_this.$container);
  30. }
  31. destroy() {
  32. this.$container.find(loading).remove();
  33. }
  34. }
  35. function destroyAll() {
  36. $(loading).remove();
  37. }
  38. function extend(Dcat) {
  39. // 全屏居中loading
  40. Dcat.loading = function (options) {
  41. if (options === false) {
  42. // 关闭loading
  43. return setTimeout(destroyAll, 70);
  44. }
  45. // 配置参数
  46. options = $.extend({
  47. zIndex: 999991014,
  48. width: '58px',
  49. shade: 'rgba(255, 255, 255, 0.1)',
  50. background: 'transparent',
  51. top: 200,
  52. svg: LOADING_SVG[0],
  53. }, options);
  54. var win = $(window),
  55. // 容器
  56. $container = $('<div class="dcat-loading" style="z-index:'+options.zIndex+';width:300px;position:fixed"></div>'),
  57. // 遮罩层直接沿用layer
  58. shadow = $('<div class="layui-layer-shade dcat-loading" style="z-index:'+(options.zIndex-2)+'; background-color:'+options.shade+'"></div>');
  59. $container.appendTo('body');
  60. if (options.shade) {
  61. shadow.appendTo('body');
  62. }
  63. function resize() {
  64. $container.css({
  65. left: (win.width() - 300)/2,
  66. top: (win.height() - options.top)/2
  67. });
  68. }
  69. // 自适应窗口大小
  70. win.on('resize', resize);
  71. resize();
  72. $container.loading(options);
  73. };
  74. // 给元素附加加载状态
  75. $.fn.loading = function (opt) {
  76. if (opt === false) {
  77. return $(this).find(loading).remove();
  78. }
  79. opt = opt || {};
  80. opt.container = $(this);
  81. return new Loading(Dcat, opt);
  82. };
  83. // 按钮加载状态
  84. $.fn.buttonLoading = function (start) {
  85. let $this = $(this),
  86. loadingId = $this.attr('data-loading'),
  87. content;
  88. if (start === false) {
  89. if (! loadingId) {
  90. return $this;
  91. }
  92. $this.find('.waves-ripple').remove();
  93. return $this
  94. .removeClass('disabled btn-loading waves-effect')
  95. .removeAttr('disabled')
  96. .removeAttr('data-loading')
  97. .html(
  98. $this.find('.' + loadingId).html()
  99. );
  100. }
  101. if (loadingId) {
  102. return $this;
  103. }
  104. content = $this.html();
  105. loadingId = 'ld-'+Dcat.helpers.random();
  106. let loading = `<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>`;
  107. let btnClass = ['btn', 'layui-layer-btn0', 'layui-layer-btn1'];
  108. for (let i in btnClass) {
  109. if ($this.hasClass(btnClass[i])) {
  110. loading = LOADING_SVG[0].replace('{color}', 'currentColor').replace('{width}', '50px;height:11px;');
  111. }
  112. }
  113. return $this
  114. .addClass('disabled btn-loading')
  115. .attr('disabled', true)
  116. .attr('data-loading', loadingId)
  117. .html(`
  118. <div class="${loadingId}" style="display:none">${content}</div>
  119. ${loading}
  120. `);
  121. }
  122. }
  123. export default extend