-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-gui.js
317 lines (242 loc) · 10.7 KB
/
app-gui.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
let GroceryList = require('./grocery-list');
class AppGui {
constructor() {
//start with all views hidden
$('.master-view').show();
$('.detail-view').hide();
this.defineEventListeners();
this.addItemHandler();
//booleans for alternating between ascending and descending sortings
this.sortAlphabeticalToggle = true;
this.sortCategoryToggle = true;
this.listPreset();
this.hideMobileForm();
}
static printAllLists() {
$('.master-list-view').empty();
//use the static propery "allInstances" (array)
let instances = GroceryList.allInstances;
instances.forEach(function(instance, index) {
$('.master-list-view').append("<button type='button' data-index='" +
index +
"'class='btn btn-default select-list-parent'><span class='select-list' style='float: left;'>" +
instance.name +
"</span> <span class='glyphicon glyphicon-remove remove-list' style='float: right;'></span></button>");
})
}
listPreset() {
$(document).on('click', '#preset', function() {
$( "#preset" ).removeClass('glyphicon glyphicon-star-empty');
$( "#preset" ).addClass('glyphicon glyphicon-star');
$( "#preset" ).delay(600).fadeOut(400);
// Add some fake lists with items
let a = new GroceryList('Handla Idag');
let b = new GroceryList('Handla Senare');
AppGui.printAllLists();
a.addItem('Apple', 4, 'Elektronik');
a.addItem('Päron', 1, 'Mat');
a.addItem('Tröja', 1, 'Kläder');
a.addItem('Yxa', 1, 'Övrigt');
a.addItem('Banan', 1, 'Mat');
b.addItem('Halsduk', 4, 'Kläder');
b.addItem('Saft', 1, 'Mat');
b.addItem('Tröja', 1, 'Kläder');
})
}
hideMobileForm() {
$(document).on('click', '.add-new-item-mobile', function() {
$(this).hide();
$('#new-item-form').removeClass('hidden-xs');
});
}
defineEventListeners() {
let that = this;
//add list
$(document).on('click', '#addList', function(e) {
e.preventDefault();
//save the value of the name input
let newListName = $('#listNameInput').val();
// format name to Letter Case
newListName = newListName.charAt(0).toUpperCase() + newListName.slice(1);
//make an new list with that name
let newList = new GroceryList(newListName);
//the all the lists instance names
let all = $('#listNameInput').val('');
AppGui.printAllLists();
});
$(document).on('click', '.remove-list', function(e) {
e.preventDefault();
let removeListID = $(this).parents('button').data('index');
GroceryList.allInstances.splice(removeListID, 1)
AppGui.printAllLists();
$(this).parents('button').fadeOut(800);
});
//changing list
$(document).on('click', '.select-list-parent', function(e){
e.preventDefault();
let activeList = AppGui.findActiveList();
//save the index of the now selected list
let changeListIndex = $(this).closest('button').data('index');
//see method "changeActiveList" in grocery-list
activeList.changeActiveList(changeListIndex);
//find the new active list
activeList = AppGui.findActiveList();
//print the list of the new active list
//Add listname to detail view
$('.list-name-header').empty().append('<h2>Shopping Lista: <span class="name">' + activeList.name + '</span></h2>')
$('.master-view').hide();
$('.detail-view').show();
AppGui.printList(activeList.items);
});
$(document).on('click', '.back-button', function(e){
$('.master-view').show();
$('.detail-view').hide();
});
$(document).on('click', '#sort-alphabetical', function(){
if ( $('#sort-alphabetical > span').hasClass('glyphicon glyphicon-chevron-up') ) {
$( "#sort-alphabetical > span").removeClass('glyphicon glyphicon-chevron-up');
$( "#sort-alphabetical > span" ).addClass('glyphicon glyphicon-chevron-down');
} else {
$( "#sort-alphabetical > span").removeClass('glyphicon glyphicon-chevron-down');
$( "#sort-alphabetical > span" ).addClass('glyphicon glyphicon-chevron-up');
}
//find the active list
let activeList = AppGui.findActiveList();
//A-Z
if(that.sortAlphabeticalToggle){
//OBS sortAlpabetical returns an ARRAY
activeList.items = activeList.sortAlphabetical(true)
that.sortAlphabeticalToggle = false;
}
//Z-A
else{
//OBS sortAlpabetical returns an ARRAY
activeList.items = activeList.sortAlphabetical(false)
that.sortAlphabeticalToggle = true;
}
//printList takes an array
AppGui.printList(activeList.items);
});
$(document).on('click', '#sort-by-category', function(){
if ( $('#sort-by-category > span').hasClass('glyphicon glyphicon-chevron-up') ) {
$( "#sort-by-category > span").removeClass('glyphicon glyphicon-chevron-up');
$( "#sort-by-category > span" ).addClass('glyphicon glyphicon-chevron-down');
} else {
$( "#sort-by-category > span").removeClass('glyphicon glyphicon-chevron-down');
$( "#sort-by-category > span" ).addClass('glyphicon glyphicon-chevron-up');
}
//find the active list
let activeList = AppGui.findActiveList();
if(that.sortCategoryToggle){
//OBS sortByCategory returns an ARRAY
activeList.items = activeList.sortByCategory(that.sortCategoryToggle);
that.sortCategoryToggle = false;
}
else{
activeList.items = activeList.sortByCategory(that.sortCategoryToggle);
that.sortCategoryToggle = true;
}
//printList takes an array
AppGui.printList(activeList.items);
});
$(document).on('click', '#showAllItems', function(){
let activeList = AppGui.findActiveList();
AppGui.printList(activeList.displayAllItems());
});
$(document).on('click', '#showBoughtItems', function(){
let activeList = AppGui.findActiveList();
AppGui.printList(activeList.filterBoughtItems());
});
$(document).on('click', '#showUnboughtItems', function(){
let activeList = AppGui.findActiveList();
AppGui.printList(activeList.filterUnboughtItems());
});
//buy item
$(document).on('click', '.buy-item', function(e) {
e.preventDefault();
let activeList = AppGui.findActiveList();
//get data-index from the item clicked
let itemIndex = $(this).closest('tr').data("index");
let itemName = $(this)[0].nextSibling.nextSibling.innerHTML;
// Toggle bought/unbougt icon
if($(this).children('span').hasClass("glyphicon-unchecked")){
//the item is now marked as bought
// Icon has now been checked
$(this).children('span').removeClass("glyphicon-unchecked");
$(this).children('span').addClass(" glyphicon-ok");
activeList.setItemBought(itemName);
}else if($(this).children('span').hasClass("glyphicon-ok")){
//the item is now marked as unbought
// Icon has now been unchecked
$(this).children('span').removeClass("glyphicon-ok");
$(this).children('span').addClass(" glyphicon-unchecked");
activeList.setItemUnbought(itemName);
}
// Get checked Radio btn value
// then print out filtered items
if($("#showAllItems").is(':checked')){
AppGui.printList(activeList.items);
}else if($("#showBoughtItems").is(':checked')){
AppGui.printList(activeList.filterBoughtItems());
}else if($("#showUnboughtItems").is(':checked')){
AppGui.printList(activeList.filterUnboughtItems());
}
});
$(document).on('click', '.remove-item', function(e) {
e.preventDefault();
let activeList = AppGui.findActiveList();
//get data-index from the item clicked
let itemIndex = $(this).data("index");
activeList.removeItemByIndex(itemIndex);
$(this).closest('tr').fadeOut(800);
AppGui.printList(activeList.items);
})
}
static findActiveList(){
let instances = GroceryList.allInstances;
let activeList = instances.find(function(instance){
return instance.active == true
});
return activeList;
}
static findActiveListIndex(){
let instances = GroceryList.allInstances;
let activeList = instances.findIndex(function(instance){
return instance.active == true
});
return activeList;
}
addItemHandler() {
$('#new-item-form').submit(function(e) {
e.preventDefault();
let name = $('.item-form-name').val();
let qty = $('.item-form-qty').val();
let category = $('.item-form-category').val();
// format name to Letter Case
name = name.charAt(0).toUpperCase() + name.slice(1);
let activeList = AppGui.findActiveList();
activeList.addItem(name, qty, category)
AppGui.printList(activeList.items);
// Toggles form visibility in Mobile View
$('#new-item-form').addClass('hidden-xs');
$('.add-new-item-mobile').show();
$('.item-form-name').val('');
$('.item-form-qty').val('');
});
}
static printList(inputArray){
$('.all-items').empty();
inputArray.forEach(function(item, index){
var itemBoughtClass = "glyphicon-unchecked";
if(item.bought){itemBoughtClass = "glyphicon-ok"}
$('.all-items').append("<tr data-index='" + index + "' ><td class='buy-item'><span style='color: #007AFF;' class='glyphicon "+itemBoughtClass+"'></span></td><td id='list-obj-cat'>" +
item.category +
"</td><td id='list-obj-name'>" +
item.name +
"</td><td id='list-obj-qty'>" +
item.quantity +
"</td><td " + "data-index='" + index + "'class='remove-item' ><span class='glyphicon glyphicon-trash'></span></td></tr>");
});
}
}
$(() => new AppGui());