186 lines
5.5 KiB
JavaScript
186 lines
5.5 KiB
JavaScript
window.AddressPopUp = function (options,itemData) {
|
|
|
|
/*
|
|
* Variables accessible
|
|
* in the class
|
|
*/
|
|
let Opts = {
|
|
active: false,
|
|
array: [],
|
|
onAfterSave: function () {
|
|
},
|
|
onClose: function () {
|
|
},
|
|
};
|
|
let data = {};
|
|
let root = this;
|
|
/*
|
|
* Constructor
|
|
*/
|
|
this.construct = function (options,itemData) {
|
|
console.log(' Address constructor called');
|
|
};
|
|
|
|
this.init = function (options,itemData) {
|
|
console.log(' Address nit called');
|
|
$.extend(Opts, options);
|
|
$.extend(data, itemData);
|
|
root.bindClickAction();
|
|
};
|
|
|
|
this.clearData=function (){
|
|
data={};
|
|
root.renderForm();
|
|
}
|
|
|
|
this.setData=function (itemData){
|
|
$.extend(data, itemData);
|
|
}
|
|
|
|
this.show=function () {
|
|
$('.addressModal').modal().show();
|
|
}
|
|
|
|
this.renderForm=function (){
|
|
$('.addressModal input').val('');
|
|
$('.addressModal textarea').val('');
|
|
$.each(data,function (key,value) {
|
|
/*
|
|
console.log('key:'+key+' value:'+value);
|
|
console.log('.addressModal'+' *[name=contact'+$.ucFirst(key)+']');
|
|
*/
|
|
$('.addressModal'+' *[name=address'+$.ucFirst(key)+']').val(value);
|
|
});
|
|
}
|
|
|
|
this.loadData=function (id){
|
|
console.log('loading id:'+id);
|
|
$('.addressModal .spinnerDiv').removeClass('d-none');
|
|
$('.addressModal .addressModalForm').addClass('d-none');
|
|
$('.addressModal').modal().show();
|
|
let url=APP.getOpts()["Url"]["addressPopup"]['show'];
|
|
url=url.replaceAll('*id*',id);
|
|
$.ajax({
|
|
url: url,
|
|
type: 'GET',
|
|
headers: {
|
|
'X-CSRF-TOKEN': APP.getApiToken()
|
|
},
|
|
success: function (data) {
|
|
root.setData(data);
|
|
root.renderForm();
|
|
$('.spinnerDiv').addClass('d-none');
|
|
$('.addressModalForm').removeClass('d-none');
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
if (xhr.responseJSON.errors) {
|
|
ajaxErrorFieldHandler(xhr.responseJSON.errors);
|
|
}else{
|
|
if(xhr.status==404){
|
|
ajaxErrorFieldHandler([{'detail':'Nincs ilyen elem.'}]);
|
|
}else{
|
|
ajaxErrorFieldHandler([{'detail':'Általános hiba.'}]);
|
|
}
|
|
}
|
|
$('.addressModal').modal('hide');
|
|
},
|
|
});
|
|
return ;
|
|
let contactData={
|
|
id:76,
|
|
name:'Miksóné Borbély Erzsébet',
|
|
email:'szamlazas1@funkcio.hu',
|
|
phone:'+36203407500'
|
|
};
|
|
root.setData(contactData);
|
|
root.renderForm();
|
|
$('.spinnerDiv').addClass('d-none');
|
|
$('.addressModalForm').removeClass('d-none');
|
|
|
|
}
|
|
|
|
this.getOpts = function () {
|
|
return Opts;
|
|
}
|
|
this.bindClickAction = function () {
|
|
$('.addressModal .btn').unbind('click');
|
|
$('.addressModal .buttonClose').bind('click', root.clickActionButtonClose);
|
|
$('.addressModal .buttonSave').bind('click', root.clickActionButtonSave);
|
|
}
|
|
|
|
this.clickActionButtonClose = function (event) {
|
|
console.log('click close');
|
|
Opts.onClose(event, root.formDataToDataObjectAPICall());
|
|
}
|
|
|
|
|
|
this.save = function (data) {
|
|
let form = $('.addressModal form');
|
|
let url=form.attr('action');
|
|
let ajaxType='POST';
|
|
if(data.id){
|
|
url=APP.getOpts()["Url"]["addressPopup"]['update'];
|
|
url=url.replaceAll('*id*',data.id);
|
|
ajaxType='PUT';
|
|
}
|
|
if(!data.status){
|
|
data.status=APP.getOpts()["addressPopup"]['defaultStatus'];
|
|
}
|
|
debug('!!!!URL!!!!! '+url);
|
|
|
|
$.ajax({
|
|
url: url,
|
|
type: ajaxType,
|
|
headers: {
|
|
'X-CSRF-TOKEN': APP.getApiToken()
|
|
},
|
|
data: data,
|
|
success: function (data) {
|
|
Opts.onAfterSave(data);
|
|
$('.addressModal').modal('toggle');
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
if (xhr.responseJSON.errors) {
|
|
ajaxErrorFieldHandler(xhr.responseJSON.errors);
|
|
}
|
|
},
|
|
});
|
|
|
|
return false;
|
|
}
|
|
|
|
this.clickActionButtonSave = function (event) {
|
|
console.log('click save');
|
|
let itemData = root.formDataToDataObjectAPICall(), savedData;
|
|
debug(itemData);
|
|
if(savedData=root.save(itemData)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
this.formDataToDataObjectAPICall = function () {
|
|
let data = new FormData(document.querySelector('.addressModal form'));
|
|
let formJSON = Object.fromEntries(data.entries());
|
|
/*Multiselect or checkbox to array:
|
|
formJSON.multiselectName = data.getAll('multiselectName');
|
|
*/
|
|
$.each(formJSON, function (item, value) {
|
|
debug($.lcFirst(item.replace('address', '')));
|
|
formJSON[$.lcFirst(item.replace('address', ''))] = value;
|
|
delete formJSON[item];
|
|
});
|
|
if(formJSON.phone){
|
|
formJSON.phone=formJSON.phone.replaceAll('_','');
|
|
}
|
|
if(formJSON.email){
|
|
formJSON.email=formJSON.email.replaceAll('_','');
|
|
}
|
|
return formJSON;
|
|
}
|
|
|
|
this.construct(options,itemData);
|
|
}
|