forked from ShanbayFE/count-labels-for-trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
41 lines (34 loc) · 1.68 KB
/
background.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
const showHoursCount = () => {
const arrayMethods = Object.getOwnPropertyNames( Array.prototype );
const attachArrayMethodsToNodeList = (methodName) => {
if(methodName !== "length") {
NodeList.prototype[methodName] = Array.prototype[methodName];
}
};
arrayMethods.forEach((item) => attachArrayMethodsToNodeList(item));
const listCards = document.querySelectorAll('.list-cards');
const estimateLabels = listCards.map((list) => list.querySelectorAll('.card-label-yellow'));
const actualLabels = listCards.map((list) => list.querySelectorAll('.card-label-green'));
const getHourCount = (labels) => labels.map((label) => {
const labelsArray = label.map((item) => parseFloat(item.innerHTML));
return labelsArray.length === 0 ? 0 :
labelsArray.reduce((pre, next) => pre + next);
});
const estimateTimeCounts = getHourCount(estimateLabels);
const actualTimeCounts = getHourCount(actualLabels);
listCards.map((item, i) => {
const countContainer = document.createElement('div');
countContainer.className = 'time-label-count';
countContainer.innerHTML = `<span class="actual-time">${actualTimeCounts[i]}</span><span class="estimate-time">${estimateTimeCounts[i]}</span>`;
if (!!item) {
const oldCountContainer = item.previousSibling;
if (oldCountContainer.className === 'time-label-count') {
item.parentNode.replaceChild(countContainer, oldCountContainer);
} else {
item.parentNode.insertBefore(countContainer, item);
}
}
});
};
window.onload = () => showHoursCount();
setInterval(showHoursCount, 3000);