-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (113 loc) · 4.03 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
'use strict';
const STORE = {
items:[
{name: "apples", checked: false},
{name: "oranges", checked: false},
{name: "milk", checked: true},
{name: "bread", checked: false}
],
hideChecked: false
}
function generateItemElement(item, itemIndex, template) {
return `
<li class="js-item-index-element" data-item-index="${itemIndex}">
<span class="shopping-item js-shopping-item ${item.checked ? "shopping-item__checked" : ''}">${item.name}</span>
<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>`;
}
function generateShoppingItemsString(shoppingList) {
console.log("Generating shopping list element");
const items = shoppingList.map((item, index) => generateItemElement(item, index));
return items.join("");
}
function renderShoppingList() {
// render the shopping list in the DOM
console.log('`renderShoppingList` ran');
const shoppingListItemsString = generateShoppingItemsString(STORE.items);
// insert that HTML into the DOM
$('.js-shopping-list').html(shoppingListItemsString);
}
function addItemToShoppingList(itemName) {
console.log(`Adding "${itemName}" to shopping list`);
STORE.items.push({name: itemName, checked: false});
}
function handleNewItemSubmit() {
$('#js-shopping-list-form').submit(function(event) {
event.preventDefault();
console.log('`handleNewItemSubmit` ran');
const newItemName = $('.js-shopping-list-entry').val();
$('.js-shopping-list-entry').val('');
addItemToShoppingList(newItemName);
renderShoppingList();
});
}
function toggleCheckedForListItem(items) {
console.log("Toggling checked property for item at index ");
STORE[items.checked] = !STORE[items.checked];
}
function toggleHiddenItems (){
if ($(item.checked) === ".shopping-list__checked"){
STORE[items.checked] == true;
}
}
function hideAllCheckedItems(){
//This function finds all checked items
console.log("Now checking for hidden items");
//Filter all checked items from shopping list
$(".filter").on('click', '#js-hide-checked', event => {
console.log('All items filtered');
const filteredItems = STORE.items.filter((item) => items.checked === false);
});
//Re-render new shopping list
renderShoppingList(filteredItems);
}
function getItemIndexFromElement(item) {
const itemIndexString = $(item)
.closest('.js-item-index-element')
.attr('data-item-index');
return parseInt(itemIndexString, 10);
}
function handleItemCheckClicked() {
$('.js-shopping-list').on('click', `.js-item-toggle`, event => {
console.log('`handleItemCheckClicked` ran');
const itemIndex = getItemIndexFromElement(event.currentTarget);
toggleCheckedForListItem(itemIndex);
renderShoppingList();
});
}
function deleteItem (item) {
const itemIndexString = $(item)
.closest('.js-item-index-element')
.attr('data-item-index');
const itemIndexNumber = parseInt(itemIndexString, 10)
STORE.items.splice(itemIndexNumber, 1);
}
function handleDeleteItemClicked() {
$('.js-shopping-list').on('click', `.js-item-delete`, event => {
console.log('`handleItemDeleteClicked` ran');
const itemIndex = getItemIndexFromElement(event.currentTarget);
deleteItem(itemIndex);
renderShoppingList();
});
console.log('`handleDeleteItemClicked` ran')
}
// this function will be our callback when the page loads. it's responsible for
// initially rendering the shopping list, and activating our individual functions
// that handle new item submission and user clicks on the "check" and "delete" buttons
// for individual shopping list items.
function handleShoppingList() {
renderShoppingList();
handleNewItemSubmit();
handleItemCheckClicked();
handleDeleteItemClicked();
hideAllCheckedItems();
}
// when the page loads, call `handleShoppingList`
$(handleShoppingList);