UNPKG

yanzhen-design-vue

Version:

183 lines (182 loc) 5.75 kB
import { ref, isReadonly, toRaw, computed } from "vue"; import { isArray, isEmptyValue } from "@yanzhen-libs/utils"; import { unref } from "@yanzhen-libs/utils-vue"; import { merge, isPlainObject, flattenDeep } from "lodash-es"; import "../constants/index.mjs"; import { handleDataType } from "../utils/handleDataType.mjs"; import "../enum/index.mjs"; import { useContactsOptions } from "./use-contacts-options.mjs"; import { useSelectedIds } from "./use-contacts-common-props.mjs"; import { contactsDataTypes } from "../constants/contacts.mjs"; import { getContactsLabel } from "../enum/contacts.mjs"; const contactsDataCache = ref({}); function useContactsMap(_el) { return contactsDataCache; } function handleContactsData(target, source, iteratee) { if (!isArray(source)) source = []; if (!isArray(target)) target = []; if (isReadonly(source)) source = toRaw(source); for (let item of source) { if (isReadonly(item)) item = toRaw(item); if (typeof iteratee === "function") { item = iteratee(item); } else if (typeof iteratee === "string") { item.type = iteratee; } item = merge(unref(contactsDataCache)[item.value] ?? {}, item); unref(contactsDataCache)[item.value] = item; target.push(item); } return target; } const OPTION_CONTEXT_KEY = Symbol("option"); function isContactsData(item) { return (item == null ? void 0 : item[OPTION_CONTEXT_KEY]) === true; } function getSelectedValue(item, config = {}) { var _a; const { contactsMap = useContactsMap(), optionsMap, localMap } = config; if (isContactsData(item)) return item; const id = isPlainObject(item) ? item == null ? void 0 : item.value : item; for (const map of [contactsMap, localMap, optionsMap]) { const info = (_a = unref(map)) == null ? void 0 : _a[id]; if (isPlainObject(info)) { if (isContactsData(item)) return item; return new Proxy(info, { get(target, p, receiver) { var _a2; if (p === OPTION_CONTEXT_KEY) { return true; } return Reflect.get(((_a2 = unref(optionsMap)) == null ? void 0 : _a2[id]) ?? {}, p, receiver) ?? Reflect.get(target, p, receiver); }, set(target, p, newValue, receiver) { if (p === OPTION_CONTEXT_KEY) { return false; } return Reflect.set(target, p, newValue, receiver); } }); } } return { value: id, label: id }; } function useContactsSelectedGroup(selectedValue) { const _contactsMap = useContactsMap(); if (!selectedValue) selectedValue = useSelectedIds(); const _selectedValue = computed(() => { return unref(selectedValue).map((value) => { const contactsMap = unref(_contactsMap); return getSelectedValue(value, { contactsMap }); }); }); function getSelectedGroupItem(key, values = unref(selectedValue)) { const groups = [...contactsDataTypes]; const children = () => { return unref(values).reduce( (prev, value) => { const item = getSelectedValue(value); const flag = key === void 0 ? !groups.includes(handleDataType(item)) : handleDataType(item) == key; if (item && flag) { prev.push(item); } return prev; }, [] ); }; const proxy = key === void 0 ? { value: "-1", label: "未知" } : { value: key, label: getContactsLabel(key) }; return new Proxy( { ...proxy, children: [] }, { get(target, p, receiver) { if (p === "children") { target["children"] = children(); } return Reflect.get(target, p, receiver); } } ); } const selectedGroup = computed(() => { const groups = [...contactsDataTypes]; return [void 0, ...groups].map((key) => getSelectedGroupItem(key)); }); const selectedGroupMap = [...contactsDataTypes].reduce( (prev, key) => { prev[key] = computed(() => getSelectedGroupItem(key)); return prev; }, {} ); return { groups: [...contactsDataTypes], selectedValue: _selectedValue, getSelectedGroupItem, selectedGroup, selectedGroupMap }; } function useContactsSelectedData(el, emit) { const localRef = ref([]); const _localMap = ref({}); const _contactsMap = useContactsMap(); const _selectedIds = useSelectedIds(); const { optionsMap: _optionsMap } = useContactsOptions(); function getSelectedValues(selectedIds = unref(_selectedIds)) { return selectedIds.reduce( (prev, id) => { if (isEmptyValue(id)) return prev; const item = getSelectedValue(id, { contactsMap: _contactsMap, optionsMap: _optionsMap, localMap: _localMap }); if (item) prev.push(item); return prev; }, [] ); } function setSelectedValues(items = []) { const optionsMap = unref(_optionsMap); const contactsMap = unref(_contactsMap); localRef.value = flattenDeep([items]).map((id) => { const info = (contactsMap == null ? void 0 : contactsMap[id]) ?? (optionsMap == null ? void 0 : optionsMap[id]); switch (true) { case isPlainObject(info): unref(_localMap)[info.value] = info; return info; } return id; }); emit == null ? void 0 : emit("update:selectedValue", unref(localRef)); } const methods = { getSelectedValues, setSelectedValues }; const selectedValue = computed({ get: () => getSelectedValues(), set: setSelectedValues }); return { ...methods, selectedValue }; } export { handleContactsData, useContactsMap, useContactsSelectedData, useContactsSelectedGroup };