-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.js
413 lines (400 loc) · 15.8 KB
/
client.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* global TrelloPowerUp */
var Promise = TrelloPowerUp.Promise;
var SIGMA_ICON = './sigma.svg';
var getBadges = function(t){
// we used to store costs in a board-level object, but
// https://github.com/webrender/trello-costello/issues/11
// reported that the length of the field could exceed the
// 4,000 character limit for trello data. costs are now stored
// in card level objects, and this is here to convert old board-
// level costs to the new card-level objects
return t.card('id')
.then(function(card) {
return t.get('board', 'shared', 'costs')
.then(function(oldCosts) {
var returnCosts = function () {
return t.get('card', 'shared', 'costs')
.then(function(costs){
return t.get('board', 'shared', 'costFields')
.then(function(costFields){
var badges = [];
if (!costFields) {
// create array
var newCostFields = ['Total Cost'];
return t.set('board', 'shared', 'costFields', newCostFields)
.then(function() {
return getBadges(t);
});
}
if (costs) {
if(Array.isArray(costs)) {
costs.forEach(function(cost, idx){
if (cost) {
badges.push({
text: costFields[idx] + ': ' + parseFloat(cost).toLocaleString(undefined,{minimumFractionDigits:2}),
color: (cost == 0) ? 'red' : null
});
}
});
return badges;
} else {
// Initially, the card-level object used the cost title as the key,
// but I realized this would cause issues when renaming titles.
// costs are now stored in an array of objects, where the first object
// is always the default title.
var newCostArray = [];
newCostArray.push(costs['Total Cost']);
return t.set('card', 'shared', 'costs', newCostArray)
.then(function() {
return t.set('board','shared','refresh',Math.random())
.then(function() {
return getBadges(t);
});
});
}
} else {
return [];
}
});
});
}
// oldcosts: these are legacy costs from v1 that were stored on the board-level object
if (oldCosts && oldCosts[card.id]) {
return t.set('card', 'shared', 'costs', [oldCosts[card.id]])
.then(function() {
delete oldCosts[card.id];
return t.set('board', 'shared', 'costs', oldCosts)
.then(function() {
return returnCosts();
});
})
} else {
return returnCosts();
}
});
});
};
var getBoardButtons = function(t) {
// get all the cards
return t.get('board', 'shared', 'costFields')
.then(function(costFields) {
return t.cards('id', 'name', 'idList', 'labels')
.then(function(cards) {
var getCosts = [];
// get all the card costs
cards.forEach(function(card){
getCosts.push(t.get(card.id, 'shared', 'costs'))
});
return Promise.all(getCosts)
.then(function(costArray) {
var sums = Array(costFields.length).fill(0);
// for each card
costArray.forEach(function(cardCosts) {
// for each cost on the card
if (cardCosts && Array.isArray(cardCosts)) {
cardCosts.forEach(function(cost,idx) {
if(cost)
sums[idx] += parseFloat(cost);
});
}
});
var boardButtons = [];
sums.forEach(function(sum, idx) {
boardButtons.push({
icon: SIGMA_ICON,
text: costFields[idx] + ': ' + parseFloat(sum).toLocaleString(undefined,{minimumFractionDigits:2}),
callback: function(t) {
return t.lists('id', 'name')
.then(function(lists){
var entries = [];
var activeIds = cards.map(function(card){return card.id;});
var summaryByColumn = function(t) {
var listSums = {};
var columnEntries = [];
costArray.forEach(function(cardCosts, cardIdx) {
if (cardCosts && cardCosts.length > 0) {
var cardId = cards[cardIdx].id;
// for each active card
if (activeIds.indexOf(cardId) > -1) {
// if it has this cost attached
if (cardCosts[idx]) {
// see if listSums already has a sum under this listId
if (!listSums[cards[cardIdx].idList]) {
// if not create it
listSums[cards[cardIdx].idList] = 0;
}
// add the cost to the list sum
listSums[cards[cardIdx].idList] += parseFloat(cardCosts[idx]);
}
}
}
});
Object.keys(listSums).forEach(function(listId) {
var listName = lists.find(function(list){return listId == list.id}).name;
columnEntries.push({
text: listName + ': ' + parseFloat(listSums[listId]).toFixed(2).toLocaleString(undefined,{minimumFractionDigits:2})
});
});
return t.popup({
title: 'Summary by Column',
items: columnEntries
});
}
var summaryByLabel = function(t) {
var listSums = {};
var columnEntries = [];
cards.forEach(function(card, cardIdx){
if (costArray[cardIdx] && costArray[cardIdx][idx]) {
if (card.labels.length > 0) {
card.labels.forEach(function(label) {
var displayName = label.name || label.color;
if (listSums[displayName]) {
listSums[displayName] += parseFloat(costArray[cardIdx][idx]);
} else {
listSums[displayName] = parseFloat(costArray[cardIdx][idx]);
}
});
}
}
});
for (var listSum in listSums) {
columnEntries.push({text: listSum + ': ' + parseFloat(listSums[listSum]).toFixed(2).toLocaleString(undefined,{minimumFractionDigits:2})});
}
return t.popup({
title: 'Summary by Label',
items: columnEntries
});
}
entries.push({text: '🔍 Summary by Column...', callback: summaryByColumn});
entries.push({text: '🔍 Summary by Label...', callback: summaryByLabel});
costArray.forEach(function(cardCosts, cardIdx) {
if (cardCosts && cardCosts.length > 0 && cardCosts[idx]) {
var cost = cards[cardIdx].id;
if (activeIds.indexOf(cost) > -1) {
var cb = function(a){t.showCard(a);};
entries.push({
text: parseFloat(cardCosts[idx]).toLocaleString(undefined,{minimumFractionDigits:2}) + ' - ' + cards.find(function(card){return card.id == cost;}).name,
callback: cb.bind(null, cost)
});
}
}
});
return t.popup({
title: 'Cost Summary',
items: entries
});
});
}
});
});
return boardButtons;
});
});
});
}
var getSettings = function(t) {
return t.get('board', 'shared', 'costFields')
.then(function(costFields) {
return t.popup({
title: 'Manage Cost Fields',
items: function(t, options) {
var buttons = [{
text: options.search !== '' ? 'Add cost field: ' + options.search : '(Enter a title to add cost field)',
callback: function(t) {
costFields.push(options.search);
return t.set('board', 'shared', 'costFields', costFields)
.then(function() {
return getSettings(t);
});
}
}];
if (costFields && Array.isArray(costFields) && costFields.length > 0) {
costFields.forEach(function(costField, idx) {
buttons.push({
text: costField,
callback: function(t) {
return t.popup({
title: 'Set Field Name',
items: function(t, subopt) {
return [{
text: subopt.search !== '' ? 'Rename field to "' + subopt.search + '"': '(Enter a new name for this field.)',
callback: function(t) {
costFields[idx] = subopt.search;
return t.set('board', 'shared', 'costFields', costFields)
.then(function() {
return getSettings(t);
});
}
}, {
text: 'Delete ' + costField + ' field.',
callback: function(t) {
// not only do we need to delete this field from the costField array,
// we also need to delete that index from any card-level costs object
return t.cards('id')
.then(function(cards) {
var requests = [];
cards.forEach(function(card) {
requests.push(t.get(card.id, 'shared', 'costs'));
});
return Promise.all(requests)
.then(function(cardCostsArray) {
if (cardCostsArray) {
var updates = [];
cardCostsArray.forEach(function(cardCosts, cardIdx) {
if (cardCosts) {
cardCosts.splice(idx, 1);
}
updates.push(t.set(cards[cardIdx].id, 'shared', 'costs', cardCosts));
});
if (updates) {
return Promise.all(updates)
.then(function() {
costFields.splice(idx, 1);
return t.set('board', 'shared', 'costFields', costFields)
.then(function(){
return getSettings(t);
});
});
} else {
costFields.splice(idx, 1);
return t.set('board', 'shared', 'costFields', costFields)
.then(function(){
return getSettings(t);
});
}
} else {
costFields.splice(idx, 1);
return t.set('board', 'shared', 'costFields', costFields)
.then(function(){
return getSettings(t);
});
}
});
});
}
}]
},
search: {
placeholder: costFields[0]
}
});
}
});
});
}
buttons.push({
icon: SIGMA_ICON,
text: 'Reset Cost Fields',
callback: function(t) {
return t.popup({
title: 'Reset Cost Fields: Are you sure?',
items: function(t, options) {
var buttons = [{
text: 'Yes, reset all fields.',
callback: function(t) {
// create array
var newCostFields = ['Total Cost'];
return t.set('board', 'shared', 'costFields', newCostFields)
.then(function() {
return t.cards('id')
.then(function(cards) {
var requests = []
cards.forEach(function(card) {
requests.push(t.remove(card.id, 'shared', 'costs', ));
});
return Promise.all(requests)
.then(function(cardCostsArray) {
t.closePopup();
return getBadges(t);
});
});
});
}
}];
return buttons;
},
});
}
});
return buttons;
},
search: {
placeholder: 'Enter new cost field',
empty: 'Error',
searching: 'Processing...'
}
});
});
}
var getButtons = function(t) {
return t.get('board', 'shared', 'costFields')
.then(function(costFields){
return t.get('card', 'shared', 'costs')
.then(function(costs){
var buttons = [];
costFields.forEach(function(cost, idx){
buttons.push({
icon: SIGMA_ICON,
text: costs && costs[idx] ? costFields[idx] + ': ' + parseFloat(costs[idx]).toLocaleString(undefined,{minimumFractionDigits:2}) :'Add ' + costFields[idx] + '...',
callback: t.memberCanWriteToModel('card') ? function(t) {
return t.popup({
title: 'Set ' + costFields[idx] + '...',
items: function(t, options) {
var newCost = parseFloat(options.search).toFixed(2)
var buttons = [{
text: !Number.isNaN(parseFloat(options.search)) ? 'Set ' + costFields[idx] + ' to ' + parseFloat(newCost).toLocaleString(undefined,{minimumFractionDigits:2}) : '(Enter a number to set ' + costFields[idx] + '.)',
callback: function(t) {
if (newCost != 'NaN') {
var newCosts = costs ? costs : Array(costFields.length).fill(null);
newCosts[idx] = newCost;
return t.set('card','shared','costs', newCosts)
.then(function() {
return t.set('board','shared','refresh',Math.random())
.then(function() {
return t.closePopup();
});
});
}
return t.closePopup();
}
}];
if (costs && costs[idx]) {
buttons.push({
text: 'Remove ' + costFields[idx] + '.',
callback: function(t) {
var newCosts = costs ? costs : Array(costFields.length).fill(null);
newCosts[idx] = null;
t.set('card','shared','costs', newCosts);
return t.closePopup();
}
});
}
return buttons;
},
search: {
placeholder: 'Enter Cost',
empty: 'Error',
searching: 'Processing...'
}
});
} : null
});
});
return buttons;
});
});
}
TrelloPowerUp.initialize({
'board-buttons': function(t, options){
return getBoardButtons(t);
},
'card-badges': function(t, options){
return getBadges(t);
},
'card-buttons': function(t, options) {
return getButtons(t);
},
'show-settings': function(t, options) {
return getSettings(t);
}
});