UNPKG

tav-ui

Version:
127 lines (124 loc) 3.72 kB
import { reactive, getCurrentInstance, ref, unref, computed, toRaw, watchEffect, nextTick } from 'vue'; import { tryOnUnmounted } from '@vueuse/core'; import { isEqual } from 'lodash-es'; import { isFunction } from '../../../utils/is2.mjs'; import { error } from '../../../utils/log2.mjs'; import { buildUUID } from '../../../utils/uuid2.mjs'; const isProdMode = () => true; const dataTransferRef = reactive({}); const visibleData = reactive({}); function useDrawer() { if (!getCurrentInstance()) { throw new Error("useDrawer() can only be used inside setup() or functional components!"); } const drawer = ref(null); const loaded = ref(false); const uid = ref(""); function register(drawerInstance, uuid) { isProdMode() && tryOnUnmounted(() => { drawer.value = null; loaded.value = null; dataTransferRef[unref(uid)] = null; }); if (unref(loaded) && isProdMode() && drawerInstance === unref(drawer)) { return; } uid.value = uuid ?? buildUUID(); drawer.value = drawerInstance; loaded.value = true; drawerInstance.emitVisible = (visible, uid2) => { visibleData[uid2] = visible; }; } const getInstance = () => { const instance = unref(drawer); if (!instance) { error("useDrawer instance is undefined!"); } return instance; }; const methods = { setDrawerProps: (props) => { getInstance()?.setDrawerProps(props); }, getVisible: computed(() => { return visibleData[~~unref(uid)]; }), openDrawer: (visible = true, data, openOnSet = true) => { getInstance()?.setDrawerProps({ visible }); if (!data) return; if (openOnSet) { dataTransferRef[unref(uid)] = null; dataTransferRef[unref(uid)] = toRaw(data); return; } const equal = isEqual(toRaw(dataTransferRef[unref(uid)]), toRaw(data)); if (!equal) { dataTransferRef[unref(uid)] = toRaw(data); } }, closeDrawer: () => { getInstance()?.setDrawerProps({ visible: false }); } }; return [register, methods]; } const useDrawerInner = (callbackFn) => { const drawerInstanceRef = ref(null); const currentInstance = getCurrentInstance(); const uidRef = ref(""); if (!getCurrentInstance()) { throw new Error("useDrawerInner() can only be used inside setup() or functional components!"); } const getInstance = () => { const instance = unref(drawerInstanceRef); if (!instance) { error("useDrawerInner instance is undefined!"); return; } return instance; }; const register = (modalInstance, uuid) => { isProdMode() && tryOnUnmounted(() => { drawerInstanceRef.value = null; }); uidRef.value = uuid ?? buildUUID(); drawerInstanceRef.value = modalInstance; currentInstance?.emit("register", modalInstance, uuid); }; watchEffect(() => { const data = dataTransferRef[unref(uidRef)]; if (!data) return; if (!callbackFn || !isFunction(callbackFn)) return; nextTick(() => { callbackFn(data); }); }); return [ register, { changeLoading: (loading = true) => { getInstance()?.setDrawerProps({ loading }); }, changeOkLoading: (loading = true) => { getInstance()?.setDrawerProps({ confirmLoading: loading }); }, getVisible: computed(() => { return visibleData[~~unref(uidRef)]; }), closeDrawer: () => { getInstance()?.setDrawerProps({ visible: false }); }, setDrawerProps: (props) => { getInstance()?.setDrawerProps(props); } } ]; }; export { useDrawer, useDrawerInner }; //# sourceMappingURL=useDrawer2.mjs.map