|
1 | | -import React, { PropTypes } from 'react' |
| 1 | +import React, {PropTypes} from 'react' |
2 | 2 | import _ from 'lodash' |
3 | | -import filepicker from 'filepicker-js' |
| 3 | +import * as filepicker from 'filestack-js' |
| 4 | + |
4 | 5 | require('./FilePicker.scss') |
5 | 6 |
|
6 | 7 | class FilePicker extends React.Component { |
7 | | - constructor(props) { |
8 | | - super(props) |
9 | | - this.state = { dragText : props.options.dragText } |
10 | | - this.onChange = this.onChange.bind(this) |
11 | | - } |
12 | | - |
13 | | - onChange(event) { |
14 | | - this.props.onSuccess(this.props.options.multiple ? event.fpfiles : event.fpfile) |
15 | | - } |
16 | | - |
17 | | - componentWillReceiveProps(nextProps) { |
18 | | - this.setState({ dragText : nextProps.options.dragText }) |
19 | | - } |
20 | | - |
21 | | - componentDidMount() { |
22 | | - const filepickerElement = this.refs.filepicker |
23 | | - const filepickerButton = this.refs.filepickerButton |
24 | | - const filepickerProgress = this.refs.filepickerProgress |
25 | | - filepicker.setKey(this.props.apiKey) |
26 | | - const opts = _.assign({}, this.props.options) |
27 | | - opts.container = opts.storeContainer |
28 | | - opts.dragEnter = () => { |
29 | | - this.setState({ dragText : 'Drop to upload'}) |
30 | | - filepickerElement.classList.add('drag-entered') |
| 8 | + constructor(props) { |
| 9 | + super(props) |
| 10 | + this.state = {dragText: props.options.dragText} |
| 11 | + this.onChange = this.onChange.bind(this) |
31 | 12 | } |
32 | | - opts.dragLeave = () => { |
33 | | - this.setState({ dragText : opts.dragText }) |
34 | | - filepickerElement.classList.remove('drag-entered') |
| 13 | + |
| 14 | + onChange(event) { |
| 15 | + this.props.onSuccess(this.props.options.multiple ? event.fpfiles : event.fpfile) |
35 | 16 | } |
36 | | - opts.onSuccess = (files) => { |
37 | | - this.setState({ dragText : opts.dragText }) |
38 | | - filepickerElement.classList.remove('in-progress') |
39 | | - this.props.onSuccess(this.props.options.multiple ? files : files[0]) |
| 17 | + |
| 18 | + componentWillReceiveProps(nextProps) { |
| 19 | + this.setState({dragText: nextProps.options.dragText}) |
40 | 20 | } |
41 | | - opts.onError = () => { |
42 | | - filepickerElement.classList.remove('in-progress') |
43 | | - this.setState({ dragText : opts.dragText }) |
| 21 | + |
| 22 | + componentDidMount() { |
| 23 | + |
| 24 | + const filepickerElement = this.refs.filepicker |
| 25 | + const filepickerButton = this.refs.filepickerButton |
| 26 | + const filepickerProgress = this.refs.filepickerProgress |
| 27 | + |
| 28 | + const apikey = this.props.apiKey |
| 29 | + const clientOptions = {} |
| 30 | + if (this.props.options.cname) { clientOptions.cname = this.props.options.cname } |
| 31 | + const client = filepicker.init(apikey, clientOptions) |
| 32 | + |
| 33 | + const opts = {} |
| 34 | + opts.displayMode = 'dropPane' |
| 35 | + opts.container = 'filepicker-drag-drop-pane' |
| 36 | + opts.maxFiles = 4 |
| 37 | + |
| 38 | + opts.storeTo = {} |
| 39 | + opts.storeTo.container = this.props.options.storeContainer |
| 40 | + opts.storeTo.region = 'us-east-1' |
| 41 | + |
| 42 | + opts.dropPane = {} |
| 43 | + opts.dropPane.customText = ' ' |
| 44 | + opts.dropPane.overlay = false |
| 45 | + opts.dropPane.showIcon = false |
| 46 | + opts.dropPane.disableClick = true |
| 47 | + opts.dropPane.onDragEnter = () => { |
| 48 | + this.setState({dragText: 'Drop to upload'}) |
| 49 | + filepickerElement.classList.add('drag-entered') |
| 50 | + } |
| 51 | + opts.dropPane.onDragLeave = () => { |
| 52 | + this.setState({dragText: this.props.options.dragText}) |
| 53 | + filepickerElement.classList.remove('drag-entered') |
| 54 | + } |
| 55 | + opts.dropPane.onSuccess = (files) => { |
| 56 | + this.setState({dragText: this.props.options.dragText}) |
| 57 | + filepickerElement.classList.remove('in-progress') |
| 58 | + this.props.onSuccess(this.props.options.multiple ? files : files[0]) |
| 59 | + } |
| 60 | + opts.dropPane.onError = () => { |
| 61 | + filepickerElement.classList.remove('in-progress') |
| 62 | + this.setState({dragText: this.props.options.dragText}) |
| 63 | + } |
| 64 | + opts.dropPane.onProgress = (percentage) => { |
| 65 | + filepickerElement.classList.remove('drag-entered') |
| 66 | + filepickerElement.classList.add('in-progress') |
| 67 | + filepickerProgress.style.width = percentage + '%' |
| 68 | + } |
| 69 | + opts.dropPane.onClick = () => { |
| 70 | + const overlayOpts = {} |
| 71 | + overlayOpts.maxFiles = opts.maxFiles |
| 72 | + overlayOpts.uploadInBackground = false |
| 73 | + overlayOpts.onFileUploadFinished = (files) => { |
| 74 | + this.props.onSuccess(this.props.options.multiple ? files : files[0]) |
| 75 | + } |
| 76 | + overlayOpts.storeTo = opts.storeTo |
| 77 | + overlayOpts.fromSources = this.props.options.fromSources |
| 78 | + client.picker(overlayOpts).open() |
| 79 | + } |
| 80 | + |
| 81 | + client.picker(opts).open(); |
| 82 | + |
| 83 | + filepickerElement.addEventListener('change', this.onChange, false) |
44 | 84 | } |
45 | | - opts.onProgress = (percentage) => { |
46 | | - filepickerElement.classList.remove('drag-entered') |
47 | | - filepickerElement.classList.add('in-progress') |
48 | | - filepickerProgress.style.width = percentage + '%' |
| 85 | + |
| 86 | + componentWillUnmount() { |
| 87 | + this.refs.filepicker.removeEventListener('change', this.onChange, false) |
| 88 | + } |
| 89 | + |
| 90 | + render() { |
| 91 | + const {mode, options} = this.props |
| 92 | + const {dragText} = this.state |
| 93 | + |
| 94 | + // add data-fp- prefix to all keys |
| 95 | + const opts = _.mapKeys(options, (v, k) => { |
| 96 | + const hyphenated = k.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase() |
| 97 | + return `data-fp-${hyphenated}` |
| 98 | + }) |
| 99 | + return ( |
| 100 | + <div ref="filepicker" className="filepicker"> |
| 101 | + <input type={mode} onChange={this.onChange} {...opts}/> |
| 102 | + <div className="filepicker-drag-drop-pane" id="filepicker-drag-drop-pane" {...opts}> |
| 103 | + <span className="filepicker-drag-drop-text">{ dragText }</span> |
| 104 | + <button type="button" className="tc-btn tc-btn-secondary tc-btn-sm">Add File</button> |
| 105 | + </div> |
| 106 | + <div className="filepicker-progress" ref="filepickerProgress"></div> |
| 107 | + </div> |
| 108 | + ) |
49 | 109 | } |
50 | | - filepicker.makeDropPane(filepickerElement, opts) |
51 | | - filepicker.constructWidget(filepickerButton) |
52 | | - filepickerElement.addEventListener('change', this.onChange, false) |
53 | | - } |
54 | | - |
55 | | - componentWillUnmount() { |
56 | | - this.refs.filepicker.removeEventListener('change', this.onChange, false) |
57 | | - } |
58 | | - |
59 | | - render() { |
60 | | - const { mode, options } = this.props |
61 | | - const { dragText } = this.state |
62 | | - |
63 | | - // add data-fp- prefix to all keys |
64 | | - const opts = _.mapKeys(options, (v, k) => { |
65 | | - const hyphenated = k.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase() |
66 | | - return `data-fp-${hyphenated}` |
67 | | - }) |
68 | | - return ( |
69 | | - <div ref="filepicker" className="filepicker"> |
70 | | - <input type={mode} onChange={this.onChange} {...opts} /> |
71 | | - <div className="filepicker-drag-drop-pane"> |
72 | | - <span className="filepicker-drag-drop-text">{ dragText }</span> |
73 | | - <input type="filepicker" ref="filepickerButton" className="filepicker-picker" {...opts} /> |
74 | | - </div> |
75 | | - <div className="filepicker-progress" ref="filepickerProgress"></div> |
76 | | - </div> |
77 | | - ) |
78 | | - } |
79 | 110 | } |
80 | 111 |
|
81 | 112 | FilePicker.propTypes = { |
82 | | - apiKey: PropTypes.string.isRequired, |
83 | | - mode: PropTypes.string.isRequired, |
84 | | - options: PropTypes.object.isRequired, |
85 | | - onSuccess: PropTypes.func.isRequired |
| 113 | + apiKey: PropTypes.string.isRequired, |
| 114 | + mode: PropTypes.string.isRequired, |
| 115 | + options: PropTypes.object.isRequired, |
| 116 | + onSuccess: PropTypes.func.isRequired |
86 | 117 | } |
87 | 118 |
|
88 | 119 | export default FilePicker |
0 commit comments