1+ const addBox = document . querySelector ( ".add-box" ) ,
2+ popupBox = document . querySelector ( ".popup-box" ) ,
3+ search = document . getElementById ( 'searchTxt' ) ,
4+ popupTitle = popupBox . querySelector ( "header p" ) ,
5+ titleTag = popupBox . querySelector ( "input" ) ,
6+ closeIcon = popupBox . querySelector ( "header i" ) ,
7+ descTag = popupBox . querySelector ( "textarea" ) ,
8+ addBtn = popupBox . querySelectorAll ( "button" ) ;
9+
10+ const months = [ "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" ] ;
11+
12+ const notes = JSON . parse ( localStorage . getItem ( "notes" ) || "[]" ) ;
13+ let isUpdate = false , updateId ;
14+
15+ addBox . addEventListener ( "click" , ( ) => {
16+ titleTag . focus ( ) ;
17+ popupBox . classList . add ( "show" ) ;
18+ } )
19+ closeIcon . addEventListener ( "click" , ( ) => {
20+ isUpdate = false ;
21+ titleTag . value = "" ;
22+ descTag . value = "" ;
23+ addBtn [ 0 ] . innerText = "Add Note" ;
24+ popupTitle . innerText = "Add a New Note" ;
25+ popupBox . classList . remove ( "show" ) ;
26+ } )
27+
28+ function showNote ( ) {
29+ document . querySelectorAll ( ".note" ) . forEach ( note => note . remove ( ) ) ;
30+ notes . forEach ( ( note , index ) => {
31+ liTag = `<li class="note">
32+ <div class="details">
33+ <p>${ note . title } </p>
34+ <span>${ note . description } </span>
35+ </div>
36+ <div class="bottom-content">
37+ <span>${ note . date } </span>
38+ <div class="settings">
39+ <i onclick="showMenu(this)" class="uil uil-ellipsis-h"></i>
40+ <ul class="menu">
41+ <li onclick="updateNote(${ index } ,'${ note . title } ','${ note . description } ')"> <i class="uil uil-pen"></i>Edit</li>
42+ <li onclick="deleteNote(${ index } )"> <i class="uil uil-trash"></i>Delete</li>
43+ </ul>
44+ </div>
45+ </div>
46+ </li>`
47+ addBox . insertAdjacentHTML ( "afterend" , liTag ) ;
48+ } ) ;
49+ }
50+ showNote ( ) ;
51+
52+ function showMenu ( elem ) {
53+ elem . parentElement . classList . add ( "show" ) ;
54+ document . addEventListener ( "click" , e => {
55+
56+ if ( e . target . tagName != "I" || e . target != elem ) {
57+ elem . parentElement . classList . remove ( "show" ) ;
58+ }
59+ } )
60+ }
61+ function deleteNote ( noteId ) {
62+ let confirmDel = confirm ( "Are you sure you want to delete this note?" ) ;
63+ if ( ! confirmDel ) return ;
64+ notes . splice ( noteId , 1 ) ; //deletes one note of given id
65+ localStorage . setItem ( "notes" , JSON . stringify ( notes ) ) ; //saving updated notes to local storage
66+ showNote ( ) ;
67+ }
68+
69+ function updateNote ( noteId , title , desc ) {
70+ isUpdate = true ;
71+ updateId = noteId ;
72+ addBox . click ( ) ;
73+ titleTag . value = title ;
74+ descTag . value = desc ;
75+ addBtn [ 0 ] . innerText = "Update Note" ;
76+ popupTitle . innerText = "Update a Note" ;
77+ console . log ( noteId , title , desc ) ;
78+ }
79+
80+ for ( var i = 0 ; i < addBtn . length ; i ++ ) {
81+ addBtn [ i ] . addEventListener ( "click" , e => {
82+ e . preventDefault ( ) ;
83+ }
84+ ) }
85+ addBtn [ 0 ] . addEventListener ( "click" , e => {
86+ let noteTitle = titleTag . value . toUpperCase ( ) ,
87+ noteDesc = descTag . value ;
88+
89+ if ( noteTitle || noteDesc ) {
90+ let dateObj = new Date ( ) ,
91+ day = dateObj . getDate ( ) ,
92+ month = months [ dateObj . getMonth ( ) ] ,
93+ year = dateObj . getFullYear ( ) ;
94+
95+ let noteInfo = {
96+ title : noteTitle , description : noteDesc ,
97+ date : `${ month } ${ day } , ${ year } `
98+ }
99+ if ( ! isUpdate ) {
100+ notes . push ( noteInfo ) ; //adding new notes
101+ }
102+ else {
103+ isUpdate = false ;
104+ notes [ updateId ] = noteInfo ;
105+ }
106+ localStorage . setItem ( "notes" , JSON . stringify ( notes ) ) ; //saving notes to local storage
107+ closeIcon . click ( ) ;
108+ showNote ( ) ;
109+ }
110+ } )
111+
112+ search . addEventListener ( "input" , function ( ) {
113+
114+ let inputVal = search . value . toUpperCase ( ) ;
115+ let noteCard = document . getElementsByClassName ( 'note' ) ;
116+ Array . from ( noteCard ) . forEach ( function ( element ) {
117+ let cardTxt = element . getElementsByTagName ( "p" ) [ 0 ] . innerText ; //searching by title
118+ if ( cardTxt . includes ( inputVal ) ) {
119+ element . style . display = "block" ;
120+ }
121+ else {
122+ element . style . display = "none" ;
123+ }
124+ //console.log(cardTxt);
125+
126+ } )
127+ } )
0 commit comments