UNPKG

@fe6/water-pro

Version:

An enterprise-class UI design language and Vue-based implementation

93 lines (76 loc) 2.55 kB
/** @format */ import { isEqual } from 'lodash-es'; import { ref, onUnmounted, unref, reactive, toRaw, computed } from 'vue'; import { isProdMode } from '../../../_util/env'; import warning from '../../../_util/warning'; var dataTransferRef = reactive({}); var visibleData = reactive({}); /** * @description: Applicable to independent modal and call outside */ export function useModal() { var modalRef = ref(null); var loadedRef = ref(false); var uidRef = ref(''); function register(modalMethod) { var uuid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; uidRef.value = uuid; isProdMode() && onUnmounted(function () { modalRef.value = null; loadedRef.value = false; dataTransferRef[unref(uidRef)] = null; }); if (unref(loadedRef) && isProdMode() && modalMethod === unref(modalRef)) { return; } modalRef.value = modalMethod; modalMethod.emitVisible = function (visible, uid) { visibleData[uid] = visible; }; } var getInstance = function getInstance() { var instance = unref(modalRef); if (!instance) { warning('useModal instance is undefined!'); } return instance; }; var methods = { setModalProps: function setModalProps(props) { var _a; (_a = getInstance()) === null || _a === void 0 ? void 0 : _a.setModalProps(props); }, getVisible: computed(function () { return visibleData[~~unref(uidRef)]; }), redoModalHeight: function redoModalHeight() { var _a, _b; (_b = (_a = getInstance()) === null || _a === void 0 ? void 0 : _a.redoModalHeight) === null || _b === void 0 ? void 0 : _b.call(_a); }, openModal: function openModal() { var visible = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var data = arguments.length > 1 ? arguments[1] : undefined; var openOnSet = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var _a; (_a = getInstance()) === null || _a === void 0 ? void 0 : _a.setModalProps({ visible: visible }); if (!data) { return; } if (openOnSet) { dataTransferRef[unref(uidRef)] = null; dataTransferRef[unref(uidRef)] = toRaw(data); return; } var equal = isEqual(toRaw(dataTransferRef[unref(uidRef)]), toRaw(data)); if (!equal) { dataTransferRef[unref(uidRef)] = toRaw(data); } } }; return { register: register, methods: methods }; }