-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
505 lines (408 loc) · 17.5 KB
/
script.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
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
$(document).ready(async function() {
const adovisTitle = $('#adovis-title')
if (!Array.prototype.last) {
Array.prototype.last = function() {
return this[this.length - 1]
}
}
$(window).bind('hashchange', function(e) {
location.reload()
});
let storage = window.localStorage
let credentials = storage.getItem("credentials")
let epicId = window.location.hash.trim()
if (epicId.startsWith('#')) {
epicId = epicId.substr(1)
}
let showClosed = false
if (storage.getItem('showClosed'))
showClosed = true
let show2ndLevelChildren = false
if (storage.getItem('show2ndLevelChildren'))
show2ndLevelChildren = true
console.log("epic ID is [", epicId, "]")
function requestVso(resource, query) {
return $.ajax({
type: 'GET',
url: 'https://skype.visualstudio.com/DefaultCollection/_apis/' + resource + '?api-version=1.0&' + $.param(query),
dataType: 'json',
headers: {
'Authorization': 'Basic ' + btoa(credentials)
}
})
}
class WorkItem {
static async getById(id) {
var response = await requestVso('wit/workitems', { 'id': id, '$expand': 'all'})
console.log('get by ID response', response)
return WorkItem.fromVsoResponse(response)
}
static async getManyById(ids) {
if (!ids || !ids.length)
return []
var response = await requestVso('wit/workitems', { 'ids': ids.join(','), '$expand': 'all'})
console.log('get many by ID response', response)
return response.value.map(function(item) {
return WorkItem.fromVsoResponse(item)
})
}
async getChildren() {
return await WorkItem.getManyById(this.childIds)
}
static fromVsoResponse(response) {
var item = new WorkItem()
item.id = response.id.toString()
item.wiType = response.fields['System.WorkItemType']
item.wiTypeShort = response.fields['System.WorkItemType'].replace(/ /g, '')
item.title = response.fields['System.Title'].replace(/\[DOR\]/g, '')
item.url = response._links.html.href.replace(/skype\.visualstudio\.com/, 'dev.azure.com/skype')
item.state = response.fields['System.State']
item.stateShort = response.fields['System.State'].replace(/ /g, '')
item.swag = response.fields['Skype.Swag']
item.assignedTo = response.fields['System.AssignedTo']
item.assignedToShort = WorkItem.getAssignedToShort(item.assignedTo)
item.column = response.fields['System.BoardColumn']
if (!item.column) {
console.log('found work item with unknown column', response)
item.column = 'Unknown'
}
item.columnShort = item.column.replace(/ /g, '')
const parentIds = WorkItem.getRelatedIds(response.relations, 'System.LinkTypes.Hierarchy-Reverse')
item.parentId = parentIds.length ? parentIds[0] : null;
item.childIds = WorkItem.getRelatedIds(response.relations, 'System.LinkTypes.Hierarchy-Forward')
item.predecessorIds = WorkItem.getRelatedIds(response.relations, 'System.LinkTypes.Dependency-Reverse')
item.successorIds = WorkItem.getRelatedIds(response.relations, 'System.LinkTypes.Dependency-Forward')
return item
}
static getRelatedIds(relations, relType) {
return relations
.filter(function(item) {
return item.rel == relType
})
.map(function(item) {
return item.url.split('/').last()
})
}
static getAssignedToShort(value) {
return value ? value.replace(/[^A-Z]/g, '') : ''
}
}
async function renderEpic(epicId) {
var chart = $("#chart")
adovisTitle.show()
chart.hide()
chart.empty()
var item = await WorkItem.getById(epicId)
console.log('work item', item)
chart.append(
'<h1 class="feature">' +
'<a href=' + item.url + ' target="_blank">' + item.wiType + ' ' + item.id + '</a> ' +
item.title +
'</h1>')
var children = await item.getChildren()
console.log('direct children', children)
var childrenToFetch = []
for (var item of children)
childrenToFetch = childrenToFetch.concat(item.childIds)
if (show2ndLevelChildren) {
var children2 = await WorkItem.getManyById(childrenToFetch)
console.log('children of 2nd level', children)
var children2WithExternalDependencies = []
for (var child2 of children2) {
for (var cessorId of child2.predecessorIds.concat(child2.successorIds)) {
var childCessors = children.filter(function(child) {
return child.id == cessorId
})
if (childCessors.length)
children2WithExternalDependencies.push(child2)
var child2Cessors = children2.filter(function(child) {
return child.id == cessorId && child.parentId != child2.parentId
})
if (child2Cessors.length)
children2WithExternalDependencies.push(child2)
}
}
var tryExtractMore = true
for (var i = 0; i < 10 && tryExtractMore; i++) {
tryExtractMore = false
var allChildren = children.concat(children2)
var extractedChildren = children.concat(children2WithExternalDependencies)
console.log('all children', allChildren)
console.log('extracted children', extractedChildren)
for (var child2 of children2WithExternalDependencies.slice()) {
var notExtractedPredecessorIds = child2.predecessorIds.filter(function(predecessorId) {
return (
allChildren.some(e => e.id == predecessorId) &&
!extractedChildren.some(e => e.id == predecessorId)
)
})
if (!notExtractedPredecessorIds.length)
continue
tryExtractMore = true
children2.forEach(function(child) {
if (notExtractedPredecessorIds.includes(child.id))
children2WithExternalDependencies.push(child)
})
}
}
children = children.concat(children2WithExternalDependencies)
}
if (!showClosed)
children = children.filter(item => item.state != 'Closed')
console.log('children', children)
var roots = {}
children.forEach(function(item) {
roots[item.id] = item
})
var graph = {}
children.forEach(function(item) {
graph[item.id] = {
workItem: item,
longestSwag: 0,
longestOffset: 0,
longestPredecessor: null
}
item.successorIds
.forEach(function(id) {
delete roots[id]
})
})
console.log('roots', roots)
console.log('graph', graph)
renderDependencyGraph(chart, roots, graph)
if (graph) {
chart.show(epicId)
adovisTitle.hide()
}
}
function renderDependencyGraph($chart, roots, graph) {
var calculateOffsets = function(swag, offset, predecessor, ids) {
ids.forEach(function(id) {
var graphItem = graph[id]
if (!graphItem)
return
if (offset > graphItem.longestOffset) {
graphItem.longestSwag = swag
graphItem.longestOffset = offset
graphItem.longestPredecessor = predecessor
}
var successorIds = graphItem.workItem.successorIds
if (successorIds) {
calculateOffsets(
(swag != null && graphItem.workItem.swag) ? (swag + graphItem.workItem.swag) : null,
offset + (graphItem.workItem.swag ? graphItem.workItem.swag : 1),
graphItem.workItem,
successorIds)
}
})
}
calculateOffsets(0, 0, null, Object.keys(roots))
var renderItems = function($chart, predecessor, unorderedIds) {
var ids = unorderedIds.slice(0)
ids.sort()
ids.forEach(function(id) {
var graphItem = graph[id]
if (!graphItem || graphItem.longestPredecessor != predecessor)
return
var numPredecessors = graphItem.workItem.predecessorIds.length
var numOtherPredecessors = numPredecessors - 1
var hasOtherPredecessors = numOtherPredecessors > 0
var numOtherSuccessors = 0
graphItem.workItem.successorIds.forEach(function(successorId) {
var successorGraphItem = graph[successorId]
if (!successorGraphItem)
return
if (successorGraphItem.longestPredecessor != graphItem.workItem)
numOtherSuccessors++
})
var hasOtherSuccessors = numOtherSuccessors > 0
var columnTag = (
'<span ' +
'class="column-' + graphItem.workItem.columnShort + '" ' +
'>' +
graphItem.workItem.column +
'</span> '
)
var predecessorsTag = (
hasOtherPredecessors
? '<span class="predecessors" title="Other predecessors, click to highlight">' + numOtherPredecessors + '</span> '
: ''
)
var successorsTag = (
hasOtherSuccessors
? '<span class="successors" title="Other successors, click to highlight">' + numOtherSuccessors + '</span> '
: ''
)
var assignedToTag = (
graphItem.workItem.assignedToShort && graphItem.workItem.state != 'Closed'
? ('<span class="assignedTo">' + graphItem.workItem.assignedToShort + '</span> ')
: ''
)
var workItemHref = (
'<a ' +
'href="' + graphItem.workItem.url + '"' +
' target="_blank">' +
id +
'</a> '
)
var title = (
'<span class="title">' +
graphItem.workItem.title +
'</span>'
)
const barLenght = 40;
const thisSwag = graphItem.workItem.swag ? graphItem.workItem.swag : '?'
const totalSwag =
(graphItem.longestSwag != null && graphItem.workItem.swag)
? (graphItem.longestSwag + graphItem.workItem.swag)
: '?'
$chart.append(
'<div ' +
'class="chart-row" ' +
'data-work-item-id="' + graphItem.workItem.id + '" ' +
'id="row-workItem' + graphItem.workItem.id + '">' +
'<div ' +
'style="' +
'margin-left: ' + graphItem.longestOffset * barLenght + 'px; ' +
'" ' +
'>' +
'<div class="task-' + graphItem.workItem.wiTypeShort + '">' +
columnTag +
assignedToTag +
predecessorsTag +
successorsTag +
workItemHref +
title + (
graphItem.workItem.wiType == 'Epic'
? ' <span class="swag" title="Swag / total swag to completion">' + thisSwag + ' / ' + totalSwag + '</span>'
: ''
) +
'</div>' +
'</div>' +
'</div>')
var successorIds = graphItem.workItem.successorIds
if (successorIds) {
renderItems($chart, graphItem.workItem, successorIds)
}
})
}
renderItems($chart, null, Object.keys(roots))
var toggleHighlightDependencies = function($row) {
var workItemId = $row.data('workItemId')
var highlighted = $row.hasClass('chart-row-highlighted')
var graphItem = graph[workItemId]
if (!highlighted) {
$row.addClass('chart-row-highlighted')
if (graphItem.longestPredecessor)
graphItem.workItem.predecessorIds.forEach(function(predecessorId) {
if (predecessorId != graphItem.longestPredecessor.id)
$('#row-workItem' + predecessorId).addClass('chart-row-predecessor-highlighted')
})
graphItem.workItem.successorIds.forEach(function(successorId) {
var successorGraphItem = graph[successorId]
if (successorGraphItem && workItemId != successorGraphItem.longestPredecessor.id)
$('#row-workItem' + successorId).addClass('chart-row-successor-highlighted')
})
} else {
$row.removeClass('chart-row-highlighted')
if (graphItem.longestPredecessor)
graphItem.workItem.predecessorIds.forEach(function(predecessorId) {
if (predecessorId != graphItem.longestPredecessor.id)
$('#row-workItem' + predecessorId).removeClass('chart-row-predecessor-highlighted')
})
graphItem.workItem.successorIds.forEach(function(successorId) {
var successorGraphItem = graph[successorId]
if (successorGraphItem && workItemId != successorGraphItem.longestPredecessor.id)
$('#row-workItem' + successorId).removeClass('chart-row-successor-highlighted')
})
}
}
$('.chart-row').click(
function() {
var $this = $(this)
var $current = $('.chart-row-highlighted')
if ($current.length != 0 && !$current.is($this)) {
toggleHighlightDependencies($current)
}
toggleHighlightDependencies($this)
})
}
function login() {
$("#view-main").show()
$("#view-login").hide()
credentials = "adovis:" + $("#input-pat").val()
$("#input-pat").val("")
storage.setItem("credentials", credentials)
renderEpic(epicId);
}
async function visualize(epicId) {
epicId = $("#input-epicId").val()
window.location.hash = '#' + epicId
await renderEpic(epicId)
}
function logout() {
$("#view-main").hide()
$("#view-login").show()
credentials = null
storage.removeItem("credentials")
}
function updateInputEpicValue(epicId) {
$("#input-epicId").val(epicId)
}
function toggleShowClosed() {
if (showClosed) {
showClosed = false;
storage.removeItem('showClosed')
} else {
showClosed = true;
storage.setItem('showClosed', 'true')
}
visualize()
}
function toggleShowClosed() {
if (showClosed) {
showClosed = false;
storage.removeItem('showClosed')
} else {
showClosed = true;
storage.setItem('showClosed', 'true')
}
visualize()
}
function toggleShow2ndLevelChildren() {
if (show2ndLevelChildren) {
show2ndLevelChildren = false;
storage.removeItem('show2ndLevelChildren')
} else {
show2ndLevelChildren = true;
storage.setItem('show2ndLevelChildren', 'true')
}
visualize()
}
$('#button-login').click(login)
$('#button-visualize').click(visualize)
$('#button-logout').click(logout)
$('#checkbox-show-closed').click(toggleShowClosed).attr('checked', showClosed)
$('#checkbox-show-2nd-level-children').click(toggleShow2ndLevelChildren).attr('checked', show2ndLevelChildren)
$('#input-epicId').keypress(function (e) {
if (e.which == 13) {
visualize()
return false
}
})
$('#input-pat').keypress(function (e) {
if (e.which == 13) {
login()
return false
}
})
if (!credentials) {
$('#view-login').show()
return
}
$("#view-main").show()
if (epicId) {
updateInputEpicValue(epicId)
await renderEpic(epicId)
}
})