| 
									
										
										
										
											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-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-07 09:51:14 +01:00
										 |  |  | function showRecentNotes($el) { | 
					
						
							|  |  |  |     $el.autocomplete("val", ""); | 
					
						
							|  |  |  |     $el.autocomplete("open"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function initNoteAutocomplete($el) { | 
					
						
							|  |  |  |     if (!$el.hasClass("aa-input")) { | 
					
						
							| 
									
										
										
										
											2018-11-05 21:12:03 +01:00
										 |  |  |         const $showRecentNotesButton = $("<div>").addClass("input-group-append").append( | 
					
						
							|  |  |  |             $("<span>") | 
					
						
							| 
									
										
										
										
											2018-11-09 11:57:52 +01:00
										 |  |  |                 .addClass("input-group-text show-recent-notes-button jam jam-clock") | 
					
						
							| 
									
										
										
										
											2018-11-05 21:12:03 +01:00
										 |  |  |                 .prop("title", "Show recent notes")); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $el.after($showRecentNotesButton); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:51:14 +01:00
										 |  |  |         $showRecentNotesButton.click(() => showRecentNotes($el)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $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
										 |  |  | 
 | 
					
						
							|  |  |  |         $el.on('autocomplete:selected', function(event, suggestion, dataset) { | 
					
						
							|  |  |  |             $el.prop("data-selected-path", suggestion.path); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |         $el.on('autocomplete:closed', () => { | 
					
						
							|  |  |  |             $el.prop("data-selected-path", ""); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  |         return $(this).prop("data-selected-path"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |         $(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
										 |  |  | } |