forked from drmproductions/ActiveCollabInlineTimers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
174 lines (133 loc) · 4.21 KB
/
utils.js
File metadata and controls
174 lines (133 loc) · 4.21 KB
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
module.exports = {
getTimer: function (timers, project, task) {
if (!Number.isInteger(project) || !Number.isInteger(task)) {
throw new Error('project and task must be an integer');
}
var result = null;
timers.some(function (timer) {
if (timer.project == project && timer.task == task) {
result = timer;
return true;
}
});
if (result === null) {
var result = {
project: project, task: task, state: 'stopped', total: 0, start: 0, jobTypeId: null, billable: null, summary: ''
};
timers.push(result);
}
return result;
},
getDefaultJobTypeId: function (session) {
var defaultJobTypeId = null;
// if session.prefs.jobTypeId is set, but it's not a jobType anymore, we use the lowest job type available
session.jobTypes.some(function (jobType) {
// use the jobtype in our prefs if its set
if (jobType.id == session.prefs.jobTypeId) {
defaultJobTypeId = session.prefs.jobTypeId;
return true;
}
// otherwise we use the lowest jobtype
if (defaultJobTypeId == null || jobType.id < defaultJobTypeId) {
defaultJobTypeId = jobType.id;
}
});
return defaultJobTypeId;
},
timeStringValid: function (timeString) {
var split = timeString.split(':');
if (split.length != 2) return false;
var hours = parseInt(split[0]);
var mins = parseInt(split[1]);
if (isNaN(hours) || isNaN(mins)) return false;
if (hours > 23) return false;
if (mins > 59) return false;
return true;
},
setTimerTimeFromString: function (timer, timeString) {
if (!this.timeStringValid(timeString)) return false;
var split = timeString.split(':');
var hours = parseInt(split[0]);
var mins = parseInt(split[1]);
if (hours == 0 && mins == 0) {
timer.total = 0;
timer.start = 0;
timer.state = 'stopped';
return true;
}
var d = new Date(0);
d.setUTCHours(hours);
d.setUTCMinutes(mins);
if (timer.state == 'stopped') {
timer.total = d.getTime();
timer.start = Date.now();
timer.state = 'paused';
}
else if (timer.state == 'paused') {
timer.total = d.getTime();
}
else if (timer.state == 'running') {
timer.total = d.getTime();
timer.start = Date.now();
}
return true;
},
currentTab: function (cb) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { cb(tabs[0]); });
},
activeCollabDateString: function (date) {
var y = date.getFullYear(), m = date.getMonth() + 1, d = date.getDate();
m = (m.toString().length == 1) ? '0' + m : m;
d = (d.toString().length == 1) ? '0' + d : d;
return y + "-" + m + "-" + d;
},
totalTime: function (timer, roundBy, minimumValue, removeSeconds) {
var total = timer.total + (timer.state == 'running' ? (Date.now() - timer.start) : 0);
if (removeSeconds === true) {
total -= total % 10000;
}
if (Number.isInteger(roundBy) && roundBy !== 0) {
total = Math.ceil(total / roundBy) * roundBy;
}
if (Number.isInteger(minimumValue) && total < minimumValue) {
total = minimumValue;
}
return total;
},
formattedTime: function (timer, roundBy, minimumValue) {
var time = this.totalTime(timer, roundBy, minimumValue, true), text = '';
var d = new Date(time);
var h = d.getUTCHours().toString();
var m = d.getUTCMinutes().toString();
h = (h.length == 1 ? '0' + h : h);
m = (m.length == 1 ? '0' + m : m);
return h + ':' + m;
},
copyArray: function (arr) {
return arr.map(function (obj) { return Object.assign({}, obj) });
},
getSendableTimers: function (timers) {
var sendableTimers = [];
timers.forEach(function (timer) {
sendableTimers.push({ project: timer.project, task: timer.task });
});
return sendableTimers;
},
waterfall: function (funcs, cb) {
var i = 0, next = function () {
var args = [];
Array.prototype.push.apply(args, arguments);
if (args.length < 1) args.push(null);
// return right away if there was an error while running the last func
var err = args.shift();
if (err !== null) return cb(err);
// call the cb when we're done with all the funcs
if (i == funcs.length) {
return cb.apply(this, [null].concat(args));
}
// call the next func, passing it the next
// variable, and the previous funcs passed arguments
funcs[i++].apply(this, [next].concat(args));
}; next(null);
}
};