import {iTreeUtils} from "./createTreeUtils"; import {iTreeKeyOrNode, iTreeProps, iTreeState} from "./TreeCore.type"; import {nextTick, toRaw} from "@peryl/react-compose"; import {CheckboxStatus} from "../../utils/constant"; import {toArray} from "@peryl/utils/toArray"; import {iTreeNode} from "./createTreeNode"; export function createTreeMethods( { utils, props, state, checkModel, expandModel, }: { utils: iTreeUtils, props: iTreeProps, state: iTreeState, checkModel: { value: any }, expandModel: { value: string[] | null | undefined } } ) { const expandMethods = { /** * 展开节点 * @author 韦胜健 * @date 2022/9/21 14:17 */ expand: async (keyOrNode: iTreeKeyOrNode, emitChange = true): Promise<{ expandKeys: string[], collapseKeys: string[] }> => { const expandKeys: string[] = []; const collapseKeys: string[] = []; await utils.handleKeyOrNode(keyOrNode, async (treeNode) => { if (treeNode.isLoading() || treeNode.empty) { /*这个节点正在加载中,不做任何处理*/ return; } if (!treeNode.isExpanded()) { /*不需要等加载完数据之后再派发expand,立即触发expand*/ nextTick().then(async () => { /*处理懒加载数据逻辑*/ if ( props.lazy && !treeNode.isLoaded() && !treeNode.isLeaf() ) { /*如果是懒加载模式,并且这个节点还没有懒加载结束,并且不是叶子节点*/ if (!props.getChildrenData) { throw new Error('tree: props.getChildrenData is required when props.lazy is true!'); } treeNode.setLoading(true); try { const childrenData = await props.getChildrenData(treeNode); treeNode.setChildrenData(childrenData); treeNode.setLoaded(true); } finally { treeNode.setLoading(false); } } }); /*手风琴模式,展开某个节点的时候,关闭兄弟节点*/ if (props.according) { const brothers = !!treeNode.parent ? treeNode.parent.children() : utils.getTreeNodeComputedData().treeNodeList; if (!!brothers) { await Promise.all(brothers.map(async child => { child.id !== treeNode.id && (collapseKeys.push(...await expandMethods.collapse(child, false))); })); } } !treeNode.isLeaf() && expandKeys.push(treeNode.key); } if (props.autoExpandParent) { expandKeys.push(...treeNode.parents().map(i => i.key)); } }); if (emitChange && (expandKeys.length + collapseKeys.length > 0)) { const expandMap = { ...state.expandedMap }; expandKeys.forEach(key => { if (!expandMap[key]) { expandMap[key] = true; } }); collapseKeys.forEach(key => { if (!!expandMap[key]) { delete expandMap[key]; } }); expandModel.value = Object.keys(expandMap); } return { expandKeys, collapseKeys }; }, /** * 关闭节点 * @author 韦胜健 * @date 2022/9/21 14:19 */ collapse: async (keyOrNode: iTreeKeyOrNode, emitChange = true): Promise => { const collapseKeys: string[] = []; await utils.handleKeyOrNode(keyOrNode, async (treeNode) => { if (treeNode.empty) { return; } if (treeNode.isExpanded()) { collapseKeys.push(treeNode.key); } if (props.autoCollapseChildren) { collapseKeys.push(...treeNode.getChildrenKeys()); } }); if (emitChange && collapseKeys.length > 0) { const expandMap = { ...state.expandedMap }; collapseKeys.forEach(key => delete expandMap[key]); expandModel.value = Object.keys(expandMap); } return collapseKeys; }, /** * 切换节点的展开收起状态 * @author 韦胜健 * @date 2022/9/21 14:21 */ toggleExpand: async (keyOrNode: iTreeKeyOrNode): Promise => { await utils.handleKeyOrNode(keyOrNode, treeNode => { treeNode.isExpanded() ? expandMethods.collapse(treeNode) : expandMethods.expand(treeNode); }); }, /** * 展开所有节点,懒加载模式下无效 * @author 韦胜健 * @date 2022/9/21 14:24 */ expandAll: async () => { expandModel.value = Object.keys(utils.getTreeNodeComputedData().key2treeNode); }, /** * 收起所有节点 * @author 韦胜健 * @date 2022/9/21 14:28 */ collapseAll: async () => { expandModel.value = null; }, }; const checkMethods = { /** * 选中节点数据 * @author 韦胜健 * @date 2022.9.21 22:46 */ check: async (keyOrNode: iTreeKeyOrNode, emitChange = true): Promise => { const checkKeys: string[] = []; await utils.handleKeyOrNode(keyOrNode, treeNode => { if (treeNode.empty || !treeNode.isCheckAble()) {return;} checkKeys.push(treeNode.key); /*非严格选择模式(严格选择模式下,选中或者取消选中一个节点不会影响其父节点子节点的选中状态)*/ if (!props.checkStrictly) { /*自动选中子节点*/ if (props.autoCheckChildren) { checkKeys.push(...treeNode.getChildrenKeys()); } /*自动选中父节点*/ if (props.autoCheckParent) { const parents = treeNode.parents(); parents.forEach(parent => { if (parent.checkStatus() !== CheckboxStatus.check) { const children = parent.children()!.filter(i => !i.empty); /*如果存在父节点,并且父节点处于未选中状态,并且除了当前选中的节点之外的其他节点都处于选中状态,则自动选中父节点*/ if (children.length === 0 || children.every(i => i.key === treeNode.key || i.checkStatus() === CheckboxStatus.check || checkKeys.indexOf(i.key) > -1)) { checkKeys.push(parent.key); } } }); } } }); if (checkKeys.length > 0 && emitChange) { const checkMap = { ...state.checkedMap }; checkKeys.forEach(i => {checkMap[i] = true;}); utils.setCheckModel(Object.keys(checkMap)); } return checkKeys; }, /** * 取消选中节点数据 * @author 韦胜健 * @date 2022.9.21 22:46 */ uncheck: async (keyOrNode: iTreeKeyOrNode, emitChange = true): Promise => { const unCheckKeys: string[] = []; await utils.handleKeyOrNode(keyOrNode, treeNode => { if (treeNode.empty || !treeNode.isCheckAble()) {return;} unCheckKeys.push(treeNode.key); /*非严格选择模式(严格选择模式下,选中或者取消选中一个节点不会影响其父节点子节点的选中状态)*/ if (!props.checkStrictly) { /*自动取消选中子节点*/ if (props.autoCheckChildren) { unCheckKeys.push(...treeNode.getChildrenKeys()); } /*自动取消选中父节点*/ if (props.autoCheckParent && !!treeNode.parent) { unCheckKeys.push(...treeNode.parents().map(i => i.key)); } } }); if (unCheckKeys.length > 0 && emitChange) { const checkMap = { ...state.checkedMap }; unCheckKeys.forEach(i => {delete checkMap[i]; }); utils.setCheckModel(Object.keys(checkMap)); } return unCheckKeys; }, /** * 切换节点的选中状态 * @author 韦胜健 * @date 2022.9.21 22:47 */ toggleCheck: async (keyOrNode: iTreeKeyOrNode): Promise => { await utils.handleKeyOrNode(keyOrNode, treeNode => { treeNode.checkStatus() !== CheckboxStatus.check ? checkMethods.check(treeNode) : checkMethods.uncheck(treeNode); }); }, /** * 选中所有节点 * @author 韦胜健 * @date 2022.9.21 22:47 */ checkAll: async () => { utils.setCheckModel(Object.values(utils.getTreeNodeComputedData().key2treeNode).filter(i => !!i && !i.empty && i.isCheckAble()).map(i => i!.key)); }, /** * 取消选中所有节点 * @author 韦胜健 * @date 2022.9.21 22:52 */ unCheckAll: async () => { utils.setCheckModel(null); }, /** * 刷新某个节点的选中状态 * @author 韦胜健 * @date 2022/9/22 19:56 */ refreshCheckStatus: async (keyOrNode: iTreeKeyOrNode, emitChange = true) => { if (!props.multiple) {return;} const checkKeys: string[] = []; const unCheckKeys: string[] = []; await nextTick(); await utils.handleKeyOrNode(keyOrNode, async (treeNode) => { if (props.checkStrictly || treeNode.empty || !treeNode.isCheckAble()) {return;} const treeNodes = [treeNode, ...treeNode.parents()]; await Promise.all(treeNodes.map(async treeNode => { const children = treeNode.children()?.filter(i => !!i && !i.empty); if (!children || children.length === 0) {return;} if (children.every(i => i.checkStatus() === CheckboxStatus.check || checkKeys.indexOf(i.key) > -1)) { /*每个子节点都已经选中了,此时treeNode应该选中*/ if (!state.checkedMap[treeNode.key]) {checkKeys.push(treeNode.key);} } else if (children.every(i => i.checkStatus() === CheckboxStatus.uncheck || unCheckKeys.indexOf(i.key) > -1)) { /*每个子节点都未选中此时treeNode应该未选中*/ if (state.checkedMap[treeNode.key]) {unCheckKeys.push(treeNode.key);} } else { /*没有全部选中*/ if (state.checkedMap[treeNode.key]) {unCheckKeys.push(treeNode.key);} } })); }); if (emitChange) { const checkMap = { ...state.checkedMap }; checkKeys.forEach(i => {checkMap[i] = true;}); unCheckKeys.forEach(i => delete checkMap[i]); utils.setCheckModel(Object.keys(checkMap)); } }, /** * 清空无效的选中的key * @author 韦胜健 * @date 2022/9/22 20:56 */ clearInvalidChecked: async () => { const { key2treeNode } = utils.getTreeNodeComputedData(); const checkedKeys = toArray((checkModel.value || [])).filter(i => !!key2treeNode[utils.getNodeDataKey(i)]).map(i => utils.getNodeDataKey(i)); utils.setCheckModel(props.multiple || state.singleCheckType.singleValueType === 'all' ? checkedKeys : checkedKeys[0]); }, }; const otherMethods = { /** * 获取当前高亮节点 * @author 韦胜健 * @date 2022.9.21 21:20 */ getCurrent: () => { return !state.current ? null : utils.getTreeNode(state.current); }, /** * 设置当前高亮节点 * @author 韦胜健 * @date 2022.9.21 21:21 */ setCurrent: (key: string | undefined) => { state.current = key; }, /** * 获取父节点的所有子节点 * @author 韦胜健 * @date 2022/9/27 11:20 */ getParentChildren: (node: iTreeNode): iTreeNode[] => { return !!node.parent ? node.parent.children() : utils.getTreeNodeComputedData().treeNodeList; }, /** * 获取父节点的所有子数据 * @author 韦胜健 * @date 2022/9/27 11:26 */ getParentChildrenData: (node: iTreeNode): any[] => { return !!node.parent ? node.parent.childrenData()! : utils.getTreeNodeComputedData().data!; }, /** * 获取响应式的子节点数据 * @author 韦胜健 * @date 2022/9/27 11:18 */ getReactiveChildrenData(treeNode: iTreeNode): any[] { let childrenData = treeNode.childrenData(); if (!childrenData) { treeNode.setChildrenData([]); childrenData = treeNode.childrenData(); } return childrenData!; }, /** * 移除自身节点 * @author 韦胜健 * @date 2022/9/27 11:30 */ removeSelf: (treeNode: iTreeNode) => { const index = otherMethods.getParentChildrenData(treeNode).findIndex(treeNodeData => utils.getNodeDataKey(treeNodeData) === treeNode.key); if (index > -1) { otherMethods.getParentChildrenData(treeNode).splice(index, 1); } }, /** * 将start节点插入到move节点前面 * @author 韦胜健 * @date 2022/9/27 11:30 */ previousSibling: (move: iTreeNode, start: iTreeNode) => { /*将start移除*/ otherMethods.removeSelf(start); /*将start移动到move前面*/ let moveParentChildrenData = otherMethods.getParentChildrenData(move); const index = moveParentChildrenData.findIndex(treeNodeData => utils.getNodeDataKey(treeNodeData) === move.key); moveParentChildrenData.splice(index, 0, toRaw(start.data)); }, /** * 将start节点插入到move节点后面 * @author 韦胜健 * @date 2022/9/27 11:37 */ nextSibling: (move: iTreeNode, start: iTreeNode) => { /*将start移除*/ otherMethods.removeSelf(start); /*将start移动到move前面*/ let moveParentChildrenData = otherMethods.getParentChildrenData(move); const index = moveParentChildrenData.findIndex(treeNodeData => utils.getNodeDataKey(treeNodeData) === move.key); moveParentChildrenData.splice(index + 1, 0, toRaw(start.data)); }, /** * 将start节点插图到move节点里边 * @author 韦胜健 * @date 2022/9/27 11:37 */ unshiftChild: (move: iTreeNode, start: iTreeNode) => { /*将start移除*/ otherMethods.removeSelf(start); /*将start插入到move里边*/ let moveParentChildrenData = otherMethods.getReactiveChildrenData(move); moveParentChildrenData.unshift(toRaw(start.data)); }, /** * 单选某个节点 * @author 韦胜健 * @date 2022/10/26 14:42 */ singleSelect: (treeNode: iTreeNode) => { /*如果是多选或者node不可以选中,则不做单选处理*/ if (props.multiple || !treeNode.isCheckAble()) {return;} /*如果只能选中最后一个节点并且node是不会叶子节点,则不做单选处理*/ if (state.singleCheckType.checkLast && !treeNode.isLeaf()) {return;} state.current = treeNode.key; /*处理单选*/ if (state.singleCheckType.singleValueType === 'last') { utils.setCheckModel(treeNode.key); } else { utils.setCheckModel([treeNode, ...treeNode.parents()].reverse().map(i => i.key)); } }, }; return { ...expandMethods, ...checkMethods, ...otherMethods, }; } export type iTreeMethods = ReturnType