yanzhen-design-vue
Version:
311 lines (310 loc) • 10.8 kB
JavaScript
;
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");
const contacts$1 = require("./constants/contacts.cjs");
require("./enum/index.cjs");
const useContactsConfig = require("./hook/use-contacts-config.cjs");
const useContactsCommonProps = require("./hook/use-contacts-common-props.cjs");
const useContactsSelectedData = require("./hook/use-contacts-selected-data.cjs");
const useContactsModelValue = require("./hook/use-contacts-model-value.cjs");
const useContactsOptions = require("./hook/use-contacts-options.cjs");
const handleTreeNode = require("./utils/handleTreeNode.cjs");
const handleActiveTabKey = require("./utils/handleActiveTabKey.cjs");
const handleDataType = require("./utils/handleDataType.cjs");
const contacts = require("./enum/contacts.cjs");
const useContacts = (props, emit) => {
const { disableIds, multiple, title, allExpand } = vue.toRefs(props);
const {
_loading,
_query,
_contactsTabs,
_activeTab,
_userTabs,
_userActiveTab,
_isTreeMode,
_noMore,
_dataSource,
dataSource,
searchQueryList,
handleLoadMore,
handleSearch
} = useContactsConfig.useContactsConfig(props);
const slots = vue.useSlots();
const _ref = vue.ref();
const { value: _modelValue, ...vModels } = useContactsModelValue.useContactsVModels(null, emit);
const modelValue = vue.useModel(props, "value");
const _rightElRef = vue.ref();
const _leftElRef = vue.ref();
const _selectedIds = useContactsCommonProps.useSelectedIds();
const { selectedValue: _selectedValue } = useContactsSelectedData.useContactsSelectedData(null, emit);
const { selectedGroupMap } = useContactsSelectedData.useContactsSelectedGroup(_selectedIds);
const { optionsDisabledIds } = useContactsOptions.useContactsOptions();
const _disabledIds = vue.computed(() => {
const ids = [utilsVue.unref(optionsDisabledIds), utilsVue.unref(disableIds)];
return lodashEs.flattenDeep(ids);
});
const _placeholder = vue.computed(() => {
const labels = [];
for (const { value: activeTab } of utilsVue.unref(_contactsTabs)) {
let label;
switch (activeTab) {
case contacts$1.Contacts.USUALLY:
case contacts$1.Contacts.USER:
label = contacts.getContactsLabel(contacts$1.Contacts.USER);
break;
default:
label = contacts.getContactsLabel(activeTab);
}
if (!labels.includes(label)) labels.push(label);
}
return `请输入${labels.join("、")}.`;
});
const _listScrollRef = vue.ref();
const _props = vue.computed(() => {
return {
title: props.title,
multiple: props.multiple,
isTreeMode: utilsVue.unref(_isTreeMode)
};
});
function ifAddSelectedValue(item) {
const selectedValue = utilsVue.unref(_selectedValue);
return selectedValue.some(({ value, type }) => {
if (item.type && type) {
switch (true) {
case (value === item.value && type === item.type):
case (value === item.value && item.type === contacts$1.Contacts.USUALLY && type === contacts$1.Contacts.USER):
return true;
}
return false;
}
return value === item.value;
});
}
function handleTriggerItem(items, isDelete = false, force = false) {
var _a;
const multiple2 = utilsVue.unref(_props).multiple;
let selectedValue = utilsVue.unref(_selectedValue);
if (!multiple2) {
selectedValue = isDelete || ifAddSelectedValue(items == null ? void 0 : items[0]) ? [] : [...items];
} else {
if (items.length === 1) {
selectedValue = isDelete || ifAddSelectedValue(items == null ? void 0 : items[0]) ? selectedValue.filter(({ value }) => items[0].value !== value) : selectedValue.concat([...items]);
} else if (force !== true && items.some((item) => item.checked !== true)) {
selectedValue = selectedValue.concat([...items]);
} else if (isDelete) {
const itemIds = items.map((item) => item.value);
selectedValue = selectedValue.filter(({ value }) => !itemIds.includes(value));
} else {
selectedValue = selectedValue.concat([...items]);
}
}
const form = {
value: multiple2 ? selectedValue.map(({ value }) => value) : ((_a = selectedValue == null ? void 0 : selectedValue[0]) == null ? void 0 : _a.value) ?? void 0
};
function handleAsyncValue(type, item) {
const key = `${type}List`;
if (lodashEs.has(vModels, key)) {
let list = form[key];
if (multiple2) {
if (!utils.isArray(list)) list = [];
form[key] = list.concat(item.value);
} else if (utils.isEmptyValue(list)) {
form[key] = item.value;
}
}
}
if (!utils.isEmptyArray(selectedValue)) {
for (const item of selectedValue) {
handleAsyncValue(item.type, item);
if (item.type === contacts$1.Contacts.USUALLY) {
handleAsyncValue(contacts$1.Contacts.USER, item);
}
}
}
_selectedValue.value = selectedValue;
for (const [key, ref2] of utils.toPairs(vModels)) {
if (vue.isRef(ref2)) ref2.value = form[key];
}
if (vue.isRef(_modelValue)) {
_modelValue.value = form.value;
} else {
modelValue.value = form.value;
}
return selectedValue;
}
function isDisableItem(item) {
switch (true) {
case item.disabled:
case item.disableCheckbox:
case utilsVue.unref(_disabledIds).includes(item.value):
return true;
}
return false;
}
function handleExtractItems(item, options) {
if (utils.isArray(item)) {
return lodashEs.flattenDeep(item.map((value) => handleExtractItems(value, options)));
}
const deep = lodashEs.isBoolean(options) ? options : (options == null ? void 0 : options.deep) ?? true;
const excludeDisabled = lodashEs.isBoolean(options) ? true : (options == null ? void 0 : options.excludeDisabled) ?? true;
if (excludeDisabled && isDisableItem(item)) return [];
const multiple2 = props.multiple;
const activeTab = utilsVue.unref(_activeTab);
let items = [];
const isKeywords = utilsVue.unref(_query).keywords.length !== 0;
switch (true) {
case isKeywords:
case item.type === activeTab:
case (activeTab === contacts$1.Contacts.USUALLY && [contacts$1.Contacts.USER, contacts$1.Contacts.USUALLY].includes(item.type)):
items = [handleTreeNode.handleTreeNode(item, _selectedIds)];
break;
}
const flag = deep && multiple2 && !(item == null ? void 0 : item.isLeaf) && utils.isArray(item.children);
if (flag && !isKeywords) {
const children = item.children.map((value) => handleExtractItems(value, options));
return items.concat(lodashEs.flattenDeep(children));
}
return items;
}
const handleTrigger = (item, isDelete = false) => {
const items = handleExtractItems(item);
if (items.length === 0) return;
const values = handleTriggerItem(items, isDelete);
emit("change", values);
};
function handleDelete(item) {
if (isDisableItem(item)) return;
const values = handleTriggerItem([item], true);
emit("change", values);
}
function handleAllDelete() {
for (const item of utilsVue.unref(_selectedValue)) {
if (isDisableItem(item)) return;
handleTriggerItem([item], true);
}
emit("change", utilsVue.unref(_selectedValue));
}
const handleItemClick = (item) => {
return handleTrigger(item, item == null ? void 0 : item.checked);
};
const handleTreeExpand = async (expandedKeys, info) => {
const dataRef = info.node.dataRef;
if (info.expanded && dataRef && utils.isEmptyArray(dataRef.children)) {
dataRef.loading = true;
await handleLoadMore({ parent: info.node });
dataRef.loading = false;
}
};
const handleTreeSelect = async (selectedKeys, info) => {
if (info.node) handleItemClick(info.node);
};
const selectState = vue.computed(() => {
var _a;
const state = {
show: utilsVue.unref(_isTreeMode) !== true,
checked: false,
indeterminate: false,
dataSource: []
};
if (!state.show) {
return state;
}
state.dataSource = handleExtractItems(utilsVue.unref(_dataSource), {
deep: false,
excludeDisabled: false
});
if (state.dataSource.length === 0) {
return state;
}
const activeKey = handleActiveTabKey.handleActiveTabKey({ activeTab: _activeTab, userActiveTab: _userActiveTab });
const key = handleDataType.handleDataTypeKey(activeKey);
const ids = state.dataSource.map(({ value }) => value);
const items = ((_a = utilsVue.unref(selectedGroupMap[key])) == null ? void 0 : _a.children) ?? [];
const selectedValue = items.filter(({ value }) => ids.includes(value));
if (selectedValue.length === 0) {
return state;
}
state.checked = selectedValue.length >= state.dataSource.length;
state.indeterminate = !state.checked && utilsVue.unref(selectedValue).length > 0;
return state;
});
function handleAllSelectTrigger(_state) {
const { show, checked, indeterminate } = utilsVue.unref(selectState);
if (!show) return;
let values;
const dataSource2 = handleExtractItems(utilsVue.unref(_dataSource), {
deep: false
});
switch (true) {
case (!indeterminate && checked):
values = handleTriggerItem(dataSource2, true, true);
break;
default:
values = handleTriggerItem(dataSource2, false);
break;
}
emit("change", values);
}
const handleTabChange = (key) => {
var _a, _b;
if (!utils.isEmptyValue(key)) {
_activeTab.value = key;
} else {
_activeTab.value = (_b = (_a = utilsVue.unref(_contactsTabs)) == null ? void 0 : _a[0]) == null ? void 0 : _b.value;
}
if (utilsVue.unref(_isTreeMode)) {
handleLoadMore({
force: false
}).then((r) => r);
}
};
const ctx = {
multiple,
allExpand,
title,
_ref,
_noMore,
_modelValue,
_dataSource,
dataSource,
searchQueryList,
_query,
_placeholder,
_contactsTabs,
_activeTab,
_userTabs,
_userActiveTab,
_props,
_loading,
_rightElRef,
_leftElRef,
_listScrollRef,
_selectedValue,
_selectedIds,
_disabledIds,
_isTreeMode,
selectState
};
const methods = {
handleSearch,
handleLoadMore,
handleTreeExpand,
handleTreeSelect,
handleDelete,
handleAllDelete,
handleItemClick,
handleTabChange,
handleAllSelectTrigger
};
return {
slots,
ctx,
methods,
...methods
};
};
exports.useContacts = useContacts;