mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-11-06 13:09:50 +08:00
47 lines
1020 B
TypeScript
47 lines
1020 B
TypeScript
import { unwrapInjected } from "../utils";
|
|
import {
|
|
inject,
|
|
computed,
|
|
watchEffect,
|
|
type Ref,
|
|
type InjectionKey
|
|
} from "vue-demi";
|
|
import { EChartsType } from "../types";
|
|
|
|
export const LOADING_OPTIONS_KEY =
|
|
"ecLoadingOptions" as unknown as InjectionKey<
|
|
UnknownRecord | Ref<UnknownRecord>
|
|
>;
|
|
|
|
type UnknownRecord = Record<string, unknown>;
|
|
|
|
export function useLoading(
|
|
chart: Ref<EChartsType | undefined>,
|
|
loading: Ref<boolean>,
|
|
loadingOptions: Ref<UnknownRecord | 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
|
|
};
|