forked from filantus/my_react_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (112 loc) · 3.55 KB
/
app.js
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
// window.ee = new EventEmitter();
const Component = React.Component;
class File extends Component {
getClassName(){
let posfix = ' ';
if (this.props.isSuperSelected) {
posfix = ' superselected';
}
else if (this.props.isSelected) {
posfix = ' selected'
}
return `file ${posfix}`
}
render() {
console.log(this.props.isSuperSelected)
return (
<tr className={this.getClassName()}
onClick={(e) => this.props.onFileClick(e, this.props.name)}
onDoubleClick={(e)=> this.props.onFileDoubleClick(e, this.props.name)}
>
<td>{this.props.is_dir ? <img src="/static/images/icons/folder.png"/> :
<img src="/static/images/icons/file.png"/>}</td>
<td>{this.props.name}</td>
<td>{this.props.size}</td>
<td>{this.props.created.replace(/-/g, '.')}</td>
</tr>
);
}
}
class Menu extends Component {
constructor(props) {
super(props);
this.state = {
selectedItems: [],
superSelectedItems: [],
};
axios.get('/files')
.then(res => {
console.log(res.data);
this.setState({'files_data': res.data});
});
}
onFileDoubleClick(e, name) {
e.preventDefault();
if ((this.state.superSelectedItems.indexOf(name)) !=0) {
let nextState = this.state.superSelectedItems.slice();
nextState.push(name);
this.setState({superSelectedItems: nextState});
}
else {
// TODO remove from superSelectedItems slice
}
}
onChildClick(e, name) {
e.preventDefault();
if ((this.state.selectedItems.indexOf(name)) !=0) {
let nextState = this.state.selectedItems.slice();
nextState.push(name);
this.setState({selectedItems: nextState});
}
else {
// TODO remove from selectedItems slice
}
}
render() {
const files_data = this.state.files_data || [];
const files = files_data.sort(function (a, b) {
return a.is_dir < b.is_dir
}).map(function (item, index) {
return <File
key={item.name}
name={item.name}
size={item.size}
is_dir={item.is_dir}
created={item.created}
isSelected={true ? (this.state.selectedItems.indexOf(item.name) != -1) : false}
isSuperSelected={true ? (this.state.superSelectedItems.indexOf(item.name) != -1) : false}
onFileClick={(e, name)=>this.onChildClick(e, name)}
onFileDoubleClick={(e, name)=>this.onFileDoubleClick(e, name)}
/>
}.bind(this));
return (
<table className='menu'>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Size</th>
<th>Created</th>
</tr>
</thead>
<tbody>{files}</tbody>
</table>
);
}
}
class App extends Component {
render() {
const { value, onIncreaseClick } = this.props
return (
<div>
<Menu />
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
</div>
)
}
}
ReactDOM.render(
<App/>,
document.getElementById('root')
)