You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

3 months ago
  1. import { isRef, ref } from 'vue';
  2. import { Loading } from './service.mjs';
  3. import { isObject, hyphenate, isString } from '@vue/shared';
  4. const INSTANCE_KEY = Symbol("ElLoading");
  5. const createInstance = (el, binding) => {
  6. var _a, _b, _c, _d;
  7. const vm = binding.instance;
  8. const getBindingProp = (key) => isObject(binding.value) ? binding.value[key] : void 0;
  9. const resolveExpression = (key) => {
  10. const data = isString(key) && (vm == null ? void 0 : vm[key]) || key;
  11. if (data)
  12. return ref(data);
  13. else
  14. return data;
  15. };
  16. const getProp = (name) => resolveExpression(getBindingProp(name) || el.getAttribute(`element-loading-${hyphenate(name)}`));
  17. const fullscreen = (_a = getBindingProp("fullscreen")) != null ? _a : binding.modifiers.fullscreen;
  18. const options = {
  19. text: getProp("text"),
  20. svg: getProp("svg"),
  21. svgViewBox: getProp("svgViewBox"),
  22. spinner: getProp("spinner"),
  23. background: getProp("background"),
  24. customClass: getProp("customClass"),
  25. fullscreen,
  26. target: (_b = getBindingProp("target")) != null ? _b : fullscreen ? void 0 : el,
  27. body: (_c = getBindingProp("body")) != null ? _c : binding.modifiers.body,
  28. lock: (_d = getBindingProp("lock")) != null ? _d : binding.modifiers.lock
  29. };
  30. el[INSTANCE_KEY] = {
  31. options,
  32. instance: Loading(options)
  33. };
  34. };
  35. const updateOptions = (newOptions, originalOptions) => {
  36. for (const key of Object.keys(originalOptions)) {
  37. if (isRef(originalOptions[key]))
  38. originalOptions[key].value = newOptions[key];
  39. }
  40. };
  41. const vLoading = {
  42. mounted(el, binding) {
  43. if (binding.value) {
  44. createInstance(el, binding);
  45. }
  46. },
  47. updated(el, binding) {
  48. const instance = el[INSTANCE_KEY];
  49. if (binding.oldValue !== binding.value) {
  50. if (binding.value && !binding.oldValue) {
  51. createInstance(el, binding);
  52. } else if (binding.value && binding.oldValue) {
  53. if (isObject(binding.value))
  54. updateOptions(binding.value, instance.options);
  55. } else {
  56. instance == null ? void 0 : instance.instance.close();
  57. }
  58. }
  59. },
  60. unmounted(el) {
  61. var _a;
  62. (_a = el[INSTANCE_KEY]) == null ? void 0 : _a.instance.close();
  63. el[INSTANCE_KEY] = null;
  64. }
  65. };
  66. export { vLoading };
  67. //# sourceMappingURL=directive.mjs.map