-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_info.js
282 lines (241 loc) · 12.2 KB
/
time_info.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
/**
* @file Logic for the time info browser action popup
*/
class TimeInfo {
constructor() {
this.shiftSpanEl = document.getElementById('shift_span')
this.shiftNotesEl = document.getElementById('shift_notes')
this.shiftNotesInput = this.shiftNotesEl.getElementsByTagName('textarea')[0]
this.customFieldsContainer = document.getElementById('custom_fields')
this.customFieldsEls = {};
this.confirmModalEl = document.getElementById('confirm_modal');
this.confirmModal = new Modal(this.confirmModalEl, {});
this.clockInButton = document.getElementById('clock_in')
this.clockInConfirmButton = document.getElementById('clock_in_confirm')
this.clockOutButton = document.getElementById('clock_out')
this.getTodaysNotesButton = document.getElementById('get_todays_notes');
this.blurWarningEl = document.getElementById('blur_warning');
this.timesheetId = null; // Store the active timesheet
this.notesEdited = false;
this.isClockedIn = null;
this.heartbeat();
this.repeat = setInterval(this.heartbeat, 3000);
// Don't spam the network when the popup is closed
window.onblur = () => {
clearInterval(this.repeat);
console.log('BLURRED. Will no longer update.');
setTimeout(() => {
this.blurWarningEl.style.display = 'block';
setTimeout(() => this.blurWarningEl.style.opacity = 1, 10)
}, 300)
}
window.onfocus = () => {
clearInterval(this.repeat);
this.repeat = setInterval(this.heartbeat.bind(this), 3000);
console.log('Got focus. Updates resumed.');
this.blurWarningEl.style.display = 'none';
this.blurWarningEl.style.opacity = 0;
}
this.getTodaysNotesButton.onclick = () => {
chrome.runtime.sendMessage({getTodaysNotes: true}, (response) => {
console.log(response);
var notesDiv = document.getElementById('todays_notes_div');
var tblContents = "<table><thead><tr><th>Project</th><th>Notes</th></tr></thead><tbody>";
for (var note of response.notes) {
console.log(note);
tblContents += "<tr><td>" + note.job_name + "</td><td>" + note.note + "</td></tr>";
}
tblContents += "</tbody></table>";
notesDiv.innerHTML = tblContents;
window.getSelection().removeAllRanges();
let range = document.createRange();
range.selectNode(notesDiv.firstElementChild);
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
this.getTodaysNotesButton.classList.add('success');
setTimeout(() => {
this.getTodaysNotesButton.classList.remove('success');
}, 2000);
});
}
var typingTimer; //timer identifier
var doneTypingInterval = 2000; //time in ms
//on keyup, start the countdown
this.shiftNotesEl.onkeyup = () => {
this.notesEdited = true;
clearTimeout(typingTimer);
typingTimer = setTimeout(this.doneTyping.bind(this), doneTypingInterval);
}
//on keydown, clear the countdown
this.shiftNotesEl.onkeydown = () => {
clearTimeout(typingTimer);
}
this.shiftNotesInput.onblur = () =>{
// If textarea loses focus, immediately save changes
if (this.notesEdited) {
clearTimeout(typingTimer);
this.doneTyping();
}
}
this.confirmModalEl.addEventListener('shown.bs.modal', (e) => {
if (this.isClockedIn) {
this.confirmModal.setContent(`
<div class="modal-header">
<h5 class="modal-title">Clock Out</h5>
</div>
<div class="modal-body">
Are you sure you want to clock out?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-warning" id="clock_out_confirm">Confirm</button>
</div>
`);
var clockOutConfirmButton = document.getElementById('clock_out_confirm')
clockOutConfirmButton.onclick = () => {
chrome.runtime.sendMessage({clockOut: {timesheetId: this.timesheetId}}, (response) => {
if (!response.error) this.confirmModal.hide();
this.heartbeat();
});
}
} else {
this.confirmModal.setContent(`
<div class="modal-header">
<h5 class="modal-title">Clock In</h5>
</div>
<div class="modal-body">
Select assignment to clock in to.
<select id="jobs-select"></select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-warning" id="clock_in_confirm">Confirm</button>
</div>
`);
var clockInConfirmButton = document.getElementById('clock_in_confirm')
clockInConfirmButton.onclick = () => {
var jobId = document.getElementById('jobs-select').value;
chrome.runtime.sendMessage({clockIn: {jobId: jobId}}, (response) => {
if (!response.error) this.confirmModal.hide();
this.heartbeat();
});
}
var jobsSelectEl = document.getElementById('jobs-select');
chrome.runtime.sendMessage({getJobs: {updateIcon: true}}, (resp) => {
for (var job of resp.jobs) {
var optionEl = document.createElement('option');
optionEl.value = job.id;
optionEl.text = job.name;
jobsSelectEl.appendChild(optionEl);
}
})
}
});
}
heartbeat() {
console.log('Heartbeat')
chrome.runtime.sendMessage({getNow: {updateIcon: true}}, (response) => {
if (response) {
if (response.error) {
console.error(response.error.message);
clearInterval(this.repeat);
} else if (response.isCached === true) {
console.log('Got a cached response')
}
this.timesheetId = response.status.timesheetId;
var isClockedInEl = document.getElementById('is_clocked_in')
this.isClockedIn = response.status.clockedIn;
if (response.status.clockedIn) {
isClockedInEl.innerText = 'Clocked In'
isClockedInEl.className = 'alert alert-success'
var jobNameEl = document.getElementById('job_name');
jobNameEl.innerText = 'Job: ' + response.status.jobName;
this.clockInButton.style.display = 'none'
this.clockOutButton.style.display = 'block'
// Show custom field info
if (response.customFields) {
for (let [key, val] of Object.entries(response.customFields)) {
if (this.customFieldsEls[key]) continue;
var field;
if (val.type === "drop_down") {
console.log(val.options);
field = document.createElement('select');
for (let opt of val.options) {
// !! HACK, it feels like the value should be the ID of the customfielditem, but it's the name instead
if (opt.name === val.value) {
val.value = opt.id;
}
// !! end hack
var optionEl = document.createElement('option');
optionEl.value = opt.id;
optionEl.innerText = opt.text;
field.appendChild(optionEl);
}
} else {
field = document.createElement('input');
field.innerText = key;
}
field.name = key;
field.value = val.value;
this.customFieldsEls[key] = field;
this.customFieldsContainer.appendChild(field);
}
}
} else {
isClockedInEl.innerText = 'Clocked Out'
isClockedInEl.className = 'alert alert-warning'
this.clockOutButton.style.display = 'none'
this.clockInButton.style.display = 'block'
}
var weekTimeEl = document.querySelector("#week_total .data");
weekTimeEl.innerText = response.totals.week;
var dayTimeEl = document.querySelector("#day_total .data");
dayTimeEl.innerText = response.totals.day;
var shiftTimeEl = document.querySelector('#shift_time .data');
shiftTimeEl.innerText = response.status.shift_time
if (response.status.timesheetId) {
if (!this.notesEdited)
this.shiftNotesInput.value = response.status.shift_notes
var start = new Date(response.status.start);
var end = response.status.end ? new Date(response.status.end) : null;
this.shiftSpanEl.innerHTML = this.formatTime(start) + ' – ' + (end ? this.formatTime(end) : 'now')
if (shiftTimeEl.parentElement.style.display === 'none') shiftTimeEl.parentElement.style.display = 'block';
if (this.shiftNotesEl.style.display === 'none') this.shiftNotesEl.style.display = 'block';
} else {
shiftTimeEl.parentElement.style.display = 'none';
this.shiftNotesEl.style.display = 'none';
}
var body = document.getElementsByTagName('body')[0];
if (body.style.display === 'none') body.style.display = 'block'
}
});
}
formatTime(date) {
var timeWithAmPm = date.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true })
// Slice off AM/PM
return timeWithAmPm.substr(0, timeWithAmPm.length-3)
}
//user is finished typing, do something
doneTyping () {
console.log('User finished typing, handling notes update')
if (this.timesheetId) {
chrome.runtime.sendMessage({updateNotes: {
timesheetId: this.timesheetId,
notes: this.shiftNotesInput.value}
}, (response) => {
this.notesEdited = false;
if (response.error) {
var errorAlert = document.createElement('div');
errorAlert.className = 'alert alert-danger';
errorAlert.innerText = response.error.message;
this.shiftNotesEl.appendChild(errorAlert);
} else {
// Tell the user it saved
this.shiftNotesEl.querySelector('.saved').style.opacity = 1;
setTimeout(() => {this.shiftNotesEl.querySelector('.saved').style.opacity = 0}, 3000);
}
});
}
}
}