|
1 | | -function buildForm(){ |
| 1 | +import { notes, categories, icons } from './data.js'; |
| 2 | +import { makeRandomID, getDatesFromText } from "./functions.js"; |
2 | 3 |
|
| 4 | + |
| 5 | +let statisticsTable = document.getElementById('stats-table'), |
| 6 | + notesTable = document.getElementById('active-archive-table'); |
| 7 | + |
| 8 | +let activeNoteTableShown = true; |
| 9 | + |
| 10 | + |
| 11 | +function loadIconsIntoHeader(){ |
| 12 | + Array.from(document.getElementsByClassName('header-icon')).forEach(col => { |
| 13 | + if (col.classList.contains('archive')) |
| 14 | + col.innerHTML = icons.ARCHIVE_ICON; |
| 15 | + if (col.classList.contains('delete')) |
| 16 | + col.innerHTML = icons.DELETE_ICON; |
| 17 | + // Array.from(col.getElementsByTagName('path')).forEach( path => path.classList.add('header-icon')); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function buildForm(note){ |
| 22 | + let form = document.createElement('form'); |
| 23 | + |
| 24 | + form.innerHTML = ` |
| 25 | + <input type="text" name="name" value="${typeof note.name === "string"? note.name : ''}" placeholder="Name"> |
| 26 | + <select name="categories"> |
| 27 | + ` + Object.keys(categories).map(c => `<option value="${c}" ${note.category === c? 'selected' : ''}>${c}</option>`) + ` |
| 28 | + </select> |
| 29 | + <input type="text" name="content" value="${note.content? note.content : ''}" placeholder="Content"> |
| 30 | + <input type="submit" value="Submit"> |
| 31 | + `; |
| 32 | + |
| 33 | + form.onsubmit = (event)=>{ |
| 34 | + event.preventDefault(); |
| 35 | + let newNote = { |
| 36 | + id: (note)? note.id : makeRandomID(10), |
| 37 | + name: event.target.name.value, |
| 38 | + created: (note)? note.created : new Date(), |
| 39 | + category: event.target.categories.value, |
| 40 | + content: event.target.content.value, |
| 41 | + dates: getDatesFromText(event.target.content.value), |
| 42 | + archived: (note)? note.archived : false |
| 43 | + } |
| 44 | + |
| 45 | + if (typeof note.name === "string") |
| 46 | + updateNote(newNote); |
| 47 | + else |
| 48 | + createNote(newNote); |
| 49 | + |
| 50 | + document.getElementsByClassName('wrapper-div')[0].remove(); |
| 51 | + }; |
| 52 | + |
| 53 | + |
| 54 | + let wrapperDiv = document.createElement('div'); |
| 55 | + wrapperDiv.className = 'wrapper-div'; |
| 56 | + wrapperDiv.append(form); |
| 57 | + |
| 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() |
| 79 | +} |
| 80 | + |
| 81 | +function refreshTables(){ |
| 82 | + clearAllTables(); |
| 83 | + buildNotesTable(); |
| 84 | + buildStatisticTable(); |
3 | 85 | } |
| 86 | +function clearAllTables(){ |
| 87 | + clearInnerHTML(notesTable); |
| 88 | + clearInnerHTML(statisticsTable); |
| 89 | +} |
| 90 | +function clearInnerHTML(parent){ |
| 91 | + while (parent.firstChild) |
| 92 | + parent.removeChild(parent.firstChild); |
| 93 | +} |
| 94 | +function buildStatisticTable(){ |
| 95 | + let Ideas, Quotes, Tasks, Thoughts, IdeasActive, QuotesActive, TasksActive, ThoughtsActive; |
| 96 | + Ideas = Quotes = Tasks = Thoughts = IdeasActive = QuotesActive = TasksActive = ThoughtsActive = 0; |
| 97 | + |
| 98 | + notes.forEach(note => { |
| 99 | + if (note.category === 'Idea'){ |
| 100 | + Ideas++; |
| 101 | + if (!note.archived) |
| 102 | + IdeasActive++; |
| 103 | + } |
| 104 | + if (note.category === 'Quote') { |
| 105 | + Quotes++; |
| 106 | + if (!note.archived) |
| 107 | + QuotesActive++; |
| 108 | + } |
| 109 | + if (note.category === 'Task') { |
| 110 | + Tasks++; |
| 111 | + if (!note.archived) |
| 112 | + TasksActive++; |
| 113 | + } |
| 114 | + if (note.category === 'Random Thought') { |
| 115 | + Thoughts++; |
| 116 | + if (!note.archived) |
| 117 | + ThoughtsActive++; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + statisticsTable.innerHTML += (Ideas)? buildStatTr('Idea', IdeasActive, Ideas) : ``; |
| 122 | + statisticsTable.innerHTML += (Quotes)? buildStatTr('Quote', QuotesActive, Quotes) : ``; |
| 123 | + statisticsTable.innerHTML += (Tasks)? buildStatTr('Task', TasksActive, Tasks) : ``; |
| 124 | + statisticsTable.innerHTML += (Thoughts)? buildStatTr('Random Thought', ThoughtsActive, Thoughts) : ``; |
| 125 | +} |
| 126 | +function buildStatTr(category, active, total){ |
| 127 | + return ` |
| 128 | + <tr> |
| 129 | + <td className="category-icon stats-icon">${ categories[category] }</td> |
| 130 | + <td className="category2">${ category }</td> |
| 131 | + <td className="active">${ active }</td> |
| 132 | + <td className="archived">${ total-active }</td> |
| 133 | + </tr> |
| 134 | + `; |
| 135 | +} |
| 136 | + |
| 137 | +function buildNotesTable(){ |
| 138 | + notes.forEach(note => { |
| 139 | + if (!note.archived === activeNoteTableShown) |
| 140 | + notesTable.append( buildNotesTr(note) ); |
| 141 | + }) |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +function buildNotesTr(note){ |
| 146 | + let tr = document.createElement('tr'); |
| 147 | + tr.id = note.id; |
| 148 | + tr.innerHTML = ` |
| 149 | + <td className="category-icon">${ categories[note.category] }</td> |
| 150 | + <td className="name">${ note.name }</td> |
| 151 | + <td className="created">${ note.created.toLocaleDateString() }</td> |
| 152 | + <td className="category1">${ note.category }</td> |
| 153 | + <td className="content">${ note.content }</td> |
| 154 | + <td className="dates">${ note.dates }</td> |
| 155 | + `; |
| 156 | + let tdEdit = document.createElement('td'), |
| 157 | + tdArchive = document.createElement('td'), |
| 158 | + tdDelete = document.createElement('td'); |
| 159 | + |
| 160 | + tdEdit.className = "row-icon edit"; |
| 161 | + tdEdit.addEventListener("click", ()=>{ buildForm(note) }); |
| 162 | + tdEdit.innerHTML = icons.EDIT_ICON; |
| 163 | + |
| 164 | + tdArchive.className = "row-icon archive"; |
| 165 | + tdArchive.addEventListener("click", ()=>{ changeArchiveState(note) }); |
| 166 | + tdArchive.innerHTML = (activeNoteTableShown)? icons.UNARCHIVE_ICON : icons.ARCHIVE_ICON; |
| 167 | + |
| 168 | + tdDelete.className = "row-icon delete"; |
| 169 | + tdDelete.addEventListener("click", ()=>{ deleteNote(note.id) }); |
| 170 | + tdDelete.innerHTML = icons.DELETE_ICON; |
| 171 | + |
| 172 | + tr.append(tdEdit, tdArchive, tdDelete); |
| 173 | + return tr; |
| 174 | +} |
| 175 | + |
| 176 | +export { |
| 177 | + refreshTables, |
| 178 | + loadIconsIntoHeader, |
| 179 | + notes |
| 180 | +} |
| 181 | + |
| 182 | +function switchTables() { |
| 183 | + activeNoteTableShown = !activeNoteTableShown; |
| 184 | + clearInnerHTML(notesTable); |
| 185 | + buildNotesTable(); |
| 186 | + document.getElementsByClassName('header-icon archive')[0].innerHTML = (activeNoteTableShown)? icons.ARCHIVE_ICON : icons.UNARCHIVE_ICON; |
| 187 | +} |
| 188 | + |
| 189 | +document.getElementById("table-switcher").addEventListener("click", switchTables); |
| 190 | +document.getElementById("create-note-button").addEventListener("click", buildForm); |
| 191 | +// document.getElementById("build-stats").addEventListener("click", refreshTables); |
0 commit comments