Commit 0e033d8d authored by 邓超's avatar 邓超

fix: 重构app菜单拖拽逻辑

parent cf398fb9
Pipeline #38841 passed with stages
in 22 minutes 19 seconds
...@@ -377,62 +377,94 @@ const MiniMenu = props => { ...@@ -377,62 +377,94 @@ const MiniMenu = props => {
}); });
setRoleList(arr2); setRoleList(arr2);
}; };
const loop = (data, key, parentID, callback) => {
for (let i = 0; i < data.length; i++) {
if (data[i].menuID === key) {
return callback(data[i], i, data, parentID);
}
if (data[i].children) {
loop(data[i].children, key, data[i].menuID, callback);
}
}
};
// 树的拖动 // 树的拖动
const handleDrop = infos => { const handleDrop = infos => {
console.log(infos, 'newTree'); const dropKey = infos.node.key;
const { pos } = infos.node.props; const dragKey = infos.dragNode.key;
const dragKey = infos.dragNode.props.eventKey; const dropPos = infos.node.pos.split('-');
const dragPos = infos.dragNode.props.pos.split('-'); // 拖动的节点
const dropKey = infos.node.props.eventKey;
const dropPos = infos.node.props.pos.split('-'); // 拖动结束的节点
const endIndex = Number(dropPos[dropPos.length - 1]);
const dropPosition = const dropPosition =
infos.dropPosition - Number(dropPos[dropPos.length - 1]); infos.dropPosition - Number(dropPos[dropPos.length - 1]);
// let obj = findNum(newTreeList, dragKey, getArrList); const data = [...treeData];
let obj; // 找到拖拽的元素
findNum(newTreeList, dragKey, (arr, id, parentId, index) => { let dragObj;
obj = { arr, id, parentId, index }; let dropObj;
return { arr, id, parentId, index }; let id;
let dragList;
loop(data, dropKey, -1, item => {
dropObj = item;
}); });
console.log(obj, 'obj'); loop(data, dragKey, -1, (item, index, arr) => {
let arrList = []; if (infos.dropToGap) {
if ( arr.splice(index, 1);
dragPos.length !== dropPos.length || dragObj = item;
dragPos[dragPos.length - 2] !== dropPos[dropPos.length - 2] } else if (
dropObj.menuType !== 'MiniAppMenuThree' &&
dropObj.menuType !== 'MiniAppMenu'
) { ) {
notification.warning({ arr.splice(index, 1);
message: '提示', dragObj = item;
duration: 3,
description: '操作失败',
});
return null;
} }
let idIndex; });
obj.arr.map((item, index) => { if (!infos.dropToGap) {
if (item.menuID === obj.id) { // Drop on the content
idIndex = index; loop(data, dropKey, -1, item => {
return; item.children = item.children || [];
if (
item.menuType !== 'MiniAppMenuThree' &&
dropObj.menuType !== 'MiniAppMenu'
) {
// where to insert 示例添加到头部,可以是随意位置
item.children.unshift(dragObj);
id = item.menuID;
dragList = item.children.map(val => val.menuID);
} }
arrList.push(item.menuID);
}); });
if (idIndex > endIndex) { } else if (
(infos.node.props.children || []).length > 0 && // Has children
infos.node.props.expanded && // Is expanded
dropPosition === 1 // On the bottom gap
) {
loop(data, dropKey, -1, item => {
item.children = item.children || [];
// where to insert 示例添加到头部,可以是随意位置
item.children.unshift(dragObj);
id = item.menuID;
dragList = item.children.map(val => val.menuID);
});
} else {
let ar;
let i;
loop(data, dropKey, -1, (item, index, arr, parentID) => {
ar = arr;
i = index;
id = parentID;
});
if (dropPosition === -1) { if (dropPosition === -1) {
arrList.splice(endIndex, 0, obj.id); ar.splice(i, 0, dragObj);
} else if (dropPosition === 1) { } else {
arrList.splice(endIndex + 1, 0, obj.id); ar.splice(i + 1, 0, dragObj);
} }
} else if (idIndex < endIndex) { dragList = ar.map(val => val.menuID);
if (dropPosition === -1) {
arrList.splice(endIndex - 1, 0, obj.id);
} else if (dropPosition === 1) {
arrList.splice(endIndex, 0, obj.id);
} }
// 如果拖拽到菜单下不做任何处理
if (!dragList) {
return;
} }
dragMenu({ dragMenu({
menuID: obj.id, menuID: dragKey,
newParentID: obj.parentId.toString() || '-1', newParentID: id.toString() || '-1',
menuList: String(arrList) || '', menuList: String(dragList) || '',
_version: 9999, _version: 9999,
_dc: Date.now(), _dc: Date.now(),
}).then(res => { }).then(res => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment