2020-10-20 21:50:53 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2020-10-20 21:52:56 +02:00
|
|
|
import {BoardView} from './blocks/boardView'
|
2020-10-21 03:54:39 +02:00
|
|
|
import {BoardTree} from './viewModel/boardTree'
|
2020-10-20 21:52:56 +02:00
|
|
|
import {OctoUtils} from './octoUtils'
|
|
|
|
import {Utils} from './utils'
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
class CsvExporter {
|
2020-10-22 04:54:21 +02:00
|
|
|
static exportTableCsv(boardTree: BoardTree, view?: BoardView): void {
|
2020-10-20 21:50:53 +02:00
|
|
|
const {activeView} = boardTree
|
|
|
|
const viewToExport = view ?? activeView
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
const rows = CsvExporter.generateTableArray(boardTree, view)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:52:56 +02:00
|
|
|
let csvContent = 'data:text/csv;charset=utf-8,'
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
rows.forEach((row) => {
|
|
|
|
const encodedRow = row.join(',')
|
2020-10-20 21:52:56 +02:00
|
|
|
csvContent += encodedRow + '\r\n'
|
2020-10-20 21:50:53 +02:00
|
|
|
})
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
const filename = `${Utils.sanitizeFilename(viewToExport.title)}.csv`
|
|
|
|
const encodedUri = encodeURI(csvContent)
|
|
|
|
const link = document.createElement('a')
|
2020-10-20 21:52:56 +02:00
|
|
|
link.style.display = 'none'
|
2020-10-20 21:50:53 +02:00
|
|
|
link.setAttribute('href', encodedUri)
|
|
|
|
link.setAttribute('download', filename)
|
|
|
|
document.body.appendChild(link) // FireFox support
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
link.click()
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
// TODO: Remove or reuse link
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
private static generateTableArray(boardTree: BoardTree, view?: BoardView): string[][] {
|
|
|
|
const {board, cards, activeView} = boardTree
|
|
|
|
const viewToExport = view ?? activeView
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
const rows: string[][] = []
|
|
|
|
const visibleProperties = board.cardProperties.filter((template) => viewToExport.visiblePropertyIds.includes(template.id))
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
{
|
|
|
|
// Header row
|
|
|
|
const row: string[] = []
|
|
|
|
visibleProperties.forEach((template) => {
|
|
|
|
row.push(template.name)
|
2020-10-20 21:52:56 +02:00
|
|
|
})
|
2020-10-20 21:50:53 +02:00
|
|
|
rows.push(row)
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
cards.forEach((card) => {
|
|
|
|
const row: string[] = []
|
|
|
|
visibleProperties.forEach((template) => {
|
|
|
|
const propertyValue = card.properties[template.id]
|
2020-10-20 21:52:56 +02:00
|
|
|
const displayValue = OctoUtils.propertyDisplayValue(card, propertyValue, template) || ''
|
2020-10-20 21:50:53 +02:00
|
|
|
if (template.type === 'number') {
|
|
|
|
const numericValue = propertyValue ? Number(propertyValue).toString() : undefined
|
|
|
|
row.push(numericValue)
|
|
|
|
} else {
|
|
|
|
// Export as string
|
|
|
|
row.push(`"${displayValue}"`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
rows.push(row)
|
2020-10-20 21:52:56 +02:00
|
|
|
})
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
return rows
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:50:53 +02:00
|
|
|
export {CsvExporter}
|