-
Notifications
You must be signed in to change notification settings - Fork 3
/
task_table.js
98 lines (80 loc) · 2.31 KB
/
task_table.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
var state = require('./state.js')
var api = require('./kanbanize_api.js')
var Table = require('cli-table')
var color = require('bash-color')
var boardColumns = require('./board_columns.js')
const taskIdGetter = state.getterFactory('taskId')
function ColumnularTable(headers) {
var table = new Table({
head: headers
})
var buckets = {}
headers.forEach(function(bucketName) {
buckets[bucketName] = []
})
return {
add: function(header, value) {
buckets[header].push(value)
},
build: function() {
var rowCount = headers.map(function bucketNamesToSizes(bucketName) {
return buckets[bucketName].length
}).reduce(function maxBucketSize(largestSoFar, bucketSize) {
return (largestSoFar > bucketSize) ? largestSoFar : bucketSize
})
function headerToBucket(headerName) {
return buckets[headerName]
}
function bucketToColumnValue(bucket) {
return bucket[i] || ''
}
for (var i = 0; i < rowCount; ++i) {
var newRow = headers.map(headerToBucket).map(bucketToColumnValue)
table.push(newRow)
}
return table.toString()
}
}
}
function displayAllTasks(user, taskId, columnsICareAbout) {
api('get_all_tasks', {}, function(tasks) {
var table = new ColumnularTable(columnsICareAbout)
if (user !== 'all') {
tasks = tasks.filter(function(task) {
return task.assignee === user
})
}
tasks.filter(function(task) {
return columnsICareAbout.indexOf(task.columnname) !== -1
}).forEach(function(task) {
var current = task.taskid == taskId
var bug = task.type === 'Bug'
var title = bug ? color.red(task.title) : task.title
var taskid = current ? color.cyan(task.taskid) : task.taskid
var text = taskid + ": " + title
table.add(task.columnname, text)
})
console.log(table.build())
})
}
function displayTaskTable(user, columns) {
taskIdGetter(function(err, taskId) {
taskId = err ? null : taskId
if (columns === 'all') {
boardColumns.get(function(columns) {
displayAllTasks(user, taskId, columns)
})
} else {
columns = columns.split(',').map(function(column) {
return column.trim()
})
displayAllTasks(user, taskId, columns)
}
})
}
module.exports = function() {
state.fetchAll(['user', 'columns'], displayTaskTable)
}
module.exports.all = function() {
state.fetchAll(['columns'], columns => displayTaskTable('all', columns))
}