-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
251 lines (208 loc) · 6.55 KB
/
options.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
(function () {
'use strict';
const
view = {
list: {
get element() {
return document.querySelector('.time-reports');
},
get message() {
return document.querySelector('.message');
},
get items() {
return document.querySelectorAll('.time-report');
},
name(item) {
return item.querySelector('.name');
},
days(item) {
return item.querySelector('.days');
},
time(item) {
return item.querySelector('.time');
},
comment(item) {
return item.querySelector('.comment');
},
edit(item) {
return item.querySelector('.edit');
},
remove(item) {
return item.querySelector('.remove');
}
},
reportForm: {
get name() {
return document.getElementById('name');
},
get timeIn() {
return document.getElementById('timeIn');
},
get timeOut() {
return document.getElementById('timeOut');
},
get comment() {
return document.getElementById('comment');
},
get daySelectContainer() {
return document.querySelector('.day-select-container');
},
get daySelect() {
return document.querySelectorAll('.day-select');
},
get submit() {
return document.getElementById('addReport');
},
get cancel() {
return document.getElementById('cancelReport');
}
}
},
weekDays = fmtApi.constants.days,
defaultValues = {
name: '',
timeIn: '', timeOut: '', comment: '',
days: [1, 2, 3, 4, 5]
};
let
templateItem,
emptyItemListMessage,
values;
document.addEventListener('DOMContentLoaded', onLoad, false);
async function onLoad() {
templateItem = view.list.element.removeChild(view.list.items[0]);
emptyItemListMessage = view.list.element.removeChild(view.list.message);
initializeReportForm();
setFormValues(defaultValues);
await populateReportList();
}
async function onEditClick(e) {
const reports = await fmtApi.reports.all();
const editedReport = reports.find(r => r.id === e.target.dataset.reportId);
if (!editedReport) {
await populateReportList();
return;
}
setFormValues(editedReport);
}
async function onSubmit(e) {
e.preventDefault();
await fmtApi.reports.save({
...values,
days: Array.from(values.days)
});
setFormValues(defaultValues);
await populateReportList();
}
function onCancelClick() {
setFormValues(defaultValues);
}
async function onRemoveClick(e) {
await fmtApi.reports.remove(e.target.dataset.reportId);
await populateReportList();
}
function onDaySelect(e) {
if (e.target.checked) {
values.days.add(parseInt(e.target.value));
} else {
values.days.delete(parseInt(e.target.value));
}
setButtonStatus();
}
// ------------------
function initializeReportForm() {
buildDaySelect();
const fieldSetter = (field) =>
(e) => {
values[field] = e.target.value;
setButtonStatus();
};
view.reportForm.name.addEventListener('input', fieldSetter('name'), false);
view.reportForm.timeIn.addEventListener('input', fieldSetter('timeIn'), false);
view.reportForm.timeOut.addEventListener('input', fieldSetter('timeOut'), false);
view.reportForm.comment.addEventListener('input', fieldSetter('comment'), false);
view.reportForm.submit.addEventListener('click', onSubmit, false);
view.reportForm.cancel.addEventListener('click', onCancelClick, false);
}
function setButtonStatus() {
const
nameProvided = !!values.name,
timeOrCommentProvided = values.timeIn || values.timeOut || values.comment,
atLeastOneDaySelected = values.days.size !== 0
;
view.reportForm.submit.disabled = !nameProvided || !timeOrCommentProvided || !atLeastOneDaySelected;
}
async function populateReportList() {
const
listElt = view.list.element,
existingNodes = Array.from(view.list.items)
.map(node => listElt.removeChild(node)),
reports = await fmtApi.reports.all();
if (reports.length === 0) {
listElt.appendChild(emptyItemListMessage);
return;
}
if (view.list.element.contains(emptyItemListMessage)) {
view.list.element.removeChild(emptyItemListMessage);
}
for (let i = 0; i < reports.length; i++) {
const
report = reports[i],
node = existingNodes[i]
? existingNodes[i]
: templateItem.cloneNode(true),
editBtn = view.list.edit(node),
removeBtn = view.list.remove(node)
;
view.list.name(node).textContent = report.name;
view.list.days(node).textContent = fmtApi.reports.renderDays(report.days);
view.list.time(node).textContent = `From ${report.timeIn} to ${report.timeOut}`;
view.list.comment(node).textContent = report.comment;
editBtn.dataset.reportId = report.id;
removeBtn.dataset.reportId = report.id;
editBtn.addEventListener('click', onEditClick);
removeBtn.addEventListener('click', onRemoveClick);
listElt.appendChild(node);
}
}
function buildDaySelect() {
const daySelectContainer = view.reportForm.daySelectContainer;
weekDays
.slice(1, 7)
.concat(weekDays[0])
.forEach((day, i) => {
const
label = document.createElement('label'),
span = document.createElement('span'),
chkBx = document.createElement('input');
chkBx.value = `${(i + 1) % 7}`;
chkBx.type = 'checkbox';
chkBx.classList.add('day-select');
chkBx.addEventListener('change', onDaySelect);
span.textContent = day;
label.appendChild(chkBx);
label.appendChild(span);
daySelectContainer.appendChild(label);
});
}
function setFormValues(report) {
values = {
...report,
days: new Set(report.days)
};
view.reportForm.name.value = values.name;
view.reportForm.timeIn.value = values.timeIn;
view.reportForm.timeOut.value = values.timeOut;
view.reportForm.comment.value = values.comment;
view.reportForm.daySelect.forEach(chkBx => {
chkBx.checked = values.days.has(parseInt(chkBx.value));
});
setButtonStatus();
view.reportForm.submit.textContent = report.id
? 'Save changes'
: 'Add a time report';
view.reportForm.cancel.style.display = report.id
? 'inline-block'
: 'none';
}
})();