2022-01-08 12:30:01 +01:00
|
|
|
import { reactive, watch } from 'vue';
|
|
|
|
import { throttle } from 'throttle-debounce';
|
|
|
|
import { Form, GetFormResultType } from '@/scripts/form';
|
|
|
|
import * as os from '@/os';
|
|
|
|
|
|
|
|
export type Widget<P extends Record<string, unknown>> = {
|
|
|
|
id: string;
|
|
|
|
data: Partial<P>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WidgetComponentProps<P extends Record<string, unknown>> = {
|
|
|
|
widget?: Widget<P>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WidgetComponentEmits<P extends Record<string, unknown>> = {
|
2022-05-25 09:43:12 +02:00
|
|
|
(ev: 'updateProps', props: P);
|
2022-01-08 12:30:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export type WidgetComponentExpose = {
|
|
|
|
name: string;
|
|
|
|
id: string | null;
|
|
|
|
configure: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useWidgetPropsManager = <F extends Form & Record<string, { default: any; }>>(
|
|
|
|
name: string,
|
|
|
|
propsDef: F,
|
|
|
|
props: Readonly<WidgetComponentProps<GetFormResultType<F>>>,
|
|
|
|
emit: WidgetComponentEmits<GetFormResultType<F>>,
|
|
|
|
): {
|
|
|
|
widgetProps: GetFormResultType<F>;
|
|
|
|
save: () => void;
|
|
|
|
configure: () => void;
|
|
|
|
} => {
|
|
|
|
const widgetProps = reactive(props.widget ? JSON.parse(JSON.stringify(props.widget.data)) : {});
|
|
|
|
|
|
|
|
const mergeProps = () => {
|
|
|
|
for (const prop of Object.keys(propsDef)) {
|
|
|
|
if (widgetProps.hasOwnProperty(prop)) continue;
|
|
|
|
widgetProps[prop] = propsDef[prop].default;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
watch(widgetProps, () => {
|
|
|
|
mergeProps();
|
|
|
|
}, { deep: true, immediate: true, });
|
|
|
|
|
|
|
|
const save = throttle(3000, () => {
|
2022-06-10 07:36:55 +02:00
|
|
|
emit('updateProps', widgetProps);
|
2022-01-08 12:30:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const configure = async () => {
|
|
|
|
const form = JSON.parse(JSON.stringify(propsDef));
|
|
|
|
for (const item of Object.keys(form)) {
|
|
|
|
form[item].default = widgetProps[item];
|
|
|
|
}
|
|
|
|
const { canceled, result } = await os.form(name, form);
|
|
|
|
if (canceled) return;
|
|
|
|
|
|
|
|
for (const key of Object.keys(result)) {
|
|
|
|
widgetProps[key] = result[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
save();
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
widgetProps,
|
|
|
|
save,
|
|
|
|
configure,
|
|
|
|
};
|
|
|
|
};
|