-
Notifications
You must be signed in to change notification settings - Fork 479
/
ImagePicker.jsx
142 lines (129 loc) · 4.01 KB
/
ImagePicker.jsx
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
import PropTypes from 'prop-types';
import React from 'react';
import {getStore} from '@cdo/apps/redux';
import AssetManager from './AssetManager';
import color from '../../util/color';
import IconLibrary from './IconLibrary';
import {ICON_PREFIX} from '@cdo/apps/applab/constants';
const extensionFilter = {
// Note: .jfif files will be converted to .jpg by the server.
image: '.jpg, .jpeg, .jfif, .gif, .png',
audio: '.mp3',
document: '.jpg, .jpeg, .gif, .png, .pdf, .doc, .docx'
};
/**
* A component for managing hosted assets.
*/
export default class ImagePicker extends React.Component {
static propTypes = {
assetChosen: PropTypes.func,
assetsChanged: PropTypes.func,
typeFilter: PropTypes.string,
uploadsEnabled: PropTypes.bool.isRequired,
showUnderageWarning: PropTypes.bool.isRequired,
useFilesApi: PropTypes.bool,
soundPlayer: PropTypes.object,
disableAudioRecording: PropTypes.bool,
//For logging purposes
projectId: PropTypes.string,
elementId: PropTypes.string
};
state = {mode: 'files'};
getAssetNameWithPrefix = icon => {
this.props.assetChosen(ICON_PREFIX + icon);
};
setIconMode = () => this.setState({mode: 'icons'});
setFileMode = () => this.setState({mode: 'files'});
render() {
const isFileMode = this.state.mode === 'files';
const styles = {
root: {
margin: '0 0 0 5px'
},
fileModeToggle: {
float: 'left',
margin: '0 20px 0 0',
fontFamily: isFileMode ? '"Gotham 5r"' : null,
color: isFileMode ? null : '#999',
fontSize: '16px',
cursor: 'pointer'
},
iconModeToggle: {
margin: 0,
fontSize: '16px',
fontFamily: isFileMode ? null : '"Gotham 5r"',
color: isFileMode ? '#999' : null,
cursor: 'pointer'
},
divider: {
borderColor: color.purple,
margin: '5px 0'
},
warning: {
color: color.red,
fontSize: 13,
fontWeight: 'bold'
}
};
let modeSwitch,
title = this.props.assetChosen ? (
<p className="dialog-title">Choose Assets</p>
) : (
<p className="dialog-title">Manage Assets</p>
);
const imageTypeFilter =
!this.props.typeFilter || this.props.typeFilter === 'image';
if (this.props.assetChosen && imageTypeFilter) {
modeSwitch = (
<div id="modeSwitch">
<p onClick={this.setFileMode} style={styles.fileModeToggle}>
My Files
</p>
<p onClick={this.setIconMode} style={styles.iconModeToggle}>
Icons
</p>
<hr style={styles.divider} />
</div>
);
}
const disableAudio =
this.props.disableAudioRecording || !!this.props.assetChosen;
const reduxState = getStore().getState();
let levelName, isStartMode;
if (reduxState && reduxState.level) {
levelName = reduxState.level.name;
isStartMode = reduxState.level.isStartMode;
}
const body =
!this.props.assetChosen || this.state.mode === 'files' ? (
<AssetManager
assetChosen={this.props.assetChosen}
assetsChanged={this.props.assetsChanged}
allowedExtensions={extensionFilter[this.props.typeFilter]}
uploadsEnabled={this.props.uploadsEnabled}
useFilesApi={this.props.useFilesApi}
projectId={this.props.projectId}
soundPlayer={this.props.soundPlayer}
disableAudioRecording={disableAudio}
imagePicker={true}
elementId={this.props.elementId}
levelName={levelName}
isStartMode={isStartMode}
/>
) : (
<IconLibrary assetChosen={this.getAssetNameWithPrefix} />
);
return (
<div className="modal-content" style={styles.root}>
{title}
{this.props.showUnderageWarning && (
<p style={styles.warning}>
Warning: Do not upload anything that contains personal information.
</p>
)}
{modeSwitch}
{body}
</div>
);
}
}