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
2020-02-05 22:08:45 +01:00
function getHoistedNoteId ( ) {
2020-11-23 22:52:48 +01:00
const activeTabContext = appContext . tabManager . getActiveTabContext ( ) ;
2020-10-13 23:41:55 +02:00
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 ( ) ) ;
2019-04-16 21:40:04 +02:00
}
2020-02-10 20:57:56 +01:00
function isRootNode ( node ) {
2019-04-16 21:40:04 +02:00
// 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 ( ) ;
2019-04-16 21:40:04 +02:00
}
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' ) ;
2021-02-07 21:27:09 +01:00
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 ,
2019-04-16 21:40:04 +02:00
unhoist ,
isTopLevelNode ,
2020-02-02 22:04:28 +01:00
isRootNode ,
checkNoteAccess
2020-08-24 23:33:27 +02:00
}