2024-02-18 11:48:38 +02:00
|
|
|
import treeService = require('./tree');
|
|
|
|
import sql = require('./sql');
|
|
|
|
import BBranch = require('../becca/entities/bbranch.js');
|
2022-02-05 12:06:23 +01:00
|
|
|
|
2024-02-18 11:48:38 +02:00
|
|
|
function moveBranchToNote(branchToMove: BBranch, targetParentNoteId: string) {
|
2023-11-16 23:31:33 +01:00
|
|
|
if (branchToMove.parentNoteId === targetParentNoteId) {
|
2022-02-05 12:06:23 +01:00
|
|
|
return {success: true}; // no-op
|
|
|
|
}
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
const validationResult = treeService.validateParentChild(targetParentNoteId, branchToMove.noteId, branchToMove.branchId);
|
2022-02-05 12:06:23 +01:00
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
return [200, validationResult];
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:48:38 +02:00
|
|
|
const maxNotePos = sql.getValue<number | null>('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [targetParentNoteId]);
|
|
|
|
const newNotePos = !maxNotePos ? 0 : maxNotePos + 10;
|
2022-02-05 12:06:23 +01:00
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
const newBranch = branchToMove.createClone(targetParentNoteId, newNotePos);
|
2022-02-05 12:06:23 +01:00
|
|
|
newBranch.save();
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
branchToMove.markAsDeleted();
|
2022-02-05 12:06:23 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
branch: newBranch
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:33:04 +03:00
|
|
|
function moveBranchToBranch(branchToMove: BBranch, targetParentBranch: BBranch, branchId: string) {
|
|
|
|
// TODO: Unused branch ID argument.
|
2023-11-16 23:31:33 +01:00
|
|
|
const res = moveBranchToNote(branchToMove, targetParentBranch.noteId);
|
2022-02-05 12:06:23 +01:00
|
|
|
|
2024-02-18 11:48:38 +02:00
|
|
|
if (!("success" in res) || !res.success) {
|
2022-02-05 12:06:23 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// expanding so that the new placement of the branch is immediately visible
|
2023-11-16 23:31:33 +01:00
|
|
|
if (!targetParentBranch.isExpanded) {
|
|
|
|
targetParentBranch.isExpanded = true;
|
|
|
|
targetParentBranch.save();
|
|
|
|
}
|
2022-02-05 12:06:23 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:48:38 +02:00
|
|
|
export = {
|
2022-02-05 12:06:23 +01:00
|
|
|
moveBranchToBranch,
|
|
|
|
moveBranchToNote
|
|
|
|
};
|