| 
									
										
										
										
											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()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (result.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-11-07 00:23:50 +01:00
										 |  |  |         cb(result.map(row => { | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |             return { | 
					
						
							|  |  |  |                 label: row.label, | 
					
						
							|  |  |  |                 value: row.label + ' (' + row.value + ')' | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-11-07 00:23:50 +01:00
										 |  |  |         cb([{ | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |             label: "No results", | 
					
						
							|  |  |  |             value: "No results" | 
					
						
							|  |  |  |         }]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function initNoteAutocomplete($el) { | 
					
						
							|  |  |  |     if (!$el.hasClass("ui-autocomplete-input")) { | 
					
						
							| 
									
										
										
										
											2018-11-05 21:12:03 +01:00
										 |  |  |         const $showRecentNotesButton = $("<div>").addClass("input-group-append").append( | 
					
						
							|  |  |  |             $("<span>") | 
					
						
							|  |  |  |                 .addClass("input-group-text show-recent-notes-button") | 
					
						
							|  |  |  |                 .prop("title", "Show recent notes")); | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $el.after($showRecentNotesButton); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $showRecentNotesButton.click(() => $el.autocomplete("search", "")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await $el.autocomplete({ | 
					
						
							|  |  |  |             appendTo: $el.parent().parent(), | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |             source: autocompleteSource, | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  |             minLength: 0, | 
					
						
							|  |  |  |             change: function (event, ui) { | 
					
						
							|  |  |  |                 $el.trigger("change"); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             select: function (event, ui) { | 
					
						
							|  |  |  |                 if (ui.item.value === 'No results') { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ko.bindingHandlers.noteAutocomplete = { | 
					
						
							|  |  |  |     init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | 
					
						
							|  |  |  |         initNoteAutocomplete($(element)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |     initNoteAutocomplete, | 
					
						
							|  |  |  |     autocompleteSource | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  | } |