| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | import server from "./server.js"; | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  | import noteDetailService from "./note_detail.js"; | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  | // this key needs to have this value so it's hit by the tooltip
 | 
					
						
							|  |  |  | const SELECTED_PATH_KEY = "data-note-path"; | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 00:23:50 +01:00
										 |  |  | async function autocompleteSource(term, cb) { | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |     const result = await server.get('autocomplete' | 
					
						
							| 
									
										
										
										
											2018-11-07 00:23:50 +01:00
										 |  |  |         + '?query=' + encodeURIComponent(term) | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |         + '¤tNoteId=' + noteDetailService.getCurrentNoteId()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:35:29 +01:00
										 |  |  |     if (result.length === 0) { | 
					
						
							|  |  |  |         result.push({ | 
					
						
							|  |  |  |             title: "No results", | 
					
						
							|  |  |  |             path: "" | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-07 09:35:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     cb(result); | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  | function clearText($el) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |     $el.setSelectedPath(""); | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  |     $el.autocomplete("val", "").change(); | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  | function showRecentNotes($el) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |     $el.setSelectedPath(""); | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |     $el.autocomplete("val", ""); | 
					
						
							|  |  |  |     $el.autocomplete("open"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:28:52 +01:00
										 |  |  | function initNoteAutocomplete($el, options) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |     if (!$el.hasClass("note-autocomplete-input")) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:28:52 +01:00
										 |  |  |         options = options || {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |         $el.addClass("note-autocomplete-input"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |         const $clearTextButton = $("<a>") | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  |                 .addClass("input-group-text input-clearer-button jam jam-close") | 
					
						
							|  |  |  |                 .prop("title", "Clear text field"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |         const $showRecentNotesButton = $("<a>") | 
					
						
							| 
									
										
										
										
											2018-11-09 11:57:52 +01:00
										 |  |  |                 .addClass("input-group-text show-recent-notes-button jam jam-clock") | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  |                 .prop("title", "Show recent notes"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |         const $goToSelectedNoteButton = $("<a>") | 
					
						
							|  |  |  |             .addClass("input-group-text go-to-selected-note-button jam jam-arrow-right"); | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:28:52 +01:00
										 |  |  |         const $sideButtons = $("<div>") | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  |             .addClass("input-group-append") | 
					
						
							|  |  |  |             .append($clearTextButton) | 
					
						
							| 
									
										
										
										
											2018-11-14 11:28:52 +01:00
										 |  |  |             .append($showRecentNotesButton); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!options.hideGoToSelectedNoteButton) { | 
					
						
							|  |  |  |             $sideButtons.append($goToSelectedNoteButton); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $el.after($sideButtons); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 23:16:26 +01:00
										 |  |  |         $clearTextButton.click(() => clearText($el)); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |         $showRecentNotesButton.click(() => showRecentNotes($el)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |         $goToSelectedNoteButton.click(() => { | 
					
						
							|  |  |  |             if ($el.hasClass("disabled")) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeService.activateNote($el.getSelectedPath()); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |         $el.autocomplete({ | 
					
						
							|  |  |  |             appendTo: document.querySelector('body'), | 
					
						
							|  |  |  |             hint: false, | 
					
						
							|  |  |  |             autoselect: true, | 
					
						
							|  |  |  |             openOnFocus: true, | 
					
						
							|  |  |  |             minLength: 0 | 
					
						
							|  |  |  |         }, [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 source: autocompleteSource, | 
					
						
							|  |  |  |                 displayKey: 'title', | 
					
						
							|  |  |  |                 templates: { | 
					
						
							|  |  |  |                     suggestion: function(suggestion) { | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |                         return suggestion.highlighted; | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  |         $el.on('autocomplete:selected', (event, suggestion) => $el.setSelectedPath(suggestion.path)); | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |         $el.on('autocomplete:closed', () => { | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  |             if (!$el.val().trim()) { | 
					
						
							| 
									
										
										
										
											2018-11-19 21:58:52 +01:00
										 |  |  |                 clearText($el); | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $el; | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  | $.fn.getSelectedPath = function() { | 
					
						
							|  |  |  |     if (!$(this).val().trim()) { | 
					
						
							|  |  |  |         return ""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |         return $(this).attr(SELECTED_PATH_KEY); | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  | $.fn.setSelectedPath = function(path) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |     path = path || ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |     $(this).attr(SELECTED_PATH_KEY, path); | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $(this) | 
					
						
							|  |  |  |         .closest(".input-group") | 
					
						
							|  |  |  |         .find(".go-to-selected-note-button") | 
					
						
							| 
									
										
										
										
											2018-11-14 19:33:48 +01:00
										 |  |  |         .toggleClass("disabled", !path.trim()) | 
					
						
							|  |  |  |         .attr(SELECTED_PATH_KEY, path); // we also set attr here so tooltip can be displayed
 | 
					
						
							| 
									
										
										
										
											2018-11-14 00:05:09 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | ko.bindingHandlers.noteAutocomplete = { | 
					
						
							|  |  |  |     init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | 
					
						
							|  |  |  |         initNoteAutocomplete($(element)); | 
					
						
							| 
									
										
										
										
											2018-11-07 20:32:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |         $(element).setSelectedPath(bindingContext.$data.selectedPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 20:32:11 +01:00
										 |  |  |         $(element).on('autocomplete:selected', function(event, suggestion, dataset) { | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |             bindingContext.$data.selectedPath = $(element).val().trim() ? suggestion.path : ''; | 
					
						
							| 
									
										
										
										
											2018-11-07 20:32:11 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |     initNoteAutocomplete, | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |     showRecentNotes | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | } |