11export class NoteManager {
22 constructor ( { el, notes} ) {
33 this . el = el ;
4- this . notes = notes . map ( note => new Note ( note ) ) ;
4+ this . notesEl = null ;
5+ this . notes = notes . map ( note => new Note ( note , this ) ) ;
56
7+ this . onNoteChange = ( ) => { } ;
8+ this . createNotesWrapper ( ) ;
69 this . renderNotes ( ) ;
710 }
811
912 addNote ( note ) {
10- this . notes . push ( note ) ;
13+ this . notes . push ( new Note ( note , this ) ) ;
14+ this . renderNotes ( ) ;
15+ }
16+
17+ prependNote ( note ) {
18+ this . notes . unshift ( new Note ( note , this ) ) ;
19+ this . renderNotes ( ) ;
1120 }
1221
1322 removeNote ( note ) {
1423 this . notes . splice ( this . notes . indexOf ( note ) , 1 ) ;
24+ this . renderNotes ( ) ;
25+ }
26+
27+ createNotesWrapper ( ) {
28+ this . notesEl = document . createElement ( 'div' ) ;
29+ this . notesEl . className = 'tc-notes' ;
30+ this . el . appendChild ( this . notesEl ) ;
1531 }
1632
1733 renderNotes ( ) {
18- this . el . innerHTML = '' ;
34+ this . notesEl . innerHTML = '' ;
1935 this . notes . forEach ( note => this . renderNote ( note ) ) ;
2036 }
2137
2238 renderNote ( note ) {
23- this . el . appendChild ( note . getNoteEl ( ) )
39+ this . notesEl . appendChild ( note . createNoteEl ( ) )
2440 }
2541}
2642
2743export class Note {
2844
29- constructor ( { title, body} ) {
45+ constructor ( { title, body} , noteManager ) {
46+ this . el = null ;
3047 this . title = title ;
3148 this . body = body ;
49+ this . noteManager = noteManager ;
3250 }
3351
3452 static getNoteTpl ( ) {
3553 return `
3654 <div class="tc-note">
3755 <div class="tc-note-header">
38- <span>
39- <i class="fas fa-plus "></i>
56+ <span class="tc-note-close" >
57+ <i class="fas fa-times "></i>
4058 </span>
4159 </div>
4260 <div class="tc-note-title" contenteditable="">
@@ -48,14 +66,33 @@ export class Note {
4866 </div>` ;
4967 }
5068
51- getNoteEl ( ) {
69+ createNoteEl ( ) {
5270 let tpl = Note . getNoteTpl ( ) ;
5371 tpl = tpl
5472 . replace ( '{{title}}' , this . title )
5573 . replace ( '{{body}}' , this . body )
5674 ;
5775 const div = document . createElement ( 'div' ) ;
5876 div . innerHTML = tpl ;
59- return div . children [ 0 ] ;
77+ this . el = div . children [ 0 ] ;
78+ this . attachEventListeners ( ) ;
79+ return this . el ;
80+ }
81+
82+ attachEventListeners ( ) {
83+ const btnClose = this . el . querySelector ( '.tc-note-close' ) ;
84+ btnClose . onclick = ( ) => {
85+ this . noteManager . removeNote ( this ) ;
86+ } ;
87+ const title = this . el . querySelector ( '.tc-note-title' ) ;
88+ title . oninput = ( ev ) => {
89+ this . title = ev . target . innerText ;
90+ this . noteManager . onNoteChange ( this ) ;
91+ } ;
92+ const body = this . el . querySelector ( '.tc-note-body' ) ;
93+ body . oninput = ( ev ) => {
94+ this . body = ev . target . innerText ;
95+ this . noteManager . onNoteChange ( this ) ;
96+ }
6097 }
6198}
0 commit comments