1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| function treeDataRefresh( { dragGrade, dragElementId }, { dropGrade, dropElementId } ) { if (dragElementId === dropElementId) return; const getAndDeleteDragLabelObj = (dragElement, data) => { let result; if (dragGrade - dropGrade > 1 || dragGrade - dropGrade < 0) return result; const innerFn = (dragElementId, data) => { data.forEach((treeObj, index, array) => { if (treeObj.id === Number(dragElementId)) { array.splice(index, 1); result = treeObj; } else { treeObj.children && innerFn(dragElementId, treeObj.children); } }); };
innerFn(dragElementId, data); return result; }; const setDragLabelObjToTreeData = (dragLabelObj, dropElementId, data) => { for (let i = 0; i < data.length; i++) { const treeObj = data[i]; if (treeObj.id === Number(dropElementId)) { if (dragGrade - dropGrade === 1) { treeObj.children ? treeObj.children.unshift(dragLabelObj) : (treeObj["children"] = [dragLabelObj]); } else if (dragGrade - dropGrade === 0) { data.splice(i + 1, 0, dragLabelObj); break; } } else { treeObj.children && setDragLabelObjToTreeData( dragLabelObj, dropElementId, treeObj.children ); } } };
let dragLabelObj = getAndDeleteDragLabelObj(dragElementId, treeData);
if (dragElementId) { dragLabelObj && setDragLabelObjToTreeData(dragLabelObj, dropElementId, treeData); } else { treeData.unshift(dragLabelObj); } }
|