-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
127 lines (101 loc) · 3.02 KB
/
popup.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
(function () {
'use strict';
const
view = {
get noReport() {
return document.querySelector('.no-report');
},
get openOption() {
return document.querySelector('.openOption');
},
get form() {
return document.querySelector('.timesheet-form');
},
get reportSelect() {
return document.getElementById('reportSelect');
},
get reportInfo() {
return document.querySelector('.report-info');
},
get days() {
return document.querySelector('.days');
},
get time() {
return document.querySelector('.time');
},
get comment() {
return document.querySelector('.comment');
},
get submit() {
return document.getElementById('submit');
}
},
reportsById = {},
values = {
reportSelect: ''
};
document.addEventListener('DOMContentLoaded', onLoad, false);
fmtApi.utils.setMessageProxy({
[fmtApi.events.timeSheetFillAck]: onFormFillAck
});
async function onLoad() {
await initializeForm();
view.openOption.addEventListener('click', () => {
chrome.tabs.create({url: '/options.html'});
});
}
function onReportSelect(e) {
e.preventDefault();
values.reportSelect = reportsById[e.target.value];
renderReport(values.reportSelect);
}
function onSubmit(e) {
e.preventDefault();
sendFormData();
}
function onFormFillAck() {
view.submit.disabled = false;
}
// ------------------
async function initializeForm() {
const reports = await fmtApi.reports.all();
if (reports.length === 0) {
const formElt = view.form;
formElt.parentElement.removeChild(formElt);
return;
}
const noReportElt = view.noReport;
noReportElt.parentElement.removeChild(noReportElt);
values.reportSelect = reports[0];
initializeReportSelect(reports);
renderReport(values.reportSelect);
view.submit.addEventListener('click', onSubmit, false);
}
function initializeReportSelect(reports) {
const selectElt = view.reportSelect;
reports.forEach((report) => {
const optionElt = document.createElement('option');
optionElt.value = report.id;
optionElt.textContent = report.name;
reportsById[report.id] = report;
selectElt.appendChild(optionElt);
});
selectElt.addEventListener('change', onReportSelect, false);
}
function renderReport(report) {
view.reportInfo.style.display = 'block';
view.days.textContent = fmtApi.reports.renderDays(report.days);
view.time.textContent = `Time in: ${report.timeIn}, time out: ${report.timeOut}`;
view.comment.textContent = report.comment.length > 100
? `${report.comment.slice(0, 100)}...`
: report.comment;
}
async function sendFormData() {
const currentTab = await fmtApi.utils.getCurrentTab();
view.submit.disabled = true;
chrome.tabs.sendMessage(currentTab.id, {
type: fmtApi.events.timeSheetFormSubmit,
payload: values.reportSelect
});
}
})();