image-manager.js
5.14 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(function() {
class ImageManager extends React.Component {
constructor(props) {
super(props);
this.state = {
images: [],
hasMore: false,
page: 0
};
}
show(callback) {
$(React.findDOMNode(this)).show();
this.callback = callback;
}
hide() {
console.log('test');
$(React.findDOMNode(this)).hide();
}
selectImage(image) {
if(this.callback) {
this.callback(image);
}
this.hide();
}
componentDidMount() {
var _this = this;
// Set initial images
$.getJSON('/images/all', data => {
this.setState({
images: data.images,
hasMore: data.hasMore
});
});
// Create dropzone
this.dropZone = new Dropzone(React.findDOMNode(this.refs.dropZone), {
url: '/upload/image',
init: function() {
var dz = this;
this.on("sending", function(file, xhr, data) {
data.append("_token", document.querySelector('meta[name=token]').getAttribute('content'));
});
this.on("success", function(file, data) {
_this.state.images.unshift(data);
_this.setState({
images: _this.state.images
});
//$(file.previewElement).fadeOut(400, function() {
// dz.removeFile(file);
//})
});
}
});
}
loadMore() {
this.state.page++;
$.getJSON('/images/all/' + this.state.page, data => {
this.setState({
images: this.state.images.concat(data.images),
hasMore: data.hasMore
});
});
}
overlayClick(e) {
if(e.target.className === 'overlay') {
this.hide();
}
}
render() {
var loadMore = this.loadMore.bind(this);
var selectImage = this.selectImage.bind(this);
var overlayClick = this.overlayClick.bind(this);
var hide = this.hide.bind(this);
return (
<div className="overlay" onClick={overlayClick}>
<div id="image-manager">
<div className="image-manager-content">
<div className="dropzone-container" ref="dropZone">
<div className="dz-message">Drop files or click here to upload</div>
</div>
<ImageList data={this.state.images} loadMore={loadMore} selectImage={selectImage} hasMore={this.state.hasMore}/>
</div>
<div className="image-manager-sidebar">
<button className="neg button image-manager-close" onClick={hide}>x</button>
<h2>Images</h2>
</div>
</div>
</div>
);
}
}
class ImageList extends React.Component {
render() {
var selectImage = this.props.selectImage;
var images = this.props.data.map(function(image) {
return (
<Image key={image.id} image={image} selectImage={selectImage} />
);
});
return (
<div className="image-manager-list clearfix">
{images}
{ this.props.hasMore ? <div className="load-more" onClick={this.props.loadMore}>Load More</div> : null }
</div>
);
}
}
class Image extends React.Component {
constructor(){
super();
this._dblClickTime = 350;
this._cClickTime = 0;
}
setImage() {
this.props.selectImage(this.props.image);
}
imageClick() {
var cTime = (new Date()).getTime();
var timeDiff = cTime - this._cClickTime;
console.log(timeDiff);
if(this._cClickTime !== 0 && timeDiff < this._dblClickTime) {
// DoubleClick
this.setImage();
} else {
// Single Click
}
this._cClickTime = cTime;
}
render() {
var imageClick = this.imageClick.bind(this);
return (
<div>
<img onClick={imageClick} src={this.props.image.thumbnail}/>
</div>
);
}
}
class ImageInfoPage extends React.Component {
render() {
}
}
if(document.getElementById('image-manager-container')) {
window.ImageManager = React.render(
<ImageManager />,
document.getElementById('image-manager-container')
);
}
})();