Repository navigation
Expand file tree
/
Copy pathview.js
More file actions
199 lines (161 loc) · 6.28 KB
/
Copy pathview.js
File metadata and controls
199 lines (161 loc) · 6.28 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
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
// --- Abstraction de classe pour le rendu ---
class TaskRenderer {
constructor() {
if (this.constructor === TaskRenderer) {
throw new Error("Classe abstraite 'TaskRenderer' ne peut pas être instanciée directement.");
}
}
// Méthode abstraite
rendre(task) {
throw new Error("Méthode 'rendre()' doit être implémentée");
}
}
// Classes concrètes de rendu par catégorie
class WorkTaskRenderer extends TaskRenderer {
rendre(task) {
const li = document.createElement('li');
li.id = task.id;
li.classList.add('task-travail'); // Rouge
li.innerHTML = `<span>${task.text} <em>(Travail)</em></span>`;
return li;
}
}
class HomeTaskRenderer extends TaskRenderer {
rendre(task) {
const li = document.createElement('li');
li.id = task.id;
li.classList.add('task-maison'); // Bleu
li.innerHTML = `<span>${task.text} <em>(Maison)</em></span>`;
return li;
}
}
class MiscTaskRenderer extends TaskRenderer {
rendre(task) {
const li = document.createElement('li');
li.id = task.id;
li.classList.add('task-divers'); // Vert
li.innerHTML = `<span>${task.text} <em>(Divers)</em></span>`;
return li;
}
}
// Gère l'affichage (DOM) et capture les actions utilisateur
class View {
constructor() {
// L'élément racine de l'application
this.app = this.getElement('#root');
// Création du titre
this.title = this.createElement('h1');
this.title.textContent = 'Liste de Tâches';
// Création du formulaire d'ajout
this.form = this.createElement('form');
this.input = this.createElement('input');
this.input.type = 'text';
this.input.placeholder = 'Ajouter une tâche...';
this.input.name = 'todo';
// Modif TP : Menu déroulant pour la catégorie
this.categorySelect = this.createElement('select');
this.categorySelect.name = 'category';
const optionTravail = this.createElement('option');
optionTravail.value = 'travail';
optionTravail.textContent = 'Travail';
const optionMaison = this.createElement('option');
optionMaison.value = 'maison';
optionMaison.textContent = 'Maison';
const optionDivers = this.createElement('option');
optionDivers.value = 'divers';
optionDivers.textContent = 'Divers';
this.categorySelect.append(optionTravail, optionMaison, optionDivers);
this.submitButton = this.createElement('button');
this.submitButton.textContent = 'Ajouter';
// Assemblage du formulaire
this.form.append(this.input, this.categorySelect, this.submitButton);
// Modif TP : Filtrage optionnel (Select pour le filtre)
this.filterSelect = this.createElement('select');
this.filterSelect.id = 'filter';
const optionAll = this.createElement('option');
optionAll.value = 'all';
optionAll.textContent = 'Toutes les catégories';
const optionFilterTravail = this.createElement('option');
optionFilterTravail.value = 'travail';
optionFilterTravail.textContent = 'Travail';
const optionFilterMaison = this.createElement('option');
optionFilterMaison.value = 'maison';
optionFilterMaison.textContent = 'Maison';
const optionFilterDivers = this.createElement('option');
optionFilterDivers.value = 'divers';
optionFilterDivers.textContent = 'Divers';
this.filterSelect.append(optionAll, optionFilterTravail, optionFilterMaison, optionFilterDivers);
this.app.append(this.filterSelect);
// Création de la liste (<ul>)
this.todoList = this.createElement('ul', 'todo-list');
// Ajout de tous les éléments à la racine
this.app.append(this.title, this.form, this.todoList);
// Initialisation des renderers
this.renderers = {
'travail': new WorkTaskRenderer(),
'maison': new HomeTaskRenderer(),
'divers': new MiscTaskRenderer()
};
}
// --- Helpers pour la manipulation du DOM ---
createElement(tag, className) {
const element = document.createElement(tag);
if (className) element.classList.add(className);
return element;
}
getElement(selector) {
return document.querySelector(selector);
}
// --- Méthodes d'affichage ---
get _todoText() {
return this.input.value;
}
get _todoCategory() {
return this.categorySelect.value;
}
_resetInput() {
this.input.value = '';
}
displayTodos(todos) {
// Vider la liste existante
while (this.todoList.firstChild) {
this.todoList.removeChild(this.todoList.firstChild);
}
if (todos.length === 0) {
const p = this.createElement('p', 'empty-message');
p.textContent = 'Aucune tâche ! Ajoutez-en une.';
this.todoList.append(p);
} else {
// Créer les éléments de liste pour chaque tâche
todos.forEach(todo => {
// Utilisation du Strategy/Renderer approprié via la catégorie
const renderer = this.renderers[todo.category] || this.renderers['divers'];
const li = renderer.rendre(todo);
// Gestion optionnelle du mode "complete" (barrer le texte)
if (todo.complete) {
const strike = this.createElement('s');
strike.textContent = todo.text;
const span = li.querySelector('span');
span.innerHTML = "";
span.append(strike, ` (${todo.category})`);
}
this.todoList.append(li);
});
}
}
// --- Gestion des événements (Events Binding) ---
bindAddTodo(handler) {
this.form.addEventListener('submit', event => {
event.preventDefault();
if (this._todoText) {
handler(this._todoText, this._todoCategory);
this._resetInput();
}
});
}
bindFilterTodos(handler) {
this.filterSelect.addEventListener('change', event => {
handler(event.target.value);
});
}
}