Opal-Estate-Pro/assets/js/frontend/uploader.js

195 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2019-09-10 06:27:33 +02:00
/* global tinymce, wpCookies, autosaveL10n, switchEditors */
// Back-compat
window.opalestate_uploader = function() {
return true;
};
/**
* @summary Adds autosave to the window object on dom ready.
*
* @since 3.9.0
*
* @param {jQuery} $ jQuery object.
* @param {window} The window object.
*
*/
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
( function( $, window ) {
/**
* @summary Auto saves the post.
*
* @since 3.9.0
*
* The object with all functions for autosave.
*/
function opalestate_uploader() {
2020-05-04 10:53:09 +02:00
$document = $( document );
function is_image_file( file ) {
2019-09-10 06:27:33 +02:00
const acceptedImageTypes = ['image/gif', 'image/jpeg', 'image/png'];
return file && acceptedImageTypes.includes(file['type'])
}
function check_number_files( file , i ) {
if( is_image_file(file) ) {
if( i+1 > opalesateJS.mfile_image ){
2020-05-04 10:53:09 +02:00
return false;
2019-09-10 06:27:33 +02:00
}
} else {
if( i+1 > opalesateJS.mfile_other ){
2020-05-04 10:53:09 +02:00
return false;
2019-09-10 06:27:33 +02:00
}
}
2020-05-04 10:53:09 +02:00
return true;
2019-09-10 06:27:33 +02:00
}
function check_filesize ( file , i ) {
if( is_image_file(file) ) {
if( file.size > opalesateJS.size_image ){
var myToast = $.toast({
heading: file.name,
text: opalesateJS.error_upload_size,
icon: 'error',
2020-05-04 10:53:09 +02:00
position: 'bottom-right',
hideAfter: 3500,
2019-09-10 06:27:33 +02:00
showHideTransition: 'fade'
});
return false;
} else {
return true;
}
} else {
2020-05-04 10:53:09 +02:00
return true;
2019-09-10 06:27:33 +02:00
}
}
/**
*
*/
function trigger_button_upload(){
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
var handleUpload = function ( _container ){
2020-05-04 10:53:09 +02:00
var file_btn = $( 'input.select-file', _container );
2019-09-10 06:27:33 +02:00
var allow_files = [];
// var all_selected = [];
var name = $(this).data( 'name' );
2020-05-04 10:53:09 +02:00
var issingle = $(_container).data('single');
2019-09-10 06:27:33 +02:00
var show_icon = $(_container).data( 'show-icon' );
var on_select_files = function ( files, _container ) {
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
if ( window.File && window.FileList && window.FileReader ) {
$(files).each( function( i, file ){
if( check_number_files( file, i+$(".uploader-item-preview",_container).length ) == false ){
return ;
2020-05-04 10:53:09 +02:00
}
2019-09-10 06:27:33 +02:00
if( check_filesize( file, i ) ) {
var picReader = new FileReader();
2020-05-04 10:53:09 +02:00
picReader.addEventListener("load", function ( event ) {
2019-09-10 06:27:33 +02:00
var input = '<div class="uploader-item-preview">';
var picFile = event.target;
if ( picFile.result ) {
if( show_icon == 1 ) {
2020-05-04 10:53:09 +02:00
input += '<div class="inner preview-icon"><span class="btn-close fa fa-close"></span><i class="fas fa-paperclip"></i> '+ file.name +' </div>';
2019-09-10 06:27:33 +02:00
} else {
2020-05-04 10:53:09 +02:00
input += '<div class="inner preview-image"><span class="btn-close' +
' fa fa-close"></span><img src="'+picFile.result+'"></div>';
2019-09-10 06:27:33 +02:00
}
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
}
2020-05-04 10:53:09 +02:00
input += '</div>';
var a = $(input) ;
2019-09-10 06:27:33 +02:00
if( issingle ){
$( ".uploader-item-preview", _container ).remove();
all_selected = [];
}
$( _container ).prepend( a );
2020-05-04 10:53:09 +02:00
a.prop( 'file', file );
2019-09-10 06:27:33 +02:00
} );
picReader.readAsDataURL( file );
}
} );
}
};
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
file_btn.on("change", function( event ){
on_select_files( event.target.files, _container, allow_files );
2020-05-04 10:53:09 +02:00
} );
2019-09-10 06:27:33 +02:00
$( _container ).on( "click", ".btn-close", function(){
if( confirm(opalesateJS.confirmed ) ){
if( $("input", $(this).parent().parent()).length ){
var rinput = $("<input type=\"hidden\" name=\"remove_image_id[]\" value=\""+ $("input", $(this).parent().parent()).val() +"\">");
$(_container).append( rinput );
}
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
$(this).parent().parent().remove();
}
} );
$( ".button-placehold", _container ).click( function(){
file_btn.trigger("click");
2020-05-04 10:53:09 +02:00
} );
2019-09-10 06:27:33 +02:00
}
$(".cmb2-uploader-files").each( function(){
handleUpload( this )
} );
// fix for submittion form
window.CMB2 = window.CMB2 || {};
window.CMB2.metabox().find('.cmb-repeatable-group').on( 'cmb2_add_row', function(i, row ) {
2020-05-04 10:53:09 +02:00
var _container = $( row );
2019-09-10 06:27:33 +02:00
if( $(".cmb2-uploader-files", _container ).length ) {
2020-05-04 10:53:09 +02:00
$( ".uploader-item-preview", _container ).remove();
2019-09-10 06:27:33 +02:00
$(".cmb2-uploader-files", _container ).each( function(){
var name = $( 'input', this ).attr('name');
$( this ).attr('data-name', name );
$(this).data( 'name', name );
handleUpload( this );
});
}
} );
}
function upload_attachments( name, files ) {
alert( name );
}
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
/**
* @summary Sets the autosave time out.
*
* Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading,
* then save to the textarea before setting initialCompareString.
* This avoids any insignificant differences between the initial textarea content and the content
* extracted from the editor.
*
* @since 3.9.0
*
* @returns {void}
*/
$document.on( 'body', function( event, editor ) {
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
}).ready( function() {
2020-05-04 10:53:09 +02:00
trigger_button_upload();
2019-09-10 06:27:33 +02:00
});
return {
2020-05-04 10:53:09 +02:00
2019-09-10 06:27:33 +02:00
};
}
/** @namespace wp */
window.wp = window.wp || {};
window.wp.opalestate_uploader = opalestate_uploader();
2020-05-04 10:53:09 +02:00
}( jQuery, window ));