-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-totals.js
114 lines (103 loc) · 3.38 KB
/
card-totals.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
import { trelloUi } from "./trello-ui.js";
import {
cardListChanged,
cardChanged,
cardTitleChanged
} from "./board-events.js";
export function updateCardTotals(changedElements) {
if (cardListChanged(changedElements)) {
// change to column
const cardContainer = changedElements.target;
const column = cardContainer.parentNode;
updateTotalColumnViews(column, cardContainer);
}
if (cardChanged(changedElements)) {
// change to card
const cardContainer = changedElements.target.parentNode;
const column = cardContainer.parentNode;
updateTotalColumnViews(column, cardContainer);
}
if (cardTitleChanged(changedElements)) {
const cardContainer =
changedElements.target.parentNode.parentNode.parentNode;
const column = cardContainer.parentNode;
updateTotalColumnViews(column, cardContainer);
}
}
function updateTotalColumnViews(column, cardContainer) {
const totalsNode = column.getElementsByClassName(
"agile-trello-col-totals"
)[0];
const points = totalPoints(column);
const newTotalsNode = totalsView({
points,
cardTotal: cardContainer.childNodes.length
});
if (totalsNode) {
totalsNode.innerText = newTotalsNode.innerText;
} else {
column.insertBefore(newTotalsNode, cardContainer);
}
}
function totalsView({ points, cardTotal }) {
const colInfo = document.createElement("span");
colInfo.setAttribute("class", "agile-trello-col-totals");
let pointsWord = points === 1 ? " point" : " points";
let cardTotalWord = cardTotal === 1 ? " ticket" : " tickets";
colInfo.innerText = points + pointsWord + " / " + cardTotal + cardTotalWord;
return colInfo;
}
function totalPoints(node) {
let points = 0;
trelloUi.getCardsInColumn(node).forEach(function(node) {
points += extractPoints(trelloUi.getCardTitle(node));
});
return points;
}
const STORY_POINTS_REGEX = /^\((\d+)\)/;
function extractPoints(title) {
const points = title.match(STORY_POINTS_REGEX);
if (points) return Number(points[1]);
return 0;
}
function updateCardPoints({ points }) {
const badgeInfo = document.createElement("div");
badgeInfo.setAttribute("class", "badge");
badgeInfo.setAttribute("title", "Story points");
const storyPoints = document.createElement("span");
storyPoints.setAttribute("class", "agile-trello-card-points badge-text");
storyPoints.innerText = points;
badgeInfo.appendChild(storyPoints);
return badgeInfo;
}
export function estimatePointsForCards(changedElements) {
if (cardChanged(changedElements)) {
const card = changedElements.target;
const points = extractPoints(trelloUi.getCardTitle(card));
displayEstimatePoints(points, card);
}
if (cardTitleChanged(changedElements)) {
const card = changedElements.target.parentNode.parentNode;
const points = extractPoints(changedElements.target.innerText);
displayEstimatePoints(points, card);
}
}
function displayEstimatePoints(points, card) {
const cardPointsElement = card.getElementsByClassName(
"agile-trello-card-points"
)[0];
if (points > 0) {
const newCardPoints = updateCardPoints({
points
});
if (cardPointsElement) {
cardPointsElement.innerText = newCardPoints.innerText;
} else {
card
.getElementsByClassName("js-custom-field-badges")[0]
.append(newCardPoints);
}
} else if (cardPointsElement) {
cardPointsElement.parentNode.removeChild(cardPointsElement);
}
}