| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | const labelsDialog = (function() { | 
					
						
							|  |  |  |     const $showDialogButton = $(".show-labels-button"); | 
					
						
							|  |  |  |     const $dialog = $("#labels-dialog"); | 
					
						
							|  |  |  |     const $saveLabelsButton = $("#save-labels-button"); | 
					
						
							|  |  |  |     const $labelsBody = $('#labels-table tbody'); | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const labelsModel = new LabelsModel(); | 
					
						
							|  |  |  |     let labelNames = []; | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     function LabelsModel() { | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         this.labels = ko.observableArray(); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         this.loadLabels = async function() { | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |             const noteId = noteEditor.getCurrentNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const labels = await server.get('notes/' + noteId + '/labels'); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             self.labels(labels.map(ko.observable)); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             addLastEmptyRow(); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             labelNames = await server.get('labels/names'); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             // label might not be rendered immediatelly so could not focus
 | 
					
						
							|  |  |  |             setTimeout(() => $(".label-name:last").focus(), 100); | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             $labelsBody.sortable({ | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |                 handle: '.handle', | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 containment: $labelsBody, | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |                 update: function() { | 
					
						
							|  |  |  |                     let position = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                     // we need to update positions by searching in the DOM, because order of the
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                     // labels in the viewmodel (self.labels()) stays the same
 | 
					
						
							|  |  |  |                     $labelsBody.find('input[name="position"]').each(function() { | 
					
						
							|  |  |  |                         const attr = self.getTargetLabel(this); | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         attr().position = position++; | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         this.deleteLabel = function(data, event) { | 
					
						
							|  |  |  |             const attr = self.getTargetLabel(event.target); | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  |             const attrData = attr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (attrData) { | 
					
						
							|  |  |  |                 attrData.isDeleted = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 attr(attrData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 addLastEmptyRow(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |         function isValid() { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             for (let attrs = self.labels(), i = 0; i < attrs.length; i++) { | 
					
						
							| 
									
										
										
										
											2018-02-06 21:18:09 -05:00
										 |  |  |                 if (self.isEmptyName(i)) { | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         this.save = async function() { | 
					
						
							| 
									
										
										
										
											2018-02-04 23:16:45 -05:00
										 |  |  |             // we need to defocus from input (in case of enter-triggered save) because value is updated
 | 
					
						
							|  |  |  |             // on blur event (because of conflict with jQuery UI Autocomplete). Without this, input would
 | 
					
						
							|  |  |  |             // stay in focus, blur wouldn't be triggered and change wouldn't be updated in the viewmodel.
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             $saveLabelsButton.focus(); | 
					
						
							| 
									
										
										
										
											2018-02-04 23:16:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             if (!isValid()) { | 
					
						
							|  |  |  |                 alert("Please fix all validation errors and try saving again."); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |             const noteId = noteEditor.getCurrentNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const labelsToSave = self.labels() | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |                 .map(attr => attr()) | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 .filter(attr => attr.labelId !== "" || attr.name !== ""); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const labels = await server.put('notes/' + noteId + '/labels', labelsToSave); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             self.labels(labels.map(ko.observable)); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             addLastEmptyRow(); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  |             utils.showMessage("Labels have been saved."); | 
					
						
							| 
									
										
										
										
											2018-02-04 20:23:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             noteEditor.loadLabelList(); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function addLastEmptyRow() { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const attrs = self.labels().filter(attr => attr().isDeleted === 0); | 
					
						
							| 
									
										
										
										
											2018-02-05 21:07:18 -05:00
										 |  |  |             const last = attrs.length === 0 ? null : attrs[attrs.length - 1](); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 21:07:18 -05:00
										 |  |  |             if (!last || last.name.trim() !== "" || last.value !== "") { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 self.labels.push(ko.observable({ | 
					
						
							|  |  |  |                     labelId: '', | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |                     name: '', | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  |                     value: '', | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |                     isDeleted: 0, | 
					
						
							|  |  |  |                     position: 0 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |                 })); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         this.labelChanged = function (data, event) { | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             addLastEmptyRow(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const attr = self.getTargetLabel(event.target); | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             attr.valueHasMutated(); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isNotUnique = function(index) { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const cur = self.labels()[index](); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (cur.name.trim() === "") { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             for (let attrs = self.labels(), i = 0; i < attrs.length; i++) { | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |                 const attr = attrs[i](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (index !== i && cur.name === attr.name) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isEmptyName = function(index) { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const cur = self.labels()[index](); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             return cur.name.trim() === "" && (cur.labelId !== "" || cur.value !== ""); | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         this.getTargetLabel = function(target) { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |             const context = ko.contextFor(target); | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  |             const index = context.$index(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             return self.labels()[index]; | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function showDialog() { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |         glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         await labelsModel.loadLabels(); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |         $dialog.dialog({ | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |             modal: true, | 
					
						
							|  |  |  |             width: 800, | 
					
						
							| 
									
										
										
										
											2018-02-03 12:44:22 -05:00
										 |  |  |             height: 500 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'alt+a', e => { | 
					
						
							|  |  |  |         showDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     ko.applyBindings(labelsModel, document.getElementById('labels-dialog')); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     $(document).on('focus', '.label-name', function (e) { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |         if (!$(this).hasClass("ui-autocomplete-input")) { | 
					
						
							|  |  |  |             $(this).autocomplete({ | 
					
						
							|  |  |  |                 // shouldn't be required and autocomplete should just accept array of strings, but that fails
 | 
					
						
							|  |  |  |                 // because we have overriden filter() function in init.js
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 source: labelNames.map(attr => { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |                     return { | 
					
						
							|  |  |  |                         label: attr, | 
					
						
							|  |  |  |                         value: attr | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }), | 
					
						
							|  |  |  |                 minLength: 0 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $(this).autocomplete("search", $(this).val()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     $(document).on('focus', '.label-value', async function (e) { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |         if (!$(this).hasClass("ui-autocomplete-input")) { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const labelName = $(this).parent().parent().find('.label-name').val(); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             if (labelName.trim() === "") { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             const labelValues = await server.get('labels/values/' + encodeURIComponent(labelName)); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             if (labelValues.length === 0) { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $(this).autocomplete({ | 
					
						
							|  |  |  |                 // shouldn't be required and autocomplete should just accept array of strings, but that fails
 | 
					
						
							|  |  |  |                 // because we have overriden filter() function in init.js
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 source: labelValues.map(attr => { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  |                     return { | 
					
						
							|  |  |  |                         label: attr, | 
					
						
							|  |  |  |                         value: attr | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }), | 
					
						
							|  |  |  |                 minLength: 0 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $(this).autocomplete("search", $(this).val()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 00:54:50 -04:00
										 |  |  |     $showDialogButton.click(showDialog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     return { | 
					
						
							|  |  |  |         showDialog | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })(); |