@vuesax-alpha/nightly
Version:
A Component Library for Vue 3
84 lines (81 loc) • 2.49 kB
JavaScript
import { ref, isRef } from 'vue';
import { isObject, isString, hyphenate } from '@vue/shared';
import { isEqual } from 'lodash-unified';
import '../../../utils/index.mjs';
import { loading } from './method.mjs';
const INSTANCE_KEY = Symbol("VsLoading");
const createInstance = (el, binding) => {
var _a;
const vm = binding.instance;
const getBindingProp = (key) => isObject(binding.value) ? binding.value[key] : void 0;
const resolveExpression = (key) => {
const data = isString(key) && (vm == null ? void 0 : vm[key]) || key;
if (data)
return ref(data);
return data;
};
const getProp = (name, def) => {
var _a2, _b;
return resolveExpression(
(_b = (_a2 = getBindingProp(name)) != null ? _a2 : el.getAttribute(`element-loading-${hyphenate(name)}`)) != null ? _b : def
);
};
const options = {
text: getProp("text"),
color: getProp("color"),
background: getProp("background"),
target: (_a = getBindingProp("target")) != null ? _a : el,
opacity: getProp("opacity"),
progress: getProp("progress"),
percent: getProp("percent"),
scale: getProp("scale"),
type: getProp("type")
};
el[INSTANCE_KEY] = {
options,
instance: loading(options)
};
};
const updateOptions = (newOptions, originalOptions) => {
for (const key of Object.keys(originalOptions)) {
if (isRef(originalOptions[key])) {
originalOptions[key].value = newOptions[key];
}
}
};
const vLoading = {
mounted(el, binding) {
if (binding.value == true || isObject(binding.value) && binding.value.visible) {
createInstance(el, binding);
}
},
updated(el, binding) {
var _a;
const instance = el[INSTANCE_KEY];
if (!isEqual(binding.value, binding.oldValue)) {
if (isObject(binding.value)) {
if (binding.value.visible) {
if (!((_a = binding.oldValue) == null ? void 0 : _a.visible)) {
createInstance(el, binding);
}
if (instance)
updateOptions(binding.value, instance.options);
} else {
instance == null ? void 0 : instance.instance.close();
}
} else {
if (binding.value) {
createInstance(el, binding);
} else {
instance == null ? void 0 : instance.instance.close();
}
}
}
},
unmounted(el) {
var _a;
(_a = el[INSTANCE_KEY]) == null ? void 0 : _a.instance.close();
}
};
export { vLoading };
//# sourceMappingURL=directive.mjs.map