Loading.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 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 = $(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. destory() {
  33. this.$container.find(loading).remove();
  34. }
  35. }
  36. function destroyAll() {
  37. $(loading).remove();
  38. }
  39. function extend(Dcat) {
  40. // 全屏居中loading
  41. Dcat.loading = function (options) {
  42. if (options === false) {
  43. // 关闭loading
  44. return setTimeout(destroyAll, 70);
  45. }
  46. // 配置参数
  47. options = $.extend({
  48. color: '#5c6bc6',
  49. zIndex: 999991014,
  50. width: '58px',
  51. shade: 'rgba(255, 255, 255, 0.1)',
  52. background: 'transparent',
  53. top: 200,
  54. svg: LOADING_SVG[1],
  55. }, options);
  56. var win = $(window),
  57. // 容器
  58. $container = $('<div class="dcat-loading" style="z-index:'+options.zIndex+';width:300px;position:fixed"></div>'),
  59. // 遮罩层直接沿用layer
  60. shadow = $('<div class="layui-layer-shade dcat-loading" style="z-index:'+(options.zIndex-2)+'; background-color:'+options.shade+'"></div>');
  61. $container.appendTo('body');
  62. if (options.shade) {
  63. shadow.appendTo('body');
  64. }
  65. function resize() {
  66. $container.css({
  67. left: (win.width() - 300)/2,
  68. top: (win.height() - options.top)/2
  69. });
  70. }
  71. // 自适应窗口大小
  72. win.on('resize', resize);
  73. resize();
  74. $container.loading(options);
  75. };
  76. //
  77. $.fn.loading = function (opt) {
  78. if (opt === false) {
  79. return $(this).find(loading).remove();
  80. }
  81. opt = opt || {};
  82. opt.container = $(this);
  83. return new Loading(Dcat, opt);
  84. };
  85. }
  86. export default extend