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

73 lines
2.0 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";
import dialogService from "../widgets/dialog.js";
import froca from "./froca.js";
2018-12-11 21:53:56 +01:00
function getHoistedNoteId() {
2021-05-22 12:35:41 +02:00
const activeNoteContext = appContext.tabManager.getActiveContext();
2021-05-22 12:26:45 +02:00
return activeNoteContext ? activeNoteContext.hoistedNoteId : 'root';
2018-12-11 21:53:56 +01:00
}
2018-12-15 20:29:08 +01:00
async function unhoist() {
2021-05-22 12:35:41 +02:00
const activeNoteContext = appContext.tabManager.getActiveContext();
2020-11-23 22:52:48 +01:00
2021-05-22 12:26:45 +02:00
if (activeNoteContext) {
await activeNoteContext.unhoist();
2020-11-23 22:52:48 +01:00
}
2018-12-15 20:29:08 +01:00
}
2020-02-10 20:57:56 +01:00
function isTopLevelNode(node) {
return isHoistedNode(node.getParent());
}
function isHoistedNode(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();
}
async function isHoistedInHiddenSubtree() {
const hoistedNoteId = getHoistedNoteId();
if (hoistedNoteId === 'root') {
return false;
}
const hoistedNote = await froca.getNote(hoistedNoteId);
const hoistedNotePath = treeService.getSomeNotePath(hoistedNote);
return treeService.isNotePathInHiddenSubtree(hoistedNotePath);
}
2021-05-22 12:26:45 +02:00
async function checkNoteAccess(notePath, noteContext) {
const resolvedNotePath = await treeService.resolveNotePath(notePath, noteContext.hoistedNoteId);
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;
}
2021-05-22 12:26:45 +02:00
const hoistedNoteId = noteContext.hoistedNoteId;
2020-02-02 22:04:28 +01:00
if (!resolvedNotePath.includes(hoistedNoteId) && !resolvedNotePath.includes("hidden")) {
if (!await dialogService.confirm("Requested note is outside of hoisted note subtree and you must unhoist to access the note. Do you want to proceed with unhoisting?")) {
2020-02-02 22:04:28 +01:00
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,
isHoistedNode,
checkNoteAccess,
isHoistedInHiddenSubtree
2020-08-24 23:33:27 +02:00
}