| 
									
										
										
										
											2023-02-17 14:49:45 +01:00
										 |  |  | const BNote = require('../../src/becca/entities/bnote'); | 
					
						
							|  |  |  | const BBranch = require('../../src/becca/entities/bbranch'); | 
					
						
							|  |  |  | const BAttribute = require('../../src/becca/entities/battribute'); | 
					
						
							| 
									
										
										
										
											2022-01-10 17:09:20 +01:00
										 |  |  | const becca = require('../../src/becca/becca'); | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  | const randtoken = require('rand-token').generator({source: 'crypto'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 13:52:37 +01:00
										 |  |  | /** @returns {BNote} */ | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2023-01-03 13:52:37 +01:00
										 |  |  |         new BAttribute({ | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  |             attributeId: id(), | 
					
						
							|  |  |  |             noteId: this.note.noteId, | 
					
						
							|  |  |  |             type: 'label', | 
					
						
							|  |  |  |             isInheritable, | 
					
						
							|  |  |  |             name, | 
					
						
							|  |  |  |             value | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     relation(name, targetNote) { | 
					
						
							| 
									
										
										
										
											2023-01-03 13:52:37 +01:00
										 |  |  |         new BAttribute({ | 
					
						
							| 
									
										
										
										
											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 = "") { | 
					
						
							| 
									
										
										
										
											2023-01-03 13:52:37 +01:00
										 |  |  |         new BBranch({ | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 13:52:37 +01:00
										 |  |  |     const note = new BNote(row); | 
					
						
							| 
									
										
										
										
											2020-05-25 00:25:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return new NoteBuilder(note); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     NoteBuilder, | 
					
						
							|  |  |  |     findNoteByTitle, | 
					
						
							|  |  |  |     note | 
					
						
							|  |  |  | }; |