Notes/src/public/app/services/hoisted_note.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-01-24 15:44:24 +01:00
import appContext from "./app_context.js";
2020-02-02 22:04:28 +01:00
import treeService from "./tree.js";
2018-12-11 21:53:56 +01:00
function getHoistedNoteId() {
2020-11-23 22:52:48 +01:00
const activeTabContext = appContext.tabManager.getActiveTabContext();
2020-11-23 22:52:48 +01:00
return activeTabContext ? activeTabContext.hoistedNoteId : 'root';
2018-12-11 21:53:56 +01:00
}
2018-12-15 20:29:08 +01:00
async function unhoist() {
2020-11-23 22:52:48 +01:00
const activeTabContext = appContext.tabManager.getActiveTabContext();
if (activeTabContext) {
await activeTabContext.unhoist();
}
2018-12-15 20:29:08 +01:00
}
2020-02-10 20:57:56 +01:00
function isTopLevelNode(node) {
return isRootNode(node.getParent());
}
2020-02-10 20:57:56 +01:00
function isRootNode(node) {
// even though check for 'root' should not be necessary, we keep it just in case
return node.data.noteId === "root"
2020-02-10 20:57:56 +01:00
|| node.data.noteId === getHoistedNoteId();
}
2020-11-23 23:11:21 +01:00
async function checkNoteAccess(notePath, tabContext) {
2020-02-02 22:04:28 +01:00
// notePath argument can contain only noteId which is not good when hoisted since
// then we need to check the whole note path
2020-08-24 23:33:27 +02:00
const resolvedNotePath = await treeService.resolveNotePath(notePath);
2020-02-02 22:04:28 +01:00
2020-08-24 23:33:27 +02:00
if (!resolvedNotePath) {
2020-02-02 22:04:28 +01:00
console.log("Cannot activate " + notePath);
return false;
}
2020-11-23 23:11:21 +01:00
const hoistedNoteId = tabContext.hoistedNoteId;
2020-02-02 22:04:28 +01:00
2020-08-24 23:33:27 +02:00
if (hoistedNoteId !== 'root' && !resolvedNotePath.includes(hoistedNoteId)) {
2020-02-02 22:04:28 +01:00
const confirmDialog = await import('../dialogs/confirm.js');
2020-02-02 22:04:28 +01:00
if (!await confirmDialog.confirm("Requested note is outside of hoisted note subtree and you must unhoist to access the note. Do you want to proceed with unhoisting?")) {
return false;
}
// unhoist so we can activate the note
await unhoist();
}
return true;
}
2018-12-11 21:53:56 +01:00
export default {
getHoistedNoteId,
unhoist,
isTopLevelNode,
2020-02-02 22:04:28 +01:00
isRootNode,
checkNoteAccess
2020-08-24 23:33:27 +02:00
}