2020-12-01 20:59:17 +08:00
|
|
|
import { createLoading } from '/@/components/Loading';
|
|
|
|
import type { Directive, App } from 'vue';
|
|
|
|
|
|
|
|
const loadingDirective: Directive = {
|
|
|
|
mounted(el, binding) {
|
|
|
|
const tip = el.getAttribute('loading-tip');
|
|
|
|
const background = el.getAttribute('loading-background');
|
|
|
|
const size = el.getAttribute('loading-size');
|
|
|
|
const fullscreen = !!binding.modifiers.fullscreen;
|
|
|
|
const instance = createLoading(
|
|
|
|
{
|
|
|
|
tip,
|
|
|
|
background,
|
|
|
|
size: size || 'large',
|
|
|
|
loading: !!binding.value,
|
|
|
|
absolute: !fullscreen,
|
|
|
|
},
|
2021-08-24 22:41:48 +08:00
|
|
|
fullscreen ? document.body : el,
|
2020-12-01 20:59:17 +08:00
|
|
|
);
|
|
|
|
el.instance = instance;
|
|
|
|
},
|
|
|
|
updated(el, binding) {
|
|
|
|
const instance = el.instance;
|
|
|
|
if (!instance) return;
|
|
|
|
instance.setTip(el.getAttribute('loading-tip'));
|
|
|
|
if (binding.oldValue !== binding.value) {
|
2021-12-01 22:01:50 +08:00
|
|
|
instance.setLoading?.(binding.value && !instance.loading);
|
2020-12-01 20:59:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
unmounted(el) {
|
|
|
|
el?.instance?.close();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export function setupLoadingDirective(app: App) {
|
|
|
|
app.directive('loading', loadingDirective);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default loadingDirective;
|