-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
272 lines (236 loc) · 7.25 KB
/
cart.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Shopper</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="{{ static_url("cart.css") }}">
</head>
<body>
<div class="collapse" id="navbar-header">
<div class="container-fluid bg-inverse p-a-1">
<h3>Collapsed content</h3>
<p>Toggleable via the navbar brand.</p>
</div>
</div>
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<span class="navbar-brand glyphicon glyphicon-shopping-cart"></span>
<button class="btn navbar-btn navbar-toggler" data-toggle="collapse" data-target="#navbar-header">
☰
</button>
</div>
</div>
</nav>
<!-- Modal -->
<div class="modal fade" id="item-modal" tabindex="-1" role="dialog" aria-labelledby="item-modal-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="item-modal-label">New Item</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<input type="text" class="form-control" id="item-input">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id='do-new-item'>Add</button>
</div>
</div>
</div>
</div>
<div>
<h2 class="text-center">Shopping List</h2>
<ul id="cart" class="list-group list-group-flush center-block">
{% for item in items %}
<li class="cart-item list-group-item {% if item['done'] != 0 %}greyout{% end %}" id="{{ item['id'] }}">
<span class="cart-item-tick glyphicon glyphicon-ok-sign {% if item['done'] != 0 %}cart-item-have{% end %}"></span>
<span class="cart-item-name">{{ escape(item['name']) }}</span>
<span class="badge">{{ item['count'] }}</span>
</li>
{% end %}
</ul>
<button id='do-remove-checked' class="btn btn-danger center-block">
Remove Checked Items
</button>
</div>
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container-fluid">
<div class="navbar-header">
<span class="navbar-brand glyphicon glyphicon-wrench"></span>
<button id="do-plus" class="btn navbar-btn">
<span class="glyphicon glyphicon-plus"></span>
</button>
<button id="do-minus" class="btn navbar-btn">
<span class="glyphicon glyphicon-minus"></span>
</button>
<button id="do-scan-barcode" class="btn navbar-btn">
<span class="glyphicon glyphicon-barcode"></span>
</button>
<button id="do-delete" class="btn navbar-btn">
<span class="glyphicon glyphicon-trash"></span>
</button>
<button class="btn navbar-btn" data-toggle="modal" data-target="#item-modal">
<span class="glyphicon glyphicon-tag"></span>
</button>
<button id="do-refresh" class="btn navbar-btn">
<span class="glyphicon glyphicon-refresh"></span>
</button>
</div>
</div>
</nav>
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
function checkForUpdates() {
}
$(document).ready(function(){
$('#cart').on('click', '.cart-item', function() {
$(this).siblings().removeClass("list-group-item-info");
$(this).toggleClass("list-group-item-info");
});
$('#cart').on('click', '.cart-item-tick', function(event) {
event.stopPropagation();
var item = $(this).parent();
toggleItemDone(item);
});
function toggleItemDone(item) {
var tick = item.children('.cart-item-tick');
var itemID = item.attr('id');
var itemDone = !tick.hasClass('cart-item-have');
var request = {
item: {
done: Number(itemDone)
}
};
$.ajax({
url: '/list/1/item/' + itemID,
type: 'PUT',
data: JSON.stringify(request),
success: function(data, status) {
tick.toggleClass("cart-item-have");
item.toggleClass("greyout");
}
});
}
function updateItemCount(item, amount) {
var badge = item.children('.badge')
var itemID = item.attr('id')
var itemCount = parseInt(badge.text()) + amount
var request = {
item: {
count: itemCount
}
};
$.ajax({
url: '/list/1/item/' + itemID,
type: 'PUT',
data: JSON.stringify(request),
success: function(data, status) {
badge.text(itemCount)
}
});
}
$("#do-plus").click(function() {
var item = $('.list-group-item-info')
updateItemCount(item, 1)
});
$("#do-minus").click(function() {
var item = $('.list-group-item-info')
updateItemCount(item, -1)
});
$('#item-modal').on('shown.bs.modal', function () {
$('#item-input').focus();
})
var html =
' \
<li class="cart-item list-group-item" id="temp-item"> \
<span class="cart-item-tick glyphicon glyphicon-ok-sign"></span> \
<span class="cart-item-name">Temporary Item</span> \
<span class="badge">1</span> \
</li> \
'
function addItem(item) {
var dom = $($.parseHTML(html));
console.log(dom);
console.log(html);
var tick = dom.children('.cart-item-tick');
var badge = dom.children('.badge')
dom.children('.cart-item-name').text(item.name)
dom.attr('id', item.id)
if (item.done != 0) {
tick.toggleClass("cart-item-have");
dom.toggleClass("greyout");
}
badge.text(item.count)
$('#cart').append(dom);
}
function getItem(url) {
$.get(url, function(data) {
var obj = jQuery.parseJSON(data);
console.log(obj);
$('#item-modal').modal('hide');
addItem(obj.item);
});
}
$("#do-new-item").click(function() {
var name = $('#item-input').val();
var request = {
item: {
name: name,
count: 1,
done: 0,
code: 'manual-' + name
}
};
$.ajax({
url: '/list/1/item',
type: 'POST',
data: JSON.stringify(request),
success: function(data, status, xhr) {
console.log(data)
console.log(status)
url = xhr.getResponseHeader('Location');
getItem(url);
}
});
});
function deleteItem(item) {
var itemID = item.attr('id')
$.ajax({
url: '/list/1/item/' + itemID,
type: 'DELETE',
success: function(data, status) {
item.remove();
}
});
}
$("#do-delete").click(function() {
var item = $('.list-group-item-info')
deleteItem(item);
});
$("#do-remove-checked").click(function() {
$('.cart-item:has(> .cart-item-have)').each(function() {
console.log('bob')
deleteItem($(this));
});
});
setInterval(checkForUpdates, 5000);
});
</script>
</body>
</html>