This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from a-barbieri/master
- Loading branch information
Showing
12 changed files
with
266 additions
and
266 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
90 changes: 90 additions & 0 deletions
90
app/assets/javascripts/binda/components/fileupload_custom_script.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,90 @@ | ||
|
||
export function custom_fileupload ( target ) { | ||
|
||
let $this = $( target ) | ||
$this.fileupload({ | ||
|
||
url: $this.data('url'), // This should return json, not a proper page | ||
dropZone: $this, | ||
dataType: 'json', | ||
autoUpload: true, | ||
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, | ||
maxFileSize: 999000, | ||
// Enable image resizing, except for Android and Opera, | ||
// which actually support image resizing, but fail to | ||
// send Blob objects via XHR requests: | ||
disableImageResize: /Android(?!.*Chrome)|Opera/ | ||
.test(window.navigator.userAgent), | ||
previewMaxWidth: 100, | ||
previewMaxHeight: 100, | ||
previewCrop: true | ||
|
||
}).on('fileuploadadd', function (e, data) { | ||
|
||
data.context = $this.find('.details'); | ||
$.each(data.files, function (index, file) { | ||
$this.find('.fileupload--filename').text(file.name) | ||
}); | ||
$this.find('.fileupload--details').removeClass('fileupload--details--hidden') | ||
|
||
}).on('fileuploadprocessalways', function (e, data) { | ||
|
||
var index = data.index, | ||
file = data.files[index], | ||
node = $(data.context.children()[index]); | ||
if (file.error) { | ||
node | ||
.append('<br>') | ||
.append($('<span class="text-danger"/>').text(file.error)); | ||
} | ||
if (index + 1 === data.files.length) { | ||
data.context.find('button') | ||
.text('Upload') | ||
.prop('disabled', !!data.files.error); | ||
} | ||
|
||
}).on('fileuploadprogressall', function (e, data) { | ||
|
||
var progress = parseInt(data.loaded / data.total * 100, 10); | ||
console.log(progress) | ||
$this.find('.progress .progress-bar').css( | ||
'width', | ||
progress + '%' | ||
) | ||
|
||
}).on('fileuploaddone', function (e, data) { | ||
|
||
$.each(data.result.files, function (index, file) { | ||
if (file.url) { | ||
setTimeout( function() { | ||
// remove context | ||
data.context.remove() | ||
// reset progress bar | ||
$this.find('.progress .progress-bar').css('width', '0%') | ||
// append/replace image | ||
$this.find('.form-item--asset--image').attr('src', file.url).attr('alt', file.name) | ||
$this.find('.fileupload--remove-image-btn').removeClass('invisible') | ||
}, 500 ) // this 500ms of timeout is based on a .2s CSS transition. See fileupload stylesheets | ||
setTimeout( function() { | ||
$this.find('.fileupload--details').addClass('fileupload--details--hidden') | ||
}, 300 ) | ||
} else if (file.error) { | ||
var error = $('<span class="text-danger"/>').text(file.error); | ||
$(data.context.children()[index]) | ||
.append('<br>') | ||
.append(error); | ||
} | ||
}); | ||
|
||
}).on('fileuploadfail', function (e, data) { | ||
|
||
$.each(data.files, function (index) { | ||
var error = $('<span class="text-danger"/>').text('File upload failed.'); | ||
$(data.context.children()[index]) | ||
.append('<br>') | ||
.append(error); | ||
}); | ||
|
||
}).prop('disabled', !$.support.fileInput) | ||
.parent().addClass($.support.fileInput ? undefined : 'disabled'); | ||
} |
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
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
Oops, something went wrong.