-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathImageViewerContainer.coffee
79 lines (65 loc) · 2.34 KB
/
ImageViewerContainer.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
'use strict'
React = require 'react'
PropTypes = require 'prop-types'
{ createElement } = React
ImageViewerElement = require './ImageViewer.cjsx'
class ImageViewer extends React.Component
@propTypes =
files: PropTypes.array.isRequired
showNotifications: PropTypes.bool
initialFile: PropTypes.object.isRequired
onFileChange: PropTypes.func
constructor: (props) ->
super(props)
currentIndex = props.files.indexOf props.initialFile
this.state =
file: props.initialFile
currentIndex: currentIndex
nextFile: currentIndex + 1 < props.files.length
prevFile: currentIndex - 1 >= 0
this.viewNext = this.viewNext.bind this
this.viewPrevious = this.viewPrevious.bind this
this.selectFile = this.selectFile.bind this
this.updateArrowsState = this.updateArrowsState.bind this
this.toggleZoom = this.toggleZoom.bind this
componentDidMount: ->
this.props.onFileChange({file: this.state.file}) if this.props.onFileChange
componentWillUpdate: ->
this.props.onFileChange({file: this.state.file}) if this.props.onFileChange
viewNext: ->
file = this.props.files[this.state.currentIndex + 1]
this.updateArrowsState(file)
viewPrevious: ->
file = this.props.files[this.state.currentIndex - 1]
this.updateArrowsState(file)
selectFile: (file) ->
file = file
this.updateArrowsState(file)
updateArrowsState: (file) ->
currentIndex = this.props.files.indexOf file
nextFile = currentIndex + 1 < this.props.files.length
prevFile = currentIndex - 1 >= 0
this.setState
file: file
currentIndex: currentIndex
nextFile: nextFile
prevFile: prevFile
imageZoomedIn: false
toggleZoom: ->
imageZoomedIn = this.state.imageZoomedIn
this.setState
imageZoomedIn: !imageZoomedIn
render: ->
createElement ImageViewerElement,
showNotifications: this.props.showNotifications
files: this.props.files
file: this.state.file
prevFile: this.state.prevFile
nextFile: this.state.nextFile
currentIndex: this.state.currentIndex
imageZoomedIn: this.state.imageZoomedIn
viewNext: this.viewNext
viewPrevious: this.viewPrevious
selectFile: this.selectFile
toggleZoom: this.toggleZoom
module.exports = ImageViewer