UNPKG

yanzhen-design-vue

Version:

425 lines (424 loc) 15.3 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const utils = require("@yanzhen-libs/utils"); const utilsVue = require("@yanzhen-libs/utils-vue"); const lodashEs = require("lodash-es"); require("../enum/index.cjs"); const listMode = require("../constants/list-mode.cjs"); const user$1 = require("../constants/user.cjs"); const usually = require("../constants/usually.cjs"); const contacts$2 = require("../constants/contacts.cjs"); const contacts = require("../contacts.cjs"); const handleDataType = require("../utils/handleDataType.cjs"); const createContactsDefaultRef = require("../enum/createContactsDefaultRef.cjs"); const useContactsAjax = require("./use-contacts-ajax.cjs"); const useContactsSelectedData = require("./use-contacts-selected-data.cjs"); const useContactsCommomProps = require("./use-contacts-commom-props.cjs"); const useContactsLoading = require("./use-contacts-loading.cjs"); const contacts$1 = require("../enum/contacts.cjs"); const user = require("../enum/user.cjs"); const { ns } = contacts.contactsOptions; const useContactsConfig = (props) => { const watcher = utilsVue.createWatcher(); const { getSearchList, getUsuallyList, getUserList, getOutOrgList, getRoleList, getOrganizationList, getGroupList } = useContactsAjax.useContactsApi(props); const handleCurrentPageNo = (total = 0, size = 100) => { return Math.ceil(total / size); }; const _contactsMap = useContactsSelectedData.useContactsMap(); const _showTabKeys = useContactsCommomProps.useContactsShowTabKeys(); const allTabs = contacts$1.getContactsTypesMaps(); const _contactsTabs = vue.computed(() => { const showTabKeys = vue.unref(_showTabKeys); switch (true) { case utils.isEmptyArray(showTabKeys): case (showTabKeys.includes("value") && showTabKeys.length === 1): return allTabs; } return allTabs.filter(({ value }) => showTabKeys.includes(value)); }); const _userTabs = vue.computed(() => { const showTabKeys = vue.unref(_showTabKeys); const showUserTabKeys = showTabKeys.filter((k) => { if (contacts$2.Contacts.USER === k) return true; return useContactsCommomProps.showUserTabKeyPropValues.includes(k); }); if (showUserTabKeys.length > 0) { const tabs = user.getUserTypesMaps(); const userTabKey = tabs.filter( ({ value }) => showTabKeys.includes(`${value}-${contacts$2.Contacts.USER}`) ); if (userTabKey.length > 0) { return userTabKey; } return tabs; } return []; }); const _activeTab = vue.ref(contacts$2.Contacts.USUALLY); const _userActiveTab = vue.ref(user$1.User.INTERNAL); watcher.set("_contactsTabs", { source: () => vue.unref(_contactsTabs).map((tab) => tab.value), handler(values) { if (utils.isEmptyArray(values)) return; const activeTab = vue.unref(_activeTab); if (!values.includes(activeTab)) { _activeTab.value = values[0]; } }, deep: true, debounce: 300, immediate: true }); watcher.set("_userTabs", { source: () => vue.unref(_userTabs).map((tab) => tab.value), handler(values) { const activeTab = vue.unref(_activeTab); const userActiveTab = vue.unref(_userActiveTab); switch (true) { case activeTab === contacts$2.Contacts.USER: if (activeTab) { if (utils.isEmptyArray(values)) return; if (!values.includes(userActiveTab)) { _userActiveTab.value = values[0]; } } break; } }, deep: true, debounce: 300, immediate: true }); const dataSource = contacts$1.useContactsDefaultRefs(); const _query = vue.ref({ keywords: "", loading: false }); const searchQueryList = createContactsDefaultRef.createContactsDefaultRef(); async function handleSearchList() { const { keywords } = vue.unref(_query); if (utils.isEmptyValue(keywords)) return; vue.unref(_query).loading = true; try { const result = await getSearchList({ data: { keywords, showTabKeys: vue.unref(_showTabKeys) } }); if (result.code === 0) { searchQueryList.value = useContactsSelectedData.handleContactsData([], result.data, (item) => { item.type = handleDataType.handleDataType(item); item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); } } catch { } vue.unref(_query).loading = false; } const handleDebounceSearchList = lodashEs.debounce(handleSearchList, 500); async function handleSearch(debounce2 = false) { if (debounce2 !== true) { await handleDebounceSearchList(); return; } await handleSearchList(); } const _isTreeMode = vue.computed(() => { const activeTab = vue.unref(_activeTab); const userActiveTab = vue.unref(_userActiveTab); return contacts$1.getContactsListMode(activeTab, userActiveTab) === listMode.ListMode.TREE; }); const { loading, more } = useContactsLoading.useContactsLoading({ activeTab: _activeTab, userActiveTab: _userActiveTab }); const _pageSize = vue.computed(() => { return props.pageSize; }); const _dataSource = vue.computed(() => { const activeTab = vue.unref(_activeTab); const userActiveTab = vue.unref(_userActiveTab); if (!activeTab) return []; switch (activeTab) { case contacts$2.Contacts.USER: return vue.unref(dataSource[activeTab][userActiveTab]); default: return vue.unref(dataSource[activeTab]); } }); const _usuallyQuery = vue.computed(() => { let type = props.usuallyUserRequestType ?? usually.Usually.ALL; if (utils.isTrue(props.needOutUser)) { type = usually.Usually.OUTSIDE; } return { type, onlyOutsideUser: type === usually.Usually.OUTSIDE }; }); const handleUsuallyList = async (options) => { if (vue.unref(loading) || vue.unref(more)) return; const { ...query } = vue.unref(_usuallyQuery); loading.value = true; const result = await getUsuallyList({ data: query }); if ((result == null ? void 0 : result.code) === 0) { const ref2 = dataSource.usually; if (!lodashEs.isArray(vue.unref(ref2))) ref2.value = []; const data = useContactsSelectedData.handleContactsData(vue.unref(ref2), result.data, (item) => { item.type = contacts$2.Contacts.USER; item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); if (data.length > 0) { } } more.value = true; }; const _userQuery = vue.computed(() => { const mode = vue.unref(_userActiveTab); const list = vue.unref(dataSource.user[mode]) ?? []; const pageSize = vue.unref(_pageSize); return { mode, pageSize, pageNo: handleCurrentPageNo(list.length, pageSize) + 1 }; }); const handleUserList = async (options) => { var _a, _b; const dataRef = (_a = options == null ? void 0 : options.parent) == null ? void 0 : _a.dataRef; const noMore = !dataRef ? vue.unref(more) : !utils.isEmptyArray(dataRef.children); if (vue.unref(loading) || noMore) return; const { ...query } = vue.unref(_userQuery); const pageSize = vue.unref(_pageSize); if (options == null ? void 0 : options.parent) { query.pageNo = handleCurrentPageNo((_b = dataRef.children) == null ? void 0 : _b.length, pageSize) + 1; } loading.value = true; const result = await getUserList({ data: { ...query, ...options } }); if ((result == null ? void 0 : result.code) === 0) { if (options == null ? void 0 : options.parent) { dataRef.children = useContactsSelectedData.handleContactsData([], result.data, (item) => { item.type = handleDataType.handleDataType(item); item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); dataRef.loading = false; dataRef.isLeaf = dataRef.children.length === 0; } else { const userActiveTab = vue.unref(_userActiveTab); const ref2 = dataSource.user[userActiveTab]; if (!lodashEs.isArray(vue.unref(ref2))) ref2.value = []; const data = useContactsSelectedData.handleContactsData(vue.unref(ref2), result.data, (item) => { item.type = handleDataType.handleDataType(item); item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); if (data.length >= result.total) { more.value = true; } } } else { more.value = true; } }; const _outOrgQuery = vue.computed(() => { const list = vue.unref(dataSource.outorg) ?? []; const pageSize = vue.unref(_pageSize); return { pageNo: handleCurrentPageNo(list.length, pageSize) + 1, pageSize }; }); const handleOutOrgList = async (options) => { if (vue.unref(loading) || vue.unref(more)) return; const { ...query } = vue.unref(_outOrgQuery); loading.value = true; const result = await getOutOrgList({ data: query }); if ((result == null ? void 0 : result.code) === 0) { const ref2 = dataSource.outorg; if (!lodashEs.isArray(vue.unref(ref2))) ref2.value = []; const data = useContactsSelectedData.handleContactsData(vue.unref(ref2), result.data, (item) => { item.type = contacts$2.Contacts.OUTORG; return item; }); if (data.length >= result.total) { more.value = true; } } else { more.value = true; } }; const _roleQuery = vue.computed(() => { const list = vue.unref(dataSource.role) ?? []; const pageSize = vue.unref(_pageSize); return { pageNo: handleCurrentPageNo(list.length, pageSize) + 1, pageSize }; }); const handleRoleList = async (options) => { if (vue.unref(loading) || vue.unref(more)) return; const { ...query } = vue.unref(_roleQuery); loading.value = true; const result = await getRoleList({ data: query }); if ((result == null ? void 0 : result.code) === 0) { const ref2 = dataSource.role; if (!lodashEs.isArray(vue.unref(ref2))) ref2.value = []; const data = useContactsSelectedData.handleContactsData(vue.unref(ref2), result.data, (item) => { item.type = contacts$2.Contacts.ROLE; item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); if (data.length >= result.total) { more.value = true; } } else { more.value = true; } }; const _organizationQuery = vue.computed(() => { const list = vue.unref(dataSource.organization) ?? []; const pageSize = vue.unref(_pageSize); return { pageNo: handleCurrentPageNo(list.length, pageSize) + 1, pageSize }; }); const handleOrganizationList = async (options) => { var _a, _b; const dataRef = (_a = options == null ? void 0 : options.parent) == null ? void 0 : _a.dataRef; const noMore = !dataRef ? vue.unref(more) : !utils.isEmptyArray(dataRef.children); if (vue.unref(loading) || noMore) return; const query = vue.unref(_organizationQuery); const pageSize = vue.unref(_pageSize); if (options == null ? void 0 : options.parent) { query.pageNo = handleCurrentPageNo((_b = dataRef.children) == null ? void 0 : _b.length, pageSize) + 1; } loading.value = true; const result = await getOrganizationList({ data: { ...query, ...options } }); if ((result == null ? void 0 : result.code) === 0) { const ref2 = dataSource.organization; if (!(options == null ? void 0 : options.parent) && !lodashEs.isArray(vue.unref(ref2))) { ref2.value = []; } const data = useContactsSelectedData.handleContactsData((options == null ? void 0 : options.parent) ? [] : vue.unref(ref2), result.data, (item) => { item.type = contacts$2.Contacts.ORGANIZATION; item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); if (options == null ? void 0 : options.parent) { dataRef.children = data; dataRef.loading = false; dataRef.isLeaf = data.length === 0; } else if (data.length >= result.total) { more.value = true; } } else { more.value = true; } }; const _groupQuery = vue.computed(() => { const list = vue.unref(dataSource.group) ?? []; const pageSize = vue.unref(_pageSize); return { pageNo: handleCurrentPageNo(list.length, pageSize) + 1, pageSize }; }); const handleGroupList = async (options) => { var _a, _b; const dataRef = (_a = options == null ? void 0 : options.parent) == null ? void 0 : _a.dataRef; const noMore = !dataRef ? vue.unref(more) : !utils.isEmptyArray(dataRef.children); if (vue.unref(loading) || noMore) return; const query = vue.unref(_groupQuery); const pageSize = vue.unref(_pageSize); if (options == null ? void 0 : options.parent) { query.pageNo = handleCurrentPageNo((_b = dataRef.children) == null ? void 0 : _b.length, pageSize) + 1; } loading.value = true; const result = await getGroupList({ data: { ...query, ...options } }); if ((result == null ? void 0 : result.code) === 0) { const ref2 = dataSource.group; if (!(options == null ? void 0 : options.parent) && !lodashEs.isArray(vue.unref(ref2))) { ref2.value = []; } const data = useContactsSelectedData.handleContactsData((options == null ? void 0 : options.parent) ? [] : vue.unref(ref2), result.data, (item) => { item.type = contacts$2.Contacts.GROUP; item.class = item.isLeaf ? ns.is("leaf") : ""; return item; }); if (options == null ? void 0 : options.parent) { dataRef.children = data; dataRef.loading = false; dataRef.isLeaf = data.length === 0; } else if (data.length >= result.total) { more.value = true; } } else { more.value = true; } }; const handleLoadMore = async (options) => { if ((options == null ? void 0 : options.force) !== false) { loading.value = false; } if (vue.unref(loading)) return; const activeTab = vue.unref(_activeTab); try { switch (activeTab) { case contacts$2.Contacts.USUALLY: await handleUsuallyList(options); break; case contacts$2.Contacts.USER: await handleUserList(options); break; case contacts$2.Contacts.OUTORG: await handleOutOrgList(options); break; case contacts$2.Contacts.ROLE: await handleRoleList(options); break; case contacts$2.Contacts.ORGANIZATION: await handleOrganizationList(options); break; case contacts$2.Contacts.GROUP: await handleGroupList(options); break; } } catch { } loading.value = false; }; vue.onUnmounted(() => { watcher.stop(); }); return { _dataSource, dataSource, searchQueryList, _loading: loading, _noMore: more, _query, _contactsTabs, _activeTab, _userTabs, _userActiveTab, _isTreeMode, _contactsMap, handleLoadMore, handleSearch }; }; exports.useContactsConfig = useContactsConfig;