This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
the-abrechner.js
458 lines (409 loc) · 15.9 KB
/
the-abrechner.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
Abrechnungs = new Mongo.Collection("abrechnungs");
Persons = new Mongo.Collection("persons");
Items = new Mongo.Collection("items");
Router.configure({
layoutTemplate: 'main',
notFoundTemplate: 'notFound'
});
Router.map(function(){
this.route('home', {
path: '/',
});
this.route('abrechner', {
path: '/:link',
waitOn: function() {
var link = this.params.link;
Session.set("link", link);
return [
Meteor.subscribe("abrechnungs", link),
Meteor.subscribe("items", link),
Meteor.subscribe("persons", link)];
},
data: function() {
return Abrechnungs.findOne({"link": this.params.link});
}
});
});
Router.onBeforeAction('dataNotFound');
if (Meteor.isClient) {
//Work around for template rendering animation
Template.message.onRendered(function(){
$(this.firstNode).fadeIn();
});
function showMessage(text, where){
text += "This will definitely lead to distrust and bloodshed.";
Blaze.renderWithData(Template.message,
{"text" : text},
where, //Append to
$(where).children(":first-child").get(0)); //but before first child
}
Template.main.onRendered(function(){
/***********************************************
* Encrypt Email script- Please keep notice intact
* Tool URL: http://www.dynamicdrive.com/emailriddler/
* **********************************************/
var emailriddlerarray=[108,117,107,46,112,117,101,104,114,105,110,103,101,114,64,103,109,97,105,108,46,99,111,109];
var encryptedemail_id42=''; //variable to contain encrypted email
for (var i=0; i<emailriddlerarray.length; i++)
encryptedemail_id42+=String.fromCharCode(emailriddlerarray[i]);
$("#email").attr("href", "mailto:" + encryptedemail_id42);
});
Template.socialMedia.events({
"click .social" : function(event){
var type = $(event.target).data('type');
if (type == "twitter" || type == "facebook" || type == "googleplus") {
//The data
var url = window.location.href;
// title depending on current route
var where = Router.current().route.getName();
var title = "";
if (where == 'home') {
title = "The Urlaubs Abrechnungs Machine";
} else if (where == 'abrechner') {
title = this.title;
}
//Sharers
var link = "", width = "", height = "";
if (type == "twitter") {
link = 'https://twitter.com/share?url=' + url + '&text=' + title,
width = 685, height = 500;
}
if (type == "facebook") {
link ='https://www.facebook.com/sharer/sharer.php?u=' + url,
width = 626, height = 436;
}
if (type == "googleplus") {
link = 'https://plus.google.com/share?url=' + url,
width = 600, height = 600;
}
//Share!!
var left = (window.innerWidth/2) - (width/2),
top = (window.innerHeight/2) - (height/2);
window.open(link, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
} else {
console.log("Where do you want to share today?!")
}
}
});
Template.home.events({
"submit .new-abrechner": function(event) {
var form = event.target;
$(form).find(".message").remove();
var title = event.target.title.value;
var description = event.target.description.value;
var message = "";
if (title.length < 1)
message += "What, no title? ";
if (title.length > 20)
message += "A title with more than 20 characters is silly. ";
if (description.length > 255)
message += "A description with more than 255 characters is silly. ";
if (message)
showMessage(message, form);
else
Meteor.call("addAbrechner", title, description, function(err, link){
Router.go('abrechner', {"link": link});
});
return false;
}
});
Template.abrechner.helpers({
personsForAbrechnung: function(){
return Persons.find();
},
itemsForAbrechnung: function(){
return Items.find();
},
hasPersons: function() {
if (Persons.find().count())
return true;
},
hasItems: function() {
if (Items.find().count())
return true;
},
personNameById: function(personId) {
var person = Persons.findOne(personId);
if (person)
return person.name;
else
return "";
},
host: function(){
return window.location.host;
},
optText: function(itemId) {
var item = Items.findOne(itemId);
if (item.amount < 0 && ! this.pays)
return "gets nothing from this item";
if (item.amount > 0 && ! this.pays)
return "pays nothing for this item";
var shares = item.shares;
var cnt = 0;
for (var i = 0; i < shares.length; i++)
if (shares[i].pays)
cnt++;
if (item.amount < 0 && this.pays)
return "gets " + (item.amount / cnt).toFixed(2) + " from this item";
if (item.amount > 0 && this.pays)
return "pays " + (item.amount / cnt).toFixed(2) + " for this item";
},
sendAbrechner: function(){
return "mailto:?subject=" + encodeURI('Let\'s use the Urlaubs Abrechnungs Machine') +
"&body=" +
encodeURI(this.title) + "%0D%0A" +
encodeURI(this.description) + "%0D%0A" + "%0D%0A" +
encodeURI(window.location.href);
}
});
Template.abrechner.events({
// Add new Person
"submit .new-person": function (event) {
var form = event.target;
$(form).find(".message").remove();
var link = Session.get("link");
var name = event.target.person.value;
var message = "";
if (name.length < 1)
message += "What, your person has no name? ";
if (name.length > 20)
message += "Persons with names with more than 20 characters are silly. ";
if (Persons.find({"name": name}).count() > 0)
message += "There is already someone with that name. ";
if (message){
showMessage(message, form);
} else {
Meteor.call("addPerson", link, name);
event.target.person.value = "";
}
return false;
},
"click .delete-person": function () {
var form = $("form.new-person").get(0);
$(form).find(".message").remove();
var link = Session.get("link");
var personId = this._id;
var message = "";
if (Items.find({"paidBy" : personId}).count() > 0)
message = "Hey, this person has already paid for something. ";
if (message)
showMessage(message, form)
else
Meteor.call("removePerson", link, personId);
return false;
},
// Add new item
"submit .new-item": function (event) {
var form = event.target;
$(form).find(".message").remove();
var link = Session.get("link");
var title = event.target.item.value;
var amount = event.target.amount.value.replace(",",".");
var personId = event.target.person.value;
var message = "";
if (title.length < 1)
message += "What, no title? ";
if (title.length > 20)
message += "Item titles with more than 20 characters are silly. ";
if (!$.isNumeric(amount))
message += "You specified a silly number for the costs! ";
if (Persons.find(personId).count() < 1)
message += "Who paid for the item? ";
if (message){
showMessage(message, form);
} else {
amount = parseFloat(amount)
Meteor.call("addItem", link, title, amount, personId);
event.target.item.value = "";
event.target.amount.value = "";
}
return false;
},
"click .delete-item": function () {
var link = Session.get("link");
var itemId = this._id;
Meteor.call("removeItem", link, itemId);
},
"change .pays-share": function(event) {
var link = Session.get("link");
var itemId = $(event.target).data("item-id");
var personId = this.person;
var checked = event.target.checked;
Meteor.call("updateShare",link, itemId, personId, checked);
},
"click .delete-abrechnung": function(event) {
var link = Session.get("link");
var abrechnungsId = this._id;
Meteor.call("removeAbrechnung", link, abrechnungsId);
Router.go('home');
},
"click .mark-url": function(event) {
$("#url").focus();
$("#url").select();
}
});
Template.breakdown.onRendered(function(){
//Datatables
var table = $('#breakdownTable').dataTable({
"ordering": true,
"paging": false,
"info" : false,
"bFilter": false,
}).api();
Tracker.autorun(function(){
var persons = new Array();
Persons.find().forEach(function(person) {
return persons[person._id] = { "name": person.name, "paid": 0, "pays": 0};
});
//Go through all items and fill persons Array
Items.find().forEach(function(item){
//Add paid amount to paying person
persons[item.paidBy].paid += item.amount;
//Check how many persons want to pay and divide
var divider = 0;
item.shares.forEach(function(share){
if (share.pays)
divider += 1;
});
var absShare = 0;
if (divider > 0)
absShare = item.amount / divider;
//Add pays amount to all persons
item.shares.forEach(function(share){
if (share.pays)
persons[share.person].pays += absShare;
});
});
table.clear();
Object.keys(persons).forEach( function(key) {
table.row.add([persons[key].name,
persons[key].paid.toFixed(2),
persons[key].pays.toFixed(2),
(persons[key].paid - persons[key].pays).toFixed(2)]);
});
table.draw();
});
// Draw pie if container rendered for the first time
var paperH = 300, paperW = 300
var paper = Raphael("raphael-container", paperW, paperH);
Tracker.autorun(function(){
var items = Items.find().fetch();
paper.clear();
var totalAmounts = [];
var totalItems = [];
items.forEach(function(item){
if (item.amount > 0) {
totalAmounts.push(item.amount);
totalItems.push(item.title + " - € " + item.amount);
}
});
var chart = paper.piechart(150, 150, 100, totalAmounts, {
legend: totalItems,
legendpos: "south",
});
var textH = $("#raphael-container svg text").height() ;
if (textH == 0)
textH = 14;
var legendH = textH * items.length;
paper.setSize(paperW, paperH + legendH);
});
});
Template.breakdown.helpers({
toFixed: function(number) {
return number.toFixed(2);
},
diffClass: function(diff) {
if (diff < 0)
return "text-danger";
else
return "text-success";
},
sum: function(){
var sum = 0;
Items.find().forEach(function(item){
sum += item.amount;
});
return sum.toFixed(2);
},
fairShare: function() {
var sum = 0;
Items.find().forEach(function(item){
sum += item.amount;
});
return sum / Persons.find().count()
}
})
}
if (Meteor.isServer) {
Meteor.publish("abrechnungs", function (link) {
return Abrechnungs.find({"link": link});
});
Meteor.publish("items", function (link) {
return Items.find({"link": link});
});
Meteor.publish("persons", function (link) {
return Persons.find({"link": link});
});
}
Meteor.methods({
/*
* Create a hash5 link based on current Time and a random int
* For collision should be tested
*/
_createLink: function() {
//Link must be unique!!
do {
var rand = new Date().getTime().toString() + Math.floor(Math.random() * 10000 + 1).toString();
var hash = CryptoJS.MD5(rand);
var link = hash.toString();
} while(Abrechnungs.find({"link" : link}).count() > 0)
return link;
},
addAbrechner: function(title, description) {
var link = Meteor.call("_createLink");
Abrechnungs.insert({"title": title, "description": description, "link": link, createdAt: new Date()});
return link;
},
addPerson: function(link, name){
var personId = Persons.insert({"link": link, "name": name})
Items.update(
{"link": link},
{$push: {
"shares" : {
"person": personId, "pays" : true
}}}, {multi: true})
},
removePerson: function(link, personId) {
Items.update({"link": link}, {$pull: {"shares" : {"person" : personId}}}, {multi: true});
Persons.remove(personId);
},
addItem: function(link, title, amount, personId) {
//Create share array for all existing persons
var shares = Persons.find({"link": link}).map(function(p){
return {
person: p._id,
pays: true
}
});
Items.insert({"link": link,
"title" : title,
"amount" : amount,
"paidBy" : personId,
"shares" : shares })
},
removeItem: function(link, itemId) {
Items.remove({"_id": itemId, "link": link});
},
updateShare: function (link, itemId, personId, pays) {
Items.update({
"_id" : itemId,
"link": link,
"shares.person": personId
}, {$set: {"shares.$.pays": pays}});
},
removeAbrechnung: function(link, abrechnungsId) {
Items.remove({"link": link});
Persons.remove({"link": link});
Abrechnungs.remove({"_id": abrechnungsId, "link": link});
}
});