From 22baff8b13a23211b872057545a73ad35000d24e Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 30 Nov 2022 09:59:24 +0900 Subject: [PATCH] all called functions can have functions options setable during init function_options is a new object that will be passed on as the last parameter on all functions --- src/ajaxFileUploadSimple.js | 30 ++++++++++++++++++++---------- test/other.js | 11 +++++++---- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/ajaxFileUploadSimple.js b/src/ajaxFileUploadSimple.js index c081e8e..fba5e46 100755 --- a/src/ajaxFileUploadSimple.js +++ b/src/ajaxFileUploadSimple.js @@ -208,7 +208,7 @@ function afusPostUpload(target_file, file_pos, target_router, file_info, data) document.getElementById(target_file + '-uid-' + file_pos).value = file_info.file_uid; // append uid, file name, size into uploaded too if (typeof AFUS_functions[target_file].fileUploaded === 'function') { - AFUS_functions[target_file].fileUploaded(target_file, file_pos, target_router, data); + AFUS_functions[target_file].fileUploaded(target_file, file_pos, target_router, data, AFUS_config[target_file].function_options); } } @@ -366,6 +366,15 @@ function afusUploaderConfigCheck(config) // must be boolean config.auto_submit = false; } + if (!keyInObject('function_options', config)) { + config.function_options = {}; + } else if (!( + typeof config.function_options === 'object' && + config.function_options !== null + )) { + // must be object + config.function_options = {}; + } // set path name config.path = window.location.pathname; // do we end in .php, we need to remove the name then, we just want the path @@ -379,7 +388,7 @@ function afusUploaderConfigCheck(config) // write general config things into config AFUS_config[target_file] = {}; for (var ent of [ - 'target_file', 'target_form', 'path', + 'target_file', 'target_form', 'path', 'function_options', 'max_files', 'max_file_size', 'allowed_extensions', 'allowed_file_types', 'target_router', 'target_action', 'form_parameters', 'auto_submit' ]) { @@ -478,6 +487,7 @@ function afusSupportAjaxUploadWithProgress() * Added BEFORE fileBeforeUpload parameters * {Object} [translation={}] Translated strings pushed into the AFUS_strings object * {Boolean} [auto_submit=false] if we override the submit button and directly upload + * {Object} [function_options={}] Additional data to pass to functions * {Function} [fileChange=''] Function run on change of -file element entry * Parameters are target_file, file_pos, target_router * {Function} [fileChangeAll=''] Function run on change of -file element entry @@ -546,7 +556,7 @@ function initAjaxUploader(config) // eslint-disable-line document.getElementById(target_file + '-abort-all-div').style.display = 'none'; // call clear post if (typeof AFUS_functions[target_file].fileClear === 'function') { - AFUS_functions[target_file].fileClear(target_file, config.target_router); + AFUS_functions[target_file].fileClear(target_file, config.target_router, AFUS_config[target_file].function_options); } }); } @@ -733,7 +743,7 @@ function afusPassThroughEvent(target_file, max_files, target_router, auto_submit } // delete if (typeof AFUS_functions[target_file].fileRemove === 'function') { - AFUS_functions[target_file].fileRemove(target_file, i, target_router); + AFUS_functions[target_file].fileRemove(target_file, i, target_router, AFUS_config[target_file].function_options); } }); el_sub.innerHTML = AFUS_strings[target_file].remove || 'Remove'; @@ -755,12 +765,12 @@ function afusPassThroughEvent(target_file, max_files, target_router, auto_submit document.getElementById(target_file + '-upload-status').appendChild(el); // file change update per file if (typeof AFUS_functions[target_file].fileChange === 'function') { - AFUS_functions[target_file].fileChange(target_file, i, target_router); + AFUS_functions[target_file].fileChange(target_file, i, target_router, AFUS_config[target_file].function_options); } } // file change update after all files processed if (typeof AFUS_functions[target_file].fileChangeAll === 'function') { - AFUS_functions[target_file].fileChangeAll(target_file, target_router); + AFUS_functions[target_file].fileChangeAll(target_file, target_router, AFUS_config[target_file].function_options); } // updated file list render document.getElementById(target_file + '-upload-status').style.display = ''; @@ -888,7 +898,7 @@ function afusInitFullFormAjaxUpload(target_file, target_form, config) afusFinalPostUpload(target_file); // if there is a final function, call it if (typeof AFUS_functions[target_file].fileUploadedAll === 'function') { - AFUS_functions[target_file].fileUploadedAll(target_file, config.target_router, AFUS_config[target_file].all_success); + AFUS_functions[target_file].fileUploadedAll(target_file, config.target_router, AFUS_config[target_file].all_success, AFUS_config[target_file].function_options); } }); // Avoid normal form submission @@ -914,7 +924,7 @@ class afusAsyncUploader // additional data to append to the form submit (global) this.form_append = {}; if (typeof AFUS_functions[this.target_file].fileBeforeUploadAll === 'function') { - for (const [key, value] of Object.entries(AFUS_functions[this.target_file].fileBeforeUploadAll(this.target_file, AFUS_config[this.target_file].target_router))) { + for (const [key, value] of Object.entries(AFUS_functions[this.target_file].fileBeforeUploadAll(this.target_file, AFUS_config[this.target_file].target_router, AFUS_config[target_file].function_options))) { this.form_append[key] = value; } } @@ -1030,7 +1040,7 @@ function afusSendFile(target_file, file, form_append) } // external data gets added if (typeof AFUS_functions[target_file].fileBeforeUpload === 'function') { - for (const [key, value] of Object.entries(AFUS_functions[target_file].fileBeforeUpload(target_file, file_pos, AFUS_config[target_file].target_router))) { + for (const [key, value] of Object.entries(AFUS_functions[target_file].fileBeforeUpload(target_file, file_pos, AFUS_config[target_file].target_router, AFUS_config[target_file].function_options))) { formData.append(key, value); } } @@ -1210,7 +1220,7 @@ function afusOnReadyStateChangeHandler(target_file, file_pos, target_router, evt AFUS_config[target_file].all_success = false; // call per file upload error function if exsts if (typeof AFUS_functions[target_file].fileUploadError === 'function') { - AFUS_functions[target_file].fileUploadError(target_file, file_pos, target_router, responseData.content); + AFUS_functions[target_file].fileUploadError(target_file, file_pos, target_router, responseData.content, AFUS_config[target_file].function_options); } } } else { diff --git a/test/other.js b/test/other.js index 8210bac..df480bf 100644 --- a/test/other.js +++ b/test/other.js @@ -96,12 +96,14 @@ function ajaxWrapper(call_id, queryString = {}, control = {}, url = 'backend.php /** * FILE CHANGE: * helper call for ajax file upload on file selected - * @param {Number} file_pos Position in upload queue - * @param {String} target_file The file upload target prefix id + * @param {String} target_file The file upload target prefix id + * @param {Number} file_pos Position in upload queue + * @param {String} target_router [description] + * @param {Object} options additional functions options */ -function fileChangeFunction(target_file, file_pos, target_router) +function fileChangeFunction(target_file, file_pos, target_router, options={}) { - console.log('{FILE} CHANGE [%s/%s] FUNCTION CALL [%s]', target_file, file_pos, target_router); + console.log('{FILE} CHANGE [%s/%s] FUNCTION CALL [%s]: [%o]', target_file, file_pos, target_router, options); clearAlerts(); // console.log('Upload Status: %s', $('#' + target_file + '-upload-status').outerHeight()); } @@ -328,6 +330,7 @@ $(document).ready(function () { target_action: '', form_parameters: {'parameter_a': 'Value 123'}, auto_submit: false, + function_options: {action_box_id: 'actionBox-alt', 'other': 'free'}, fileChange: fileChangeFunction, fileChangeAll: fileChangeFunctionAll, fileRemove: fileRemoveFunction,