forked from TehShrike/k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_columns.js
50 lines (42 loc) · 1.1 KB
/
board_columns.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
var state = require('./state.js')
var api = require('./kanbanize_api.js')
const columnsGetter = state.getterFactory('boardColumns')
var alreadyFetchedBoardsOnceThisRun = false
function getBoardsFromApi(cb) {
api('get_board_structure', {}, function(response) {
var columns = response.columns.map(function(column) {
return column.lcname
})
state.set('boardColumns', columns)
if (typeof cb === 'function') {
cb(columns)
}
})
}
function fetchColumnsIfNecessary(cb) {
columnsGetter((err, columns) => {
if (err) {
console.log(err)
} else if (!columns) {
getBoardsFromApi(cb)
} else {
cb(columns)
}
})
}
function getColumnNextTo(indexDelta, columnName, cb) {
fetchColumnsIfNecessary(function columnCallback(columns) {
var index = columns.indexOf(columnName)
if (index === -1 && !alreadyFetchedBoardsOnceThisRun) {
alreadyFetchedBoardsOnceThisRun = true
getBoardsFromApi(columnCallback)
} else {
cb(index == -1 ? null : columns[index + indexDelta])
}
})
}
module.exports = {
get: fetchColumnsIfNecessary,
refresh: getBoardsFromApi,
getColumnNextTo: getColumnNextTo
}