| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const attributesDialog = (function() { | 
					
						
							|  |  |  |     const dialogEl = $("#attributes-dialog"); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     const attributesModel = new AttributesModel(); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     function AttributesModel() { | 
					
						
							|  |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         this.attributes = ko.observableArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.loadAttributes = async function() { | 
					
						
							|  |  |  |             const noteId = noteEditor.getCurrentNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const attributes = await server.get('notes/' + noteId + '/attributes'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             self.attributes(attributes.map(ko.observable)); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |             addLastEmptyRow(); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  |         function isValid() { | 
					
						
							|  |  |  |             for (let attrs = self.attributes(), i = 0; i < attrs.length; i++) { | 
					
						
							|  |  |  |                 if (self.isEmptyName(i) || self.isNotUnique(i)) { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         this.save = async function() { | 
					
						
							| 
									
										
										
										
											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-02-04 17:22:21 -05:00
										 |  |  |             const attributesToSave = self.attributes() | 
					
						
							|  |  |  |                 .map(attr => attr()) | 
					
						
							|  |  |  |                 .filter(attr => attr.attributeId !== "" || attr.name !== ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const attributes = await server.put('notes/' + noteId + '/attributes', attributesToSave); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             self.attributes(attributes.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
										 |  |  | 
 | 
					
						
							|  |  |  |             showMessage("Attributes have been saved."); | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2018-02-04 17:22:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function addLastEmptyRow() { | 
					
						
							|  |  |  |             const attrs = self.attributes(); | 
					
						
							|  |  |  |             const last = attrs[attrs.length - 1](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //            console.log("last", attrs.map(attr => attr()));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (last.name.trim() !== "" || last.value !== "") { | 
					
						
							|  |  |  |                 console.log("Adding new row"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 self.attributes.push(ko.observable({ | 
					
						
							|  |  |  |                     attributeId: '', | 
					
						
							|  |  |  |                     name: '', | 
					
						
							|  |  |  |                     value: '' | 
					
						
							|  |  |  |                 })); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.attributeChanged = function (row) { | 
					
						
							|  |  |  |             console.log(row); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             addLastEmptyRow(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const attr of self.attributes()) { | 
					
						
							|  |  |  |                 if (row.attributeId === attr().attributeId) { | 
					
						
							|  |  |  |                     attr.valueHasMutated(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isNotUnique = function(index) { | 
					
						
							|  |  |  |             const cur = self.attributes()[index](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (cur.name.trim() === "") { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (let attrs = self.attributes(), i = 0; i < attrs.length; i++) { | 
					
						
							|  |  |  |                 const attr = attrs[i](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (index !== i && cur.name === attr.name) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isEmptyName = function(index) { | 
					
						
							|  |  |  |             const cur = self.attributes()[index](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return cur.name.trim() === "" && (cur.attributeId !== "" || cur.value !== ""); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function showDialog() { | 
					
						
							|  |  |  |         glob.activeDialog = dialogEl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog({ | 
					
						
							|  |  |  |             modal: true, | 
					
						
							|  |  |  |             width: 800, | 
					
						
							| 
									
										
										
										
											2018-02-03 12:44:22 -05:00
										 |  |  |             height: 500 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         attributesModel.loadAttributes(); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'alt+a', e => { | 
					
						
							|  |  |  |         showDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-21 23:06:25 -05:00
										 |  |  |     ko.applyBindings(attributesModel, document.getElementById('attributes-dialog')); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     return { | 
					
						
							|  |  |  |         showDialog | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })(); |