Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ new(uploads): revamp uploads and image uploads markup. #494

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions assets/js/shared-ui/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,53 @@
if ( file ) {
message.text( file.name );
}

});

// check whether element exist then execute js
if($( 'SUI_BODY_CLASS .sui-file-upload' ).length){
if($( 'SUI_BODY_CLASS .sui-upload--file' ).length) {
// This will trigger on file change.
$( 'SUI_BODY_CLASS .sui-file-browser input[type="file"]' ).on( 'change', function() {
var parent = $( this ).parent();
$( 'SUI_BODY_CLASS .sui-upload--browser input[type="file"]' ).on( 'change', function() {
var parent = $( this ).closest( '.sui-upload' );
var filename = $( this ).val();
var imageContainer = parent.find( '.sui-upload-image' );
var imageContainer = parent.find( '.sui-upload__image' );
if(filename) {
var lastIndex = filename.lastIndexOf( "\\" );
if ( lastIndex >= 0 ) {
filename = filename.substring(lastIndex + 1);
// To show uploaded file preview.
if( imageContainer.length ){
var reader = new FileReader();
var imagePreview = imageContainer.find( '.sui-image-preview' );
var imagePreview = imageContainer.find( '.sui-upload__image--preview' );
reader.onload = function ( e ) {
imagePreview.attr( 'style', 'background-image: url('+e.target.result+' );' );
}
reader.readAsDataURL($( this )[0].files[0]);
}
parent.find( '.sui-upload-file > span' ).text(filename);
parent.addClass( 'sui-has_file' );
parent.find( '.sui-upload__file > span' ).text(filename);
parent.addClass( 'sui-upload--has-file' );
}
} else {
if( imageContainer.length ){
var imagePreview = imageContainer.find( '.sui-image-preview' );
var imagePreview = imageContainer.find( '.sui-upload__image--preview' );
imagePreview.attr( 'style', 'background-image: url();' );
}
parent.find( '.sui-upload-file > span' ).text( '' );
parent.removeClass( 'sui-has_file' );
parent.find( '.sui-upload__file > span' ).text( '' );
parent.removeClass( 'sui-upload--has-file' );
}
});

// This will trigger on click of upload button
$( 'SUI_BODY_CLASS .sui-file-browser .sui-upload-button' ).on( 'click', function(){
$( 'SUI_BODY_CLASS .sui-upload--browser .sui-upload__button--upload' ).on( 'click', function(){
selectFile($( this ));
});

// This will trigger when user wants to remove the selected upload file
$( 'SUI_BODY_CLASS .sui-file-upload [aria-label="Remove file"]' ).on( 'click', function(){
$( 'SUI_BODY_CLASS .sui-upload--file [aria-label="Remove file"]' ).on( 'click', function(){
removeFile($( this ));
});

// This will trigger reupload of file
$( 'SUI_BODY_CLASS .sui-file-browser .sui-upload-image' ).on( 'click', function(){
$( 'SUI_BODY_CLASS .sui-upload--browser .sui-upload__image' ).on( 'click', function(){
selectFile($( this ));
});

Expand All @@ -74,7 +73,7 @@
return (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) && 'FormData' in window && 'FileReader' in window;
}();

var uploadArea = $( 'SUI_BODY_CLASS .sui-upload-button' );
var uploadArea = $( 'SUI_BODY_CLASS .sui-upload__button' );

if(isAdvancedUpload){
var droppedFiles = false;
Expand All @@ -98,18 +97,18 @@
// function to set uploaded file
const uploadedFile = function (element, file, filename){
var parent = element.closest( '.sui-upload' );
var imageContainer = parent.find( '.sui-upload-image' );
var imageContainer = parent.find( '.sui-upload__image' );
if(filename) {
if( imageContainer.length ){
var reader = new FileReader();
var imagePreview = imageContainer.find( '.sui-image-preview' );
var imagePreview = imageContainer.find( '.sui-upload__image--preview' );
reader.onload = function ( e ) {
imagePreview.attr( 'style', 'background-image: url('+e.target.result+' );' );
}
reader.readAsDataURL(file);
}
parent.find( '.sui-upload-file > span' ).text(filename);
parent.addClass( 'sui-has_file' );
parent.find( '.sui-upload__file > span' ).text(filename);
parent.addClass( 'sui-upload--has-file' );
}
}

Expand Down
Loading