-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcategoryForm.js
More file actions
49 lines (39 loc) · 1.38 KB
/
categoryForm.js
File metadata and controls
49 lines (39 loc) · 1.38 KB
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
import Category from './category.js'
import Validator from './validator.js'
export default class CategoryForm {
constructor() {
this.form = document.getElementById('addCategoryForm');
this.input = this.form.querySelector('input');
this.openFormButton = document.getElementById('addCategory');
this.formOverlay = document.getElementById('addCategoryOverlay');
this.openFormButton.addEventListener('click', this.showFormHandler.bind(this));
this.formOverlay.addEventListener('click', this.hideFormHandler.bind(this));
this.form.addEventListener('submit', this.submitHandler.bind(this));
}
showFormHandler() {
this.form.classList.remove('invisible');
this.formOverlay.classList.remove('invisible');
}
hideFormHandler() {
this.form.classList.add('invisible');
this.formOverlay.classList.add('invisible');
this.resetFormInput();
}
resetFormInput() {
this.input.value = 'Untitled category';
}
passParameterToUrl(id) {
return `./category.html?categoryid=${id}`
}
submitHandler(event) {
event.preventDefault();
const inputValue = this.input.value;
if (!Validator.validate(inputValue)) {
return;
}
const newCategory = new Category(inputValue);
Category.render(newCategory.name, newCategory.id);
localStorage.setItem(newCategory.id, JSON.stringify(newCategory));
this.hideFormHandler();
}
}