| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import utils from './utils.js'; | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  | import hoistedNoteService from './hoisted_note.js'; | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $tree = $("#tree"); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  | async function getParentProtectedStatus(node) { | 
					
						
							|  |  |  |     return await hoistedNoteService.isRootNode(node) ? 0 : node.getParent().data.isProtected; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function getNodeByKey(key) { | 
					
						
							|  |  |  |     return $tree.fancytree('getNodeByKey', key); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function getNoteIdFromNotePath(notePath) { | 
					
						
							| 
									
										
										
										
											2018-08-29 20:22:57 +02:00
										 |  |  |     if (!notePath) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const path = notePath.split("/"); | 
					
						
							| 
									
										
										
										
											2017-09-18 20:48:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 22:12:05 +02:00
										 |  |  |     const lastSegment = path[path.length - 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // path could have also tabId suffix
 | 
					
						
							|  |  |  |     return lastSegment.split("-")[0]; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  | async function getNotePath(node) { | 
					
						
							|  |  |  |     if (!node) { | 
					
						
							|  |  |  |         console.error("Node is null"); | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const path = []; | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  |     while (node && !await hoistedNoteService.isRootNode(node)) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (node.data.noteId) { | 
					
						
							|  |  |  |             path.push(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         node = node.getParent(); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 22:45:55 +02:00
										 |  |  |     if (node) { // null node can happen directly after unhoisting when tree is still hoisted but option has been changed already
 | 
					
						
							|  |  |  |         path.push(node.data.noteId); // root or hoisted noteId
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-21 22:42:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return path.reverse().join("/"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  | async function getNoteTitle(noteId, parentNoteId = null) { | 
					
						
							|  |  |  |     utils.assertArguments(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 15:27:22 +02:00
										 |  |  |     const note = await treeCache.getNote(noteId); | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         return "[not found]"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let {title} = note; | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (parentNoteId !== null) { | 
					
						
							| 
									
										
										
										
											2019-10-26 09:51:08 +02:00
										 |  |  |         const branchId = note.parentToBranch[parentNoteId]; | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 09:51:08 +02:00
										 |  |  |         if (branchId) { | 
					
						
							| 
									
										
										
										
											2019-10-26 09:58:00 +02:00
										 |  |  |             const branch = treeCache.getBranch(branchId); | 
					
						
							| 
									
										
										
										
											2019-10-26 09:51:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (branch && branch.prefix) { | 
					
						
							|  |  |  |                 title = branch.prefix + ' - ' + title; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return title; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNotePathTitle(notePath) { | 
					
						
							|  |  |  |     utils.assertArguments(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const titlePath = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 22:38:36 -04:00
										 |  |  |     if (notePath.startsWith('root/')) { | 
					
						
							|  |  |  |         notePath = notePath.substr(5); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // special case when we want just root's title
 | 
					
						
							|  |  |  |     if (notePath === 'root') { | 
					
						
							|  |  |  |         return await getNoteTitle(notePath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  |     let parentNoteId = 'root'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteId of notePath.split('/')) { | 
					
						
							|  |  |  |         titlePath.push(await getNoteTitle(noteId, parentNoteId)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parentNoteId = noteId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return titlePath.join(' / '); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     getParentProtectedStatus, | 
					
						
							|  |  |  |     getNodeByKey, | 
					
						
							|  |  |  |     getNotePath, | 
					
						
							|  |  |  |     getNoteIdFromNotePath, | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  |     getNoteTitle, | 
					
						
							|  |  |  |     getNotePathTitle, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |