-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboard-cards.html
928 lines (920 loc) · 45.6 KB
/
board-cards.html
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="lib/VSS.SDK.min.js" type="text/javascript"></script>
<script type="text/javascript">
"use strict";
VSS.init({
explicitNotifyLoaded: true,
usePlatformScripts: true,
usePlatformStyles: true
});
VSS.ready(async function () {
consoleLog(`VSS ready`);
consoleLog(`Registering backlog tab object`)
VSS.register('backlogTabObject', {
pageTitle: function(state) {
return false;
},
updateContext: function(tabContext) {
},
isInvisible: function(state) {
return false;
},
isDisabled: function(state) {
return false;
}
});
consoleLog(`Checking whether document is ready`);
checkReady();
});
VSS.notifyLoadSucceeded();
function checkReady() {
consoleLog(`checkReady()`);
if (document.readyState != 'complete') {
consoleLog(`Document is not ready, will try again in 100ms`);
setTimeout(checkReady, 100);
} else if (document.readyState === 'complete') {
consoleLog(`Document readyState = complete`);
consoleLog(`Requiring VSS and TFS clients`);
VSS.require(["VSS/Service","TFS/WorkItemTracking/RestClient","TFS/WorkItemTracking/Contracts","TFS/Work/RestClient",],
async function (VSS_Service, TFS_Wit_WebApi,TFS_Contracts,TFS_WorkClient) {
consoleLog(`Main function started`)
if (document.readyState === 'complete') {
consoleLog(`Document ready state = complete`)
const witClient = await VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
const contracts = TFS_Contracts;
const workClient = TFS_WorkClient;
const projectId = await VSS.getWebContext().project.id;
const teamId = await VSS.getWebContext().team.id;
const projectName = await VSS.getWebContext().project.name;
const teamContext = { projectId: projectId, teamId: teamId, project: "", team: ""};
const iteration = await getIteration();
const areaPath = await getUsersSavedAreaPath();
await setPrintHeader(iteration,areaPath);
await getIterations(workClient,teamContext,iteration);
await getWorkItemTypes(witClient,projectId);
await getAreaPaths(witClient,projectId,contracts,areaPath);
const selectedCardSize = await displayPrintSizeOptions();
await displayFieldOptions();
try {
consoleLog(`Trying to displayWorkItems()`)
await displayWorkItems(witClient,contracts,workClient,projectId,teamContext,iteration,projectName,areaPath,selectedCardSize);
} catch(err) {
consoleLog(`Error: ${err}`);
await removeLoader();
await displayPrint();
alert(`An error occurred. The error reported was: ${err}`);
}
}
});
};
}
let consoleMessageCount = 1;
let parentCount = 0;
async function getUserFields() {
consoleLog(`getUserFields()`)
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
const userSelectedFields = await dataService.getValue("userFields", {scopeType: "User"});
if(userSelectedFields) {
if(userSelectedFields === '') {
const userFields = await setInitialUserFields();
consoleLog(`Blank value found. Initialisation completed. Contents of userFields is now...`)
console.dir(userFields);
return userFields;
} else {
consoleLog(`Contents of userFields is...`)
console.dir(userSelectedFields);
return userSelectedFields;
}
} else {
const userFields = await setInitialUserFields();
consoleLog(`userSelectedFields did not exist. Initialisation completed. Contents of userFields is now...`)
console.dir(userFields);
return userFields;
}
}
function Field(name,selected){
this.name = name;
this.selected = selected;
}
async function setInitialUserFields() {
consoleLog(`setInitialUserFields()`)
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let fieldsArray = new Array();
let fieldObject1 = new Field('Original estimate',false);
fieldsArray.push(fieldObject1);
let fieldObject2 = new Field('Assigned to',false);
fieldsArray.push(fieldObject2);
await dataService.setValue("userFields", fieldsArray, {scopeType: "User"});
if(await dataService.setValue("userFields", fieldsArray, {scopeType: "User"})) {
let userFields = await dataService.setValue("userFields", fieldsArray, {scopeType: "User"});
consoleLog(`userFields after setInitialUserFields() is:`)
console.dir(userFields)
return userFields;
} else {
return false;
}
}
async function displayFieldOptions(){
consoleLog(`displayFieldOptions()`);
let fieldsSectionHeader = document.createElement('h2');
fieldsSectionHeader.innerHTML = 'Select additional fields to include';
let fieldsSectionDiv = document.getElementById('fieldOptions');
fieldsSectionDiv.insertBefore(fieldsSectionHeader,fieldsSectionDiv.childNodes[0]);
let userFields = await getUserFields();
const userFieldsLength = Object.keys(userFields).length;
if(userFields) {
consoleLog(`User has fields to display: ${userFieldsLength}`);
if(userFieldsLength == 1) {
consoleLog(`User has one field selected`);
let name = userFields[0].name;
let fieldsArray = new Array();
if(name === 'Original estimate') {
consoleLog(`User has selected 'Original estimate'`);
let fieldObject1 = new Field('Original estimate',true);
fieldsArray.push(fieldObject1);
let fieldObject2 = new Field('Assigned to',false);
fieldsArray.push(fieldObject2);
} else if (name === 'Assigned to') {
consoleLog(`User has selected 'Assigned to'`);
let fieldObject1 = new Field('Original estimate',false);
fieldsArray.push(fieldObject1);
let fieldObject2 = new Field('Assigned to',true);
fieldsArray.push(fieldObject2);
} else {
consoleLog(`User has an unknown field selected`);
}
consoleLog(`After adding the missing field, fieldsArray is now:`);
console.dir(fieldsArray);
const fieldsArrayLength = Object.keys(fieldsArray).length;
consoleLog(`fieldsArrayLength: ${fieldsArrayLength}`);
await createFieldInputs(fieldsArray,fieldsArrayLength);
} else {
consoleLog(`userFields prior to entering for loop is:`);
console.dir(userFields);
await createFieldInputs(userFields,userFieldsLength);
}
await addUpdateFieldsButton();
} else {
consoleLog(`userFields is not set`);
}
}
async function createFieldInputs(userFields,userFieldsLength) {
for(let x = 0; x < userFieldsLength; x++) {
let name = userFields[x].name;
let selected = userFields[x].selected;
let input = document.createElement('input');
input.setAttribute('type','checkbox');
input.setAttribute('name', name);
input.setAttribute('value',name);
if(selected === true) {
input.setAttribute('selected',true);
input.setAttribute('checked','checked');
} else if (selected === false) {
input.setAttribute('selected',false);
}
input.setAttribute('class','field-input');
let label = document.createElement('label');
label.setAttribute('for',name);
label.innerHTML = ' ' + name;
let linebreak = document.createElement('br');
document.getElementById('fields').appendChild(input);
document.getElementById('fields').appendChild(label);
document.getElementById('fields').appendChild(linebreak);
}
}
async function getCardSize() {
consoleLog(`getCardSize()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let userCardSize = await dataService.getValue("userCardSize", {scopeType: "User"});
let cardSize = '';
if(userCardSize === undefined || userCardSize === '') {
cardSize = 'small';
return cardSize;
} else {
cardSize = userCardSize; // will be either 'small' or 'large'
consoleLog(`User has a saved card size: ${cardSize}`);
return cardSize;
}
}
async function displayPrintSizeOptions() {
consoleLog(`displayPrintSizeOptions()`);
let cardSizeSectionHeader = document.createElement('h2');
cardSizeSectionHeader.innerHTML = 'Select card size for printing';
let cardSizeSectionDiv = document.getElementById('cardSizeOptions');
cardSizeSectionDiv.insertBefore(cardSizeSectionHeader,cardSizeSectionDiv.childNodes[0]);
let selectedCardSize = await getCardSize();
let nextCardSize = '';
let linkString = ``;
if(selectedCardSize === 'small') {
nextCardSize = 'large';
linkString = `Small - 3 x 5 per A4 sheet`;
} else if(selectedCardSize === 'large') {
nextCardSize = 'small';
linkString = `Large - 2 x 3 per A4 sheet`;
} else {
selectedCardSize = 'small';
linkString = `Small - 3 x 5 per A4 sheet`;
nextCardSize = 'large';
consoleLog(`User's selected card size was not 'small' or 'large'.`);
}
let ul = document.getElementById('cardSize');
let li = document.createElement('li');
let a = document.createElement('a');
let linkText = document.createTextNode(linkString);
a.appendChild(linkText);
a.title = selectedCardSize;
a.id = selectedCardSize;
a.onclick = async function() {
await VSS.getService(VSS.ServiceIds.ExtensionData).then(async function(dataService) {
// Set value in user scope
await dataService.setValue('userCardSize', selectedCardSize, {scopeType: 'User'}).then(function(selectedCardSize) {
consoleLog(`User's selected card size is now ${selectedCardSize}`);
});
});
location.reload(true); // setting true forces the browser to GET again, rather than refreshing from cache
return false;
}
a.textContent += ' (selected)' ;
a.classList.add('embolden');
li.appendChild(a);
ul.appendChild(li);
let a2 = document.createElement('a');
let li2 = document.createElement('li');
let linkString2 = '';
if(nextCardSize === 'small') {
linkString2 = `Small - 3 x 5 per A4 sheet`;
} else if (nextCardSize === 'large'){
linkString2 = `Large - 2 x 3 per A4 sheet`;
}
let linkText2 = document.createTextNode(linkString2)
a2.appendChild(linkText2);
a2.title = nextCardSize;
a2.id = nextCardSize;
a2.onclick = async function() {
await VSS.getService(VSS.ServiceIds.ExtensionData).then(async function(dataService) {
// Set value in user scope
await dataService.setValue('userCardSize', nextCardSize, {scopeType: 'User'}).then(function(nextCardSize) {
consoleLog(`User's selected card size is now ${nextCardSize}`);
});
});
location.reload(true); // setting true forces the browser to GET again, rather than refreshing from cache
return false;
}
li2.appendChild(a2);
ul.appendChild(li2);
return selectedCardSize;
}
async function getIteration() {
consoleLog(`getIteration()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let iterationExistsCheck = await dataService.getValue("userIterationPath", {scopeType: "User"});
if(iterationExistsCheck === undefined) {
let iteration = '@CurrentIteration';
return iteration;
} else {
let iteration = dataService.getValue("userIterationPath", {scopeType: "User"});
return iteration;
}
}
function appendCountOfWorkItems(count) {
consoleLog(`appendCountOfWorkItems()`);
let workItemSelectSectionHeader = document.getElementById('workItemSelectSectionHeader');
let currentContent = workItemSelectSectionHeader.innerHTML;
let newContent = `${currentContent} with ${count} work items.`;
workItemSelectSectionHeader.innerHTML = newContent;
return true;
}
async function getWorkItemIds(witClient,query,projectId,contracts) {
consoleLog(`getWorkItemIds()`);
let workItemIds = [];
try {
await witClient.queryByWiql(query, projectId).then(
function(result) {
if (result.queryType == contracts.QueryType.Flat) {
workItemIds = result.workItems.map(val => val.id).slice(0, Math.min(200, result.workItems.length));
consoleLog(`getWorkItemIds has found: ${workItemIds}`)
}
}
);
} catch(err) {
consoleLog(`Error: ${err}`);
}
return workItemIds;
}
async function saveWorkItemTypes(){
consoleLog(`saveWorkItemTypes()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let selectedWorkItemInputs = await getSelectedWorkItemTypes();
if(selectedWorkItemInputs) {
await dataService.setValue("userWorkItems", selectedWorkItemInputs, {scopeType: "User"});
consoleLog(`User's selected work item types are now set to ${selectedWorkItemInputs}`);
if(await dataService.getValue("userWorkItems", {scopeType: "User"})) {
let savedWorkItems = dataService.getValue("userWorkItems", {scopeType: "User"});
location.reload(true);
return true;
} else {
consoleLog(`User has not selected any work item types to include.`);
return false;
};
} else {
consoleLog(`User has not selected any work item types to include.`);
return false;
};
}
async function saveFields(){
consoleLog(`saveFields()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let selectedFields = await getSelectedFields();
if(selectedFields) {
let selectedFieldsLength = selectedFields.length;
consoleLog(`selectedFieldsLength: ${selectedFieldsLength}`);
let fieldsArray = new Array();
selectedFields.forEach(function(item) {
consoleLog(`selectedField: ${item}`);
let fieldObject = new Field(item,true);
fieldsArray.push(fieldObject);
});
consoleLog(`Setting userFields`);
await dataService.setValue("userFields", fieldsArray, {scopeType: "User"});
if(await dataService.getValue("userFields", {scopeType: "User"})) {
let savedUserFields = await dataService.getValue("userFields", {scopeType: "User"});
if(savedUserFields){
consoleLog(`User's saved userFields now set to:`);
console.dir(savedUserFields);
location.reload(true);
return true;
} else {
console.log(`userFields does not exist`)
}
} else {
consoleLog(`User has not selected any fields to include.`);
return false;
};
} else {
consoleLog(`User has not selected any fields to include.`);
return false;
};
}
async function getSelectedFields() {
consoleLog(`getSelectedFields()`);
const inputs = document.getElementsByClassName('field-input');
let inputsArray = [];
const inputsLength = inputs.length;
if(inputsLength && inputsLength > 0){
for (let x = 0; x < inputsLength; x++) {
if(inputs[x].checked) {
inputsArray.push(inputs[x].value);
consoleLog(`Pushing: ${inputs[x].value}`);
} else {
consoleLog(`Not pushing ${inputs[x].value}`);
}
}
let inputsArrayLength = inputsArray.length;
if(inputsArrayLength > 0){
return inputsArray;
} else {
await setInitialUserFields();
consoleLog(`User didn't select any fields, so setInitialUserFields()`);
location.reload(true);
return false;
}
} else {
alert('Could not find any fields that can be included.');
return false;
}
}
async function getSelectedWorkItemTypes() {
consoleLog(`getSelectedWorkItemTypes()`);
const inputs = document.getElementsByClassName('input-work-item-type');
let inputsArray = [];
const inputsLength = inputs.length;
if(inputsLength && inputsLength > 0){
for (let x = 0; x < inputsLength; x++) {
if(inputs[x].checked) {
inputsArray.push(inputs[x].value);
} else {
consoleLog(`Not pushing ${inputs[x].value}`);
}
}
let inputsArrayLength = inputsArray.length;
if(inputsArrayLength > 0){
return inputsArray;
} else {
alert('You must select at least one work item type.');
return false;
}
} else {
alert('You must select at least one work item type.');
return false;
}
}
async function checkForSavedWorkItemTypes() {
consoleLog(`checkForSavedWorkItemTypes()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
if(await dataService.getValue("userWorkItems", {scopeType: "User"})) {
return true;
} else {
return false;
}
}
async function getUsersSavedWorkItemTypes() {
consoleLog(`getUsersSavedWorkItemTypes()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
const usersSavedWorkItemTypes = await dataService.getValue("userWorkItems", {scopeType: "User"});
return usersSavedWorkItemTypes;
}
function checkIfStringIsInArray(array,string) {
return array.indexOf(string) !== -1;
}
function insertWorkItemSelectHeader() {
consoleLog(`insertWorkItemSelectHeader()`);
let workItemSelectSectionHeader = document.createElement('h2');
workItemSelectSectionHeader.innerHTML = 'Select work item types for printing';
let workItemSelectDiv = document.getElementById("workItemTypes");
workItemSelectDiv.insertBefore(workItemSelectSectionHeader,workItemSelectDiv.childNodes[0]);
return true;
}
async function getWorkItemTypes(witClient,projectId) {
consoleLog(`getWorkItemTypes()`);
insertWorkItemSelectHeader();
const workItemTypes = await witClient.getWorkItemTypes(projectId);
const numberOfWorkItemTypes = workItemTypes.length;
const userHasSavedWorkItemTypes = await checkForSavedWorkItemTypes();
let usersSavedWorkItemTypes = false;
if(userHasSavedWorkItemTypes) {
usersSavedWorkItemTypes = await getUsersSavedWorkItemTypes();
}
// Default work item list - all checked
for(let x = 0; x < numberOfWorkItemTypes; x++) {
let name = workItemTypes[x].name;
let input = document.createElement('input');
input.setAttribute('type','checkbox');
input.setAttribute('name', name);
input.setAttribute('value',name);
input.setAttribute('selected',true);
if(userHasSavedWorkItemTypes) {
if(checkIfStringIsInArray(usersSavedWorkItemTypes,name)) {
input.setAttribute('checked','checked');
} else {
consoleLog(`${name} is not in the users saved work item types.`)
}
} else {
input.setAttribute('checked','checked'); // Default is the user hasn't got saved types
}
input.setAttribute('class','input-work-item-type');
let label = document.createElement('label');
label.setAttribute('for',name);
label.innerHTML = ' ' + name;
let linebreak = document.createElement('br');
document.getElementById('workItemTypes').appendChild(input);
document.getElementById('workItemTypes').appendChild(label);
document.getElementById('workItemTypes').appendChild(linebreak);
}
await addUpdateButton();
return true;
}
async function addUpdateFieldsButton() {
consoleLog(`addUpdateFieldsButton()`);
let div = document.getElementById('fields');
let button = document.createElement('button');
button.setAttribute('type','button');
button.innerHTML = 'Update selected fields';
button.id = 'updateFieldsButton';
button.addEventListener ("click", function () { saveFields(); return false;});
div.appendChild(button);
}
async function addUpdateButton() {
consoleLog(`addUpdateButton()`);
let div = document.getElementById('workItemTypes');
let button = document.createElement('button');
button.setAttribute('type','button');
button.innerHTML = 'Update work item types';
button.id = 'updateButton';
button.addEventListener ("click", function () { saveWorkItemTypes(); return false;});
div.appendChild(button);
}
async function getWorkItemDetails(witClient,workItemIds,contracts){
consoleLog(`getWorkItemDetails()`);
if(workItemIds != '') {
const workItemDetails = await witClient.getWorkItems(workItemIds,null,null,contracts.WorkItemExpand.Relations);
consoleLog(`Work Item Details: ${workItemDetails}`)
return workItemDetails;
} else {
consoleLog('No work items Ids were passed')
return false;
}
}
async function reset() {
consoleLog(`reset()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
// Set value in user scope
await dataService.setValue("userIterationPath", '@CurrentIteration', {scopeType: "User"});
await dataService.setValue("userWorkItems", '', {scopeType: "User"});
await dataService.setValue('userAreaPath', '', {scopeType: 'User'});
await dataService.setValue('userCardSize', '', {scopeType: 'User'})
await dataService.setValue("userFields", '', {scopeType: "User"});
location.reload(true);
return true;
}
function insertAreaPathSectionHeader() {
consoleLog(`insertAreaPathSectionHeader()`);
let areaPathSectionHeader = document.createElement('h2');
areaPathSectionHeader.innerHTML = 'Select area path for printing';
let areaPathSectionDiv = document.getElementById('areaPaths');
areaPathSectionDiv.insertBefore(areaPathSectionHeader,areaPathSectionDiv.childNodes[0]);
return true;
}
async function getAreaPaths(witClient,projectId,contracts,areaPath) {
consoleLog(`getAreaPaths()`);
let areaPathsArray = [];
const userHasSavedAreaPath = checkForSavedAreaPath();
let usersSavedAreaPath = false;
if(userHasSavedAreaPath) {
usersSavedAreaPath = await getUsersSavedAreaPath();
}
const areaPaths = await witClient.getClassificationNode(projectId,contracts.TreeStructureGroup.Areas,null,4);
if(areaPaths != undefined && areaPaths.hasChildren && areaPaths.children.length > 0) {
const areaPathsLength = areaPaths.children.length;
let ul = document.getElementById('areaPathNames');
for(let i = 0; i < areaPathsLength; i++ ) {
let parentAreaPath = areaPaths.name;
let areaPathName = areaPaths.children[i].name;
let fullAreaPath = parentAreaPath + '\\' + areaPathName;
let li = document.createElement('li');
let a = document.createElement('a');
a.textContent = fullAreaPath;
a.title = areaPathName;
a.id = fullAreaPath;
a.href = '#';
a.onclick = function() {
let linkId = this.getAttribute('id');
// Get data service
VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
// Set value in user scope
dataService.setValue('userAreaPath', linkId, {scopeType: 'User'}).then(function(linkId) {
consoleLog(`User's selected area path is now ${linkId}`);
});
});
location.reload(true); // setting true forces the browser to GET again, rather than refreshing from cache
return false;
}
if(userHasSavedAreaPath) {
if(usersSavedAreaPath == fullAreaPath) {
a.textContent += ' (Selected)' ;
a.classList.add('embolden')
}
}
li.appendChild(a);
ul.appendChild(li);
}
await insertAreaPathSectionHeader();
}
}
async function checkForSavedAreaPath() {
consoleLog(`checkForSavedAreaPath()`);
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
if(await dataService.getValue('userAreaPath', {scopeType: 'User'})) {
return true;
} else {
return false;
}
}
async function getUsersSavedAreaPath() {
consoleLog(`getUsersSavedAreaPath()`);
let userHasSavedAreaPath = checkForSavedAreaPath();
if(userHasSavedAreaPath === false) {
return false;
} else {
const dataService = await VSS.getService(VSS.ServiceIds.ExtensionData);
let usersSavedAreaPath = await dataService.getValue("userAreaPath", {scopeType: "User"});
if(userHasSavedAreaPath === false || usersSavedAreaPath === '' || usersSavedAreaPath === undefined) {
//consoleLog('User does not have a saved area path')
consoleLog(`User does not have a saved area path. A 404 is expected when userAreaPath is requested`)
return false;
} else {
consoleLog(`Users saved area path is ${usersSavedAreaPath}`);
return usersSavedAreaPath;
}
}
}
function triggerPrint() {
consoleLog(`triggerPrint()`);
window.focus();
window.print();
}
async function getParent(witClient,workItemDetails,id,x) {
if (workItemDetails[x].relations) {
try {
const { url } = await workItemDetails[x].relations.find(({ rel }) => rel === 'System.LinkTypes.Hierarchy-Reverse');
if(url) {
const parentId = url.split('/').slice(-1)[0];
const workItemDetails = await witClient.getWorkItem(parentId,['System.Title']);
const parentWorkItemTitleString = workItemDetails.fields['System.Title'];
let div = document.createElement('div');
div.setAttribute('class','parent');
div.innerHTML = `<span class="parent-id">${parentId}</span><span id="parent-${parentId}" class="parent-name">${parentWorkItemTitleString}</span>`;
document.getElementById(id).appendChild(div);
parentCount++;
return true;
} else {
consoleLog('An error occurred.');
return false;
}
} catch (err) {
//consoleLog(err);
return false;
}
} else {
consoleLog(`No relations`);
return true;
}
}
function removeLoader() {
consoleLog(`removeLoader()`);
const loaderExists = document.getElementById('loader');
if(loaderExists) {
consoleLog(`loaderExists`);
loaderExists.style.display = 'none';
//document.getElementById('loader').parentNode.removeChild('loader');
}
}
function displayMain() {
consoleLog(`displayMain()`);
const mainExists = document.getElementById('main');
if(mainExists){
if(mainExists.getAttribute('style')){
document.getElementById('main').style.display = 'inline';
}
}
}
async function displayWorkItems(witClient,contracts,workClient,projectId,teamContext,iteration,projectName,areaPath,selectedCardSize) {
consoleLog(`displayWorkItems()`);
let sheetCount = 0;
consoleLog(`sheetCount is: ${sheetCount}`);
let iterationChunk = '';
let queryString = 'SELECT [System.Id] FROM WorkItems WHERE ';
if(iteration != '@CurrentIteration') {
iterationChunk = `[System.IterationPath] = '${iteration}' `;
} else {
iterationChunk = `[System.IterationPath] = ${iteration} `;
}
consoleLog(`iterationChunk is: ${iterationChunk}`)
queryString = queryString + iterationChunk;
consoleLog(`queryString is: ${queryString}`)
const userHasSavedWorkItemsTypes = await checkForSavedWorkItemTypes();
if(userHasSavedWorkItemsTypes){
consoleLog(`User has saved work item types`);
let usersSavedWorkItemTypes = await getUsersSavedWorkItemTypes();
let workItemChunk = '';
let workItemsQueryStringResultLength = usersSavedWorkItemTypes.length;
for(let i = 0; i < workItemsQueryStringResultLength; i++){
let type = usersSavedWorkItemTypes[i];
if(i === 0) {
workItemChunk += ' AND (';
}
workItemChunk += `[System.WorkItemType] = '${type} '`;
if((i+1) == workItemsQueryStringResultLength){
workItemChunk += ')';
} else {
workItemChunk += ' OR ';
}
}
queryString = queryString + workItemChunk;
consoleLog(`After adding saved work item types queryString is: ${queryString}`)
} else {
consoleLog(`User does not have any saved work item types`)
}
if(areaPath) {
consoleLog(`areaPath: ${areaPath}`);
const areaPathQueryString = ` AND [System.AreaPath] = '${areaPath}'`;
queryString = queryString + areaPathQueryString;
consoleLog(`After adding the Area Path queryString is: ${queryString}`);
} else {
consoleLog(`User does not have a saved Area Path`)
}
let query = { 'query' : `${queryString}`}
const workItemIds = await getWorkItemIds(witClient,query,projectId,contracts);
consoleLog(`Passing workItemIds: ${workItemIds} to getWorkItemDetails()`)
const workItemDetails = await getWorkItemDetails(witClient,workItemIds,contracts);
const numberOfWorkItems = workItemIds.length;
consoleLog(`numberOfWorkItems is: ${numberOfWorkItems}`)
let divideAfter = 0;
consoleLog(`selectedCardSize is: ${selectedCardSize}`)
if(selectedCardSize === 'small'){
divideAfter = 15;
} else if (selectedCardSize === 'large') {
divideAfter = 6;
}
for(let x = 0; x < numberOfWorkItems; x++) {
let id = workItemDetails[x].id;
let type = workItemDetails[x].fields['System.WorkItemType'];
let title = workItemDetails[x].fields['System.Title'];
let originalEstimate = undefined;
let assignedTo = undefined;
if(workItemDetails[x].fields['Microsoft.VSTS.Scheduling.OriginalEstimate']) {
originalEstimate = workItemDetails[x].fields['Microsoft.VSTS.Scheduling.OriginalEstimate'];
}
if(workItemDetails[x].fields['System.AssignedTo']) {
assignedTo = workItemDetails[x].fields['System.AssignedTo'].displayName;
}
let styleType = type.replace(/\s+/g, '-').toLowerCase();
consoleLog(`styleType is: ${styleType}`);
if(x === 0) {
let section = document.createElement('div');
section.className = 'sheet padding-10mm';
let sheetId = `sheet${sheetCount}`;
section.id = sheetId;
document.getElementById('main').appendChild(section);
}
let div = document.createElement('div');
let className = 'workItem ' + styleType + ' ' + id + ' ' + x + ' ' + selectedCardSize;
consoleLog(`className is: ${className}`);
div.className = className;
div.setAttribute('id', id);
const stringParts = [];
stringParts.push(`<div class="type truncate">${type}</div><div class="id">${id}</div><div class="title" id="${title}-${id}">${title}</div>`);
while (div.hasChildNodes()) {
div.firstChild.remove();
}
let stringToInsert = stringParts.join('');
if (x % divideAfter === 0 && x != 0) {
stringParts.push('</div>');
sheetCount++;
let section = document.createElement('div');
section.className = 'sheet padding-10mm';
let sheetId = `sheet${sheetCount}`;
section.id = sheetId;
document.getElementById('main').appendChild(section);
}
div.insertAdjacentHTML('beforeend', stringToInsert);
document.getElementById(`sheet${sheetCount}`).appendChild(div);
let titleClass = `${title}-${id}`;
let titleElement = document.getElementById(`${titleClass}`);
if(selectedCardSize === 'small'){
//$clamp(titleElement, {clamp: 4})
} else if (selectedCardSize === 'large') {
//$clamp(titleElement, {clamp: 4})
}
await getParent(witClient,workItemDetails,id,x);
if(assignedTo) {
let div = document.createElement('div');
let className = 'assigned';
div.className = className;
div.setAttribute('id', id)
div.insertAdjacentHTML('beforeend', assignedTo);
document.getElementById(id).appendChild(div)
}
if(originalEstimate) {
let div = document.createElement('div');
let className = 'estimate';
div.className = className;
div.setAttribute('id', id)
div.insertAdjacentHTML('beforeend', originalEstimate);
document.getElementById(id).appendChild(div)
}
}
consoleLog(`${parentCount} parents fetched`);
appendCountOfWorkItems(numberOfWorkItems);
displayPrint();
displayMain();
removeLoader();
if(numberOfWorkItems >= 1) {
triggerPrint();
} else {
alert(`No work items were returned, please check the options selected and that work items are assigend to the iteration`);
}
return true;
}
function setPrintHeader(iteration,areaPath) {
consoleLog(`setPrintHeader()`);
const printHeader = document.createElement('h2');
printHeader.innerHTML = `Printing selected work item types in ${iteration}`;
if(areaPath != '') {
printHeader.innerHTML += ` and ${areaPath}`
}
printHeader.id = 'workItemSelectSectionHeader';
const div = document.getElementById('printHeader');
div.appendChild(printHeader);
}
async function getIterations(workClient,teamContext,iteration) {
consoleLog(`getIterations()`);
let teamIterations = await workClient.getClient().getTeamIterations(teamContext);
let ul = document.getElementById('iterationPaths');
for(let x = 0; x < teamIterations.length; x++) {
let iterationPathId = teamIterations[x].id;
let iterationPath = teamIterations[x].path;
let startDate = teamIterations[x].attributes['startDate'];
let endDate = teamIterations[x].attributes['finishDate'];
let todayDate = new Date();
let li = document.createElement('li');
let a = document.createElement('a');
let linkText = '';
if(todayDate > startDate && todayDate < endDate){
linkText = document.createTextNode(iterationPath + ' (Active iteration)');
a.appendChild(linkText);
} else {
linkText = document.createTextNode(iterationPath);
a.appendChild(linkText);
a.href = '#';
}
a.title = iterationPath;
a.id = iterationPath;
a.onclick = function() {
let linkId = this.getAttribute('id');
// Get data service
VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
// Set value in user scope
dataService.setValue('userIterationPath', linkId, {scopeType: 'User'}).then(function(linkId) {
consoleLog(`User's selected iteration path is now ${linkId}`);
});
});
location.reload(true); // setting true forces the browser to GET again, rather than refreshing from cache
return false;
}
li.appendChild(a);
ul.appendChild(li);
if(iterationPath === iteration) {
a.textContent += ' (Selected)' ;
a.classList.add('embolden')
}
}
insertIterationSelectHeader();
}
function insertIterationSelectHeader() {
consoleLog(`insertIterationSelectHeader()`);
let iterationSelectSectionHeader = document.createElement('h2');
iterationSelectSectionHeader.innerHTML = 'Select iteration path for printing';
let iterationSelectDiv = document.getElementById('iterations');
iterationSelectDiv.insertBefore(iterationSelectSectionHeader,iterationSelectDiv.childNodes[0]);
}
function displayPrint() {
consoleLog(`displayPrint()`);
const printExists = document.getElementById('print');
if(printExists){
if(printExists.getAttribute('style')){
document.getElementById('print').style.display = 'inline';
}
}
}
function consoleLog(msg) {
console.log(`${consoleMessageCount}: ${msg}`);
consoleMessageCount++;
}
</script>
<script type="text/javascript" src="js/clamp.js"></script>
<link rel="stylesheet" media="print" href="css/print.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
<link rel="stylesheet" media="all" href="css/cards.css">
<link rel="stylesheet" media="screen" href="css/screen.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/paper-css/0.4.1/paper.min.css">
<style>@page { size: A4 }</style>
</head>
<body class="board-cards a4">
<header id="header" class="no-print">
<h1>Azure board cards</h2>
<div id="guidance">
<p>When all work items have loaded, the browser will trigger a print dialog. This may take a little time depending on how many work items are included.</p>
<p>The following preferences are set per user, and can be <a href="#" id="resetLinkInGuidance" title="Reset defaults" onclick="reset();return false;">reset to defaults</a> when no longer required.</p>
<div id="loader">
</div>
</div>
<div id="configuration" class="no-print">
<div id="iterations">
<ul id="iterationPaths">
</ul>
</div>
</div>
<div id="workitemConfiguration" class="no-print">
<div id="workItemTypes">
</div>
</div>
<div id="areaPathConfiguration" class="no-print">
<div id="areaPaths">
<ul id="areaPathNames">
</ul>
</div>
<div id="cardSizeOptions" class="no-print">
<ul id="cardSize">
</ul>
</div>
<div id="fieldOptions" class="no-print">
<div id="fields">
</div>
</div>
</div>
<div id="contact" class="no-print">
<p>If you have any problems, please report them by <a target="_blank" href="https://github.com/nick-myers/azure-devops-board-cards/issues">raising a new Github issue</a>.</p>
</div>
<div id="print" class="no-print" style="display:none;">
<p>[ <a href="#" onclick="triggerPrint(); return false;">Print</a> - <a href="#" onclick="reset(); return false;">Restore defaults</a> ]</p>
</div>
<div id="printHeader" class="no-print">
</div>
</header>
<main id="main">
</main>
</body>
</html>