| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const glob = { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     activeDialog: null | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | // hot keys are active also inside inputs and content editables
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:04:08 -04:00
										 |  |  | jQuery.hotkeys.options.filterInputAcceptingElements = true; | 
					
						
							|  |  |  | jQuery.hotkeys.options.filterContentEditable = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(document).bind('keydown', 'alt+m', () => { | 
					
						
							| 
									
										
										
										
											2017-09-23 10:18:08 -04:00
										 |  |  |     const toggle = $(".hide-toggle"); | 
					
						
							| 
									
										
										
										
											2017-10-15 20:22:18 -04:00
										 |  |  |     const hidden = toggle.css('visibility') === 'hidden'; | 
					
						
							| 
									
										
										
										
											2017-09-23 10:18:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 20:22:18 -04:00
										 |  |  |     toggle.css('visibility', hidden ? 'visible' : 'hidden'); | 
					
						
							| 
									
										
										
										
											2017-09-23 10:18:08 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | // hide (toggle) everything except for the note content for distraction free writing
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(document).bind('keydown', 'alt+t', () => { | 
					
						
							| 
									
										
										
										
											2017-08-29 23:27:28 -04:00
										 |  |  |     const date = new Date(); | 
					
						
							| 
									
										
										
										
											2017-09-26 23:23:03 -04:00
										 |  |  |     const dateString = formatDateTime(date); | 
					
						
							| 
									
										
										
										
											2017-08-29 23:27:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $('#note-detail').summernote('insertText', dateString); | 
					
						
							| 
									
										
										
										
											2017-09-03 21:34:30 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 19:43:34 -05:00
										 |  |  | $(document).bind('keydown', 'f5', () => { | 
					
						
							| 
									
										
										
										
											2017-11-20 23:10:04 -05:00
										 |  |  |     window.location.reload(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).bind('keydown', 'ctrl+r', () => { | 
					
						
							|  |  |  |     window.location.reload(true); | 
					
						
							| 
									
										
										
										
											2017-11-16 19:43:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).bind('keydown', 'ctrl+shift+i', () => { | 
					
						
							|  |  |  |     if (isElectron()) { | 
					
						
							| 
									
										
										
										
											2017-11-20 23:10:04 -05:00
										 |  |  |         require('electron').remote.getCurrentWindow().toggleDevTools(); | 
					
						
							| 
									
										
										
										
											2017-11-16 19:43:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(window).on('beforeunload', () => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     // this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved
 | 
					
						
							|  |  |  |     // this sends the request asynchronously and doesn't wait for result
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |     noteEditor.saveNoteIfChanged(); | 
					
						
							| 
									
										
										
										
											2017-09-10 11:50:17 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Overrides the default autocomplete filter function to search for matched on atleast 1 word in each of the input term's words
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $.ui.autocomplete.filter = (array, terms) => { | 
					
						
							| 
									
										
										
										
											2017-09-18 20:07:56 -04:00
										 |  |  |     if (!terms) { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const startDate = new Date(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const results = []; | 
					
						
							|  |  |  |     const tokens = terms.toLowerCase().split(" "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const item of array) { | 
					
						
							|  |  |  |         let found = true; | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  |         const lcLabel = item.label.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2017-09-18 20:07:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const token of tokens) { | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  |             if (lcLabel.indexOf(token) === -1) { | 
					
						
							| 
									
										
										
										
											2017-09-18 20:07:56 -04:00
										 |  |  |                 found = false; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (found) { | 
					
						
							|  |  |  |             results.push(item); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.log("Search took " + (new Date().getTime() - startDate.getTime()) + "ms"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results; | 
					
						
							| 
									
										
										
										
											2017-10-11 20:37:27 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).tooltip({ | 
					
						
							|  |  |  |     items: ".note-editable a", | 
					
						
							|  |  |  |     content: function(callback) { | 
					
						
							| 
									
										
										
										
											2017-11-19 20:36:13 -05:00
										 |  |  |         const noteId = link.getNotePathFromLink($(this).attr("href")); | 
					
						
							| 
									
										
										
										
											2017-10-11 20:37:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (noteId !== null) { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |             noteEditor.loadNote(noteId).then(note => callback(note.detail.note_text)); | 
					
						
							| 
									
										
										
										
											2017-10-11 20:37:27 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     close: function(event, ui) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ui.tooltip.hover(function() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             $(this).stop(true).fadeTo(400, 1); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         function() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             $(this).fadeOut('400', function() | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 $(this).remove(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-20 23:43:20 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isElectron() { | 
					
						
							|  |  |  |     return window && window.process && window.process.type; | 
					
						
							| 
									
										
										
										
											2017-11-06 20:13:36 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 22:21:11 -05:00
										 |  |  | let appShown = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function showAppIfHidden() { | 
					
						
							|  |  |  |     if (!appShown) { | 
					
						
							|  |  |  |         appShown = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $("#container").show(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Get a reference to the loader's div
 | 
					
						
							|  |  |  |         const loaderDiv = document.getElementById("loader-wrapper"); | 
					
						
							|  |  |  |         // When the transition ends remove loader's div from display
 | 
					
						
							|  |  |  |         // so that we can access the map with gestures or clicks
 | 
					
						
							|  |  |  |         loaderDiv.addEventListener("transitionend", function(){ | 
					
						
							|  |  |  |             loaderDiv.style.display = "none"; | 
					
						
							|  |  |  |         }, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Kick off the CSS transition
 | 
					
						
							|  |  |  |         loaderDiv.style.opacity = 0.0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function initAjax() { | 
					
						
							|  |  |  |     $.ajaxSetup({ | 
					
						
							|  |  |  |         headers: { | 
					
						
							| 
									
										
										
										
											2017-11-16 19:25:27 -05:00
										 |  |  |             'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | initAjax(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // use wss for secure messaging
 | 
					
						
							|  |  |  | const ws = new WebSocket("ws://" + location.host); | 
					
						
							|  |  |  | ws.onopen = function (event) { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ws.onmessage = function (event) { | 
					
						
							|  |  |  |     console.log(event.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const message = JSON.parse(event.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (message.type === 'sync') { | 
					
						
							| 
									
										
										
										
											2017-11-26 08:24:27 -05:00
										 |  |  |         lastPingTs = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |         const data = message.data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (data.notes_tree) { | 
					
						
							|  |  |  |             console.log("Reloading tree because of background changes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteTree.reload(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) { | 
					
						
							|  |  |  |             showMessage('Reloading note because background change'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteEditor.reload(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-25 18:31:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const changesToPushCountEl = $("#changesToPushCount"); | 
					
						
							|  |  |  |         changesToPushCountEl.html(message.changesToPushCount); | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-26 08:24:27 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let lastPingTs = new Date().getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setInterval(() => { | 
					
						
							|  |  |  |     if (new Date().getTime() - lastPingTs > 5000) { | 
					
						
							|  |  |  |         showError("No communication with server"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }, 3000); |