focalboard/webapp/src/components/viewMenu.tsx

114 lines
3.4 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-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
import {Board} from '../blocks/board'
2020-10-21 03:28:55 +02:00
import {BoardView, MutableBoardView} 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 mutator from '../mutator'
import {Utils} from '../utils'
import Menu from '../widgets/menu'
type Props = {
2020-10-20 21:50:53 +02:00
boardTree?: BoardTree
board: Board,
showView: (id: string) => void
}
2020-10-14 21:39:34 +02:00
export default class ViewMenu extends React.Component<Props> {
2020-10-20 21:50:53 +02:00
handleDeleteView = async (id: string) => {
const {board, boardTree, showView} = this.props
2020-10-20 21:50:53 +02:00
Utils.log('deleteView')
const view = boardTree.activeView
const nextView = boardTree.views.find((o) => o !== view)
2020-10-20 21:50:53 +02:00
await mutator.deleteBlock(view, 'delete view')
showView(nextView.id)
2020-10-20 21:50:53 +02:00
}
2020-10-20 21:50:53 +02:00
handleViewClick = (id: string) => {
const {boardTree, showView} = this.props
Utils.log('view ' + id)
const view = boardTree.views.find((o) => o.id === id)
showView(view.id)
2020-10-20 21:50:53 +02:00
}
2020-10-20 21:50:53 +02:00
handleAddViewBoard = async (id: string) => {
const {board, boardTree, showView} = this.props
Utils.log('addview-board')
const view = new MutableBoardView()
view.title = 'Board View'
2020-10-20 21:52:56 +02:00
view.viewType = 'board'
2020-10-20 21:50:53 +02:00
view.parentId = board.id
2020-10-20 21:50:53 +02:00
const oldViewId = boardTree.activeView.id
2020-10-20 21:50:53 +02:00
await mutator.insertBlock(
view,
'add view',
async () => {
2020-10-20 21:50:53 +02:00
showView(view.id)
},
async () => {
2020-10-20 21:50:53 +02:00
showView(oldViewId)
2020-10-20 21:52:56 +02:00
})
2020-10-20 21:50:53 +02:00
}
2020-10-20 21:50:53 +02:00
handleAddViewTable = async (id: string) => {
const {board, boardTree, showView} = this.props
Utils.log('addview-table')
const view = new MutableBoardView()
2020-10-20 21:52:56 +02:00
view.title = 'Table View'
view.viewType = 'table'
view.parentId = board.id
2020-10-20 21:50:53 +02:00
view.visiblePropertyIds = board.cardProperties.map((o) => o.id)
const oldViewId = boardTree.activeView.id
2020-10-20 21:50:53 +02:00
await mutator.insertBlock(
view,
'add view',
async () => {
2020-10-20 21:50:53 +02:00
showView(view.id)
},
async () => {
showView(oldViewId)
2020-10-20 21:52:56 +02:00
})
2020-10-20 21:50:53 +02:00
}
2020-10-20 21:50:53 +02:00
render() {
const {boardTree} = this.props
2020-10-20 21:50:53 +02:00
return (
<Menu>
{boardTree.views.map((view) => (<Menu.Text
key={view.id}
id={view.id}
name={view.title}
onClick={this.handleViewClick}
/>))}
<Menu.Separator/>
{boardTree.views.length > 1 && <Menu.Text
id='__deleteView'
name='Delete View'
onClick={this.handleDeleteView}
/>}
<Menu.SubMenu
id='__addView'
name='Add View'
>
<Menu.Text
id='board'
name='Board'
onClick={this.handleAddViewBoard}
/>
<Menu.Text
id='table'
name='Table'
onClick={this.handleAddViewTable}
/>
</Menu.SubMenu>
</Menu>
)
}
}