-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
166 lines (149 loc) · 4.7 KB
/
index.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
const store = {
items: [
{ id: cuid(), name: 'apples', checked: false },
{ id: cuid(), name: 'oranges', checked: false },
{ id: cuid(), name: 'milk', checked: true },
{ id: cuid(), name: 'bread', checked: false }
],
hideCheckedItems: false
};
const generateItemElement = function (item) {
let itemTitle = `<span class='shopping-item shopping-item__checked'>${item.name}</span>`;
if (!item.checked) {
itemTitle = `
<span class='shopping-item'>${item.name}</span>
`;
}
return `
<li class='js-item-element' data-item-id='${item.id}'>
${itemTitle}
<div class='shopping-item-controls'>
<button class='shopping-item-toggle js-item-toggle'>
<span class='button-label'>check</span>
</button>
<button class='shopping-item-delete js-item-delete'>
<span class='button-label'>delete</span>
</button>
</div>
</li>`;
};
const generateShoppingItemsString = function (shoppingList) {
const items = shoppingList.map((item) => generateItemElement(item));
return items.join('');
};
/**
* Render the shopping list in the DOM
*/
const render = function () {
// Set up a copy of the store's items in a local
// variable 'items' that we will reassign to a new
// version if any filtering of the list occurs.
let items = [...store.items];
// If the `hideCheckedItems` property is true,
// then we want to reassign filteredItems to a
// version where ONLY items with a "checked"
// property of false are included.
if (store.hideCheckedItems) {
items = items.filter(item => !item.checked);
}
/**
* At this point, all filtering work has been
* done (or not done, if that's the current settings),
* so we send our 'items' into our HTML generation function
*/
const shoppingListItemsString = generateShoppingItemsString(items);
// insert that HTML into the DOM
$('.js-shopping-list').html(shoppingListItemsString);
};
const addItemToShoppingList = function (itemName) {
store.items.push({ id: cuid(), name: itemName, checked: false });
};
const handleNewItemSubmit = function () {
$('#js-shopping-list-form').submit(function (event) {
event.preventDefault();
const newItemName = $('.js-shopping-list-entry').val();
$('.js-shopping-list-entry').val('');
addItemToShoppingList(newItemName);
render();
});
};
const toggleCheckedForListItem = function (id) {
const foundItem = store.items.find(item => item.id === id);
foundItem.checked = !foundItem.checked;
};
const handleItemCheckClicked = function () {
$('.js-shopping-list').on('click', '.js-item-toggle', event => {
const id = getItemIdFromElement(event.currentTarget);
toggleCheckedForListItem(id);
render();
});
};
const getItemIdFromElement = function (item) {
return $(item)
.closest('.js-item-element')
.data('item-id');
};
/**
* Responsible for deleting a list item.
* @param {string} id
*/
const deleteListItem = function (id) {
// As with 'addItemToShoppingLIst', this
// function also has the side effect of
// mutating the global store value.
//
// First we find the index of the item with
// the specified id using the native
// Array.prototype.findIndex() method.
const index = store.items.findIndex(item => item.id === id);
// Then we call `.splice` at the index of
// the list item we want to remove, with
// a removeCount of 1.
store.items.splice(index, 1);
};
const handleDeleteItemClicked = function () {
// Like in `handleItemCheckClicked`,
// we use event delegation.
$('.js-shopping-list').on('click', '.js-item-delete', event => {
// Get the index of the item in store.items.
const id = getItemIdFromElement(event.currentTarget);
// Delete the item.
deleteListItem(id);
// Render the updated shopping list.
render();
});
};
/**
* Toggles the store.hideCheckedItems property
*/
const toggleCheckedItemsFilter = function () {
store.hideCheckedItems = !store.hideCheckedItems;
};
/**
* Places an event listener on the checkbox
* for hiding completed items.
*/
const handleToggleFilterClick = function () {
$('.js-filter-checked').click(() => {
toggleCheckedItemsFilter();
render();
});
};
/**
* This function will be our callback when the
* page loads. It is responsible for initially
* rendering the shopping list, then calling
* our individual functions that handle new
* item submission and user clicks on the
* "check" and "delete" buttons for individual
* shopping list items.
*/
const handleShoppingList = function () {
render();
handleNewItemSubmit();
handleItemCheckClicked();
handleDeleteItemClicked();
handleToggleFilterClick();
};
// when the page loads, call `handleShoppingList`
$(handleShoppingList);