forked from silverstripe/silverstripe-asset-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetAdmin.js
254 lines (229 loc) · 7.82 KB
/
AssetAdmin.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
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withRouter } from 'react-router';
import SilverStripeComponent from 'lib/SilverStripeComponent';
import backend from 'lib/Backend';
import Config from 'lib/Config';
import i18n from 'i18n';
import * as galleryActions from 'state/gallery/GalleryActions';
import * as editorActions from 'state/editor/EditorActions';
import * as breadcrumbsActions from 'state/breadcrumbs/BreadcrumbsActions';
import Editor from 'containers/Editor/Editor';
import Gallery from 'containers/Gallery/Gallery';
import Breadcrumb from 'components/Breadcrumb/Breadcrumb';
import Toolbar from 'components/Toolbar/Toolbar';
class AssetAdmin extends SilverStripeComponent {
constructor() {
super();
this.handleOpenFile = this.handleOpenFile.bind(this);
this.handleCloseFile = this.handleCloseFile.bind(this);
this.handleOpenFolder = this.handleOpenFolder.bind(this);
this.handleFileSave = this.handleFileSave.bind(this);
this.createEndpoint = this.createEndpoint.bind(this);
this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
this.compare = this.compare.bind(this);
// Build API callers from the URLs provided in configuration.
// In time, something like a GraphQL endpoint might be a better way to run.
const sectionConfig = Config.getSection('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin');
this.endpoints = {
createFolderApi: this.createEndpoint(sectionConfig.createFolderEndpoint),
readFolderApi: this.createEndpoint(sectionConfig.readFolderEndpoint, false),
updateFolderApi: this.createEndpoint(sectionConfig.updateFolderEndpoint),
updateFileApi: this.createEndpoint(sectionConfig.updateFileEndpoint),
deleteApi: this.createEndpoint(sectionConfig.deleteEndpoint),
};
}
/**
* Create a new endpoint
*
* @param {Object} endpointConfig
* @param {Boolean} includeToken
* @returns {Function}
*/
createEndpoint(endpointConfig, includeToken = true) {
return backend.createEndpointFetcher(Object.assign(
{},
endpointConfig,
includeToken ? { defaultData: { SecurityID: Config.get('SecurityID') } } : {}
));
}
componentWillReceiveProps(props) {
const viewChanged = this.compare(this.props.folder, props.folder);
if (viewChanged) {
this.setBreadcrumbs(props.folder);
}
}
/**
* Navigate to parent folder
*
* @param {Object} event
*/
handleBackButtonClick(event) {
event.preventDefault();
if (this.props.folder) {
this.handleOpenFolder(this.props.folder.parentID || 0);
} else {
this.handleOpenFolder(0);
}
}
/**
* Assign breadcrumbs from selected folder
*
* @param {Object} folder
*/
setBreadcrumbs(folder) {
const base = this.props.sectionConfig.url;
// Set root breadcrumb
const breadcrumbs = [{
text: i18n._t('AssetAdmin.FILES', 'Files'),
href: `/${base}/`,
}];
if (folder && folder.id) {
// Add parent folders
if (folder.parents) {
folder.parents.forEach((parent) => {
breadcrumbs.push({
text: parent.title,
href: `/${base}/show/${parent.id}`,
});
});
}
// Add current folder
breadcrumbs.push({
text: folder.title,
href: `/${base}/show/${folder.id}`,
});
}
this.props.actions.breadcrumbsActions.setBreadcrumbs(breadcrumbs);
}
/**
* Check if either of the two objects differ
*
* @param {Object} left
* @param {Object} right
*/
compare(left, right) {
// Check for falsiness
if (left && !right || right && !left) {
return true;
}
// Fall back to object comparison
return left && right && left.id !== right.id;
}
handleOpenFile(fileId) {
const base = this.props.sectionConfig.url;
this.props.router.push(`/${base}/show/${this.props.folderId}/edit/${fileId}`);
}
handleCloseFile() {
const base = this.props.sectionConfig.url;
this.props.router.push(`/${base}/show/${this.props.folderId}`);
}
handleOpenFolder(folderId) {
const base = this.props.sectionConfig.url;
this.props.router.push(`/${base}/show/${folderId}`);
}
/**
* @param {Number} id
* @param {Object} updates
*/
handleFileSave(id, updates) {
return this.props.actions.gallery.updateFile(this.props.updateFileApi, id, updates);
}
render() {
const sectionConfig = Config.getSection('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin');
const createFileApiUrl = sectionConfig.createFileEndpoint.url;
const createFileApiMethod = sectionConfig.createFileEndpoint.method;
let editor = (this.props.file &&
<Editor
file={this.props.file}
onClose={this.handleCloseFile}
onFileSave={this.handleFileSave}
/>
);
const showBackButton = !!(this.props.folder && this.props.folder.id);
return (
<div className="cms-content__inner no-preview">
<div className="cms-content__left cms-gallery collapse in">
<Toolbar showBackButton={showBackButton} handleBackButtonClick={this.handleBackButtonClick}>
<Breadcrumb multiline crumbs={this.props.breadcrumbs} />
</Toolbar>
<div className="gallery">
{editor}
<Gallery
files={this.props.files}
fileId={this.props.fileId}
folderId={this.props.folderId}
folder={this.props.folder}
name={this.props.name}
limit={this.props.limit}
bulkActions={this.props.bulkActions}
createFileApiUrl={createFileApiUrl}
createFileApiMethod={createFileApiMethod}
createFolderApi={this.endpoints.createFolderApi}
readFolderApi={this.endpoints.readFolderApi}
updateFolderApi={this.endpoints.updateFolderApi}
updateFileApi={this.endpoints.updateFileApi}
deleteApi={this.endpoints.deleteApi}
onOpenFile={this.handleOpenFile}
onOpenFolder={this.handleOpenFolder}
sectionConfig={this.props.sectionConfig}
/>
</div>
</div>
</div>
);
}
}
AssetAdmin.propTypes = {
config: React.PropTypes.shape({
forms: React.PropTypes.shape({
editForm: React.PropTypes.shape({
schemaUrl: React.PropTypes.string,
}),
}),
}),
sectionConfig: React.PropTypes.shape({
url: React.PropTypes.string,
}),
file: React.PropTypes.object,
folder: React.PropTypes.shape({
id: React.PropTypes.number,
title: React.PropTypes.string,
parents: React.PropTypes.array,
parentID: React.PropTypes.number,
canView: React.PropTypes.bool,
canEdit: React.PropTypes.bool,
}),
};
function mapStateToProps(state, ownProps) {
const sectionConfigKey = 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin';
const sectionConfig = state.config.sections[sectionConfigKey];
const folder = state.assetAdmin.gallery.folder;
const files = state.assetAdmin.gallery.files;
let file = null;
if (ownProps.params.fileId) {
// Calculated on the fly to avoid getting out of sync with lazy loaded fileId
file = files.find((next) => next.id === parseInt(ownProps.params.fileId, 10));
}
return {
breadcrumbs: state.breadcrumbs,
sectionConfig,
fileId: parseInt(ownProps.params.fileId, 10),
folderId: parseInt(ownProps.params.folderId, 10),
files,
file,
folder,
limit: sectionConfig.limit,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: {
gallery: bindActionCreators(galleryActions, dispatch),
editor: bindActionCreators(editorActions, dispatch),
breadcrumbsActions: bindActionCreators(breadcrumbsActions, dispatch),
},
};
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(AssetAdmin));