focalboard/webapp/src/components/tableComponent.tsx

254 lines
10 KiB
TypeScript
Raw Normal View History

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 React from 'react'
2020-10-23 19:36:20 +02:00
import {FormattedMessage} from 'react-intl'
2020-10-20 21:52:56 +02:00
import {BlockIcons} from '../blockIcons'
import {IPropertyTemplate} from '../blocks/board'
2020-10-21 03:28:55 +02:00
import {Card, MutableCard} from '../blocks/card'
2020-10-21 03:54:39 +02:00
import {BoardTree} from '../viewModel/boardTree'
2020-10-20 21:52:56 +02:00
import mutator from '../mutator'
import {Utils} from '../utils'
import MenuWrapper from '../widgets/menuWrapper'
2020-10-20 21:52:56 +02:00
import {CardDialog} from './cardDialog'
import {Editable} from './editable'
import RootPortal from './rootPortal'
import {TableRow} from './tableRow'
import ViewHeader from './viewHeader'
import ViewTitle from './viewTitle'
import TableHeaderMenu from './tableHeaderMenu'
2020-10-08 18:21:27 +02:00
2020-10-25 22:11:27 +01:00
import './tableComponent.scss'
2020-10-08 18:21:27 +02:00
type Props = {
2020-10-20 21:50:53 +02:00
boardTree?: BoardTree
showView: (id: string) => void
setSearchText: (text: string) => void
2020-10-08 18:21:27 +02:00
}
type State = {
2020-10-20 21:50:53 +02:00
shownCard?: Card
2020-10-08 18:21:27 +02:00
}
class TableComponent extends React.Component<Props, State> {
2020-10-20 21:50:53 +02:00
private draggedHeaderTemplate: IPropertyTemplate
private cardIdToRowMap = new Map<string, React.RefObject<TableRow>>()
private cardIdToFocusOnRender: string
state: State = {}
2020-10-20 21:50:53 +02:00
2020-10-22 23:10:38 +02:00
shouldComponentUpdate(): boolean {
return true
}
render(): JSX.Element {
const {boardTree, showView} = this.props
2020-10-20 21:50:53 +02:00
if (!boardTree || !boardTree.board) {
2020-10-20 21:50:53 +02:00
return (
2020-10-23 19:36:20 +02:00
<div>
<FormattedMessage
id='TableComponent.loading'
defaultMessage='Loading...'
/>
</div>
)
}
2020-10-20 21:50:53 +02:00
const {board, cards, activeView} = boardTree
2020-10-20 21:50:53 +02:00
this.cardIdToRowMap.clear()
2020-10-20 21:50:53 +02:00
return (
2020-10-25 22:11:27 +01:00
<div className='TableComponent octo-app'>
2020-10-20 21:50:53 +02:00
{this.state.shownCard &&
<RootPortal>
<CardDialog
boardTree={boardTree}
card={this.state.shownCard}
onClose={() => this.setState({shownCard: undefined})}
/>
</RootPortal>}
<div className='octo-frame'>
<ViewTitle board={board}/>
2020-10-20 21:50:53 +02:00
<div className='octo-table'>
<ViewHeader
boardTree={boardTree}
showView={showView}
setSearchText={this.props.setSearchText}
addCard={this.addCard}
/>
2020-10-20 21:50:53 +02:00
{/* Main content */}
<div className='octo-table-body'>
{/* Headers */}
<div
className='octo-table-header'
id='mainBoardHeader'
>
<div
className='octo-table-cell title-cell'
style={{overflow: 'unset'}}
2020-10-20 21:50:53 +02:00
id='mainBoardHeader'
>
<MenuWrapper>
<div
className='octo-label'
style={{cursor: 'pointer'}}
>
<FormattedMessage
id='TableComponent.name'
defaultMessage='Name'
/>
</div>
<TableHeaderMenu
boardTree={boardTree}
templateId='__name'
2020-10-23 19:36:20 +02:00
/>
</MenuWrapper>
2020-10-20 21:50:53 +02:00
</div>
{board.cardProperties.
filter((template) => activeView.visiblePropertyIds.includes(template.id)).
map((template) =>
(<div
key={template.id}
style={{overflow: 'unset'}}
2020-10-20 21:50:53 +02:00
className='octo-table-cell'
draggable={true}
onDragStart={() => {
this.draggedHeaderTemplate = template
}}
onDragEnd={() => {
this.draggedHeaderTemplate = undefined
}}
onDragOver={(e) => {
e.preventDefault(); (e.target as HTMLElement).classList.add('dragover')
}}
onDragEnter={(e) => {
e.preventDefault(); (e.target as HTMLElement).classList.add('dragover')
}}
onDragLeave={(e) => {
e.preventDefault(); (e.target as HTMLElement).classList.remove('dragover')
}}
onDrop={(e) => {
e.preventDefault(); (e.target as HTMLElement).classList.remove('dragover'); this.onDropToColumn(template)
}}
>
<MenuWrapper>
<div
className='octo-label'
style={{cursor: 'pointer'}}
>{template.name}</div>
<TableHeaderMenu
boardTree={boardTree}
templateId={template.id}
/>
</MenuWrapper>
2020-10-20 21:50:53 +02:00
</div>),
)}
</div>
{/* Rows, one per card */}
{cards.map((card) => {
const openButonRef = React.createRef<HTMLDivElement>()
const tableRowRef = React.createRef<TableRow>()
let focusOnMount = false
2020-10-20 21:50:53 +02:00
if (this.cardIdToFocusOnRender && this.cardIdToFocusOnRender === card.id) {
this.cardIdToFocusOnRender = undefined
2020-10-20 21:50:53 +02:00
focusOnMount = true
}
const tableRow = (<TableRow
key={card.id}
ref={tableRowRef}
boardTree={boardTree}
card={card}
focusOnMount={focusOnMount}
2020-10-26 20:48:15 +01:00
onSaveWithEnter={() => {
console.log("WORKING")
if (cards.length > 0 && cards[cards.length - 1] === card) {
this.addCard(false)
2020-10-20 21:50:53 +02:00
}
2020-10-26 20:48:15 +01:00
console.log("STILL WORKING")
2020-10-20 21:50:53 +02:00
}}
2020-10-22 23:10:38 +02:00
/>)
2020-10-20 21:50:53 +02:00
this.cardIdToRowMap.set(card.id, tableRowRef)
return tableRow
})}
{/* Add New row */}
<div className='octo-table-footer'>
<div
className='octo-table-cell'
onClick={() => {
this.addCard()
}}
>
2020-10-23 19:36:20 +02:00
<FormattedMessage
id='TableComponent.plus-new'
defaultMessage='+ New'
/>
2020-10-20 21:50:53 +02:00
</div>
</div>
</div>
</div>
</div >
</div >
)
2020-10-20 21:50:53 +02:00
}
2020-10-25 11:29:47 +01:00
private addCard = async (show = false) => {
2020-10-20 21:50:53 +02:00
const {boardTree} = this.props
const card = new MutableCard()
card.parentId = boardTree.board.id
card.icon = BlockIcons.shared.randomIcon()
await mutator.insertBlock(
card,
2020-10-20 21:50:53 +02:00
'add card',
async () => {
if (show) {
this.setState({shownCard: card})
} else {
// Focus on this card's title inline on next render
2020-10-20 21:50:53 +02:00
this.cardIdToFocusOnRender = card.id
}
},
)
2020-10-20 21:50:53 +02:00
}
private async onDropToColumn(template: IPropertyTemplate) {
const {draggedHeaderTemplate} = this
2020-10-20 21:50:53 +02:00
if (!draggedHeaderTemplate) {
return
}
const {boardTree} = this.props
2020-10-20 21:50:53 +02:00
const {board} = boardTree
Utils.assertValue(mutator)
2020-10-20 21:50:53 +02:00
Utils.assertValue(boardTree)
Utils.log(`ondrop. Source column: ${draggedHeaderTemplate.name}, dest column: ${template.name}`)
// Move template to new index
const destIndex = template ? board.cardProperties.indexOf(template) : 0
2020-10-20 21:50:53 +02:00
await mutator.changePropertyTemplateOrder(board, draggedHeaderTemplate, destIndex)
}
2020-10-08 18:21:27 +02:00
}
2020-10-20 21:50:53 +02:00
export {TableComponent}