-
Notifications
You must be signed in to change notification settings - Fork 18
/
github-storypoints.js
163 lines (150 loc) · 5.38 KB
/
github-storypoints.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
(function (d, w) {
'use strict';
var pointsRegEx = /^(\(([\d\.]+)\)\s*)?(.+?)(\s*\[([\d\.]+)\])?$/im; // new RegExp("^(\(([\d\.]+)\))?(.+)(\[([\d\.]+)\])?$", "i"); // Was: /^\(([\d\.]+)\)(.+)/i;
var debounce = function (func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var pluralize = (value) => (
value === 1 ? '' : 's'
);
var resetStoryPointsForColumn = (column) => {
const customElements = Array.from(column.getElementsByClassName('github-project-story-points'));
for (let e of customElements) {
const parent = e.parentNode;
if (parent.dataset.gpspOriginalContent) {
parent.innerText = parent.dataset.gpspOriginalContent;
delete parent.dataset.gpspOriginalContent;
} else {
parent.removeChild(e);
}
}
};
var titleWithPoints = (title, points, spent) => (
`<span style="font-weight:bold">${title}</span><br \>
<span class="github-project-story-points counter"
style="font-size:xx-small">${spent} spent of ${points}</span>`
);
var titleWithTotalPoints = (title, points, spent) => (
`${title}<span class="github-project-story-points" style="font-size:xx-small"> item${pluralize(title)} (${spent} spent of ${points})</span>`
);
var addStoryPointsForColumn = (column) => {
const columnCards = Array
.from(column.getElementsByClassName('issue-card'))
.filter(card => !card.classList.contains('sortable-ghost'))
.map(card => {
const titleElementContainer = Array
.from(card.getElementsByClassName('h5'))
.concat(Array.from(card.getElementsByTagName('p')))[0];
const titleElementLink = (
titleElementContainer.getElementsByTagName &&
titleElementContainer.getElementsByTagName('a') ||
[]
);
const titleElement = (
titleElementLink.length > 0
? titleElementLink[0]
: titleElementContainer
);
const title = titleElementContainer.innerText;
const story = (
pointsRegEx.exec(titleElement.innerText) ||
[null, '0', titleElement.innerText]
);
const storyPoints = parseFloat(story[2]) || 0;
const storyTitle = story[3];
const spentPoints = parseFloat(story[5]) || 0;
return {
element: card,
titleElement,
title,
titleNoPoints: storyTitle,
storyPoints,
spentPoints,
};
});
const columnCountElement = column.getElementsByClassName('js-column-card-count')[0];
let columnStoryPoints = 0;
let columnSpentPoints = 0;
for (let card of columnCards) {
columnStoryPoints += card.storyPoints;
columnSpentPoints += card.spentPoints;
if (card.storyPoints || card.spentPoints) {
card.titleElement.dataset.gpspOriginalContent = card.title;
card.titleElement.innerHTML = titleWithPoints(card.titleNoPoints, card.storyPoints, card.spentPoints);
}
}
// Apply DOM changes:
if (columnStoryPoints || columnSpentPoints) {
columnCountElement.innerHTML = titleWithTotalPoints(columnCards.length, columnStoryPoints, columnSpentPoints);
}
};
var resets = [];
var start = debounce(() => {
// Reset
for (let reset of resets) {
reset();
}
resets = [];
// Projects
const projects = d.getElementsByClassName('project-columns-container');
if (projects.length > 0) {
const project = projects[0];
const columns = Array.from(project.getElementsByClassName('js-project-column')); // Was 'col-project-custom', but that's gitenterprise; github.com is 'project-column', fortunately, both have 'js-project-column'
for (let column of columns) {
const addStoryPoints = ((c) => debounce(() => {
resetStoryPointsForColumn(c);
addStoryPointsForColumn(c);
}, 50))(column);
column.addEventListener('DOMSubtreeModified', addStoryPoints);
column.addEventListener('drop', addStoryPoints);
addStoryPointsForColumn(column);
resets.push(((c) => () => {
resetStoryPointsForColumn(c);
column.removeEventListener('DOMSubtreeModified', addStoryPoints);
column.removeEventListener('drop', addStoryPoints);
})(column));
}
}
// Issues
const issues = Array.from(d.getElementsByClassName('js-issue-row'));
for (let issue of issues) {
const titleElement = issue.getElementsByClassName('h4')[0];
const story = (
pointsRegEx.exec(titleElement.innerText) ||
[null, '0', titleElement.innerText]
);
const storyPoints = parseFloat(story[2]) || 0;
const storyTitle = story[3];
const spentPoints = parseFloat(story[5]) || 0;
if (storyPoints || spentPoints) {
titleElement.innerHTML = titleWithPoints(storyTitle, storyPoints, spentPoints);
}
}
}, 50);
// Hacks to restart the plugin on pushState change
w.addEventListener('statechange', () => setTimeout(() => {
const timelines = d.getElementsByClassName('new-discussion-timeline');
if (timelines.length > 0) {
const timeline = timelines[0];
const startOnce = () => {
timeline.removeEventListener('DOMSubtreeModified', startOnce);
start();
};
timeline.addEventListener('DOMSubtreeModified', startOnce);
}
start();
}, 500));
// First start
start();
})(document, window);