| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from '../services/tree.js'; | 
					
						
							|  |  |  | import messagingService from '../services/messaging.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import server from '../services/server.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  | import utils from "../services/utils.js"; | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  | import treeUtils from "../services/tree_utils.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const $dialog = $("#recent-notes-dialog"); | 
					
						
							|  |  |  | const $searchInput = $('#recent-notes-search-input'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // list of recent note paths
 | 
					
						
							|  |  |  | let list = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function reload() { | 
					
						
							|  |  |  |     const result = await server.get('recent-notes'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     list = result.map(r => r.notePath); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function addRecentNote(branchId, notePath) { | 
					
						
							|  |  |  |     setTimeout(async () => { | 
					
						
							|  |  |  |         // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							|  |  |  |         if (notePath && notePath === treeService.getCurrentNotePath()) { | 
					
						
							|  |  |  |             const result = await server.put('recent-notes/' + branchId + '/' + encodeURIComponent(notePath)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             list = result.map(r => r.notePath); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, 1500); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 22:42:46 -04:00
										 |  |  | async function getNoteTitle(notePath) { | 
					
						
							|  |  |  |     let noteTitle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         noteTitle = await treeUtils.getNotePathTitle(notePath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         noteTitle = "[error - can't find note title]"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         messagingService.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return noteTitle; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | async function showDialog() { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     glob.activeDialog = $dialog; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $dialog.dialog({ | 
					
						
							|  |  |  |         modal: true, | 
					
						
							|  |  |  |         width: 800, | 
					
						
							|  |  |  |         height: 100, | 
					
						
							|  |  |  |         position: { my: "center top+100", at: "top", of: window } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-02-05 23:50:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $searchInput.val(''); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     // remove the current note
 | 
					
						
							|  |  |  |     const recNotes = list.filter(note => note !== treeService.getCurrentNotePath()); | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     const items = []; | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     for (const notePath of recNotes) { | 
					
						
							|  |  |  |         items.push({ | 
					
						
							| 
									
										
										
										
											2018-03-27 22:42:46 -04:00
										 |  |  |             label: await getNoteTitle(notePath), | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |             value: notePath | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $searchInput.autocomplete({ | 
					
						
							|  |  |  |         source: items, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         minLength: 0, | 
					
						
							|  |  |  |         autoFocus: true, | 
					
						
							|  |  |  |         select: function (event, ui) { | 
					
						
							|  |  |  |             treeService.activateNode(ui.item.value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $searchInput.autocomplete('destroy'); | 
					
						
							|  |  |  |             $dialog.dialog('close'); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         focus: function (event, ui) { | 
					
						
							|  |  |  |             event.preventDefault(); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         close: function (event, ui) { | 
					
						
							|  |  |  |             if (event.keyCode === 27) { // escape closes dialog
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 $searchInput.autocomplete('destroy'); | 
					
						
							|  |  |  |                 $dialog.dialog('close'); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 // keep autocomplete open
 | 
					
						
							|  |  |  |                 // we're kind of abusing autocomplete to work in a way which it's not designed for
 | 
					
						
							|  |  |  |                 $searchInput.autocomplete("search", ""); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         create: () => $searchInput.autocomplete("search", ""), | 
					
						
							|  |  |  |         classes: { | 
					
						
							|  |  |  |             "ui-autocomplete": "recent-notes-autocomplete" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-06 22:46:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:13:26 -04:00
										 |  |  | setTimeout(reload, 100); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:16:57 -04:00
										 |  |  | messagingService.subscribeToMessages(syncData => { | 
					
						
							|  |  |  |     if (syncData.some(sync => sync.entityName === 'recent_notes')) { | 
					
						
							|  |  |  |         console.log(utils.now(), "Reloading recent notes because of background changes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         reload(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog, | 
					
						
							|  |  |  |     addRecentNote, | 
					
						
							|  |  |  |     reload | 
					
						
							|  |  |  | }; |