forked from cthit/EatIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza.js
257 lines (239 loc) · 7.37 KB
/
pizza.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
OrderItems = new Mongo.Collection("order_items");
Orders = new Mongo.Collection("orders");
function randomHash() {
var hex = parseInt(Math.random() * 0xfff).toString(16);
return ("000" + hex).slice(-3); // pad with 3 zeros
}
if (Meteor.isClient) {
var hash = window.location.hash;
Meteor.subscribe('orders', function() {
var order, error = false;
if (hash) {
hash = hash.substring(1).toLowerCase();
}
order = Orders.findOne({'hash': hash});
if (hash && !order) {
error = true;
}
Session.set('error', error);
if (!order && !error) {
console.log('Creating new order!');
do {
hash = randomHash();
order = Orders.findOne({'hash': hash});
} while (order !== undefined);
var id = Orders.insert({
'hash': hash,
createdAt: new Date()
});
order = Orders.findOne({'hash': hash});
window.location.hash = hash;
}
if (order) {
Session.set('order', order._id);
Session.set('url', window.location.href);
}
$('#share-link-qr').qrcode({
size: 150,
text: Session.get('url')
});
});
Session.set('url', window.location.href);
Template.body.helpers({
url: function() {
return Session.get('url');
},
items: function () {
var items = OrderItems.find({order: Session.get('order')}).fetch();
var pizzas = _.groupBy(items, function(item) { return item.pizza });
pizzas = _.map(pizzas, function(value, key) {
value.name = key;
return value;
});
return _.sortBy(pizzas, function(pizza){ return -pizza.length; });
},
items_total_count: function() {
var items = OrderItems.find({order: Session.get('order')}).fetch();
return items.length;
},
error: function() {
return Session.get('error');
},
timer_started: timer_started
});
Template.body.events({
"submit .new-pizza": function (event) {
var pizza = event.target,
pizzaName = pizza.pizza.value.trim(),
nick = pizza.nick.value.trim();
// comment = pizza.comment.value.trim();
if (!pizzaName || !nick) {
return false;
}
OrderItems.insert({
order: Session.get('order'),
pizza: pizza.pizza.value.trim(),
nick: pizza.nick.value.trim(),
// comment: pizza.comment.value.trim(),
createdAt: new Date()
});
pizza.pizza.value = '';
// pizza.nick.value = '';
// pizza.comment.value = '';
return false;
},
"click .pizza-item .name": function(event) {
var name = $(event.target).data('name');
$('.new-pizza [name="pizza"]').val(name).next().focus();
},
"click .pizza-item small.deletable": function(event) {
if (confirm("Are you sure?")) {
var id = $(event.target).data('id');
OrderItems.remove({_id: id});
}
},
"click #toggle-share-link-qr": function(event) {
$('#share-link-qr').toggle();
},
"click #share-link": function(e){
// Create an auxiliary hidden input
var aux = document.createElement("input");
// Get the text from the element passed into the input
aux.setAttribute("value", Session.get('url'));
// Append the aux input to the body
document.body.appendChild(aux);
// Highlight the content
aux.select();
// Execute the copy command
document.execCommand("copy");
// Remove the input from the body
document.body.removeChild(aux);
}
});
function time_left() {
var date = new Date();
var order = Orders.findOne(Session.get('order'));
var seconds = parseInt((order.timer_end - date) / 1000);
var mins = parseInt(seconds / 60);
seconds = seconds % 60;
return [mins, seconds];
}
function timer_started() {
var order = Orders.findOne(Session.get('order'));
return Boolean(order && order.timer_end);
}
function format_time(time) {
var prefix = '';
if (time[0] < 0 || time[1] < 0) {
prefix = '-';
}
time[0] = Math.abs(time[0]);
time[1] = Math.abs(time[1]);
if (time[1] < 10) {
time[1] = '0' + time[1];
}
if (isNaN(time[0])) {
return '';
}
return prefix + time.join(':');
}
Template.pizza.helpers({
classname: function() {
return timer_started() ? '' : 'deletable editable';
}
});
Template.timer.created = function() {
this.handle = Meteor.setInterval(function() {
var time = time_left();
if (time[0] === 0 && time[1] === 0 || (time[1] < 0 && !window.playing)) {
if (player && player.getPlayerState() === YT.PlayerState.CUED) {
console.log('stop!');
document.body.className += ' animated';
window.playing = true;
player.seekTo(51);
}
// Meteor.clearInterval(this.handle);
}
Session.set('time_left', format_time(time));
}.bind(this), 500);
};
Template.timer.helpers({
display_time: function() {
return Session.get('time_left');
},
timer_started: timer_started
});
Template.timer.events({
"submit .start-timer": function(event) {
if (confirm("Are you sure?")) {
var mins = parseInt(event.target.minutes.value.trim());
var end_date = (new Date()).valueOf() + mins * 60000;
Orders.update(Session.get('order'), { $set: {timer_end: end_date}})
return false;
}
}
});
Template.swish.helpers({
name_placeholder: "Name (optional)",
name: function() {
var order = Orders.findOne(Session.get('order'));
if (order) {
return order.swishName;
}
},
number_placeholder: "Swish number",
number: function() {
var order = Orders.findOne(Session.get('order'));
if (order) {
return order.swishNbr;
}
},
has_name: function() {
var order = Orders.findOne(Session.get('order'));
if (order) {
return order.swishName !== undefined;
}
return false;
},
has_number: function() {
var order = Orders.findOne(Session.get('order'));
if (order) {
return order.swishNbr !== undefined;
}
return false;
}
});
Template.swish.events({
"submit .submit-swish": function(event) {
var name = event.target.swishName.value.trim();
var number = event.target.swishNbr.value.trim();
if (name.length == 0) {
Orders.update(Session.get('order'), { $unset: {swishName: ""}});
} else {
Orders.update(Session.get('order'), { $set: {swishName: name}});
}
if (number.length == 0) {
Orders.update(Session.get('order'), { $unset: {swishNbr: ""}});
} else {
Orders.update(Session.get('order'), { $set: {swishNbr: number}});
}
return false;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish('orders', function() {
return Orders.find();
});
Meteor.publish('order_items', function() {
return OrderItems.find();
});
Meteor.setInterval(function() {
var milliseconds = 172800 * 1000; // 48 hours in ms
var time = new Date() - milliseconds;
Orders.remove({createdAt: { $lt: time }});
OrderItems.remove({createdAt: { $lt: time }});
}, 600000); // every 10th minute
});
}