Class: H_ajax

H_ajax

AJAX utility class for managing multiple concurrent AJAX requests, file uploads (including chunked uploads), progress tracking.

Provides helpers for form serialization, file validation, and optionnaly TinyMCE image upload integration.
We do not use javascript File API for a simple reason : we can't follow upload progress.
Plus you can use async() await on succes or error if you need to do promisse like.
This class is globaly instanciated as h.a but can be also found in h.libs.ajax.

This class will use H_ajax_sender to create/manipulate the xhr object.
And if there was some input files, formadata or blob sended, H.ajax_file will also be used.

new H_ajax(){Mixed}

ajax.js, line 28
See:
Returns:
Type Description
Mixed content depending module return
Example
basic usage :
automaticaly used by forms generated by helPHP server libs (h::form), you can create an ajax request like this:
---------------------------------------------------------------
     let settings = {}; //our array of params 
     settings.url = ''; // the url to send you ajax request
     settings.data = { //the data key/pairs you want to send
         'param1':'value1' ,
         'param2':'value2' ,
         'param3':'value3',
         'action':'moduleaction'
         };
     settings.success = (res) => { //what to do with the result, set a callback etc...
         do something with res;
         };
     settings.error = (err) => { //what to do in case of error...
             do something with err;
             debugger;
         };

     h.a.send(settings); //we send our request 
-----------------------------------------------------------------

Methods

compute_progress()

ajax.js, line 406
Computes the global progress for all running AJAX streams.

form_to_obj(form){Object}

ajax.js, line 100
Converts all inputs in a form to a JSON object.
Name Type Description
form HTMLFormElement The form element to serialize.
Returns:
Type Description
Object The serialized form data as an object.

get_sender(){H_ajax_sender}

ajax.js, line 284
Gets the first available XHR sender or creates a new one.
Returns:
Type Description
H_ajax_sender The sender instance.

queue(sender)

ajax.js, line 344
Queues a sender for later execution.
Name Type Description
sender H_ajax_sender The sender to queue.

readable_file_size(size){string}

ajax.js, line 145
Converts a raw byte size into a human-readable string.
Name Type Description
size number The size in bytes.
Returns:
Type Description
string Human-readable file size.

send(settings, delay, queue){H_ajax_sender|boolean}

ajax.js, line 319
Sends data according to the provided settings.
Name Type Description
settings Object AJAX settings.
delay number Optional delay before sending.
queue boolean Whether to queue the request.
Returns:
Type Description
H_ajax_sender | boolean The sender or false if queued.

staticH_ajax.check_file_data(dom_element){boolean}

ajax.js, line 161
Checks if the file inputs respect the server limitations.
Name Type Description
dom_element HTMLElement | FormData The DOM element or FormData to check.
Returns:
Type Description
boolean True if valid, false otherwise.

staticH_ajax.sleep(ms){Promise.<void>}

ajax.js, line 277
Sleep utility function (async). can be used to async / wait,
Name Type Description
ms number Milliseconds to sleep.
Returns:
Type Description
Promise.<void> Promise that resolves after the delay.
Example
await h.a.sleep(5000);
or
await H_anim.sleep(5000);

staticH_ajax.submit_on_change(selectDomElement){boolean}

ajax.js, line 129
Submits a form when a select element changes.
Name Type Description
selectDomElement HTMLElement The select element.
Returns:
Type Description
boolean True if submitted, false otherwise.

staticH_ajax.tinymce_image_upload_handler(blobInfo, success, failure)

ajax.js, line 228
TinyMCE image upload handler for AJAX image uploads. called by Tinymce.php lib optionnaly
Name Type Description
blobInfo Object TinyMCE blobInfo object.
success function Success callback.
failure function Failure callback.