-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththumbnails.js
350 lines (336 loc) · 10.3 KB
/
thumbnails.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'use strict';
import React from 'react';
import T from 'prop-types';
import {Note} from './aris';
import {CacheMedia} from './media';
import {clicker} from './utils';
// @ifdef NATIVE
import { Text } from "./styles";
import
{ View
, TouchableOpacity
, Image
, Platform
, Dimensions
} from 'react-native';
import InfiniteScrollView from 'react-native-infinite-scroll-view';
// @endif
// @ifdef WEB
import InfiniteScroll from 'react-infinite-scroller';
// @endif
// TODO: if the initial batch of thumbnails fits on screen,
// the infinite scroll view isn't attempting to load the next batch
// @ifdef NATIVE
class NoteCard extends React.Component {
constructor(props) {
super(props);
}
render() {
// console.log('mtnc: ' + JSON.stringify(this.props.note.user))
return (
<TouchableOpacity onPress={() => this.props.onSelectNote(this.props.note)} style={{
borderRadius: 5,
margin: 5,
alignItems: 'stretch',
backgroundColor: 'white',
shadowColor: 'black',
shadowOpacity: 0.2,
shadowRadius: 3,
shadowOffset: {height: 2},
elevation: 3,
width: 180,
}}>
<View style={{
// these can't just be on the Image on iOS, see
// https://facebook.github.io/react-native/docs/images#ios-border-radius-styles
borderTopLeftRadius: 5,
borderTopRightRadius: 5,
overflow: 'hidden',
}}>
<Image source={this.props.source} style={{
width: 180,
height: this.props.source ? 115 : 0,
resizeMode: 'cover',
}} />
</View>
<View style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<CacheMedia
media_id={this.props.note.user.media_id}
size="thumb_url"
auth={this.props.auth}
online={this.props.online}
withURL={(url) => (
<Image
source={url}
style={{
margin: 7,
marginRight: 0,
width: 16,
height: 16,
borderRadius: 8,
resizeMode: 'cover',
}}
/>
)}
/>
<Text style={{flex: 1, margin: 7, fontWeight: 'bold'}}>
{this.props.note.user.display_name}
</Text>
<View style={{
width: 12,
height: 12,
borderRadius: 999,
margin: 7,
backgroundColor: this.props.getColor(this.props.note),
}} />
</View>
{
this.props.game.field_id_caption && (
<Text style={{margin: 7}} numberOfLines={this.props.expand ? 4 : 1}>
{this.props.note.field_data[this.props.game.field_id_caption]}
</Text>
)
}
</TouchableOpacity>
);
}
}
// @endif
export class SiftrThumbnails extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
// @ifdef NATIVE
scrollTop() {
if (this.refs.scroll != null) {
this.refs.scroll.scrollTo({y: 0, animated: false});
}
}
render() {
if (this.props.view === 'hybrid') {
return (
<InfiniteScrollView
style={{
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
}}
canLoadMore={this.props.hasMore}
onLoadMoreAsync={this.props.loadMore}
horizontal={true}
snapToInterval={190 /* NoteCard width 180 + margin 5 * 2 */}
snapToAlignment="center"
onScroll={(event) => {
const x = event.nativeEvent.contentOffset.x;
const w = Dimensions.get('window').width;
const cardScroll = (x + w / 2) / 190;
let cardIndex = Math.floor(cardScroll);
const cardPosition = cardScroll - cardIndex;
if (!(0.4 < cardPosition && cardPosition < 0.6)) {
cardIndex = -1;
}
cardIndex -= 1; // remove blank spot at start
this.setState({cardIndex});
this.props.onMouseEnter(this.props.notes[cardIndex]);
}}
>
<View style={{
width: 190,
height: 1,
}} />
<View style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'flex-end',
justifyContent: 'center',
}}>
{
this.props.notes.map((note, i) =>
<CacheMedia
key={note.note_id}
auth={this.props.auth}
media_id={this.props.game.field_id_preview ? note.field_data[this.props.game.field_id_preview] : undefined}
size={this.props.game.field_id_preview ? 'big_thumb_url' : undefined}
online={this.props.online}
withURL={(url) =>
<NoteCard
auth={this.props.auth}
online={this.props.online}
source={url}
note={note}
onSelectNote={this.props.onSelectNote}
getColor={this.props.getColor}
expand={this.state.cardIndex === i}
game={this.props.game}
/>
}
/>
)
}
</View>
<View style={{
width: 190,
height: 1,
}} />
</InfiniteScrollView>
);
}
return <InfiniteScrollView
ref="scroll"
style={{
backgroundColor: 'rgb(246,246,246)',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
}}
canLoadMore={this.props.hasMore}
onLoadMoreAsync={this.props.loadMore}
>
<View style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
flex: 1,
}}>
{
this.props.pendingNotes.map(({dir, json}) => {
json = JSON.parse(json);
let note = new Note(json);
if (note.game_id !== this.props.game.game_id) return null;
note.pending = true;
note.dir = dir.path;
note.files = json.files;
let url = '';
// TODO get actual correct file for field_id_preview
if (Platform.OS === "ios") {
url = `${dir.path}/${json.files[0].filename}`;
} else {
url = `file://${dir.path}/${json.files[0].filename}`;
}
return (
<NoteCard
auth={this.props.auth}
online={this.props.online}
key={dir.name}
source={{uri: url}}
note={note}
onSelectNote={this.props.onSelectNote}
getColor={this.props.getColor}
game={this.props.game}
/>
);
}).concat(this.props.notes.map((note) =>
<CacheMedia
key={note.note_id}
auth={this.props.auth}
media_id={this.props.game.field_id_preview ? note.field_data[this.props.game.field_id_preview] : undefined}
size={this.props.game.field_id_preview ? 'big_thumb_url' : undefined}
online={this.props.online}
withURL={(url) =>
<NoteCard
auth={this.props.auth}
online={this.props.online}
source={url}
note={note}
onSelectNote={this.props.onSelectNote}
getColor={this.props.getColor}
game={this.props.game}
/>
}
/>
))
}
</View>
</InfiniteScrollView>;
}
// @endif
// @ifdef WEB
scrollTop() {
if (this.refs.scroll != null) {
this.refs.scroll.scrollTop = 0;
}
}
render() {
return <div className="siftr-thumbs" ref="scroll">
<InfiniteScroll
hasMore={this.props.hasMore}
loadMore={this.props.loadMore}
useWindow={false}
initialLoad={false}
>
{
this.props.notes.map((note) =>
<a
className={
"siftr-thumbnail-card" +
(this.props.mapHover.indexOf(note.note_id) !== -1 ? ' hybrid-hover' : '')
}
key={note.note_id}
href="#"
onClick={clicker(() => this.props.onSelectNote(note))}
onMouseEnter={() => this.props.onMouseEnter(note)}
onMouseLeave={() => this.props.onMouseLeave(note)}
>
{
this.props.game.field_id_preview && (
<CacheMedia
media_id={note.field_data[this.props.game.field_id_preview]}
size="big_thumb_url"
auth={this.props.auth}
withURL={(url) => (
<div
className="siftr-thumbnail"
style={{backgroundImage: url ? `url(${url})` : undefined}}
/>
)}
/>
)
}
<div className="siftr-thumbnail-info">
<div className="siftr-thumbnail-username">{note.user.display_name}</div>
<div
className="siftr-thumbnail-dot"
style={{backgroundColor: this.props.getColor(note)}}
/>
</div>
{
this.props.game.field_id_caption && (
<div className="siftr-thumbnail-caption">
{note.field_data[this.props.game.field_id_caption]}
</div>
)
}
</a>
)
}
</InfiniteScroll>
</div>
}
// @endif
}
SiftrThumbnails.propTypes = {
notes: T.arrayOf(T.instanceOf(Note)),
getColor: T.func,
onSelectNote: T.func,
onMouseEnter: T.func,
onMouseLeave: T.func,
loadMore: T.func,
hasMore: T.bool,
};
SiftrThumbnails.defaultProps = {
notes: [],
getColor: () => 'white',
onSelectNote: function(){},
onMouseEnter: function(){},
onMouseLeave: function(){},
loadMore: function(){},
hasMore: false,
};