-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-age.js
65 lines (54 loc) · 2.01 KB
/
card-age.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
import { trelloUi, trelloUrl } from "./trello-ui.js";
import { trelloApi } from "./trello-api.js";
import { cardChanged } from "./board-events.js";
import { daysSince } from "./util/time.js";
export async function fetchAllBoardData() {
const boardId = trelloUrl.getBoardId(window.location.toString());
await trelloApi.getAllCards(boardId);
}
export function cardAge(changedElements) {
if (cardChanged(changedElements)) {
const card = changedElements.target;
const cardId = trelloUrl.getCardId(card.href);
updateCard(card, cardId);
}
}
async function updateCard(card, cardId) {
const cardData = await trelloApi.getCardDetails(cardId);
if (cardData != {} && cardData.dateLastActivity) {
updateCardWithAge(card, cardData.dateLastActivity);
}
}
async function updateAllCardsOnBoard() {
const boardId = trelloUrl.getBoardId(window.location.toString());
const cardData = await trelloApi.getAllCards(boardId);
if (cardData.length > 0) {
const cards = trelloUi.getAllCards();
const cardDataById = [];
cardData.map(card => (cardDataById[card.shortLink] = card));
cards.forEach(card => {
const id = trelloUrl.getCardId(card.href);
if (cardDataById[id]) {
const lastActivity = cardDataById[id].dateLastActivity;
updateCardWithAge(card, lastActivity);
}
});
}
}
function updateCardWithAge(cardElement, cardLastActivity) {
const daysSinceLastChange = daysSince(new Date(cardLastActivity));
if (isNaN(daysSinceLastChange)) return;
const cardAgeNode = cardElement.getElementsByClassName(
"agile-trello-card-age"
)[0];
const newCardAgeNode = document.createElement("span");
newCardAgeNode.setAttribute("class", "agile-trello-card-age");
newCardAgeNode.innerText = `Last changed: ${daysSinceLastChange} day${
daysSinceLastChange === 1 ? "" : "s"
} ago`;
if (cardAgeNode) {
cardAgeNode.innerText = newCardAgeNode.innerText;
} else {
cardElement.append(newCardAgeNode);
}
}