From c694815eb313fe87b8d3b07ceceefb929015afa0 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 9 Dec 2022 15:20:26 +0900 Subject: [PATCH] Ajax file upload make all fuctions "afus" prefixed --- src/ajaxFileUploadSimple.js | 39 +++++++++++++++++++------------------ test/other.js | 12 ++++++------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/ajaxFileUploadSimple.js b/src/ajaxFileUploadSimple.js index fba5e46..fad3681 100755 --- a/src/ajaxFileUploadSimple.js +++ b/src/ajaxFileUploadSimple.js @@ -56,7 +56,7 @@ if (!String.prototype.format) { * @param {Object} object object to search key in * @return {Boolean} true/false if key exists in object */ -function keyInObject(key, object) +function afusKeyInObject(key, object) { return (Object.prototype.hasOwnProperty.call(object, key)) ? true : false; } @@ -68,7 +68,7 @@ function keyInObject(key, object) * @param {Number} bytes bytes in int * @return {String} string in GB/MB/KB */ -function formatBytes(bytes) +function afusFormatBytes(bytes) { var i = -1; do { @@ -83,7 +83,7 @@ function formatBytes(bytes) * prints out error messages based on data available from the browser * @param {Object} err error from try/catch block */ -function errorCatch(err) +function afusErrorCatch(err) { // for FF & Chrome if (err.stack) { @@ -261,7 +261,7 @@ function afusUploaderConfigCheck(config) // if target file is not set, major abort let empty = false; for (let ent of ['target_file', 'target_form']) { - if (!keyInObject(ent, config)) { + if (!afusKeyInObject(ent, config)) { config[ent] = ''; } if (!(typeof config[ent] === 'string' || config[ent] instanceof String)) { @@ -280,7 +280,7 @@ function afusUploaderConfigCheck(config) } let target_file = config.target_file; // maximum files allowed to upload at once - if (!keyInObject('max_files', config)) { + if (!afusKeyInObject('max_files', config)) { config.max_files = 1; } else { config.max_files = parseInt(config.max_files); @@ -291,7 +291,7 @@ function afusUploaderConfigCheck(config) } } // maximum file size allowed (in bytes) - if (!keyInObject('max_file_size', config)) { + if (!afusKeyInObject('max_file_size', config)) { config.max_file_size = 0; } else { // TODO: if has M/G/etc multiply by 1024 to bytes @@ -300,7 +300,7 @@ function afusUploaderConfigCheck(config) } config.file_accept = []; // allowed file extensions (eg jpg, gif) - if (!keyInObject('allowed_extensions', config)) { + if (!afusKeyInObject('allowed_extensions', config)) { config.allowed_extensions = []; } else { // must be array and always lower case @@ -318,7 +318,7 @@ function afusUploaderConfigCheck(config) } } // allowed file types in mime format, image/jpeg, etc - if (!keyInObject('allowed_file_types', config)) { + if (!afusKeyInObject('allowed_file_types', config)) { config.allowed_file_types = []; } else { // copy to new @@ -335,19 +335,19 @@ function afusUploaderConfigCheck(config) } } // target router for ajax submit - if (!keyInObject('target_router', config)) { + if (!afusKeyInObject('target_router', config)) { config.target_router = ''; } else { // must be string } // ajax form action target name - if (!keyInObject('target_action', config)) { + if (!afusKeyInObject('target_action', config)) { config.target_action = ''; } else { // must be string } // any additional parameters to be sent to the server - if (!keyInObject('form_parameters', config)) { + if (!afusKeyInObject('form_parameters', config)) { config.form_parameters = {}; } else if (!( typeof config.form_parameters === 'object' && @@ -357,7 +357,7 @@ function afusUploaderConfigCheck(config) config.form_parameters = {}; } // upload without confirmation step - if (!keyInObject('auto_submit', config)) { + if (!afusKeyInObject('auto_submit', config)) { config.auto_submit = false; } else if (!( config.auto_submit === false || @@ -366,7 +366,7 @@ function afusUploaderConfigCheck(config) // must be boolean config.auto_submit = false; } - if (!keyInObject('function_options', config)) { + if (!afusKeyInObject('function_options', config)) { config.function_options = {}; } else if (!( typeof config.function_options === 'object' && @@ -410,7 +410,7 @@ function afusUploaderConfigCheck(config) 'fileChange', 'fileChangeAll', 'fileRemove', 'fileClear', 'fileBeforeUploadAll', 'fileBeforeUpload', 'fileUploaded', 'fileUploadedAll', 'fileUploadError' ]) { - if (!keyInObject(fkt, config)) { + if (!afusKeyInObject(fkt, config)) { config[fkt] = ''; } AFUS_functions[target_file][fkt] = config[fkt]; @@ -418,14 +418,14 @@ function afusUploaderConfigCheck(config) // init strings for this groups AFUS_strings[target_file] = {}; // if set translation strings, set them to the AFUS string - if (keyInObject('translation', config)) { + if (afusKeyInObject('translation', config)) { // upload_start, upload_finished, too_many_files only for (var k of [ 'invalid_type', 'invalid_size', 'cancel', 'remove', 'upload_start', 'upload_finished', 'upload_cancled', 'too_many_files' ]) { - if (keyInObject(k, config.translation)) { + if (afusKeyInObject(k, config.translation)) { AFUS_strings[target_file][k] = config.translation[k]; } } @@ -505,7 +505,8 @@ function afusSupportAjaxUploadWithProgress() * {Function} [fileUploaded=''] Function called after upload has successfully finished * Parameters are target_file, file_pos, target_router, data * (object returned from upload function call) - * {Function} [fileUploadedAll=''] After all uploads have been done, this one will be called + * {Function} [fileUploadedAll=''] After all uploads have been done, + * this one will be called * Parameters are target_file, target_router * {Function} [fileUploadError=''] Function called after upload has failed * Parameters are target_file, file_pos, target_router, data @@ -724,7 +725,7 @@ function afusPassThroughEvent(target_file, max_files, target_router, auto_submit error_list.push(AFUS_strings[target_file].invalid_type || 'Invalid file type'); } if (valid_size === false) { - error_list.push((AFUS_strings[target_file].invalid_size || 'Maximum file size is {0}').format(formatBytes(AFUS_config[target_file].max_file_size))); + error_list.push((AFUS_strings[target_file].invalid_size || 'Maximum file size is {0}').format(afusFormatBytes(AFUS_config[target_file].max_file_size))); } el_sub.innerHTML = error_list.join(', '); } else { @@ -1195,7 +1196,7 @@ function afusOnReadyStateChangeHandler(target_file, file_pos, target_router, evt responseText = evt.target.responseText; status = evt.target.status; } catch(e) { - errorCatch(e); + afusErrorCatch(e); return; } // XMLHttpRequest.DONE == 4 diff --git a/test/other.js b/test/other.js index df480bf..48486cb 100644 --- a/test/other.js +++ b/test/other.js @@ -4,7 +4,7 @@ /* jshint esversion: 6 */ -/* global initAjaxUploader, keyInObject, errorCatch, showActionIndicator, hideActionIndicator, exists, phfo, aelx, cel */ +/* global initAjaxUploader, afusKeyInObject, afusErrorCatch, showActionIndicator, hideActionIndicator, exists, phfo, aelx, cel */ /** * clear the alert div and hide it @@ -30,7 +30,7 @@ function printMsg(msg) { var content = []; for (const t of msg) { // console.log('v: %s, t: %o', v, t); - if (keyInObject('code', t) && t.code != null && t.code.length > 0) { + if (afusKeyInObject('code', t) && t.code != null && t.code.length > 0) { t.str = '[' + t.code + '] ' + t.str; } content.push(phfo(cel('div', '', t.str, ['error-' + t.level]))); @@ -57,7 +57,7 @@ function printMsg(msg) { */ function ajaxWrapper(call_id, queryString = {}, control = {}, url = 'backend.php') { var no_action_indicator = false; - if (keyInObject('no_action_indicator', control)) { + if (afusKeyInObject('no_action_indicator', control)) { no_action_indicator = control.no_action_indicator ? true : false; } // if inidicator not visible, show before @@ -226,7 +226,7 @@ function fileUploadedFunction(target_file, file_pos, target_router, control_data } } } catch(err) { - errorCatch(err); + afusErrorCatch(err); } // chain action test var call_id = 'chainAction'; @@ -250,7 +250,7 @@ function fileUploadedFunction(target_file, file_pos, target_router, control_data // top message printMsg(data.msg); } catch (err) { - errorCatch(err); + afusErrorCatch(err); } }); } @@ -316,7 +316,7 @@ $(document).ready(function () { // top message printMsg(data.msg); } catch (err) { - errorCatch(err); + afusErrorCatch(err); } });