-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert attachment input to react component
- Loading branch information
Murilo Varela
committed
Jul 14, 2017
1 parent
cdf8e46
commit ff8c292
Showing
3 changed files
with
124 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
app/assets/javascripts/components/story/StoryAttachment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react'; | ||
|
||
class StoryAttachment extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.saveInput = (input) => { this.filesInput = input }; | ||
this.random = (Math.floor(Math.random() * 10000) + 1); | ||
this.progressElementId = "documents_progress_" + this.random; | ||
this.finishedElementId = "documents_finished_" + this.random; | ||
this.attachinaryContainerId = "attachinary_container_" + this.random; | ||
} | ||
|
||
componentDidMount() { | ||
const $filesInput = $(this.filesInput); | ||
$filesInput.off('fileuploadprogressall'); | ||
$filesInput.on('fileuploadprogressall', (function(_this, _progressElementId, _finishedElementId) { | ||
return function(e, data) { | ||
var $progress = $('#' + _progressElementId); | ||
if ( $progress.is(":hidden") ) { | ||
$progress.show(); | ||
} | ||
|
||
var progress = parseInt(data.loaded / data.total * 100, 10); | ||
$progress.css('width', progress + "%"); | ||
|
||
if (progress == 100) { | ||
$progress.css('width', "1px"); | ||
$progress.hide(); | ||
|
||
$('#' + _finishedElementId).show(); | ||
} | ||
}; | ||
})(this, this.progressElementId, this.finishedElementId)); | ||
} | ||
|
||
renderAttachmentInput() { | ||
const { name, attachinaryProps } = this.props; | ||
let attachinary = attachinaryProps( | ||
'documents', | ||
this.progressElementId, | ||
this.finishedElementId, | ||
this.attachinaryContainerId | ||
); | ||
|
||
return( | ||
<input | ||
type='file' | ||
name={name} | ||
className='attachinary-input' | ||
ref={this.saveInput} | ||
multiple={attachinary.multiple} | ||
data-attachinary={attachinary.dataAttachinary} | ||
data-form-data={attachinary.dataFormData} | ||
data-url={attachinary.dataUrl} | ||
/> | ||
); | ||
} | ||
|
||
renderProgressBar() { | ||
return( | ||
<div | ||
id={this.progressElementId} | ||
className='attachinary_progress_bar' | ||
/> | ||
); | ||
} | ||
|
||
render() { | ||
const { name, isReadonly } = this.props; | ||
return( | ||
<div className="uploads"> | ||
<label htmlFor={name}>{ I18n.t(`story.${name}`) }</label> | ||
{ (!isReadonly) ? this.renderAttachmentInput() : null } | ||
{ (!isReadonly) ? this.renderProgressBar() : null } | ||
<div id={this.attachinaryContainerId} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default StoryAttachment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters