This commit is contained in:
absurdo 2023-12-08 17:41:22 +01:00
parent 75de95acc3
commit d5c3b25447
4 changed files with 296 additions and 79 deletions

View file

@ -4,9 +4,7 @@
/* Add csrf token */
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean */
$.fn.options={};
/* options: url: url to post, loading: dom id, success: func, error_data: func, pre_callback, separated_data:boolean, upload: {progressbar: '#progressbar', 'total_loader': '#total_loader', 'status': '#status'} */
$.fn.sendPost = function (options)
{
@ -25,58 +23,61 @@
}
this.options=options;
var xhr=false;
$(this).on('submit', {url: this.options.url}, function (e) {
if(options.hasOwnProperty("upload")) {
xhr=function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
var progressbar=options.upload.progressbar;
var total_loader=options.upload.total_loader;
var status=options.upload.status;
$(total_loader).html('');
$(progressbar).attr('value', 0);
$(status).html('');
myXhr.upload.addEventListener('progress',function (event) {
$(total_loader).html("Uploaded "+event.loaded+" bytes of "+event.total);
var percent = (event.loaded / event.total) * 100;
$(progressbar).attr('value', (Math.round(percent)));
$(status).html(Math.round(percent)+"% uploaded... please wait");
}, false);
myXhr.addEventListener("load", function(event) {
$(status).html("File loaded successfully!!");
}, false);
}
return myXhr;
}
}
$(this).on('submit', function (e) {
//Get data of form
form=this;
/*if($(this).attr('enctype'))
{*/
if(options.separated_data)
{
data[options.separated_data]=new FormData($(this)[0]);
}
else
{
data=new FormData($(this)[0]);
}
xhrFields={
withCredentials: true
};
/*}
if(options.separated_data)
{
data[options.separated_data]=new FormData($(this)[0]);
}
else
{
if(options.separated_data)
{
data=new FormData($(this)[0]);
}
xhrFields={
withCredentials: true
};
data[options.separated_data]=$(this).serializeArray();
}
else
{
data=$(this).serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
}
xhrFields={
};
}*/
if(!data.hasOwnProperty("csrf_token")) {
@ -87,11 +88,8 @@
//Hide form and show the time icon
$(this).find('.error').hide();
//$(this).hide();
$(options.loading).show();
/*$(this).find('input').prop("disabled", true);
$(this).find('select').prop("disabled", true);*/
$(options.loading).show();
if(options.hasOwnProperty("pre_callback")) {
@ -100,21 +98,20 @@
}
//Ajax
$.ajax({
ajax_post={
type: "POST",
url: options.url,
data: data,
encoding: "UTF-8",
xhrFields: xhrFields,
cache:false,
contentType: false,
contentType:false,
processData: false,
success: function (data) {
$(options.loading).hide();
if(data.error) {
if(!data.hasOwnProperty("error_csrf"))
@ -134,7 +131,7 @@
if(data.hasOwnProperty('error_form'))
{
for(i in data.error_form) {
$(i).html(data.error_form[i]);
$(i).show();
@ -172,8 +169,6 @@
else
{
// If ok then post successful task.
if(options.hasOwnProperty("success")) {
@ -200,14 +195,22 @@
},
dataType: 'json'
});
};
//console.log(xhr);
if(xhr!=false) {
ajax_post.xhr=xhr;
}
$.ajax(ajax_post);
return false;
});
return this;
}
})( jQuery );