2019-04-04 17:19:10 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-04-26 07:16:01 +08:00
|
|
|
import ReactDataGrid from '@seafile/react-data-grid/';
|
2019-04-04 17:19:10 +08:00
|
|
|
import update from 'immutability-helper';
|
2019-04-26 07:16:01 +08:00
|
|
|
import { Menu, Editors } from '@seafile/react-data-grid-addons';
|
|
|
|
import GridHeaderContextMenu from './grid-header-contextmenu';
|
|
|
|
import GridContentContextMenu from './grid-content-contextmenu';
|
|
|
|
import ModalPortal from '../../components/modal-portal';
|
|
|
|
import NewColumnDialog from './new-column-dialog';
|
2019-04-04 17:19:10 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
initData: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
class AppMain extends React.Component {
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this._columns = [
|
|
|
|
{
|
|
|
|
key: 'id',
|
|
|
|
name: 'ID',
|
|
|
|
width: 80,
|
|
|
|
resizable: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let initData = props.initData;
|
|
|
|
|
|
|
|
this.state = {
|
2019-04-26 07:16:01 +08:00
|
|
|
columns: initData.columns.length ? this.deseralizeGridData(initData.columns) : this._columns,
|
|
|
|
rows: initData.rows.length ? initData.rows : this.createRows(1),
|
|
|
|
isNewColumnDialogShow: false,
|
2019-04-04 17:19:10 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
let data = nextProps.initData;
|
|
|
|
this.deseralizeGridData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
createRows = (numberOfRows) => {
|
|
|
|
let rows = [];
|
|
|
|
for (let i = 0; i < numberOfRows; i++) {
|
|
|
|
rows[i] = this.createFakeRowObjectData(i);
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
createFakeRowObjectData = (index) => {
|
|
|
|
return {id: 'id_' + index};
|
|
|
|
};
|
|
|
|
|
|
|
|
getColumns = () => {
|
|
|
|
let clonedColumns = this.state.columns.slice();
|
|
|
|
return clonedColumns;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleGridRowsUpdated = ({ fromRow, toRow, updated }) => {
|
|
|
|
let rows = this.state.rows.slice();
|
|
|
|
|
|
|
|
for (let i = fromRow; i <= toRow; i++) {
|
|
|
|
let rowToUpdate = rows[i];
|
|
|
|
let updatedRow = update(rowToUpdate, {$merge: updated});
|
|
|
|
rows[i] = updatedRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ rows });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleAddRow = ({ newRowIndex }) => {
|
|
|
|
const newRow = {
|
|
|
|
id: 'id_' + newRowIndex,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rows = this.state.rows.slice();
|
|
|
|
rows = update(rows, {$push: [newRow]});
|
|
|
|
this.setState({ rows });
|
|
|
|
};
|
|
|
|
|
|
|
|
getRowAt = (index) => {
|
|
|
|
if (index < 0 || index > this.getSize()) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.state.rows[index];
|
|
|
|
};
|
|
|
|
|
|
|
|
getSize = () => {
|
|
|
|
return this.state.rows.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
onInsertRow = () => {
|
|
|
|
let newRowIndex = this.getSize();
|
|
|
|
this.handleAddRow({ newRowIndex });
|
|
|
|
}
|
|
|
|
|
|
|
|
onInsertColumn = () => {
|
2019-04-26 07:16:01 +08:00
|
|
|
this.setState({isNewColumnDialogShow: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
onNewColumn = (columnName, columnType) => {
|
|
|
|
let editor = this.createEditor(columnType);
|
2019-04-04 17:19:10 +08:00
|
|
|
let newColumn = {
|
2019-04-26 07:16:01 +08:00
|
|
|
key: columnName,
|
|
|
|
name: columnName,
|
|
|
|
editor: editor,
|
2019-04-04 17:19:10 +08:00
|
|
|
editable: true,
|
|
|
|
width: 200,
|
|
|
|
resizable: true
|
|
|
|
};
|
2019-04-26 07:16:01 +08:00
|
|
|
|
2019-04-04 17:19:10 +08:00
|
|
|
let columns = this.state.columns.slice();
|
|
|
|
columns.push(newColumn);
|
|
|
|
this.setState({columns: columns});
|
2019-04-26 07:16:01 +08:00
|
|
|
this.onNewColumnCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
onNewColumnCancel = () => {
|
|
|
|
this.setState({isNewColumnDialogShow: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
onRowDelete = (e, data) => {
|
|
|
|
let { rowIdx } = data;
|
|
|
|
let newRows = this.state.rows.slice(0); // copy array;
|
|
|
|
newRows.splice(rowIdx, 1);
|
|
|
|
this.setState({rows: newRows});
|
|
|
|
}
|
|
|
|
|
|
|
|
onColumnDelete = (e, data) => {
|
|
|
|
let column = data.column;
|
|
|
|
let key = column.key;
|
|
|
|
let columns = this.state.columns.filter(item => item.key !== key);
|
|
|
|
this.setState({columns: columns});
|
2019-04-04 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
serializeGridData = () => {
|
|
|
|
let gridData = {
|
|
|
|
columns: JSON.stringify(this.state.columns),
|
|
|
|
rows: JSON.stringify(this.state.rows),
|
|
|
|
};
|
|
|
|
return gridData;
|
|
|
|
}
|
|
|
|
|
|
|
|
deseralizeGridData = (data) => {
|
2019-04-26 07:16:01 +08:00
|
|
|
let columns = JSON.parse(data.columns);
|
|
|
|
let rows = JSON.parse(data.rows);
|
|
|
|
columns = this.formatColumnsData(columns);
|
2019-04-04 17:19:10 +08:00
|
|
|
this.setState({
|
2019-04-26 07:16:01 +08:00
|
|
|
columns: columns,
|
|
|
|
rows: rows,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
formatColumnsData = (columns) => {
|
|
|
|
return columns.map(column => {
|
|
|
|
if (column.editor) {
|
|
|
|
let editor = this.createEditor(column.editor);
|
|
|
|
column.editor = editor;
|
|
|
|
}
|
|
|
|
return column;
|
2019-04-04 17:19:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-26 07:16:01 +08:00
|
|
|
createEditor = (editorType) => {
|
|
|
|
let editor = null;
|
|
|
|
switch (editorType) {
|
|
|
|
case 'number':
|
|
|
|
editor = <Editors.NumberEditor />;
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
editor = null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:19:10 +08:00
|
|
|
render() {
|
|
|
|
let columns = this.getColumns();
|
|
|
|
return (
|
|
|
|
<div id="main">
|
|
|
|
<ReactDataGrid
|
|
|
|
ref={ node => this.grid = node }
|
|
|
|
enableCellSelect={true}
|
|
|
|
columns={columns}
|
|
|
|
rowGetter={this.getRowAt}
|
|
|
|
rowsCount={this.getSize()}
|
|
|
|
onGridRowsUpdated={this.handleGridRowsUpdated}
|
|
|
|
enableRowSelect={true}
|
|
|
|
rowHeight={50}
|
|
|
|
minHeight={600}
|
|
|
|
rowScrollTimeout={200}
|
|
|
|
enableInsertColumn={true}
|
|
|
|
enableInsertRow={true}
|
|
|
|
onInsertRow={this.onInsertRow}
|
|
|
|
onInsertColumn={this.onInsertColumn}
|
2019-04-26 07:16:01 +08:00
|
|
|
RowsContainer={Menu.ContextMenuTrigger}
|
|
|
|
headerContextMenu={<GridHeaderContextMenu id="grid-header-contxtmenu" onColumnDelete={this.onColumnDelete} />}
|
|
|
|
contextMenu={<GridContentContextMenu id="grid-content-contxtmenu" onRowDelete={this.onRowDelete} />}
|
2019-04-04 17:19:10 +08:00
|
|
|
/>
|
2019-04-26 07:16:01 +08:00
|
|
|
{this.state.isNewColumnDialogShow && (
|
|
|
|
<ModalPortal>
|
|
|
|
<NewColumnDialog
|
|
|
|
onNewColumnCancel={this.onNewColumnCancel}
|
|
|
|
onNewColumn={this.onNewColumn}
|
|
|
|
/>
|
|
|
|
</ModalPortal>
|
|
|
|
)}
|
2019-04-04 17:19:10 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AppMain.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default AppMain;
|