ajax.js
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
$(document).ready(function () {
$(".subm2").on('click', function (obj) {
if ($(this).children("i").length > 0) {
$(this).children("i").removeClass();
$(this).children("i").addClass('fa fa-spin fa-refresh');
} else {
$(this).prepend('<i class="fa fa-spin fa-refresh" aria-hidden="true"></i> ');
}
});
$(".subm3").on('click', function (obj) {
if ($(this).children("i").length > 0) {
// $(this).children("i").removeClass();
$(this).children("i").addClass('animate__animated animate__headShake animate__infinite');
} else {
$(this).prepend('<i class="fa fa-spin fa-refresh" aria-hidden="true"></i> ');
}
});
$(".subm4").on('click', function (obj) {
if ($(this).children("i").length > 0) {
$(this).children("i").removeClass();
$(this).children("i").addClass('fa fa-spin fa-refresh text-red');
$(this).children("i").css('width','auto');
} else {
$(this).prepend('<i class="fa fa-spin fa-refresh" aria-hidden="true"></i> ');
}
});
$(".subm5").on('click', function (obj) {
if ($(this).parent("div").length > 0) {
$(this).parent("div").addClass('animate__animated animate__shakeY animate__infinite');
}
});
$(window).keydown(function (event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
function postMyForm(postURL, dataObj, cbfunc, FuncParamArr, postMethod) {
FuncParamArr = FuncParamArr || [];
postMethod = postMethod || 'GET';
var pageName = postURL;
$.ajax({
type: postMethod,
timeout: 200000,
url: pageName,
//data: parameters,
data: dataObj,
beforeSend: function () { },
success: function (msg) {
if ($(".subm").length > 0) {
if (typeof (btnText) !== 'undefined') {
$(".subm").html(btnText);
} else {
$(".subm").html('Submit');
}
$("#loader_div").hide();
$(".subm").attr('disabled', false);
}
if (isJson(msg) == false) { alert('ERROR::' + msg); $("#loader_div").hide(); return false; }
obj = JSON.parse(msg);
if (obj.success == 'done') {
cbfunc(FuncParamArr,obj);
}
else {
Swal.fire({
icon: 'error',
title: 'Error!',
timer: 2000,
text: obj.errormsg,
footer: ''
});
//RR location.reload();
}
},
error: function (jqXHR, textStatus, errorThrown) {
if ($(".subm").length > 0) {
if (typeof (btnText) !== 'undefined') {
$(".subm").html(btnText);
} else {
$(".subm").html('Submit');
}
$("#loader_div").hide();
$(".subm").attr('disabled', false);
}
if (errorThrown == 'Unauthorized') {
Swal.fire({
icon: 'error',
title: 'ERROR!',
text: 'You are not logged in!',
footer: '<a href="' + baseURL + 'login">Click here</a> to login'
});
return false;
}
if (textStatus === "timeout") {
alert("ERROR: Connection problem"); //Handle the timeout
} else {
alert("ERROR: There is something wrong.");
}
//RR location.reload();
}
});
}
function postMyFormObj(postURL, dataObj, cbfunc, FuncParamArr, postMethod) {
FuncParamArr = FuncParamArr || [];
postMethod = postMethod || 'GET';
var pageName = postURL;
var btnText = $('.subm').html();
$(".subm").html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i> Processing');
$.ajax({
type: postMethod,
timeout: 200000,
url: pageName,
data:new FormData(dataObj),
contentType:false,
cache: false, // To unable request pages to be cached
processData:false,
beforeSend: function () {},
success: function (msg) {
if ($(".subm").length > 0) {
if (typeof (btnText) !== 'undefined') {
$(".subm").html(btnText);
} else {
$(".subm").html('Submit');
}
$("#loader_div").hide();
$(".subm").attr('disabled', false);
}
if (isJson(msg) == false) { alert('ERROR::' + msg); $("#loader_div").hide(); return false; }
obj = JSON.parse(msg);
if (obj.success == 'done') {
cbfunc(FuncParamArr,obj);
}
else {
swal('Error',obj.errormsg,'error');
}
},
error: function (jqXHR, textStatus, errorThrown) {
if ($(".subm").length > 0) {
if (typeof (btnText) !== 'undefined') {
$(".subm").html(btnText);
} else {
$(".subm").html('Submit');
}
$("#loader_div").hide();
$(".subm").attr('disabled', false);
}
if (errorThrown == 'Unauthorized') {
Swal.fire({
icon: 'error',
title: 'ERROR!',
text: 'You are not logged in!',
footer: '<a href="' + baseURL + 'login">Click here</a> to login'
});
return false;
}
if (textStatus === "timeout") {
alert("ERROR: Connection problem"); //Handle the timeout
} else {
alert("ERROR: There is something wrong.");
}
//RR location.reload();
}
});
}
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function quick_ajax_post(postURL, dataObj, cbfunc, FuncParamArr) {
return postMyForm(postURL, dataObj, cbfunc, FuncParamArr, 'POST');
}
function quick_submit(postURL, dataObj) { // Add and update methods
dataObj = dataObj || document.getElementById("actionForm");
// console.log(dataObj);
return postMyFormObj(postURL, dataObj,function(){
swal({
title: "Success",
text: obj.errormsg,
icon: "success",
buttons: {
cancel: "Stay On Page",
ok: 'Go Back',
},
})
.then((done) => {
if (done) {
window.location.href = obj.backURL;
}else{
}
});
// swal('Success',obj.errormsg,'success');
}, [], 'POST');
}
function quick_ajax_get(postURL, dataObj, cbfunc, FuncParamArr) {
return postMyForm(postURL, dataObj, cbfunc, FuncParamArr, 'POST');
}
function getToken() {
return $("meta[name=csrf-token]").attr('content');
}
function quick_ajax_delete(postURL, dataObj, cbfunc, FuncParamArr) {
dataObj._token = getToken();
dataObj._method = 'DELETE';
return postMyForm(postURL, dataObj, cbfunc, FuncParamArr, 'DELETE');
}
function quick_delete(pageURL, id) {
if (confirm('Are you sure, you want to delete this record ?')) {
quick_ajax_delete(pageURL, { id: id }, delete_effect_sts, [id]);
}
}
function delete_effect_sts(paramArr) {
// $(paramArr[0]).fadeOut();
$(paramArr[0]).addClass("animate__animated animate__bounceOutLeft").fadeOut(800);
}
function quick_edit(pageText, getURL, postURL) {
loadNewPopup_BS_Add(escape(pageText), '<div id="content_edit"><h3><span style="color:red;"><i class="fa fa-spinner fa-spin"></i> Loading...</span></h3></div>');
var datastr = "postURL=" + postURL;
$.ajax({
type: "GET",
timeout: 200000,
url: getURL,
data: datastr,
success: function (html2) {
$("#content_edit").html(html2);
$('[data-toggle="popover"]').popover();
$('[data-toggle="tooltip"]').tooltip();
},
error: function (jqXHR, textStatus, errorThrown) {
if (textStatus === "timeout") {
alert("ERROR: Connection problem"); //Handle the timeout
} else {
alert("ERROR: There is something wrong.");
}
}
});
}
function quick_image_remove(id,model,column_name,folder){
if (confirm('Are you sure to delete this data?')) {
var url = route('ajaxRemoveModuleImg');
var data={id:id,model:model,column_name:column_name,folder:folder}
$.post(url,data)
.done(function( data ) {
if(data.status == 'success'){
$('.oldImageDivv').fadeOut('slow');
}
else{
alert('Error Adding/Updating Data');
}
});
}
}
function quick_image_remove_optimize(id,model,column_name,folder,removeDiv){
if (confirm('Are you sure to delete this data?')) {
var url = route('ajaxRemoveModuleImg');
var data={id:id,model:model,column_name:column_name,folder:folder}
$.post(url,data)
.done(function( data ) {
if(data.status == 'success'){
$(removeDiv).fadeOut('slow');
}
else{
alert('Error Adding/Updating Data');
}
});
}
}
function quick_popup(pageText, getURL) {
loadNewPopup_BS_Add(escape(pageText), '<div id="content_edit"><h3><div style="color:red;" class="animate__animated animate__infinite animate__headShake"><i class="fa fa-spinner fa-spin"></i> Loading...</div></h3></div>');
var datastr = "";
$.ajax({
type: "GET",
timeout: 200000,
url: getURL,
data: datastr,
success: function (html2) {
$("#content_edit").html(html2);
$('[data-toggle="popover"]').popover();
$('[data-toggle="tooltip"]').tooltip();
},
error: function (jqXHR, textStatus, errorThrown) {
if (textStatus === "timeout") {
alert("ERROR: Connection problem"); //Handle the timeout
} else {
alert("ERROR: There is something wrong.");
}
}
});
}
function loadNewPopup_BS_Add(headingText, content) {
var createMainDivs = '<div class="modal-dialog"><div class="modal-content"><div class="modal-header custom_modal_header"><button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">' + unescape(headingText) + '</h4></div><div class="modal-body">' + content + '</div><div class="modal-footer"></div></div></div>';
$('#myModal').html(createMainDivs);
$('#myModal').modal('show');
}
// start Show Hide Addional Tr for Details and Action buttons
function showme_page(val,arrowObj){
if($(val+':visible').length == 0)
{
$(val).slideDown(1000);
$(arrowObj).html('<i class="fa fa-angle-double-up" aria-hidden="true"></i>');
$(arrowObj).attr('data-original-title','Show less');
}
else{
$(val).slideUp(100);
$(arrowObj).html('<i class="fa fa-angle-double-down" aria-hidden="true"></i>');
$(arrowObj).attr('data-original-title','Show more');
}
}
function alertme(text,type,autoClose,closeAfterSec)
{
var type= type || 'success';
var autoClose= autoClose || true;
var closeAfterSec= closeAfterSec || 3000;
$(".alertme").hide();
var mhtml='<div class="alertme" id="div_alert" style="margin:5px;top:45%;position:fixed;z-index:9999;width:100%">'+
'<div style="max-width: 700px;margin: 0 auto;" class="alert alert-'+type+' alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> '+text+'</div></div>';
$("body").append(mhtml);
if(autoClose){
setTimeout(function(){$(".alertme").hide(); }, closeAfterSec);
}
}