-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSheet.jsx
125 lines (101 loc) · 2.73 KB
/
Sheet.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// XXX probably belongs in a Sheets collection.
const columns = 10;
const rows = 10;
const expandToRows = function (columns, rows, sortedCells) {
// Expand a sparse array of cells into a (columns x rows) grid,
// adding empty cells to fill in the gaps.
// Cells must be sorted by row then column. This allows
// us to insert the sparse data in one pass.
const cellList = sortedCells[Symbol.iterator]();
let nextCell = cellList.next().value;
const getCell = R.curry((row, col) => {
let currentCell = nextCell;
if (currentCell && currentCell.row === row && currentCell.col === col) {
nextCell = cellList.next().value;
return currentCell;
}
// else return an empty cell
return {col, row};
});
const expandRow = (row) => R.range(0, columns).map(getCell(row));
return R.range(0, rows).map(expandRow);
};
OriginCell = ({cellCount}) => (
// This is the cell at the upper left of the grid
// XXX probably remove cellCount stat later
<div className="cell head">
{cellCount} saved cells
</div>
);
ColumnHeadCell = ({col}) => (
<div className="cell head" >
Column {col}
</div>
);
RowHeadCell = ({rowNumber}) => (
<div className="cell head">
Row {rowNumber}
</div>
);
HeaderRow = ({columns, cells}) => (
<div className="row head">
<OriginCell key="origin cell" cellCount={cells.length} />
{R.range(0, columns)
.map((col) => <ColumnHeadCell key={`column-${col}`} col={col} />)
}
</div>
);
Sheet = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
return {
cells: Cells.find({}, {sort: {row: 1, col: 1}}).fetch()
};
},
propTypes: {
// row: React.PropTypes.array.isRequired
},
getInitialState() {
return {
selectedCell: ""
};
},
setSelection (cell) {
const key = `cell (${cell.col}, ${cell.row})`;
this.setState({selectedCell: key});
},
clearSelection () {
this.setState({selectedCell: ""});
},
renderCell (cell) {
const key = `cell (${cell.col}, ${cell.row})`;
return (
<Cell
key={key}
cell={cell}
selected={this.state.selectedCell === key}
setSelection={this.setSelection}
clearSelection={this.clearSelection} />
);
},
renderRow (cells, rowNumber) {
return (
<div className="row" key={`row-${rowNumber}`}>
<RowHeadCell
key={`rowHeadFor-${rowNumber}`}
rowNumber={rowNumber} />
{cells.map(this.renderCell)}
</div> // .row
);
},
render () {
return (
<div className="spreadsheet">
<HeaderRow
columns={columns}
cells={this.data.cells} />
{expandToRows(columns, rows, this.data.cells).map(this.renderRow)}
</div>
);
}
});