-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
336 lines (277 loc) · 10.3 KB
/
script.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
/**
* This script is based on Tania Rascia's CRUD todo app found here:
* https://github.com/taniarascia/mvc
*/
/**
* @class Model
*
* Manages the data of the application.
*
* Only deals with the actual data, and modifying that data. It doesn't
* understand or have any knowledge the input - what's modifying it, or the
* output - what will end up displaying.
*
* Gives everything you need for a fully functioning CRUD app, if you manually
* type all actions through the console, and view the output in the console.
*/
class Model {
constructor() {
// fetches from local storage
this.recipes = JSON.parse(localStorage.getItem('recipes')) || [];
// The state of the model, an array of recipe objects,
// prepopulated with some data
// this.recipes = [
// {
// id: 1,
// ingredients: ['chilli', 'garlic', 'rice'],
// method: 'fry chilli and garlic in pan, cook rice in water',
// time: '15',
// },
// {
// id: 2,
// ingredients: ['banana', 'custard', 'cinnamon'],
// method: 'slice bananas, warm custard, and sprinkle cinnamon',
// time: '7',
// },
// ]
}
// commits to local storage
_commit(recipes) {
this.onRecipesChanged(recipes);
localStorage.setItem('recipes', JSON.stringify(recipes));
}
bindRecipesChanged(callback) {
this.onRecipesChanged = callback;
}
// here is logic for adding a new recipe into our "Model" database - the part
// that handles our data
addRecipe(recipeIngredients, recipeMethod) {
// arrays use index notation, meaning the first item in an array has an
// index of 0, so this variable captures the last item in the array.
const lastRecipe = this.recipes.length - 1;
const recipe = {
id: this.recipes.length > 0 ? this.recipes[lastRecipe].id + 1 : 1,
ingredients: recipeIngredients.split(','),
method: recipeMethod,
};
// add the new recipe onto the existing array of recipe object(s)
this.recipes.push(recipe);
this.onRecipesChanged(this.recipes);
// commits changes to local storage
this._commit(this.recipes);
}
// Map through all recipes, and replace the ingredients of recipe with the
// specified id
editIngredients(id, updatedIngredients) {
this.recipes = this.recipes.map((recipe) =>
recipe.id === id
? { id: recipe.id, ingredients: updatedIngredients }
: recipe
);
// commits changes to local storage
this._commit(this.recipes);
}
// Map through all recipes, and replace the method of recipe with the
// specified id
editMethod(id, updatedMethod) {
this.recipes = this.recipes.map((recipe) =>
recipe.id === id ? { id: recipe.id, method: updatedMethod } : recipe
);
// commits changes to local storage
this._commit(this.recipes);
}
// Use the filter method to filter a recipe out of the recipes array by
// targeting its id, so we can delete it
deleteRecipe(id) {
this.recipes = this.recipes.filter((recipe) => recipe.id !== id);
// commits changes to local storage
this._commit(this.recipes);
}
}
/**
* @class View
*
* Visual representation of the model.
*
* Creates the view by manipulating the DOM:
* https://www.taniarascia.com/introduction-to-the-dom/
*/
class View {
constructor() {
this.app = this.getElement('#root');
// The title of the app
this.title = this.createElement('h1');
this.title.textContent = 'MVC Recipes' + ' ' + '🥕';
this.description = this.createElement('p', 'description');
this.description.textContent =
'Save/edit/remove your favourite recipes (thanks to local storage) with the click of a button!';
// The recipe form, with a [type="text"] input, and a submit button
this.form = this.createElement('form');
this.form.method = 'post';
this.ingredientTextarea = this.createElement('textarea');
this.ingredientTextarea.name = 'ingredients';
this.ingredientTextarea.id = 'ingredients';
this.ingredientTextarea.placeholder = 'Add ingredients (comma-separated)';
this.ingredientTextarea.name = 'ingredients';
this.ingredientTextarea.required = 'true';
this.ingredientLabel = this.createElement('label');
this.ingredientLabel.setAttribute('for', 'ingredients');
this.ingredientLabel.append(this.ingredientTextarea);
this.methodTextarea = this.createElement('textarea');
this.methodTextarea.name = 'method';
this.methodTextarea.id = 'method';
this.methodTextarea.placeholder = 'Add method';
this.methodTextarea.name = 'method';
this.methodTextarea.required = 'true';
this.methodLabel = this.createElement('label');
this.methodLabel.setAttribute('for', 'method');
this.methodLabel.append(this.methodTextarea);
this.submitButton = this.createElement('button', 'recipe_button');
this.submitButton.textContent = 'Submit';
this.submitButton.type = 'submit';
// The visual representation of the recipes
this.allRecipes = this.createElement('div', 'all-recipes');
// Append the inputs and submit button to the form
this.form.append(this.ingredientLabel, this.methodLabel, this.submitButton);
// Append the title, form, and recipes to the app
this.app.append(this.title, this.description, this.form, this.allRecipes);
}
// Create an element with an optional CSS class
createElement(tag, className) {
const element = document.createElement(tag);
if (className) element.classList.add(className);
return element;
}
// Retrieve an element from the DOM
getElement(selector) {
const element = document.querySelector(selector);
return element;
}
// For resetting all the text (unsure yet if needed)
get _ingredientText() {
return this.ingredientTextarea.value;
}
get _methodText() {
return this.methodTextarea.value;
}
_resetIngredientTextarea() {
this.ingredientTextarea.value = '';
}
_resetMethodTextarea() {
this.methodTextarea.value = '';
}
// The displayRecipes method will create the elements that the recipes consist
// of, and display them. Every time a recipe is changed, added, or removed,
// the displayRecipes method will be called again with the recipes from the
// model, resetting the recipes and redisplaying them. This will keep the view
// in sync with the model state.
displayRecipes(recipes) {
// Delete all nodes
while (this.allRecipes.firstChild) {
this.allRecipes.removeChild(this.allRecipes.firstChild);
}
// Show default message when there are no recipes
if (recipes.length === 0) {
const paragraph = this.createElement('p', 'empty-state');
paragraph.textContent = "Wanna get cookin'? Add a recipe!";
this.allRecipes.append(paragraph);
} else {
recipes.forEach((recipe) => {
const recipeBlock = this.createElement('div', 'recipe-block');
recipeBlock.id = recipe.id;
const ingredientWrapper = this.createElement('ul', 'recipe-list');
const methodWrapper = this.createElement('p', 'method');
const methodText = this.createElement('span');
// The method text will be content-editable
methodText.contentEditable = true;
methodText.classList.add('editable');
methodText.textContent = recipe.method;
methodWrapper.append(methodText);
// Each recipe will also have a delete button
const deleteButton = this.createElement('button', 'delete-button');
deleteButton.textContent = 'Delete';
recipe.ingredients.forEach((ingredient) => {
const li = this.createElement('li');
// The ingredient item text will be in a content-editable span
const ingredientText = this.createElement('span');
ingredientText.contentEditable = true;
ingredientText.classList.add('editable');
ingredientText.textContent = ingredient;
li.append(ingredientText);
ingredientWrapper.append(li);
});
recipeBlock.append(ingredientWrapper, methodWrapper, deleteButton);
this.allRecipes.append(recipeBlock);
});
}
}
bindAddRecipe(handler) {
this.form.addEventListener('submit', (event) => {
event.preventDefault();
if (this._ingredientText || this._methodText) {
handler(this._ingredientText, this._methodText);
this._resetIngredientTextarea();
this._resetMethodTextarea();
}
});
}
// adds event listener to entire recipe block wrapper so that the event
// listener is always there to listen for button clicks
bindDeleteRecipe(handler) {
this.allRecipes.addEventListener('click', (event) => {
if (event.target.className === 'delete-button') {
const id = parseInt(event.target.parentElement.id);
handler(id);
}
});
}
// alternately the event listener can be attached to all the delete buttons
// but the moment a recipe is created, the model will not know to add this
// event listener, rather the page will need a refresh.
//
// bindDeleteRecipe(handler) {
// this.allRecipes.querySelectorAll('.delete').forEach(deleteButton =>
// deleteButton.addEventListener('click', event => {
// const id = parseInt(event.target.id)
// handler(id)
// })
// )
// }
}
/**
* @class Controller
*
* Links the user input and the view output.
*
* @param model
* @param view
*/
class Controller {
constructor(model, view) {
this.model = model;
this.view = view;
this.model.bindRecipesChanged(this.onRecipesChanged);
// Display initial recipes
this.onRecipesChanged(this.model.recipes);
this.view.bindAddRecipe(this.handleAddRecipe);
//needed only if event listeners were to be added directly to buttons.
this.view.bindDeleteRecipe(this.handleDeleteRecipe);
}
onRecipesChanged = (recipes) => {
this.view.displayRecipes(recipes);
this.view.bindDeleteRecipe(this.handleDeleteRecipe);
};
handleAddRecipe = (recipeIngredients, recipeMethod) => {
this.model.addRecipe(recipeIngredients, recipeMethod);
};
handleDeleteRecipe = (id) => {
this.model.deleteRecipe(id);
};
// handleEditIngredients = (id, updatedIngredients) => {
// this.model.editIngredients(id, updatedIngredients)
// }
// handleEditMethod = (id, updatedMethod) => {
// this.model.editMethod(id, updatedMethod)
// }
}
const app = new Controller(new Model(), new View()); // eslint-disable-line no-unused-vars