2020-02-05 22:08:45 +01:00
import options from './options.js' ;
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 ( ) {
return options . get ( 'hoistedNoteId' ) ;
2018-12-11 21:53:56 +01:00
}
async function setHoistedNoteId ( noteId ) {
2020-02-05 22:08:45 +01:00
await options . save ( 'hoistedNoteId' , noteId ) ;
2018-12-12 20:39:56 +01:00
2020-02-05 22:08:45 +01:00
// FIXME - just use option load event
2020-02-16 19:21:17 +01:00
appContext . triggerEvent ( 'hoistedNoteChanged' , { noteId } ) ;
2018-12-11 21:53:56 +01:00
}
2018-12-15 20:29:08 +01:00
async function unhoist ( ) {
await setHoistedNoteId ( 'root' ) ;
}
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-02-02 22:04:28 +01:00
async function checkNoteAccess ( notePath ) {
// notePath argument can contain only noteId which is not good when hoisted since
// then we need to check the whole note path
const runNotePath = await treeService . getRunPath ( notePath ) ;
if ( ! runNotePath ) {
console . log ( "Cannot activate " + notePath ) ;
return false ;
}
2020-02-10 20:57:56 +01:00
const hoistedNoteId = getHoistedNoteId ( ) ;
2020-02-02 22:04:28 +01:00
if ( hoistedNoteId !== 'root' && ! runNotePath . includes ( hoistedNoteId ) ) {
const confirmDialog = await import ( '../dialogs/confirm.js' ) ;
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 ,
2018-12-15 20:29:08 +01:00
setHoistedNoteId ,
2019-04-16 21:40:04 +02:00
unhoist ,
isTopLevelNode ,
2020-02-02 22:04:28 +01:00
isRootNode ,
checkNoteAccess
2018-12-11 21:53:56 +01:00
}