Ajax file upload make all fuctions "afus" prefixed

This commit is contained in:
2022-12-09 15:20:26 +09:00
parent 22baff8b13
commit c694815eb3
2 changed files with 26 additions and 25 deletions

View File

@@ -56,7 +56,7 @@ if (!String.prototype.format) {
* @param {Object} object object to search key in * @param {Object} object object to search key in
* @return {Boolean} true/false if key exists in object * @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; return (Object.prototype.hasOwnProperty.call(object, key)) ? true : false;
} }
@@ -68,7 +68,7 @@ function keyInObject(key, object)
* @param {Number} bytes bytes in int * @param {Number} bytes bytes in int
* @return {String} string in GB/MB/KB * @return {String} string in GB/MB/KB
*/ */
function formatBytes(bytes) function afusFormatBytes(bytes)
{ {
var i = -1; var i = -1;
do { do {
@@ -83,7 +83,7 @@ function formatBytes(bytes)
* prints out error messages based on data available from the browser * prints out error messages based on data available from the browser
* @param {Object} err error from try/catch block * @param {Object} err error from try/catch block
*/ */
function errorCatch(err) function afusErrorCatch(err)
{ {
// for FF & Chrome // for FF & Chrome
if (err.stack) { if (err.stack) {
@@ -261,7 +261,7 @@ function afusUploaderConfigCheck(config)
// if target file is not set, major abort // if target file is not set, major abort
let empty = false; let empty = false;
for (let ent of ['target_file', 'target_form']) { for (let ent of ['target_file', 'target_form']) {
if (!keyInObject(ent, config)) { if (!afusKeyInObject(ent, config)) {
config[ent] = ''; config[ent] = '';
} }
if (!(typeof config[ent] === 'string' || config[ent] instanceof String)) { if (!(typeof config[ent] === 'string' || config[ent] instanceof String)) {
@@ -280,7 +280,7 @@ function afusUploaderConfigCheck(config)
} }
let target_file = config.target_file; let target_file = config.target_file;
// maximum files allowed to upload at once // maximum files allowed to upload at once
if (!keyInObject('max_files', config)) { if (!afusKeyInObject('max_files', config)) {
config.max_files = 1; config.max_files = 1;
} else { } else {
config.max_files = parseInt(config.max_files); config.max_files = parseInt(config.max_files);
@@ -291,7 +291,7 @@ function afusUploaderConfigCheck(config)
} }
} }
// maximum file size allowed (in bytes) // maximum file size allowed (in bytes)
if (!keyInObject('max_file_size', config)) { if (!afusKeyInObject('max_file_size', config)) {
config.max_file_size = 0; config.max_file_size = 0;
} else { } else {
// TODO: if has M/G/etc multiply by 1024 to bytes // TODO: if has M/G/etc multiply by 1024 to bytes
@@ -300,7 +300,7 @@ function afusUploaderConfigCheck(config)
} }
config.file_accept = []; config.file_accept = [];
// allowed file extensions (eg jpg, gif) // allowed file extensions (eg jpg, gif)
if (!keyInObject('allowed_extensions', config)) { if (!afusKeyInObject('allowed_extensions', config)) {
config.allowed_extensions = []; config.allowed_extensions = [];
} else { } else {
// must be array and always lower case // must be array and always lower case
@@ -318,7 +318,7 @@ function afusUploaderConfigCheck(config)
} }
} }
// allowed file types in mime format, image/jpeg, etc // 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 = []; config.allowed_file_types = [];
} else { } else {
// copy to new // copy to new
@@ -335,19 +335,19 @@ function afusUploaderConfigCheck(config)
} }
} }
// target router for ajax submit // target router for ajax submit
if (!keyInObject('target_router', config)) { if (!afusKeyInObject('target_router', config)) {
config.target_router = ''; config.target_router = '';
} else { } else {
// must be string // must be string
} }
// ajax form action target name // ajax form action target name
if (!keyInObject('target_action', config)) { if (!afusKeyInObject('target_action', config)) {
config.target_action = ''; config.target_action = '';
} else { } else {
// must be string // must be string
} }
// any additional parameters to be sent to the server // any additional parameters to be sent to the server
if (!keyInObject('form_parameters', config)) { if (!afusKeyInObject('form_parameters', config)) {
config.form_parameters = {}; config.form_parameters = {};
} else if (!( } else if (!(
typeof config.form_parameters === 'object' && typeof config.form_parameters === 'object' &&
@@ -357,7 +357,7 @@ function afusUploaderConfigCheck(config)
config.form_parameters = {}; config.form_parameters = {};
} }
// upload without confirmation step // upload without confirmation step
if (!keyInObject('auto_submit', config)) { if (!afusKeyInObject('auto_submit', config)) {
config.auto_submit = false; config.auto_submit = false;
} else if (!( } else if (!(
config.auto_submit === false || config.auto_submit === false ||
@@ -366,7 +366,7 @@ function afusUploaderConfigCheck(config)
// must be boolean // must be boolean
config.auto_submit = false; config.auto_submit = false;
} }
if (!keyInObject('function_options', config)) { if (!afusKeyInObject('function_options', config)) {
config.function_options = {}; config.function_options = {};
} else if (!( } else if (!(
typeof config.function_options === 'object' && typeof config.function_options === 'object' &&
@@ -410,7 +410,7 @@ function afusUploaderConfigCheck(config)
'fileChange', 'fileChangeAll', 'fileRemove', 'fileClear', 'fileBeforeUploadAll', 'fileChange', 'fileChangeAll', 'fileRemove', 'fileClear', 'fileBeforeUploadAll',
'fileBeforeUpload', 'fileUploaded', 'fileUploadedAll', 'fileUploadError' 'fileBeforeUpload', 'fileUploaded', 'fileUploadedAll', 'fileUploadError'
]) { ]) {
if (!keyInObject(fkt, config)) { if (!afusKeyInObject(fkt, config)) {
config[fkt] = ''; config[fkt] = '';
} }
AFUS_functions[target_file][fkt] = config[fkt]; AFUS_functions[target_file][fkt] = config[fkt];
@@ -418,14 +418,14 @@ function afusUploaderConfigCheck(config)
// init strings for this groups // init strings for this groups
AFUS_strings[target_file] = {}; AFUS_strings[target_file] = {};
// if set translation strings, set them to the AFUS string // 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 // upload_start, upload_finished, too_many_files only
for (var k of [ for (var k of [
'invalid_type', 'invalid_size', 'cancel', 'remove', 'invalid_type', 'invalid_size', 'cancel', 'remove',
'upload_start', 'upload_finished', 'upload_cancled', 'upload_start', 'upload_finished', 'upload_cancled',
'too_many_files' 'too_many_files'
]) { ]) {
if (keyInObject(k, config.translation)) { if (afusKeyInObject(k, config.translation)) {
AFUS_strings[target_file][k] = config.translation[k]; AFUS_strings[target_file][k] = config.translation[k];
} }
} }
@@ -505,7 +505,8 @@ function afusSupportAjaxUploadWithProgress()
* {Function} [fileUploaded=''] Function called after upload has successfully finished * {Function} [fileUploaded=''] Function called after upload has successfully finished
* Parameters are target_file, file_pos, target_router, data * Parameters are target_file, file_pos, target_router, data
* (object returned from upload function call) * (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 * Parameters are target_file, target_router
* {Function} [fileUploadError=''] Function called after upload has failed * {Function} [fileUploadError=''] Function called after upload has failed
* Parameters are target_file, file_pos, target_router, data * 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'); error_list.push(AFUS_strings[target_file].invalid_type || 'Invalid file type');
} }
if (valid_size === false) { 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(', '); el_sub.innerHTML = error_list.join(', ');
} else { } else {
@@ -1195,7 +1196,7 @@ function afusOnReadyStateChangeHandler(target_file, file_pos, target_router, evt
responseText = evt.target.responseText; responseText = evt.target.responseText;
status = evt.target.status; status = evt.target.status;
} catch(e) { } catch(e) {
errorCatch(e); afusErrorCatch(e);
return; return;
} }
// XMLHttpRequest.DONE == 4 // XMLHttpRequest.DONE == 4

View File

@@ -4,7 +4,7 @@
/* jshint esversion: 6 */ /* 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 * clear the alert div and hide it
@@ -30,7 +30,7 @@ function printMsg(msg) {
var content = []; var content = [];
for (const t of msg) { for (const t of msg) {
// console.log('v: %s, t: %o', v, t); // 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; t.str = '[' + t.code + '] ' + t.str;
} }
content.push(phfo(cel('div', '', t.str, ['error-' + t.level]))); 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') { function ajaxWrapper(call_id, queryString = {}, control = {}, url = 'backend.php') {
var no_action_indicator = false; 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; no_action_indicator = control.no_action_indicator ? true : false;
} }
// if inidicator not visible, show before // if inidicator not visible, show before
@@ -226,7 +226,7 @@ function fileUploadedFunction(target_file, file_pos, target_router, control_data
} }
} }
} catch(err) { } catch(err) {
errorCatch(err); afusErrorCatch(err);
} }
// chain action test // chain action test
var call_id = 'chainAction'; var call_id = 'chainAction';
@@ -250,7 +250,7 @@ function fileUploadedFunction(target_file, file_pos, target_router, control_data
// top message // top message
printMsg(data.msg); printMsg(data.msg);
} catch (err) { } catch (err) {
errorCatch(err); afusErrorCatch(err);
} }
}); });
} }
@@ -316,7 +316,7 @@ $(document).ready(function () {
// top message // top message
printMsg(data.msg); printMsg(data.msg);
} catch (err) { } catch (err) {
errorCatch(err); afusErrorCatch(err);
} }
}); });