diff --git a/assets/js/fe-views.js b/assets/js/fe-views.js index e4e5fdd96..4d47539ee 100644 --- a/assets/js/fe-views.js +++ b/assets/js/fe-views.js @@ -31,12 +31,95 @@ jQuery( function ( $ ) { $( '.gv-search-clear' ).on( 'click', this.clear_search ); $( 'a.gv-sort' ).on( 'click', this.multiclick_sort ); + + this.disable_upload_file_when_limit_reached(); + + this.fix_updating_files_after_edit(); this.number_range(); this.iframe(); }, + /** + * Fix the issue of updating files after edit where the previous value still exists in the uploaded field. + */ + fix_updating_files_after_edit: function(){ + $.each($('.ginput_preview_list'), function(index, element){ + if ($(element).children().length > 0) { + return true; + } + + $(element).parents('form').find('[name=gform_uploaded_files]').val(''); + }); + }, + + /** + * Fix the issue for file upload fields where the button is not disabled and show message for multi upload file. + */ + disable_upload_file_when_limit_reached: function(){ + + var checkUploaders = setInterval(function() { + if (typeof gfMultiFileUploader !== 'undefined' && gfMultiFileUploader.uploaders) { + clearInterval(checkUploaders); + $.each(gfMultiFileUploader.uploaders, function(index, uploader){ + uploader.bind('Init', function(up, params) { + var data = up.settings; + var max = data.gf_vars.max_files; + var fieldId = data.multipart_params.field_id; + var existingFilesCount = $('#preview_existing_files_'+fieldId).children().length; + var limitReached = existingFilesCount >= max; + gfMultiFileUploader.toggleDisabled(data, limitReached); + }); + + uploader.bind('FilesAdded', function(up, files) { + var data = up.settings; + var max = data.gf_vars.max_files; + var fieldId = data.multipart_params.field_id; + var formId = data.multipart_params.form_id; + var newFilesCount = $('#gform_preview_'+formId+'_'+fieldId).children().length; + var existingFilesCount = $('#preview_existing_files_'+fieldId).children().length; + var limitReached = existingFilesCount + newFilesCount >= max; + + $.each(files, function(i, file) { + if (max > 0 && existingFilesCount >= max){ + up.removeFile(file); + $('#'+file.id).remove(); + return; + } + + existingFilesCount++; + }); + + gfMultiFileUploader.toggleDisabled(data, limitReached); + + + // Only show message if max is greater than 1 + if(max <= 1){ + return true; + } + + // Check if message already exists + + if($("#" + up.settings.gf_vars.message_id).children().length > 0){ + return true; + } + $( "#" + up.settings.gf_vars.message_id ).prepend( "
" ); + // Announce errors. + setTimeout(function () { + wp.a11y.speak( $( "#" + up.settings.gf_vars.message_id ).text() ); + }, 1000 ); + + }); + + }); + } + }, 1); + }, + /** * Triggered when the search form changes * - Adds 'data-form-changed' attribute to