-
Notifications
You must be signed in to change notification settings - Fork 4
/
details.coffee
331 lines (278 loc) · 8.89 KB
/
details.coffee
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
@Details = createReactClass
getInitialState: ->
Store.state.showInfo = false
playing: false
showVideoControls: false
showControls: true
slideShow: false
zoom: false
componentDidMount: ->
window.addEventListener 'keyup', @onKeyUp
Store.state.openStack.push 'item'
componentWillUnmount: ->
window.removeEventListener 'keyup', @onKeyUp
onKeyUp: (e) ->
if e.target.tagName == "INPUT" || e.target.tagName == "TEXTAREA"
return
switch e.code
when 'Space'
item = Store.fetchItem @props.itemId
if @refs.video
if @refs.video.currentTime() > 0
@hideControls()
@stopVideo()
Store.navigateWithoutHistory @linkTo(1)
else
@startVideo()
else
@hideControls()
@stopVideo()
Store.navigateWithoutHistory @linkTo(1)
when 'ArrowRight', 'KeyJ', 'KeyL'
@hideControls()
@stopVideo()
Store.navigateWithoutHistory @linkTo(1)
when 'ArrowLeft', 'KeyH', 'KeyK'
@hideControls()
@stopVideo()
Store.navigateWithoutHistory @linkTo(-1)
when 'KeyF'
@hideControls()
@onFullScreen()
when 'KeyI'
@onInfo()
when 'KeyT'
Store.selectItem @props.itemId
Store.needsRedraw()
when 'KeyS'
@onSlideShow()
when 'KeyZ'
@onZoom()
onInfo: (e) ->
Store.state.showInfo = !Store.state.showInfo
Store.needsRedraw()
onStar: (e) ->
Store.toggleItemStar @props.itemId
onBullhorn: (e) ->
Store.toggleItemBullhorn @props.itemId
onSlideShow: (e) ->
newState = [email protected]
@setState
slideShow: newState
if newState
@slideshowTimer = setInterval @advanceSlideshow, 10000
else
clearInterval @slideshowTimer
advanceSlideshow: ->
@navigateNext()
onZoom: (e) ->
@setState
zoom: [email protected]
fullscreenFunctions: [
'requestFullscreen'
'mozRequestFullScreen'
'webkitRequestFullscreen'
'msRequestFullscreen'
]
fullScreenFunction: ->
html = document.documentElement
for i in @fullscreenFunctions
if html[i]?
return i
null
onFullScreen: (e) ->
html = document.documentElement
if func = @fullScreenFunction()
html[func].apply html
onSelect: (e) ->
Store.toggleSelection @props.itemId
moveTo: (dir) ->
@stopVideo()
Store.navigateWithoutHistory @linkTo(dir)
onClose: (e) ->
e.stopPropagation()
Store.navigateBack()
toggleControls: (e) ->
# Note: this preventDefault() causes the controls to be inoperable in FF
e.preventDefault()
@setState
showControls: [email protected]
onPlay: (e) ->
@refs.video.play()
@hideControls()
onPause: (e) ->
@refs.video.pause()
navigateNext: (e) ->
e.preventDefault() if e
@stopVideo()
Store.navigateWithoutHistory @linkTo(1)
navigatePrev: (e) ->
e.preventDefault() if e
@stopVideo()
Store.navigateWithoutHistory @linkTo(-1)
stopVideo: ->
if @refs.video
@refs.video.pause()
@setState
playing: false
startVideo: ->
if @refs.video
@refs.video.play()
@setState
playing: true
neighbor: (dir) ->
item = Store.getItem @props.itemId
return unless item
newIndex = item.index + dir
Store.state.items[newIndex]
largeURL: (itemId) ->
return unless itemId
item = Store.getItem itemId
if !item
return null
size = if item.variety == 'video'
'exploded'
else
'large'
return Store.resizedURL size, item
linkTo: (dir) ->
itemId = @neighbor(dir)
if itemId
return '/items/' + itemId
showControls: ->
@setState
showControls: true
hideControls: ->
@setState
showControls: false
setPlaying: (val)->
@setState
playing: val
siteIcon: ->
return @_siteIcon if @_siteIcon?
elem = document.querySelector 'link[rel=icon]'
@_siteIcon = elem.href
render: ->
Store.state.highlight = @props.itemId
item = Store.fetchItem @props.itemId
# make sure that the next batch is loaded if they are a fast clicker
margin = 10
if item
Store.executeSearch item.index - margin, item.index + margin
prevLink = @linkTo -1
nextLink = @linkTo 1
judgeMode = false
if Store.state.query
q = new SearchQuery
q.parse Store.state.query
judgeMode = true if q.options.unjudged
# preload neighbors details
if item && Store.state.showInfo
Store.getDetails @neighbor(1)
Store.getDetails @neighbor(-1)
classes = ['details-window']
classes.push 'show-controls' if @state.showControls
classes.push 'judge-bar' if judgeMode
imageStyle = {}
if @state.zoom
imageStyle.objectFit = 'cover'
<div className="details-wrapper">
<div className={classes.join ' '}>
<Swiper
curKey={@props.itemId}
prevKey={@neighbor(-1)}
nextKey={@neighbor(1)}
prevSrc={@largeURL(@neighbor(-1))}
nextSrc={@largeURL(@neighbor(1))}
moveTo={@moveTo}
>
{
if item && item.variety == 'video'
<Video
ref="video"
setPlaying={@setPlaying}
toggleControls={@toggleControls}
showControls={@showControls}
poster={@largeURL(@props.itemId)}
itemId={@props.itemId}
itemCode={item.code}
/>
else
<img style={imageStyle} ref="curImage" onClick={@toggleControls} src={@largeURL(@props.itemId)} />
}
</Swiper>
{
if item && item.variety == 'video'
if @state.playing
<ControlIcon title="Pause video" className="video-control" onClick={@onPause} icon="fa-pause"/>
else
<ControlIcon title="Play video" className="video-control" onClick={@onPlay} icon="fa-play"/>
}
<ControlIcon condition={prevLink} className="prev-control" href={prevLink} onClick={@navigatePrev} icon="fa-arrow-left" />
<ControlIcon condition={nextLink} className="control next-control" href={nextLink} onClick={@navigateNext} icon="fa-arrow-right" />
<div className="controls top">
<Link className="control home" href="/">
<img src={@siteIcon()}/>
</Link>
<div></div>
<div className="right-side">
{
if item
item.tag_ids.map (tag_id) ->
tag = Store.state.tagsById[tag_id]
if tag
<TagLink key={tag.id} className="tag-link" tag={tag}/>
}
<PresentButton
url={@largeURL @props.itemId}
streamUrl={item && item.variety == 'video' && Store.resizedURL('stream', item.id, item.code)}
/>
<Writer>
<ControlIcon
className={ "bullhorn" + if item && item.bullhorned then " active" else "" }
title="Tells others about this item"
onClick={@onBullhorn}
icon="fa-bullhorn"
/>
<ControlIcon
className="star"
title="Bookmark for future reference"
onClick={@onStar}
icon={if item && item.starred then "fa-star" else "fa-star-o"}
/>
</Writer>
{
# FIXME Only show this on devices without a keyboard
if @fullScreenFunction()
<a className="control" href="javascript:void(0)" onClick={@onFullScreen}><i className="fa fa-arrows-alt fa-fw"/></a>
}
<a className="control" href="javascript:void(0)" onClick={@onSelect}>
{
if Store.state.selection[@props.itemId]
<i className="fa fa-check-square-o fa-fw"/>
else
<i className="fa fa-square-o fa-fw"/>
}
</a>
<a className="control" href="javascript:void(0)" onClick={@onInfo}><i className="fa fa-info-circle fa-fw"/></a>
<a className="control" href="javascript:void(0)" onClick={@onClose}><i className="fa fa-close fa-fw"/></a>
</div>
</div>
{
if judgeMode
<div className="controls bottom">
<div></div>
<div className="centered">
<RateButton onNext={@navigateNext} type="down" itemId={@props.itemId} icon="fa-thumbs-o-down"/>
<RateButton onNext={@navigateNext} type="meh" itemId={@props.itemId} icon="fa-meh-o"/>
<RateButton onNext={@navigateNext} type="up" itemId={@props.itemId} icon="fa-thumbs-o-up"/>
</div>
<div></div>
</div>
}
</div>
{
if item && Store.state.showInfo
<Info item={item} onInfo={@onInfo}/>
}
</div>