-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
448 lines (383 loc) · 13.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* eslint max-classes-per-file: ["error", 2] */
class Note {
date = new Date();
id = `${new Date().getTime()}`.slice(-10);
constructor(color, title, text) {
this.color = color; // item background color
this.title = title; // input title
this.text = text; // input text
}
}
// APPLICATION ARCHITECTURE
const containerMain = document.querySelector('.container');
const containerSettings = document.querySelector('.container__settings');
const modalContainer = document.querySelector('.container__modal');
const modalInput = document.querySelector('.modal__input');
const listItems = document.querySelector('.list__items');
const listItem = document.querySelectorAll('.list__item');
const buttonCreateNewNote = document.querySelector('.button--cta');
const buttonSettings = document.querySelector('.button__settings');
const settingsOptions = document.querySelector('.settings__options');
class App {
#notes = [];
#themeColor = '#0074d9';
constructor() {
buttonCreateNewNote.addEventListener('click', this._newNote.bind(this));
listItems.addEventListener('click', this._handleNote.bind(this));
modalContainer.addEventListener('click', this._handleNote.bind(this));
containerSettings.addEventListener(
'click',
this._handleSettings.bind(this)
);
listItems.addEventListener('dragstart', this._dragAndDrop.bind(this));
// Get data from local storage
this._getLocalStorageNotes();
this._getLocalStorageTheme();
// set theme based on information from local storage
document.documentElement.style.setProperty(
'--color-theme',
this.#themeColor
);
// Show instruction
this._handleInstructionText();
}
_newNote() {
// create new note
const note = new Note(this._getRandomColor());
// Add new note to notes array
this.#notes.push(note);
// Render input form
this._renderFormInputItem(note);
// Animate new note button
this._animateButton();
containerMain.style.opacity = 0;
if (!document.querySelector('.instruction--create')) {
return;
}
document.querySelector('.instruction--create').remove();
}
// rendereds list item
_renderListItem(note) {
const html = `
<li class="list__item list__item--rendered" data-id="${
note.id
}" style='background-color:rgb(${
note.color
}); color: ${this._darkenRandomColor(note.color, 2.5)}' draggable="true">
<h2 style='color: ${this._darkenRandomColor(note.color, 2.3)}'>${
!note.title ? '' : note.title
}</h2>
<p style='scrollbar-color: ${this._darkenRandomColor(
note.color,
1.5
)} rgb(${note.color}) '>${!note.text ? '' : note.text}
</p>
</li>`;
if (!note.title && !note.text) return;
listItems.insertAdjacentHTML('afterbegin', html);
}
// renders input item
_renderFormInputItem(note) {
const html = `
<li class="list__item list__item--input" data-id="${
note.id
}" style='background-color:rgb(${note.color})'>
<button class="button button__form--save--exit" type="submit" title="save"> <img src="img/arrow.svg" alt="save note"></button>
<form class="form" name="notes__form">
<input style='color: ${this._darkenRandomColor(
note.color,
2.75
)}' maxlength="20" name="title" type="text" class="form__title" placeholder="Name..." value="${
!note.title ? '' : note.title
}"/>
<textarea
name=""
class="form__text"
required=""
placeholder="Note..."
style='scrollbar-color: ${this._darkenRandomColor(
note.color,
1.5
)} rgb(${note.color}); color: ${this._darkenRandomColor(
note.color,
2.5
)}'>${!note.text ? '' : note.text}</textarea>
</form>
<button class='button button__form--delete' title="delete"><img src="img/bin.svg" alt="delete note"></button>
</li>`;
modalInput.insertAdjacentHTML('afterbegin', html);
modalContainer.classList.remove('hidden');
this._handleInstructionText();
}
// checks what to do with a clicked part of a note
_handleNote(e) {
if (
e.target.classList.contains('container__modal') ||
e.target.closest('.button__alert--save--exit')
) {
this._handleModalVisibility();
this._handleInstructionText();
this._getLocalStorageNotes();
}
if (e.target.closest('.button__form--save--exit')) {
this._saveSelectedNote();
this._handleInstructionText();
}
if (e.target.closest('.button__form--delete')) {
this._deleteSelectedNote(e);
}
if (e.currentTarget.classList.contains('list__items')) {
const el = e.target.closest('.list__item');
if (!el) {
return;
}
containerMain.style.opacity = 0;
const note = this.#notes.find((listEl) => listEl.id === el.dataset.id);
this._renderFormInputItem(note);
if (note.title || note.text) {
this._setLocalStorage('notes', this.#notes);
}
}
}
// nadles visibility of a modal
_handleModalVisibility() {
containerMain.style.opacity = 1;
modalInput.textContent = '';
listItems.textContent = '';
modalContainer.classList.add('hidden');
if (!document.querySelector('.modal__alert')) return;
document.querySelector('.modal__alert').remove();
}
// saves selected note
_saveSelectedNote() {
const el = modalInput.querySelector('.list__item');
const note = this.#notes.find((listEl) => listEl.id === el.dataset.id);
note.title = el.querySelector('.form__title').value;
note.text = el.querySelector('.form__text').value;
// remove note from notes array if input is empty
if (!note.title && !note.text) {
this.#notes = this.#notes.filter((listEl) => listEl.id !== el.dataset.id);
}
if (note.title || note.text) {
this._setLocalStorage('notes', this.#notes);
}
this._handleModalVisibility();
this._getLocalStorageNotes();
// check if a note is already rendered in the DOM
if ([...listItems.children].some((listEl) => listEl.dataset.id === note.id))
return;
this._renderListItem(note);
}
// deletes a note
_deleteSelectedNote(e) {
const el = e.target.closest('.list__item');
const elId = listItems.querySelector(`[data-id='${el.dataset.id}']`);
this.#notes = this.#notes.filter((listEl) => listEl.id !== el.dataset.id);
containerMain.style.opacity = 1;
this._setLocalStorage('notes', this.#notes);
modalInput.textContent = '';
modalContainer.classList.add('hidden');
if (!elId) return;
elId.remove();
}
_setLocalStorage(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
_getLocalStorageNotes() {
const data = JSON.parse(localStorage.getItem('notes'));
if (!data) return;
this.#notes = data;
this.#notes.forEach((note) => {
this._renderListItem(note);
});
}
_getLocalStorageTheme() {
const data = JSON.parse(localStorage.getItem('theme'));
if (!data) return;
return (this.#themeColor = data);
}
// deletes notes from a notes array and a local storage
_reset() {
modalContainer.classList.remove('hidden');
const html = `
<div class='modal__alert'>
<p>Are you sure you want to delete all notes?</p>
<div class='alert__buttons'>
<button class='button--yes'>yes</button->
<button class='button--no'>no</button->
</div>
</div>`;
modalContainer.insertAdjacentHTML('afterbegin', html);
document.querySelector('.modal__alert').addEventListener('click', (e) => {
if (e.target.classList.contains('button--yes')) {
localStorage.removeItem('notes');
location.reload();
}
if (e.target.classList.contains('button--no')) {
location.reload();
}
});
}
// animates add button on click
_animateButton() {
const iconPlus = buttonCreateNewNote.querySelector('#icon__plus');
iconPlus.classList.add('fade-out-in');
buttonCreateNewNote.classList.add('hover');
setTimeout(() => {
iconPlus.classList.remove('fade-out-in');
buttonCreateNewNote.classList.remove('hover');
}, 500);
}
_getRandomColor(min = 0) {
const colors = [
'250,226,131',
'209,154,200',
'113,206,204',
'135,204,58',
'199,178,135',
'211,212,233',
'241,207,48',
'250,205,199',
];
const max = colors.length;
const number = Math.floor(Math.random() * (max - min) + min);
return colors[number];
}
_darkenRandomColor(inputColor, factor) {
return `rgb(${inputColor
.split(',')
.map((color) => color - (255 - color) * factor)
.join(',')})`;
}
// handles instructions visibility
_handleInstructionText() {
if (this.#notes.length > 1) {
return;
}
if (
this.#notes.length === 1 &&
document.querySelector('.list__item--input')
) {
const instructionInputForm = document.querySelector('.list__item--input');
this._renderInstructionText('save', instructionInputForm);
this._renderInstructionText('delete', instructionInputForm);
}
if (
this.#notes.length === 1 &&
document.querySelector('.list__item--rendered')
) {
const instructionEdit = document.querySelector('.list__item--rendered');
this._renderInstructionText('edit', instructionEdit);
}
if (this.#notes.length === 0) {
this._renderInstructionText('create', document.body);
}
}
// renders instruction arrows
_renderInstructionText(img, DOMelement) {
setTimeout(() => {
const html = `
<div class="instruction--${img}">
<img src="img/instruction__arrow--${img}.png" alt="instruction" />
</div>
`;
DOMelement.insertAdjacentHTML('afterbegin', html);
}, 500);
}
// handles app settings - delete all button
_handleSettings(e) {
if (e.target.closest('.button__settings') || e.target.closest('.setting')) {
buttonSettings.classList.toggle('rotate');
settingsOptions.classList.toggle('translateX');
}
if (e.target.closest('.button__settings--delete-all')) {
this._reset();
}
if (e.target.closest('.button__settings--theme')) {
this._renderThemeSelectionHTML();
this._changeTheme();
}
if (e.target.closest('.button__quote')) {
this._generateQuote();
}
}
_renderThemeSelectionHTML() {
modalContainer.classList.remove('hidden');
const html = `
<div class='modal__alert'>
<button class="button button__alert--save--exit" type="submit" title="save"> <img src="img/arrow.svg" alt="save note"></button>
<fieldset>
<legend>Select a theme:</legend>
<div class='theme__option'>
<input type="radio" id="amazon-morning" name="theme" value="amazon-morning"
checked>
<label for="amazon-morning">Amazon Morning</label>
</div>
<div class='theme__option'>
<input type="radio" id="young-banana" name="theme" value="young-banana">
<label for="young-banana">Young Banana</label>
</div>
<div class='theme__option'>
<input type="radio" id="whole-green" name="theme" value="whole-green">
<label for="whole-green">Whole Green</label>
</div>
</fieldset>
</div>`;
modalContainer.insertAdjacentHTML('afterbegin', html);
}
_changeTheme() {
const formTheme = document.querySelector('fieldset');
formTheme.addEventListener('click', (e) => {
if (e.target.closest('.theme__option'));
const selectedThemeOption = [...document.querySelectorAll('input')].find(
(el) => el.checked
);
if (selectedThemeOption.value === 'amazon-morning') {
this.#themeColor = '#0074d9';
document.documentElement.style.setProperty(
'--color-theme',
this.#themeColor
);
}
if (selectedThemeOption.value === 'young-banana') {
this.#themeColor = '#ccae62';
document.documentElement.style.setProperty(
'--color-theme',
this.#themeColor
);
}
if (selectedThemeOption.value === 'whole-green') {
this.#themeColor = '#218c74';
document.documentElement.style.setProperty(
'--color-theme',
this.#themeColor
);
}
this._setLocalStorage('theme', this.#themeColor);
});
}
_dragAndDrop(e) {
const draggable = e.target.closest('.list__item');
if (draggable) console.log('tak');
draggable.classList.add('dragging');
}
async _generateQuote() {
try {
const data = await fetch(`https://api.goprogram.ai/inspiration`);
const result = await data.json();
const html = `
<div class='modal__alert'>
<button class="button button__alert--save--exit" type="submit" title="save"> <img src="img/arrow.svg" alt="save note"></button>
<p>Quote for today is:</p>
<p class="quote">„${result.quote}”
<span class="quote__author">${result.author}</span>
</p>
</div>`;
modalContainer.classList.remove('hidden');
modalContainer.insertAdjacentHTML('afterbegin', html);
} catch (err) {
alert(err);
}
}
}
const app = new App();