| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const Note = require('../../src/becca/entities/note.js'); | 
					
						
							|  |  |  | const Branch = require('../../src/becca/entities/branch.js'); | 
					
						
							|  |  |  | const Attribute = require('../../src/becca/entities/attribute.js'); | 
					
						
							|  |  |  | const becca = require('../../src/becca/becca.js'); | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  | const randtoken = require('rand-token').generator({source: 'crypto'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @return {Note} */ | 
					
						
							|  |  |  | function findNoteByTitle(searchResults, title) { | 
					
						
							|  |  |  |     return searchResults | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         .map(sr => becca.notes[sr.noteId]) | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |         .find(note => note.title === title); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NoteBuilder { | 
					
						
							|  |  |  |     constructor(note) { | 
					
						
							|  |  |  |         this.note = note; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     label(name, value = '', isInheritable = false) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         new Attribute(becca, { | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |             attributeId: id(), | 
					
						
							|  |  |  |             noteId: this.note.noteId, | 
					
						
							|  |  |  |             type: 'label', | 
					
						
							|  |  |  |             isInheritable, | 
					
						
							|  |  |  |             name, | 
					
						
							|  |  |  |             value | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     relation(name, targetNote) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         new Attribute(becca, { | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |             attributeId: id(), | 
					
						
							|  |  |  |             noteId: this.note.noteId, | 
					
						
							|  |  |  |             type: 'relation', | 
					
						
							|  |  |  |             name, | 
					
						
							|  |  |  |             value: targetNote.noteId | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     child(childNoteBuilder, prefix = "") { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         new Branch(becca, { | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |             branchId: id(), | 
					
						
							|  |  |  |             noteId: childNoteBuilder.note.noteId, | 
					
						
							|  |  |  |             parentNoteId: this.note.noteId, | 
					
						
							| 
									
										
										
										
											2020-12-14 23:59:05 +01:00
										 |  |  |             prefix, | 
					
						
							|  |  |  |             notePosition: 10 | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function id() { | 
					
						
							|  |  |  |     return randtoken.generate(10); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-27 22:45:22 +01:00
										 |  |  | function note(title, extraParams = {}) { | 
					
						
							|  |  |  |     const row = Object.assign({ | 
					
						
							|  |  |  |         noteId: id(), | 
					
						
							|  |  |  |         title: title, | 
					
						
							|  |  |  |         type: 'text', | 
					
						
							|  |  |  |         mime: 'text/html' | 
					
						
							|  |  |  |     }, extraParams); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 21:34:57 +02:00
										 |  |  |     const note = new Note(row); | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return new NoteBuilder(note); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     NoteBuilder, | 
					
						
							|  |  |  |     findNoteByTitle, | 
					
						
							|  |  |  |     note | 
					
						
							|  |  |  | }; |