Loading.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. let tpl = '<div class="dcat-loading flex items-center justify-center pin" style="{style}">{svg}</div>',
  2. loading = '.dcat-loading',
  3. LOADING_SVG = [
  4. '<svg width="{width}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="lds-disk" style="background: none;"><g transform="translate(50,50)"><g ng-attr-transform="scale({{config.scale}})" transform="scale(0.5)"><circle cx="0" cy="0" r="50" ng-attr-fill="{{config.c1}}" fill="{color}"></circle><circle cx="0" ng-attr-cy="{{config.cy}}" ng-attr-r="{{config.r}}" ng-attr-fill="{{config.c2}}" cy="-35" r="15" fill="#ffffff" transform="rotate(101.708)"><animateTransform attributeName="transform" type="rotate" calcMode="linear" values="0 0 0;360 0 0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animateTransform></circle></g></g></svg>',
  5. '<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>',
  6. ];
  7. class Loading {
  8. constructor(Dcat, options) {
  9. options = $.extend({
  10. container: Dcat.config.pjax_container_selector,
  11. zIndex: 100,
  12. width: '52px',
  13. color: '#7985d0',
  14. background: '#fff',
  15. style: '',
  16. svg: LOADING_SVG[0]
  17. }, options);
  18. let _this = this,
  19. defStyle = 'position:absolute;left:10px;right:10px;',
  20. content;
  21. _this.$container = typeof options.container === 'object' ? options.container : $(options.container);
  22. content = $(
  23. tpl
  24. .replace('{svg}', options.svg)
  25. .replace('{color}', options.color)
  26. .replace('{color}', options.color)
  27. .replace('{width}', options.width)
  28. .replace('{style}', `${defStyle}background:${options.background};z-index:${options.zIndex};${options.style}`)
  29. );
  30. content.appendTo(_this.$container);
  31. }
  32. remove() {
  33. this.$container.find(loading).remove();
  34. }
  35. destroyAll() {
  36. destroyAll()
  37. }
  38. }
  39. function destroyAll() {
  40. $(loading).remove();
  41. }
  42. function extend(Dcat) {
  43. // 全屏居中loading
  44. Dcat.loading = function (options) {
  45. if (options === false) {
  46. // 关闭loading
  47. return setTimeout(destroyAll, 70);
  48. }
  49. // 配置参数
  50. options = $.extend({
  51. color: '#5c6bc6',
  52. zIndex: 999991014,
  53. width: '58px',
  54. shade: 'rgba(255, 255, 255, 0.1)',
  55. background: 'transparent',
  56. top: 200,
  57. svg: LOADING_SVG[1],
  58. }, options);
  59. var win = $(window),
  60. // 容器
  61. $container = $('<div class="dcat-loading" style="z-index:'+options.zIndex+';width:300px;position:fixed"></div>'),
  62. // 遮罩层直接沿用layer
  63. shadow = $('<div class="layui-layer-shade dcat-loading" style="z-index:'+(options.zIndex-2)+'; background-color:'+options.shade+'"></div>');
  64. $container.appendTo('body');
  65. if (options.shade) {
  66. shadow.appendTo('body');
  67. }
  68. function resize() {
  69. $container.css({
  70. left: (win.width() - 300)/2,
  71. top: (win.height() - options.top)/2
  72. });
  73. }
  74. // 自适应窗口大小
  75. win.on('resize', resize);
  76. resize();
  77. $container.loading(options);
  78. };
  79. //
  80. $.fn.loading = function (opt) {
  81. if (opt === false) {
  82. return $(this).find(loading).remove();
  83. }
  84. opt = opt || {};
  85. opt.container = $(this);
  86. return new Loading(Dcat, opt);
  87. };
  88. }
  89. export default extend