mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-03 16:10:26 +00:00
add data api
This commit is contained in:
@@ -26,14 +26,16 @@ class AppMain extends React.Component {
|
|||||||
resizable: true
|
resizable: true
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
let initData = props.initData;
|
let initData = props.initData;
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
columns: initData.columns.length ? this.deseralizeGridData(initData.columns) : this._columns,
|
columns: initData.columns.length ? this.deseralizeGridData(initData.columns) : this._columns,
|
||||||
rows: initData.rows.length ? initData.rows : this.createRows(1),
|
rows: initData.rows.length ? initData.rows : this.createRows(1),
|
||||||
isNewColumnDialogShow: false,
|
isNewColumnDialogShow: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.dTableStore = new DTableStore(initData);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
@@ -109,7 +111,8 @@ class AppMain extends React.Component {
|
|||||||
|
|
||||||
onInsertRow = () => {
|
onInsertRow = () => {
|
||||||
let newRowIndex = this.getSize();
|
let newRowIndex = this.getSize();
|
||||||
this.handleAddRow({ newRowIndex });
|
let rows = this.dTableStore.insertRow(newRowIndex);
|
||||||
|
this.setState({rows});
|
||||||
this.props.onContentChanged();
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,18 +129,9 @@ class AppMain extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onNewColumn = (columnName, columnType) => {
|
onNewColumn = (columnName, columnType) => {
|
||||||
let editor = this.createEditor(columnType);
|
let idx = this.state.columns.length;
|
||||||
let newColumn = {
|
let columns = this.dTableStore.insertColumn(idx, columnName, columnType);
|
||||||
key: columnName,
|
columns = this.formatColumnsData(columns);
|
||||||
name: columnName,
|
|
||||||
editor: editor,
|
|
||||||
editable: true,
|
|
||||||
width: 200,
|
|
||||||
resizable: true
|
|
||||||
};
|
|
||||||
|
|
||||||
let columns = this.state.columns.slice();
|
|
||||||
columns.push(newColumn);
|
|
||||||
this.setState({columns: columns});
|
this.setState({columns: columns});
|
||||||
this.onNewColumnCancel();
|
this.onNewColumnCancel();
|
||||||
this.props.onContentChanged();
|
this.props.onContentChanged();
|
||||||
@@ -150,24 +144,16 @@ class AppMain extends React.Component {
|
|||||||
|
|
||||||
onRowDelete = (e, data) => {
|
onRowDelete = (e, data) => {
|
||||||
let { rowIdx } = data;
|
let { rowIdx } = data;
|
||||||
let newRows = this.state.rows.slice(0); // copy array;
|
let rows = this.dTableStore.deleteRow(rowIdx);
|
||||||
newRows.splice(rowIdx, 1);
|
this.setState({rows});
|
||||||
this.setState({rows: newRows});
|
|
||||||
this.props.onContentChanged();
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onColumnDelete = (e, data) => {
|
onColumnDelete = (e, data) => {
|
||||||
let column = data.column;
|
let column = data.column;
|
||||||
let key = column.key;
|
let idx = column.idx - 1;
|
||||||
let columns = this.state.columns.filter(item => item.key !== key);
|
let columns = this.dTableStore.deleteColumn(idx);
|
||||||
let rows = this.state.rows.map(item => {
|
this.setState({columns});
|
||||||
delete item[key];
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
this.setState({
|
|
||||||
columns: columns,
|
|
||||||
rows: rows
|
|
||||||
});
|
|
||||||
this.props.onContentChanged();
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +173,8 @@ class AppMain extends React.Component {
|
|||||||
columns: columns,
|
columns: columns,
|
||||||
rows: rows,
|
rows: rows,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.dTableStore.updateStoreValues({columns, rows});
|
||||||
}
|
}
|
||||||
|
|
||||||
formatColumnsData = (columns) => {
|
formatColumnsData = (columns) => {
|
||||||
|
@@ -3,7 +3,7 @@ export default class GridColumn {
|
|||||||
constructor(object) {
|
constructor(object) {
|
||||||
this.key = object.columnName || object.name;
|
this.key = object.columnName || object.name;
|
||||||
this.name = object.columnName || object.name;
|
this.name = object.columnName || object.name;
|
||||||
this.type = object.columnType || null;
|
this.editor = object.columnType || null;
|
||||||
this.editable = object.editable || true;
|
this.editable = object.editable || true;
|
||||||
this.width = object.width || 200;
|
this.width = object.width || 200;
|
||||||
this.resizable = object.resizable || true;
|
this.resizable = object.resizable || true;
|
||||||
|
@@ -5,45 +5,43 @@ import GridRow from '../model/GridRow';
|
|||||||
function apply(value, op) {
|
function apply(value, op) {
|
||||||
|
|
||||||
let { type } = op;
|
let { type } = op;
|
||||||
value = value.slice(0); // clone a copy
|
let next = value.slice(0); // clone a copy
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case OperationTypes.DELETE_ROW : {
|
case OperationTypes.DELETE_ROW : {
|
||||||
let { rowIdx } = op;
|
let { rowIdx } = op;
|
||||||
let next = value.splice(rowIdx, 1);
|
next.splice(rowIdx, 1);
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OperationTypes.INSERT_ROW : {
|
case OperationTypes.INSERT_ROW : {
|
||||||
let { newRowIdx } = op;
|
let { newRowIdx } = op;
|
||||||
let row = new GridRow({newRowIdx});
|
let row = new GridRow({newRowIdx});
|
||||||
let next = value.push(row);
|
next.push(row);
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OperationTypes.DELETE_COLUMN : {
|
case OperationTypes.DELETE_COLUMN : {
|
||||||
let { idx } = op;
|
let { idx } = op;
|
||||||
let next = value.splice(idx, 1);
|
next.splice(idx, 1);
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OperationTypes.INSERT_COLUMN : {
|
case OperationTypes.INSERT_COLUMN : {
|
||||||
let { idx, columnName, columnType } = op;
|
let { idx, columnName, columnType } = op;
|
||||||
let column = new GridColumn({idx, columnName, columnType});
|
let column = new GridColumn({idx, columnName, columnType});
|
||||||
let next = value.push(column);
|
next.push(column);
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OperationTypes.MODIFY_CELL : {
|
case OperationTypes.MODIFY_CELL : {
|
||||||
let { rowIdx, key, newCellValue } = op;
|
let { rowIdx, key, newCellValue } = op;
|
||||||
let next = value;
|
|
||||||
next[rowIdx][key] = newCellValue;
|
next[rowIdx][key] = newCellValue;
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OperationTypes.MODIFY_COLUMN : {
|
case OperationTypes.MODIFY_COLUMN : {
|
||||||
let { idx, newColumnName } = op;
|
let { idx, newColumnName } = op;
|
||||||
let next = value;
|
|
||||||
next[idx]['key'] = newColumnName;
|
next[idx]['key'] = newColumnName;
|
||||||
next[idx]['name'] = newColumnName;
|
next[idx]['name'] = newColumnName;
|
||||||
return next;
|
return next;
|
||||||
|
@@ -22,24 +22,33 @@ export default class DTableStore {
|
|||||||
deleteRow(rowIdx) {
|
deleteRow(rowIdx) {
|
||||||
let type = OperationTypes.DELETE_ROW;
|
let type = OperationTypes.DELETE_ROW;
|
||||||
let operation = this.createOperation({type, rowIdx});
|
let operation = this.createOperation({type, rowIdx});
|
||||||
let next = operation.apply(type);
|
let next = operation.apply(this.rows);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.rows = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
insertRow(newRowIdx) {
|
insertRow(newRowIdx) {
|
||||||
let type = OperationTypes.INSERT_ROW;
|
let type = OperationTypes.INSERT_ROW;
|
||||||
let operation = this.createOperation({type, newRowIdx});
|
let operation = this.createOperation({type, newRowIdx});
|
||||||
let next = operation.apply(type);
|
let next = operation.apply(this.rows);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.rows = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteColumn(idx) {
|
deleteColumn(idx) {
|
||||||
let type = OperationTypes.INSERT_COLUMN;
|
let type = OperationTypes.DELETE_COLUMN;
|
||||||
let operation = this.createOperation({type, idx});
|
let operation = this.createOperation({type, idx});
|
||||||
let next = operation.apply(this.columns);
|
let next = operation.apply(this.columns);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.columns = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +56,10 @@ export default class DTableStore {
|
|||||||
let type = OperationTypes.INSERT_COLUMN;
|
let type = OperationTypes.INSERT_COLUMN;
|
||||||
let operation = this.createOperation({type, idx, columnName, columnType});
|
let operation = this.createOperation({type, idx, columnName, columnType});
|
||||||
let next = operation.apply(this.columns);
|
let next = operation.apply(this.columns);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.columns = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +67,10 @@ export default class DTableStore {
|
|||||||
let type = OperationTypes.MODIFY_COLUMN;
|
let type = OperationTypes.MODIFY_COLUMN;
|
||||||
let operation = this.createOperation({type, idx, oldColumnName, newColumnName});
|
let operation = this.createOperation({type, idx, oldColumnName, newColumnName});
|
||||||
let next = operation.apply(this.columns);
|
let next = operation.apply(this.columns);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.columns = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +79,10 @@ export default class DTableStore {
|
|||||||
let key = this.columns[idx].key;
|
let key = this.columns[idx].key;
|
||||||
let operation = this.createOperation({type, rowIdx, key, oldCellValue, newCellValue});
|
let operation = this.createOperation({type, rowIdx, key, oldCellValue, newCellValue});
|
||||||
let next = operation.apply(this.rows);
|
let next = operation.apply(this.rows);
|
||||||
|
|
||||||
this.operations.push(operation);
|
this.operations.push(operation);
|
||||||
|
this.rows = next;
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user