2020-02-06 05:08:45 +08:00
import options from './options.js' ;
2020-01-24 22:44:24 +08:00
import appContext from "./app_context.js" ;
2020-02-03 05:04:28 +08:00
import treeService from "./tree.js" ;
2018-12-12 04:53:56 +08:00
2020-02-06 05:08:45 +08:00
function getHoistedNoteId ( ) {
return options . get ( 'hoistedNoteId' ) ;
2018-12-12 04:53:56 +08:00
}
async function setHoistedNoteId ( noteId ) {
2020-02-06 05:08:45 +08:00
await options . save ( 'hoistedNoteId' , noteId ) ;
2018-12-13 03:39:56 +08:00
2020-02-06 05:08:45 +08:00
// FIXME - just use option load event
2020-02-17 02:21:17 +08:00
appContext . triggerEvent ( 'hoistedNoteChanged' , { noteId } ) ;
2018-12-12 04:53:56 +08:00
}
2018-12-16 03:29:08 +08:00
async function unhoist ( ) {
await setHoistedNoteId ( 'root' ) ;
}
2020-02-11 03:57:56 +08:00
function isTopLevelNode ( node ) {
return isRootNode ( node . getParent ( ) ) ;
2019-04-17 03:40:04 +08:00
}
2020-02-11 03:57:56 +08:00
function isRootNode ( node ) {
2019-04-17 03:40:04 +08:00
// even though check for 'root' should not be necessary, we keep it just in case
return node . data . noteId === "root"
2020-02-11 03:57:56 +08:00
|| node . data . noteId === getHoistedNoteId ( ) ;
2019-04-17 03:40:04 +08:00
}
2020-02-03 05:04:28 +08: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-11 03:57:56 +08:00
const hoistedNoteId = getHoistedNoteId ( ) ;
2020-02-03 05:04:28 +08: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-12 04:53:56 +08:00
export default {
getHoistedNoteId ,
2018-12-16 03:29:08 +08:00
setHoistedNoteId ,
2019-04-17 03:40:04 +08:00
unhoist ,
isTopLevelNode ,
2020-02-03 05:04:28 +08:00
isRootNode ,
checkNoteAccess
2018-12-12 04:53:56 +08:00
}