Files
Justineo e8697382a1 feat: add options for autoresize
* add `throttle: number` to specify throttle delay (#419 #562)
* add `onResize: () => void` to specify a resize callback (#714)
2023-06-13 20:35:45 +08:00

46 lines
1.0 KiB
TypeScript

import { unwrapInjected } from "../utils";
import {
inject,
computed,
watchEffect,
type Ref,
type InjectionKey,
type PropType
} from "vue-demi";
import type { EChartsType, LoadingOptions } from "../types";
export const LOADING_OPTIONS_KEY =
"ecLoadingOptions" as unknown as InjectionKey<
LoadingOptions | Ref<LoadingOptions>
>;
export function useLoading(
chart: Ref<EChartsType | undefined>,
loading: Ref<boolean>,
loadingOptions: Ref<LoadingOptions | undefined>
): void {
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
const realLoadingOptions = computed(() => ({
...unwrapInjected(defaultLoadingOptions, {}),
...loadingOptions?.value
}));
watchEffect(() => {
const instance = chart.value;
if (!instance) {
return;
}
if (loading.value) {
instance.showLoading(realLoadingOptions.value);
} else {
instance.hideLoading();
}
});
}
export const loadingProps = {
loading: Boolean,
loadingOptions: Object as PropType<LoadingOptions>
};