1
1
import { notes , categories , icons } from './data.js' ;
2
2
import { makeRandomID , getDatesFromText } from "./functions.js" ;
3
3
4
-
5
4
let 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 ;
10
7
8
+ //dynamic loading svg icons so they could be styled
11
9
function loadIconsIntoHeader ( ) {
12
10
Array . from ( document . getElementsByClassName ( 'header-icon' ) ) . forEach ( col => {
13
11
if ( col . classList . contains ( 'archive' ) )
14
12
col . innerHTML = icons . ARCHIVE_ICON ;
15
13
if ( col . classList . contains ( 'delete' ) )
16
14
col . innerHTML = icons . DELETE_ICON ;
17
- // Array.from(col.getElementsByTagName('path')).forEach( path => path.classList.add('header-icon'));
18
15
} ) ;
19
16
}
20
17
18
+ //Note function
19
+ function createNote ( note ) {
20
+ notes . push ( note ) ;
21
+ refreshTables ( ) ;
22
+ showAnnouncer ( 'Note created successfully!' ) ;
23
+ }
24
+ function updateNote ( note ) {
25
+ try {
26
+ let index = notes . findIndex ( n => n . id === note . id ) ;
27
+ if ( index < 0 )
28
+ throw "There is no such note!" ;
29
+ notes . splice ( index , 1 , note ) ;
30
+ refreshTables ( ) ;
31
+ showAnnouncer ( 'Note updated successfully!' ) ;
32
+ } catch ( e ) {
33
+ console . error ( e ) ;
34
+ showAnnouncer ( "Note wasn't updated!" , true ) ;
35
+ }
36
+ }
37
+ function deleteNote ( noteID ) {
38
+ try {
39
+ let index = notes . findIndex ( n => n . id === noteID ) ;
40
+ if ( index < 0 )
41
+ throw "There is no such note!" ;
42
+ notes . splice ( index , 1 ) ;
43
+ document . getElementById ( noteID ) . remove ( ) ;
44
+ clearInnerHTML ( statisticsTable ) ;
45
+ buildStatisticTable ( ) ;
46
+ showAnnouncer ( 'Note deleted successfully!' ) ;
47
+ } catch ( e ) {
48
+ console . error ( e ) ;
49
+ showAnnouncer ( "Note wasn't updated!" , true ) ;
50
+ }
51
+ }
52
+ function changeArchiveState ( note ) {
53
+ notes [ notes . findIndex ( n => n . id === note . id ) ] . archived = ! notes [ notes . findIndex ( n => n . id === note . id ) ] . archived ;
54
+ refreshTables ( )
55
+ showAnnouncer ( `Note ${ ( activeNoteTableShown ) ? 'archived' : 'unarchived' } successfully!` ) ;
56
+ }
57
+
58
+ //announcer for users activity
59
+ function showAnnouncer ( text , error ) {
60
+ let announcer = document . getElementById ( 'announcer' )
61
+ announcer . style . opacity = '1' ;
62
+ announcer . innerText = text ;
63
+ if ( error )
64
+ announcer . classList . add ( 'invalid-input' ) ;
65
+ setTimeout ( ( ) => {
66
+ announcer . style . opacity = '0' ;
67
+ if ( error )
68
+ announcer . classList . remove ( 'invalid-input' ) ;
69
+ } , 1500 ) ;
70
+ }
71
+
21
72
function buildForm ( note ) {
22
73
let form = document . createElement ( 'form' ) ;
23
74
24
75
form . innerHTML = `
25
- <input type="text" name="name" value="${ typeof note . name === "string" ? note . name : '' } " placeholder="Name">
76
+ <input type="text" name="name" value="${ typeof note . name === "string" ? note . name : '' } " placeholder="Name" required >
26
77
<select name="categories">
27
78
` + Object . keys ( categories ) . map ( c => `<option value="${ c } " ${ note . category === c ? 'selected' : '' } >${ c } </option>` ) + `
28
79
</select>
29
- <input type="text" name="content" value="${ note . content ? note . content : '' } " placeholder="Content">
30
- <input type="submit" value="Submit">
80
+ <textarea name="content" placeholder="Content">${ note . content ? note . content : '' } </textarea>
81
+ <input class="cancel" type="button" value="Cancel">
82
+ <input id="submit-button" type="submit" value="Submit" >
31
83
` ;
84
+ form . getElementsByClassName ( 'cancel' ) [ 0 ] . addEventListener ( "click" , ( ) => {
85
+ document . getElementsByClassName ( 'wrapper-div' ) [ 0 ] . remove ( ) ;
86
+ } ) ;
32
87
33
88
form . onsubmit = ( event ) => {
34
89
event . preventDefault ( ) ;
35
90
let newNote = {
36
- id : ( note ) ? note . id : makeRandomID ( 10 ) ,
91
+ id : ( typeof note . name === "string" ) ? note . id : makeRandomID ( 10 ) ,
37
92
name : event . target . name . value ,
38
- created : ( note ) ? note . created : new Date ( ) ,
93
+ created : ( typeof note . name === "string" ) ? note . created : new Date ( ) ,
39
94
category : event . target . categories . value ,
40
95
content : event . target . content . value ,
41
96
dates : getDatesFromText ( event . target . content . value ) ,
42
- archived : ( note ) ? note . archived : false
97
+ archived : ( typeof note . name === "string" ) ? note . archived : false
43
98
}
44
99
45
100
if ( typeof note . name === "string" )
@@ -55,29 +110,10 @@ function buildForm(note){
55
110
wrapperDiv . className = 'wrapper-div' ;
56
111
wrapperDiv . append ( form ) ;
57
112
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 ( )
113
+ document . body . prepend ( wrapperDiv ) ;
79
114
}
80
115
116
+ //table functions
81
117
function refreshTables ( ) {
82
118
clearAllTables ( ) ;
83
119
buildNotesTable ( ) ;
@@ -133,15 +169,12 @@ function buildStatTr(category, active, total){
133
169
</tr>
134
170
` ;
135
171
}
136
-
137
172
function buildNotesTable ( ) {
138
173
notes . forEach ( note => {
139
174
if ( ! note . archived === activeNoteTableShown )
140
175
notesTable . append ( buildNotesTr ( note ) ) ;
141
176
} )
142
-
143
177
}
144
-
145
178
function buildNotesTr ( note ) {
146
179
let tr = document . createElement ( 'tr' ) ;
147
180
tr . id = note . id ;
@@ -163,7 +196,7 @@ function buildNotesTr(note){
163
196
164
197
tdArchive . className = "row-icon archive" ;
165
198
tdArchive . addEventListener ( "click" , ( ) => { changeArchiveState ( note ) } ) ;
166
- tdArchive . innerHTML = ( activeNoteTableShown ) ? icons . UNARCHIVE_ICON : icons . ARCHIVE_ICON ;
199
+ tdArchive . innerHTML = ( activeNoteTableShown ) ? icons . ARCHIVE_ICON : icons . UNARCHIVE_ICON ;
167
200
168
201
tdDelete . className = "row-icon delete" ;
169
202
tdDelete . addEventListener ( "click" , ( ) => { deleteNote ( note . id ) } ) ;
@@ -173,19 +206,21 @@ function buildNotesTr(note){
173
206
return tr ;
174
207
}
175
208
176
- export {
177
- refreshTables ,
178
- loadIconsIntoHeader ,
179
- notes
180
- }
181
209
182
210
function switchTables ( ) {
183
211
activeNoteTableShown = ! activeNoteTableShown ;
184
212
clearInnerHTML ( notesTable ) ;
185
213
buildNotesTable ( ) ;
214
+ document . getElementById ( 'table-name' ) . innerText = activeNoteTableShown ? "Active notes" : "Archived notes" ;
186
215
document . getElementsByClassName ( 'header-icon archive' ) [ 0 ] . innerHTML = ( activeNoteTableShown ) ? icons . ARCHIVE_ICON : icons . UNARCHIVE_ICON ;
187
216
}
188
217
218
+ export {
219
+ refreshTables ,
220
+ loadIconsIntoHeader ,
221
+ notes
222
+ }
223
+
189
224
document . getElementById ( "table-switcher" ) . addEventListener ( "click" , switchTables ) ;
190
225
document . getElementById ( "create-note-button" ) . addEventListener ( "click" , buildForm ) ;
191
- // document.getElementById("build-stats").addEventListener("click", refreshTables);
226
+
0 commit comments