forked from rosshappnin/shopping_list_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
378 lines (309 loc) · 9.99 KB
/
app.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var listObj; // stores the state of the shopping list
const LIST_ID = 1; // Temporaliy (whilst in developemt) hard code the list id
/**
* On page load start the app
*/
$(function(){
read();
/*******************************************/
/* Event handlers
/*******************************************/
// Sets up JQuery-UI for drag and drop, re-ordrring of items
// On Sortable update handler
$('#table-items tbody').sortable({
update: function(event, ui){
$(this).children().each(function(index) {
var rowEL = $(this);
if (rowEL.attr('data-position') != (index + 1)) {
rowEL.attr('data-position', (index + 1));
updateItem(rowEL);
}
});
}
});
/**
* Add item button click handler
*
* 1 - Invoked when the 'Add item buttom is clicked
* 2 - Calls the create item function
* 3 - Clears the form inputs.
*/
$('#form-add-item').on('submit', function(event){
event.preventDefault();
createItem();
// clear inputs
$('#input-name').val('');
$('#input-price').val('');
});
// Item check button click handler
$('#table-items').on('click', '.button-check', function() {
var rowEL = $(this).closest('tr');
// toggle checked class
rowEL.toggleClass("checked");
updateItem(rowEL);
});
// Item name changed handler
$('#table-items').on('change', '.name', function() {
var rowEL = $(this).closest('tr');
updateItem(rowEL);
});
// Item price changed handler
$('#table-items').on('change', '.price', function() {
var rowEL = $(this).closest('tr');
// only update price if greater than zero
var newPrice = rowEL.find('.price').val();
if (newPrice < 0) {
newPrice = 0;
rowEL.find('.price').val(newPrice.toFixed(2));
} else {
updateItem(rowEL);
}
})
// Delete item button click handler
$('#table-items').on('click', '.button-delete', function() {
var rowEL = $(this).closest('tr');
deleteItem(rowEL);
});
// Update max-spend on change handler
$('#input-max-spend').change(function() {
updateList();
});
// Toggle max spend alert button click handler
$('#button-toggle-alert').on('click', function(event) {
event.preventDefault();
var alertBtnText = $('#button-toggle-alert').text();
$('#button-toggle-alert').text(alertBtnText == 'Alert off' ? 'Alert on' : 'Alert off');
updateList();
});
});
/*******************************************/
/* Ajax calls to the database
/*******************************************/
/**
* Updates the lisy propertes to the database
*/
function updateList() {
var newIs_alert = $('#button-toggle-alert').text() == 'Alert on' ? 1 : 0;
var newMax_spend = $('#input-max-spend').val();
$.ajax({
url: 'api/list/update.php',
method: 'PUT',
contentType: 'application/json',
data: JSON.stringify({id: LIST_ID,
max_spend: newMax_spend,
is_alert: newIs_alert
}),
success: function(response) {
console.log(response);
read();
},
error: function(response) {
console.log("ERROR:");
console.log(response);
}
});
}
/**
* 1 - Gathers item data submitted in the add new item form
* 2 - Creates a new item object with the data
* 3 - Adds the item object to the database
*/
function createItem() {
var name = $('#input-name').val();
var price = $('#input-price').val();
var is_checked = 0;
var position = $('#table-items tbody tr').length + 1;
$.ajax({
url: 'api/list/item/create.php',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({list_id: LIST_ID,
name: name,
price: price,
is_checked: is_checked,
position: position
}),
success: function(response) {
console.log(response);
read();
},
error: function(response) {
console.log("ERROR:");
console.log(response);
}
});
}
/**
* 1 - Updates the item in the database with the specified id
* 2 - calls read on success
*
* @param jquery rowEL The tr row element of the item
*/
function updateItem(rowEL) {
var id = rowEL.attr('data-id');
var newName = rowEL.find('.name').val();
var newPrice = rowEL.find('.price').val();
var newIsChecked = (rowEL.hasClass("checked") ? 1 : 0);
var newPosition = rowEL.attr('data-position');
$.ajax({
url: 'api/list/item/update.php',
method: 'PUT',
contentType: 'application/json',
data: JSON.stringify({id:id,
name: newName,
price: newPrice,
is_checked: newIsChecked,
position: newPosition
}),
success: function(response) {
console.log(response);
read();
},
error: function(response) {
console.log("ERROR:");
console.log(response);
}
});
}
/**
* 1 - Deletse the item from the databse
* 2 - calls read
*
* @param jQuery rowEL The tr row element of the item
*/
function deleteItem(rowEL) {
var id = rowEL.attr('data-id');
$.ajax({
url: 'api/list/item/delete.php',
method: 'DELETE',
contentType: 'application/json',
data: JSON.stringify({id: id}),
success: function(response) {
console.log(response);
read();
},
error: function(response) {
console.log("ERROR:");
console.log(response);
}
});
}
/**
* 1 - Fetches the shopping list data from the server,
* 2 - Assigns the new data to the listObj
* 3 - then calls refresh.
*/
function read() {
$.ajax({
method: 'GET',
url: 'api/list/read.php?id=' + LIST_ID,
contentType: 'application/json; charset=utf-8',
success: function(response) {
console.log(response);
// sets the shopping list data
listObj = $.parseJSON(response);
console.log(listObj);
refresh();
},
error: function(response) {
console.log("ERROR:");
console.log(response);
}
});
}
/**
* Rebuilds the page using data from the listObj
*/
function refresh() {
var alertBtnText = ( listObj.is_alert == 1 ? 'Alert on' : 'Alert off');
$('#button-toggle-alert').text(alertBtnText);
$('#list-title').text(listObj.title);
$('#input-max-spend').val(listObj.max_spend);
$('#table-items').attr('data-list_id', listObj.id);
// extract the items from the list object
var items = [];
// check that the listObj has an items property, if not exit.
if (listObj.hasOwnProperty("items")) {
items = listObj.items;
} else {
return false;
}
// select table body element
var tbodyEL = $('#table-items tbody');
var tfootEL = $('#table-items tfoot');
// clear table elements
tbodyEL.html('');
tfootEL.html('');
// foreach list item, add it to the table
$.each(items, function(i, item) {
// if item is checked add class checked.
var trClass = (item.is_checked == 1 ? 'class="checked"' : '');
tbodyEL.append('\
<tr ' + trClass + ' data-id="' + item.id +'" data-position="'+ item.position +'">\
<td><i class="glyphicon glyphicon-resize-vertical"></i><input type="text" class="name" value="' + item.name + '"></td>\
<td><input type="number" step="any" class="price" value="' + item.price + '"></td>\
<td>\
<button class="button-check"><i class="glyphicon glyphicon-ok"></i>Check</button>\
<button class="button-delete"><i class="glyphicon glyphicon-trash"></i>Delete</button>\
</td>\
</tr>\
');
});
tfootEL.append('\
<tr>\
<th>Total:</th>\
<td><span id="output-total"><span></td>\
</tr>\
')
// update the total and max_spend
var total = calculateTotal();
checkSpend(listObj.max_spend, total);
}
/**
* Calculates th sum total of all items in list
* And updates the total on page
*
* @return float total, the total sum of all item prices in list
*
*/
function calculateTotal()
{
var total = 0;
// sum up the total
$("#table-items .price").each(function() {
var value = $(this).val();
//add only if the value is a number
if(!isNaN(value) && value.length != 0) {
total += parseFloat(value);
}
});
// update the total on page
$('#output-total').text(total.toFixed(2));
// return the total
return total;
};
/**
* Calculates if the items total is greater than the user's se budget
* in max_spend
*
* If alert is switched on, ouotputs how much is left or over budget to screen
*
* @param float max_spend
* @param float total
*/
function checkSpend(max_spend, total) {
var messageEL = $('#max-spend-message');
var formEL = $('#form-max-spend');
formEL.removeClass('over');
if (listObj.is_alert == 1) {
// if total exceeds max spend
if (total > max_spend) {
formEL.addClass('over');
messageEL.text('You are '+ (total - max_spend).toFixed(2) +' over budget!');
} else {
messageEL.text('You have '+ ( max_spend - total).toFixed(2) +' spend remaining.');
}
} else {
messageEL.text("");
}
}