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