service.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var loading = require('./loading.js');
  5. var core = require('@vueuse/core');
  6. var shared = require('@vue/shared');
  7. var style = require('../../../utils/dom/style.js');
  8. let fullscreenInstance = void 0;
  9. const Loading = function(options = {}) {
  10. if (!core.isClient)
  11. return void 0;
  12. const resolved = resolveOptions(options);
  13. if (resolved.fullscreen && fullscreenInstance) {
  14. return fullscreenInstance;
  15. }
  16. const instance = loading.createLoadingComponent({
  17. ...resolved,
  18. closed: () => {
  19. var _a;
  20. (_a = resolved.closed) == null ? void 0 : _a.call(resolved);
  21. if (resolved.fullscreen)
  22. fullscreenInstance = void 0;
  23. }
  24. });
  25. addStyle(resolved, resolved.parent, instance);
  26. addClassList(resolved, resolved.parent, instance);
  27. resolved.parent.vLoadingAddClassList = () => addClassList(resolved, resolved.parent, instance);
  28. let loadingNumber = resolved.parent.getAttribute("loading-number");
  29. if (!loadingNumber) {
  30. loadingNumber = "1";
  31. } else {
  32. loadingNumber = `${Number.parseInt(loadingNumber) + 1}`;
  33. }
  34. resolved.parent.setAttribute("loading-number", loadingNumber);
  35. resolved.parent.appendChild(instance.$el);
  36. vue.nextTick(() => instance.visible.value = resolved.visible);
  37. if (resolved.fullscreen) {
  38. fullscreenInstance = instance;
  39. }
  40. return instance;
  41. };
  42. const resolveOptions = (options) => {
  43. var _a, _b, _c, _d;
  44. let target;
  45. if (shared.isString(options.target)) {
  46. target = (_a = document.querySelector(options.target)) != null ? _a : document.body;
  47. } else {
  48. target = options.target || document.body;
  49. }
  50. return {
  51. parent: target === document.body || options.body ? document.body : target,
  52. background: options.background || "",
  53. svg: options.svg || "",
  54. svgViewBox: options.svgViewBox || "",
  55. spinner: options.spinner || false,
  56. text: options.text || "",
  57. fullscreen: target === document.body && ((_b = options.fullscreen) != null ? _b : true),
  58. lock: (_c = options.lock) != null ? _c : false,
  59. customClass: options.customClass || "",
  60. visible: (_d = options.visible) != null ? _d : true,
  61. beforeClose: options.beforeClose,
  62. closed: options.closed,
  63. target
  64. };
  65. };
  66. const addStyle = async (options, parent, instance) => {
  67. const { nextZIndex } = instance.vm.zIndex || instance.vm._.exposed.zIndex;
  68. const maskStyle = {};
  69. if (options.fullscreen) {
  70. instance.originalPosition.value = style.getStyle(document.body, "position");
  71. instance.originalOverflow.value = style.getStyle(document.body, "overflow");
  72. maskStyle.zIndex = nextZIndex();
  73. } else if (options.parent === document.body) {
  74. instance.originalPosition.value = style.getStyle(document.body, "position");
  75. await vue.nextTick();
  76. for (const property of ["top", "left"]) {
  77. const scroll = property === "top" ? "scrollTop" : "scrollLeft";
  78. maskStyle[property] = `${options.target.getBoundingClientRect()[property] + document.body[scroll] + document.documentElement[scroll] - Number.parseInt(style.getStyle(document.body, `margin-${property}`), 10)}px`;
  79. }
  80. for (const property of ["height", "width"]) {
  81. maskStyle[property] = `${options.target.getBoundingClientRect()[property]}px`;
  82. }
  83. } else {
  84. instance.originalPosition.value = style.getStyle(parent, "position");
  85. }
  86. for (const [key, value] of Object.entries(maskStyle)) {
  87. instance.$el.style[key] = value;
  88. }
  89. };
  90. const addClassList = (options, parent, instance) => {
  91. const ns = instance.vm.ns || instance.vm._.exposed.ns;
  92. if (!["absolute", "fixed", "sticky"].includes(instance.originalPosition.value)) {
  93. style.addClass(parent, ns.bm("parent", "relative"));
  94. } else {
  95. style.removeClass(parent, ns.bm("parent", "relative"));
  96. }
  97. if (options.fullscreen && options.lock) {
  98. style.addClass(parent, ns.bm("parent", "hidden"));
  99. } else {
  100. style.removeClass(parent, ns.bm("parent", "hidden"));
  101. }
  102. };
  103. exports.Loading = Loading;
  104. //# sourceMappingURL=service.js.map