forked from marc-ed-raffalli/fill-my-timesheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-purely-hr.js
130 lines (106 loc) · 3.51 KB
/
content-purely-hr.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
/**
* Implementation for Purely HR
*/
(function () {
'use strict';
const
evtArgs = {bubbles: true, cancelable: true},
selectors = {
dayRows: '.day-data.row',
timeIn: '.time-input.start',
timeOut: '.time-input.end',
dayCommentIcon: '.phr-set-comment-for-day',
dayCommentField: '.modal-dialog #dailyCommentForm textarea.form-control',
dayCommentSave: '.modal-dialog .modal-footer button.phr-confirm-btn'
};
fmtApi.utils.setMessageProxy({
[fmtApi.events.timeSheetFill]: onTimeSheetFillRequest
});
async function onTimeSheetFillRequest(payload) {
try {
if (!window.location.host.match(/.*\.purelyhr.com/)) {
return;
}
await loopThroughSelection(
payload,
() => document.querySelectorAll(selectors.dayRows),
async (row) => {
await setInputValue(row.querySelector(selectors.timeIn), payload.timeIn);
await setInputValue(row.querySelector(selectors.timeOut), payload.timeOut);
}
);
if (payload.comment) {
await loopThroughSelection(
payload,
() => document.querySelectorAll(selectors.dayRows),
(row) => setDayComment(row, payload.comment),
true
);
}
fmtApi.utils.sendMessage({type: fmtApi.events.timeSheetFillAck});
} catch (err) {
console.error(err);
}
}
async function loopThroughSelection(payload, itemsGetter, cb, possibleRerender) {
const days = new Set(payload.days);
let rows = itemsGetter();
rows = Array.isArray(rows)
? rows
: Array.from(rows);
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
if (!row.dataset.date) {
continue;
}
const rowDate = new Date(row.dataset.date);
if (!days.has(rowDate.getDay())) {
console.log('Skipping', rowDate.toDateString());
continue;
}
if (!possibleRerender) {
await cb(row);
continue;
}
try {
// purely HR UI sometimes re-renders the rows after the comment is submitted
// it leads to timeout when getting the element
await cb(row);
} catch (err) {
if (err.message.match(/timeout for wait/i)) {
rows = itemsGetter();
i--;
}
}
}
}
async function setInputValue(elt, value) {
if (!value) {
return;
}
elt.dispatchEvent(new Event('click', evtArgs));
elt.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 8, which: 8}));
elt.value = value;
elt.dispatchEvent(new Event('change', evtArgs));
elt.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 27, which: 27}));
// wait for time picker to be hidden
return fmtApi.utils.waitForIt(() => {
const list = document.querySelector('.ui-timepicker-list');
return !list || list.clientHeight === 0;
});
}
async function setDayComment(row, value) {
if (!value) {
return;
}
row.querySelector(selectors.dayCommentIcon).dispatchEvent(new Event('click', evtArgs));
// wait for field to be visible in the modal dialog
await fmtApi.utils.waitForIt(() => {
const field = document.querySelector(selectors.dayCommentField);
return field && field.clientHeight !== 0;
});
document.querySelector(selectors.dayCommentField).value = value;
document.querySelector(selectors.dayCommentSave).dispatchEvent(new Event('click', evtArgs));
return fmtApi.utils.waitForIt(() => document.querySelector(selectors.dayCommentField) === null);
}
})();