-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclockr.js
76 lines (64 loc) · 2.17 KB
/
clockr.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
Clocks = new Meteor.Collection("clocks");
if (Meteor.isClient) {
Window.Clocks = Clocks;
var isClocking = false;
var currentClock = {};
var lastClockStart = Date.now();
Handlebars.registerHelper("inSeconds", function(ms) {
return (ms / 1000).toFixed(1);
});
Handlebars.registerHelper("inMinutes", function(ms) {
var secs = ms / 1000 % 60;
var mins = ((ms / 1000) - secs) / 60;
secs = Math.ceil(secs);
secs = (new Array(3 - secs.toString().length)).join('0') + secs;
return mins + ":" + secs;
});
Handlebars.registerHelper("inTime", function(ms) {
var t = new Date(ms);
var hours = t.getHours();
var mins = t.getMinutes();
mins = (new Array(3 - mins.toString().length)).join('0') + mins;
var secs = t.getSeconds();
secs = (new Array(3 - secs.toString().length)).join('0') + secs;
return hours + ":" + mins + ":" + secs;
});
Template.clockr.clocks = function () {
return Clocks.find({}, { sort: [["start", "desc"]] } );
};
Template.clockr.isClocking = function() {
return ( isClocking ) ? "started" : "";
};
Template.clockr.isNewDay = function() {
var difference = this.start - lastClockStart;
lastClockStart = this.start;
if( difference > 1800000 )
return "new_day";
};
Template.clockr.events({
'click #clock' : function () {
if(isClocking) { // Stop clocking and save to DB
currentClock.stop = Date.now();
currentClock.duration = currentClock.stop - currentClock.start;
Clocks.insert(currentClock);
console.log("Start: " + currentClock.start +
" | Stop: " + currentClock.stop +
" | Durattion: " + currentClock.duration);
isClocking = false;
document.getElementById("clock_label").innerText = "Start Clocking!";
} else { // Start clocking!
currentClock.start = Date.now();
console.log("Started @ " + currentClock.start);
isClocking = true;
document.getElementById("clock_label").innerText = "STHAP!";
}
},
'click .remove' : function () {
Clocks.remove(this._id);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
});
}