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
|
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { TextArea, Button } from 'app/common'
import GalleryCaptionForm from './galleryCaption.form'
import actions from 'app/actions'
const ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
export default class CaptionForm extends Component {
state = {
editing: false,
expanded: false,
media: { settings: {} },
}
constructor(props) {
super(props)
this.edit = this.edit.bind(this)
this.expand = this.expand.bind(this)
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
this.handleCancel = this.handleCancel.bind(this)
this.handleUpdateGalleryCaption = this.handleUpdateGalleryCaption.bind(this)
}
componentDidMount() {
this.setState({
media: {
...this.props.media,
settings: {
...this.props.media.settings,
}
}
})
}
edit() {
this.setState({ editing: true })
}
expand() {
this.setState({ expanded: !this.state.expanded })
}
handleChange(e){
e.preventDefault()
this.setState({
media: {
...this.state.media,
settings: {
...this.state.media,
bibliography: e.target.value,
}
}
})
}
handleSubmit(e) {
e.preventDefault()
actions.media.update(this.state.media)
this.setState({ editing: false })
}
handleUpdateGalleryCaption(caption_id, item) {
const media = {
...this.state.media,
settings: {
...this.state.media.settings,
caption_lookup: {
...this.state.media.settings.caption_lookup,
[caption_id]: { ...item }
}
}
}
// console.log(media)
actions.media.update(media)
this.setState({ media })
}
handleCancel(e) {
e.preventDefault()
this.setState({
editing: false,
media: { ...this.props.media }
})
}
render() {
return this.state.editing
? this.renderForm()
: this.renderEntry()
}
renderForm() {
const { episode, index } = this.props
const { media } = this.state
return (
<form className='caption-form' onSubmit={this.handleSubmit}>
<TextArea
title={`Edit caption ${media.id}`}
name="bibliography"
placeholder={(
media.settings.bibliography
? "Enter caption"
: "This caption was automatically generated and is not explicitly set"
)}
data={media.settings}
onChange={this.handleChange}
/>
<div className='buttons'>
<span>
<Link to={`/editor/${episode.id}/media/${media.id}/edit/`}>Edit media</Link>
</span>
<div>
<button onClick={this.handleSubmit}>Save caption</button>
<button onClick={this.handleCancel}>Cancel</button>
</div>
</div>
</form>
)
}
renderEntry() {
const { index } = this.props
const { media } = this.state
const { bibliography } = media.settings
return (
<div>
<div className={media.settings.bibliography ? 'caption-entry' : 'caption-entry generated'}onClick={this.edit}>
<div className='caption-index'>
{index}{'. '}
</div>
{media.settings.bibliography
? <div className='caption-text' dangerouslySetInnerHTML={{ __html: media.settings.bibliography }} />
: this.renderAutomaticCaption()
}
</div>
{media.type === 'gallery' && this.renderGalleryEntries()}
</div>
)
}
renderAutomaticCaption() {
const { media } = this.state
return (
<div className='caption-text'>
{media.author}
{', '}
{media.pre_title && media.pre_title}
{media.title}
{media.post_title && media.post_title}
{'. '}
{media.date && (
' ' + media.date + '.'
)}
{media.medium && (
' ' + media.medium + '.'
)}
{media.source && (
' ' + media.source.trim()
)}
{' (Generated)'}
</div>
)
}
renderGalleryEntries() {
const { index } = this.props
const { media, expanded } = this.state
const { image_order, caption_lookup } = media.settings
return (
<div className={expanded ? "gallery-captions expanded" : "gallery-captions"}>
<div className="row">
<div className='button-spacer'></div>
<button className="expander" onClick={this.expand}>{expanded ? "▼ Hide gallery entries" : "► Show gallery entries"}</button>
</div>
{expanded && image_order.map((caption_id, caption_index) => (
<GalleryCaptionForm
key={caption_index}
caption_id={caption_id}
item={caption_lookup[caption_id]}
index={index + ALPHABET[caption_index]}
onUpdate={this.handleUpdateGalleryCaption}
/>
))}
</div>
)
}
}
|