11import { notes , categories , icons } from './data.js' ;
22import { makeRandomID , getDatesFromText } from "./functions.js" ;
33
4-
54let statisticsTable = document . getElementById ( 'stats-table' ) ,
6- notesTable = document . getElementById ( 'active-archive-table' ) ;
7-
8- let activeNoteTableShown = true ;
9-
5+ notesTable = document . getElementById ( 'active-archive-table' ) ,
6+ activeNoteTableShown = true ;
107
8+ //dynamic loading svg icons so they could be styled
119function loadIconsIntoHeader ( ) {
1210 Array . from ( document . getElementsByClassName ( 'header-icon' ) ) . forEach ( col => {
1311 if ( col . classList . contains ( 'archive' ) )
1412 col . innerHTML = icons . ARCHIVE_ICON ;
1513 if ( col . classList . contains ( 'delete' ) )
1614 col . innerHTML = icons . DELETE_ICON ;
17- // Array.from(col.getElementsByTagName('path')).forEach( path => path.classList.add('header-icon'));
1815 } ) ;
1916}
2017
18+ //Note function
19+ function createNote ( note ) {
20+ notes . push ( note ) ;
21+ refreshTables ( ) ;
22+ showAnnouncer ( 'Note created successfully!' ) ;
23+ }
24+ function updateNote ( note ) {
25+ notes . splice ( notes . findIndex ( n => n . id === note . id ) , 1 , note ) ;
26+ refreshTables ( ) ;
27+ showAnnouncer ( 'Note updated successfully!' ) ;
28+ }
29+ function deleteNote ( noteID ) {
30+ notes . splice ( notes . indexOf ( notes . find ( note => note . id === noteID ) ) , 1 ) ;
31+ document . getElementById ( noteID ) . remove ( ) ;
32+ clearInnerHTML ( statisticsTable ) ;
33+ buildStatisticTable ( ) ;
34+ showAnnouncer ( 'Note deleted successfully!' ) ;
35+ }
36+ function changeArchiveState ( note ) {
37+ notes [ notes . findIndex ( n => n . id === note . id ) ] . archived = ! notes [ notes . findIndex ( n => n . id === note . id ) ] . archived ;
38+ refreshTables ( )
39+ showAnnouncer ( `Note ${ ( activeNoteTableShown ) ? 'archived' : 'unarchived' } successfully!` ) ;
40+ }
41+
42+ //announcer for users activity
43+ function showAnnouncer ( text ) {
44+ let announcer = document . getElementById ( 'announcer' )
45+ announcer . style . opacity = '1' ;
46+ announcer . innerText = text ;
47+ setTimeout ( ( ) => { announcer . style . opacity = '0' } , 1500 ) ;
48+ }
49+
2150function buildForm ( note ) {
2251 let form = document . createElement ( 'form' ) ;
2352
2453 form . innerHTML = `
25- <input type="text" name="name" value="${ typeof note . name === "string" ? note . name : '' } " placeholder="Name">
54+ <input type="text" name="name" value="${ typeof note . name === "string" ? note . name : '' } " placeholder="Name" required >
2655 <select name="categories">
2756 ` + Object . keys ( categories ) . map ( c => `<option value="${ c } " ${ note . category === c ? 'selected' : '' } >${ c } </option>` ) + `
2857 </select>
29- <input type="text" name="content" value="${ note . content ? note . content : '' } " placeholder="Content">
30- <input type="submit" value="Submit">
58+ <textarea name="content" placeholder="Content">${ note . content ? note . content : '' } </textarea>
59+ <input class="cancel" type="button" value="Cancel">
60+ <input id="submit-button" type="submit" value="Submit" >
3161 ` ;
62+ form . getElementsByClassName ( 'cancel' ) [ 0 ] . addEventListener ( "click" , ( ) => {
63+ document . getElementsByClassName ( 'wrapper-div' ) [ 0 ] . remove ( ) ;
64+ } ) ;
3265
3366 form . onsubmit = ( event ) => {
3467 event . preventDefault ( ) ;
3568 let newNote = {
36- id : ( note ) ? note . id : makeRandomID ( 10 ) ,
69+ id : ( typeof note . name === "string" ) ? note . id : makeRandomID ( 10 ) ,
3770 name : event . target . name . value ,
38- created : ( note ) ? note . created : new Date ( ) ,
71+ created : ( typeof note . name === "string" ) ? note . created : new Date ( ) ,
3972 category : event . target . categories . value ,
4073 content : event . target . content . value ,
4174 dates : getDatesFromText ( event . target . content . value ) ,
42- archived : ( note ) ? note . archived : false
75+ archived : ( typeof note . name === "string" ) ? note . archived : false
4376 }
4477
4578 if ( typeof note . name === "string" )
@@ -55,29 +88,10 @@ function buildForm(note){
5588 wrapperDiv . className = 'wrapper-div' ;
5689 wrapperDiv . append ( form ) ;
5790
58- document . body . append ( wrapperDiv ) ;
59- }
60-
61- //Note function
62- function createNote ( note ) {
63- notes . push ( note ) ;
64- refreshTables ( ) ;
65- }
66- function updateNote ( note ) {
67- notes . splice ( notes . findIndex ( n => n . id === note . id ) , 1 , note ) ;
68- refreshTables ( ) ;
69- }
70- function deleteNote ( noteID ) {
71- notes . splice ( notes . indexOf ( notes . find ( note => note . id === noteID ) ) , 1 ) ;
72- document . getElementById ( noteID ) . remove ( ) ;
73- clearInnerHTML ( statisticsTable ) ;
74- buildStatisticTable ( ) ;
75- }
76- function changeArchiveState ( note ) {
77- notes [ notes . findIndex ( n => n . id === note . id ) ] . archived = ! notes [ notes . findIndex ( n => n . id === note . id ) ] . archived ;
78- refreshTables ( )
91+ document . body . prepend ( wrapperDiv ) ;
7992}
8093
94+ //table functions
8195function refreshTables ( ) {
8296 clearAllTables ( ) ;
8397 buildNotesTable ( ) ;
@@ -133,15 +147,12 @@ function buildStatTr(category, active, total){
133147 </tr>
134148 ` ;
135149}
136-
137150function buildNotesTable ( ) {
138151 notes . forEach ( note => {
139152 if ( ! note . archived === activeNoteTableShown )
140153 notesTable . append ( buildNotesTr ( note ) ) ;
141154 } )
142-
143155}
144-
145156function buildNotesTr ( note ) {
146157 let tr = document . createElement ( 'tr' ) ;
147158 tr . id = note . id ;
@@ -163,7 +174,7 @@ function buildNotesTr(note){
163174
164175 tdArchive . className = "row-icon archive" ;
165176 tdArchive . addEventListener ( "click" , ( ) => { changeArchiveState ( note ) } ) ;
166- tdArchive . innerHTML = ( activeNoteTableShown ) ? icons . UNARCHIVE_ICON : icons . ARCHIVE_ICON ;
177+ tdArchive . innerHTML = ( activeNoteTableShown ) ? icons . ARCHIVE_ICON : icons . UNARCHIVE_ICON ;
167178
168179 tdDelete . className = "row-icon delete" ;
169180 tdDelete . addEventListener ( "click" , ( ) => { deleteNote ( note . id ) } ) ;
@@ -173,19 +184,21 @@ function buildNotesTr(note){
173184 return tr ;
174185}
175186
176- export {
177- refreshTables ,
178- loadIconsIntoHeader ,
179- notes
180- }
181187
182188function switchTables ( ) {
183189 activeNoteTableShown = ! activeNoteTableShown ;
184190 clearInnerHTML ( notesTable ) ;
185191 buildNotesTable ( ) ;
192+ document . getElementById ( 'table-name' ) . innerText = activeNoteTableShown ? "Active notes" : "Archived notes" ;
186193 document . getElementsByClassName ( 'header-icon archive' ) [ 0 ] . innerHTML = ( activeNoteTableShown ) ? icons . ARCHIVE_ICON : icons . UNARCHIVE_ICON ;
187194}
188195
196+ export {
197+ refreshTables ,
198+ loadIconsIntoHeader ,
199+ notes
200+ }
201+
189202document . getElementById ( "table-switcher" ) . addEventListener ( "click" , switchTables ) ;
190203document . getElementById ( "create-note-button" ) . addEventListener ( "click" , buildForm ) ;
191- // document.getElementById("build-stats").addEventListener("click", refreshTables);
204+
0 commit comments