-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
216 lines (180 loc) · 6.11 KB
/
inject.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var SETTINGS = {};
function isWorkingDay(date)
{
//TODO: support holidays (preferrably configurable country in settings)
return !(date.isoWeekday() == 6 || date.isoWeekday() == 7);
}
function getSprintDates(sprintTitle)
{
//Extract sprint dates from title
var match = /((\d{2})\.(\d{2})\.(\d{4})) - ((\d{2})\.(\d{2})\.(\d{4}))/g.exec(sprintTitle);
if(match === null){
return false;
}
return [match[1], match[5]]
}
function getTotalHoursInSprint(sprintTitle)
{
var teamSize = SETTINGS.teamSize;
var personHours = 0;
var sprintDates = getSprintDates(sprintTitle);
if(sprintDates === false){
return 0;
}
var fromDate = moment(sprintDates[0], 'DD.MM.YYYY');
var toDate = moment(sprintDates[1], 'DD.MM.YYYY');
var diff = toDate.diff(fromDate, 'd') + 1; // We include starting day too
if(diff < 0){
return 0;
}
var tempDate = fromDate.clone();
while(tempDate.isBefore(toDate, 'd') || tempDate.isSame(toDate, 'd')){
if(!isWorkingDay(tempDate)){
--diff;
}
tempDate.add(1, 'd');
}
var diffInHours = diff * SETTINGS.workingHours;
return teamSize * diffInHours;
}
function getWorkInProgressColumn()
{
var ret = null;
if(SETTINGS.workInProgressColumnName.length === 0){ // Not configured feature
return null;
}
jQuery('.list-header-name-assist').each(function(){
var header = jQuery(this);
var headerName = header.html();
if(headerName === SETTINGS.workInProgressColumnName){
ret = header;
return false; // break
}
});
return ret !== null ? ret.parent().parent() : null;
}
function isBetween(dt, from, to)
{
return (from.isBefore(dt) || from.isSame(dt)) && (to.isAfter(dt) || to.isSame(dt));
}
function getCurrentSprintColumn()
{
var now = moment();
var ret = null;
jQuery('.list-header-name-assist').each(function(){
var header = jQuery(this);
var headerName = header.html();
var sprintDates = getSprintDates(headerName);
var fromDate = moment(sprintDates[0], 'DD.MM.YYYY');
var toDate = moment(sprintDates[1], 'DD.MM.YYYY');
if(sprintDates !== false && isBetween(now, fromDate, toDate)){
ret = header;
return false; // break
}
});
return ret !== null ? ret.parent().parent() : null;
}
function getCurrentDoneColumn()
{
if(SETTINGS.doneColumnPrefix.length === 0){ // Not configured
return null;
}
var now = moment();
var ret = null;
jQuery('.list-header-name-assist').each(function(){
var header = jQuery(this);
var headerName = header.html();
var sprintDates = getSprintDates(headerName);
var fromDate = moment(sprintDates[0], 'DD.MM.YYYY');
var toDate = moment(sprintDates[1], 'DD.MM.YYYY');
if(sprintDates !== false && isBetween(now, fromDate, toDate) && headerName.trim().indexOf(SETTINGS.doneColumnPrefix) === 0){
ret = header;
return false; // break
}
});
return ret !== null ? ret.parent().parent() : null;
}
function countEstimate(column)
{
var estimates = 0;
column.find('.badge-text:contains("Estimate")').each(
function(el) {
var h = jQuery(this).html();
var r = /(\d+)/g.exec(h);
estimates += ~~r[0];
}
);
return estimates;
}
function countEstimates()
{
jQuery('.list-header-name-assist').each(function(){
var header = jQuery(this);
var headerName = header.html();
var totalHours = getTotalHoursInSprint(headerName);
var estimates = countEstimate(header.parent().parent());
var totalHoursLabel = jQuery('<p>').html('<b>Total:</b> ' + totalHours + ' hours');
var estimationLabel = jQuery('<p>').html('<b>Total Estimation:</b> ' + estimates.toString() + ' hours');
estimationLabel.insertAfter(header);
if(totalHours > 0){
totalHoursLabel.insertAfter(header);
}
});
}
function countBurnedTime()
{
var currentSprintColumn = getCurrentSprintColumn();
var workInProgressColumn = getWorkInProgressColumn();
var doneColumn = getCurrentDoneColumn();
if(currentSprintColumn === null || workInProgressColumn === null || doneColumn === null){
return null;
}
var burned = countEstimate(doneColumn);
var left = countEstimate(currentSprintColumn);
var currentDoing = countEstimate(workInProgressColumn);
var percent = ~~(burned / (left + burned + currentDoing) * 100);
var statTemplate = '<div class="header-search sprintBurnedTime">';
var doingStat = jQuery(statTemplate).html('<b>Current doing</b> ' + currentDoing + ' hours');
var burnedStat = jQuery(statTemplate).html('<b>Burned</b> ' + burned + ' hours so far');
var leftStat = jQuery(statTemplate).html('<b>Left</b> ' + left + ' hours');
var percentStat = jQuery(statTemplate).html('<b>Finished</b> ' + percent + '% of sprint');
var searchTool = jQuery('.header-search');
doingStat.insertAfter(searchTool);
burnedStat.insertAfter(searchTool);
leftStat.insertAfter(searchTool);
percentStat.insertAfter(searchTool);
jQuery('a.header-logo').hide();
}
function readSettings()
{
chrome.storage.sync.get({
teamSize: '1',
workingHours: '8',
workInProgressColumnName: '',
doneColumnPrefix: '',
}, function(items){
console.log('Read settings', items);
SETTINGS = items;
SETTINGS.teamSize = ~~SETTINGS.teamSize;
SETTINGS.workingHours = ~~SETTINGS.workingHours;
});
}
function waitForTrelloInit()
{
// Read settings first - they are async so we have to poll for them also
readSettings();
// Polling always work
var interval = setInterval(function(){
var startPlugin = false;
if(jQuery('.badge-text:contains("Estimate")').length > 0 &&
Object.keys(SETTINGS).length > 0){
startPlugin = true;
}
if(startPlugin){
clearInterval(interval);
countEstimates();
countBurnedTime();
}
}, 500);
}
window.addEventListener('load', waitForTrelloInit, false);