548 lines
18 KiB
JavaScript
548 lines
18 KiB
JavaScript
// SEARCH
|
|
var paniersSearch = false;
|
|
|
|
// VIEW
|
|
var modalViewPanier = false;
|
|
var btnViewPanier = false;
|
|
var currentViewPanier = 0;
|
|
|
|
var paniersBaseURL = "paniers.php";
|
|
|
|
// ADD
|
|
var modalAddPanier = false;
|
|
var formAddPanier = false;
|
|
var btnAddPanier = false;
|
|
|
|
// EDIT
|
|
var modalEditPanier = false;
|
|
var formEditPanier = false;
|
|
var btnEditPanier = false;
|
|
var currentEditPanier = 0;
|
|
|
|
// DELETE
|
|
var modalDeletePanier = false;
|
|
var btnDeletePanier = false;
|
|
var currentDeletePanier = 0;
|
|
|
|
$(document).ready( function() {
|
|
// console.log("INIT PANIERS");
|
|
|
|
// INIT SEARCH
|
|
paniersSearch = $("#paniersSearch");
|
|
if(paniersSearch.length>0) initSearchPaniers();
|
|
|
|
// INIT VIEW PANIER
|
|
modalViewPanier = $("#modalViewPanier");
|
|
btnViewPanier = $(".btnViewPanier");
|
|
if(modalViewPanier.length>0 && btnViewPanier.length>0) {
|
|
initViewPanier();
|
|
// VIEW REQUEST
|
|
var id = getUrlParameter("id");
|
|
if(id!==false && parseInt(id)>0) loadDatasInViewPanierModal(id);
|
|
}
|
|
|
|
// INIT ADD PANIER
|
|
modalAddPanier = $("#modalAddPanier");
|
|
formAddPanier = $("#formAddPanier");
|
|
btnAddPanier = $("#btnAddPanier");
|
|
if(modalAddPanier.length>0 && formAddPanier.length>0 && btnAddPanier.length>0) initAddPanier();
|
|
|
|
// INIT EDIT PANIER
|
|
modalEditPanier = $("#modalEditPanier");
|
|
formEditPanier = $("#formEditPanier");
|
|
btnEditPanier = $(".btnEditPanier");
|
|
if(modalEditPanier.length>0 && formEditPanier.length>0 && btnEditPanier.length>0) initEditPanier();
|
|
|
|
// INIT DELETE PANIER
|
|
modalDeletePanier = $("#modalDeletePanier");
|
|
btnDeletePanier = $(".btnDeletePanier");
|
|
if(modalDeletePanier.length>0 && btnDeletePanier.length>0) initDeletePanier();
|
|
});
|
|
|
|
/***** SEARCH PANIERS *****/
|
|
function initSearchPaniers() {
|
|
// console.log("INIT SEARCH PANIERS");
|
|
|
|
paniersSearch.find("input[name=search]").unbind('keypress').keypress( function(event) {
|
|
if(event.keyCode==13) searchPanier(false);
|
|
});
|
|
paniersSearch.find(".btnClearSearch").unbind('click').click( function(event) {
|
|
searchPanier(true);
|
|
});
|
|
}
|
|
|
|
function searchPanier(clear) {
|
|
paniersSearch.find("input[name=search]").blur();
|
|
paniersSearch.find(".btnClearSearch").blur();
|
|
if(clear) document.location = "?clearSearch";
|
|
else document.location = "?search="+paniersSearch.find("input[name=search]").val();
|
|
}
|
|
|
|
/***** VIEW PANIER *****/
|
|
function initViewPanier() {
|
|
// console.log("INIT VIEW PANIER");
|
|
|
|
btnViewPanier.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
clearViewPanierModal();
|
|
currentViewPanier = id;
|
|
loadDatasInViewPanierModal(id);
|
|
});
|
|
|
|
// CANCEL
|
|
modalViewPanier.on('hidden.bs.modal', function (e) {
|
|
clearViewPanierModal();
|
|
currentViewPanier = 0;
|
|
});
|
|
}
|
|
|
|
function loadDatasInViewPanierModal(id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post(paniersBaseURL, datas, function(jsonTxt) {
|
|
//console.log(jsonTxt);
|
|
var datas = JSON.parse(jsonTxt);
|
|
//console.log(datas);
|
|
|
|
modalViewPanier.find("small.db_ref > span").html(datas.ref);
|
|
|
|
modalViewPanier.find("td.nom").html(datas.nom);
|
|
modalViewPanier.find("td.groupe").html(datas.groupe_nom);
|
|
modalViewPanier.find("td.valeur").html(number_format(datas.valeur,2)+" €");
|
|
|
|
modalViewPanier.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view panier)"); });;
|
|
}
|
|
|
|
function clearViewPanierModal() {
|
|
modalViewPanier.find("small.db_ref > span").html("");
|
|
|
|
modalViewPanier.find("td.nom").html("");
|
|
modalViewPanier.find("td.groupe").html("");
|
|
modalViewPanier.find("td.valeur").html("");
|
|
}
|
|
|
|
/***** FORM PANIER *****/
|
|
function initFormPanier(form) {
|
|
form.on("submit", function(e) { e.preventDefault(); });
|
|
|
|
// NOM
|
|
initFirstUpperCaseInput(form.find("input[name=nom]"), null, function() { $(this).blur(); });
|
|
form.find("input[name=nom]").keydown(function(e) {
|
|
if(e.keyCode==13) { e.preventDefault(); $(this).blur(); }
|
|
});
|
|
|
|
// GROUPE
|
|
form.find("select[name=groupe]").change(function(e) {
|
|
$(this).blur();
|
|
// EXISTING
|
|
if(parseInt($(this).val())>0) {
|
|
switchBtnGroupeAction(form, "delete");
|
|
switchBtnGroupeSecondAction(form, "edit");
|
|
}
|
|
// CHOISIR...
|
|
else {
|
|
switchBtnGroupeAction(form, "add");
|
|
switchBtnGroupeSecondAction(form, "hide");
|
|
}
|
|
});
|
|
|
|
form.find("button.btnGroupeAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// ADD / SAVE NEW
|
|
if($(this).hasClass("add") || $(this).hasClass("save_new")) {
|
|
addInputGroupeName(form, "add");
|
|
|
|
switchBtnGroupeAction(form, "save");
|
|
switchBtnGroupeSecondAction(form, "cancel");
|
|
}
|
|
// SAVE
|
|
else if($(this).hasClass("save")) {
|
|
var ipt = form.find("input[name=groupeName]");
|
|
if(ipt.length>0) {
|
|
var ref = ipt.attr("ref");
|
|
|
|
var datas = { 'nom' : ipt.val() };
|
|
|
|
if(datas.nom=="") {
|
|
alert("Merci de renseigner le nom du groupe !");
|
|
return;
|
|
}
|
|
|
|
// ADD GROUPE
|
|
if(ref == "new") datas.action = "add_groupe";
|
|
// EDIT GROUPE
|
|
else if(parseInt(ref)>0) {
|
|
ref = parseInt(ref);
|
|
datas.action = "edit_groupe";
|
|
datas.groupe_ref = ref;
|
|
}
|
|
|
|
form.find("div.modaLoader").addClass("show");
|
|
form.find("button.btnGroupeAction").prop("disabled", true);
|
|
|
|
$.post( paniersBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
switchBtnGroupeAction(form, "delete");
|
|
switchBtnGroupeSecondAction(form, "edit");
|
|
removeInputGroupeName(form);
|
|
|
|
if(ref == "new") ref = parseInt(result);
|
|
|
|
// REFRESH GROUPES LIST
|
|
$.post( paniersBaseURL, {action: "groupes_select_list"}, function( result ) {
|
|
form.find("select[name=groupe]").html(result).val(ref);
|
|
}).fail(function() { alert(srvErrorMsg+" (form panier - add/edit groupe - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnGroupeAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form panier - add/edit groupe)"); });
|
|
}
|
|
}
|
|
// DELETE
|
|
else if($(this).hasClass("delete")) {
|
|
var ref = parseInt(form.find("select[name=groupe]").val());
|
|
var nom = form.find("select[name=groupe] option:selected").html();
|
|
if(confirm('Etês vous sûr de vouloir supprimer le groupe de panier "'+nom+'" ?')) {
|
|
form.find("div.modaLoader").addClass("show");
|
|
form.find("button.btnGroupeAction").prop("disabled", false);
|
|
|
|
var datas = { action : 'delete_groupe', groupe_ref : ref };
|
|
$.post( paniersBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
// REFRESH CONTRATS TYPES LIST
|
|
$.post( paniersBaseURL, {action: "groupes_select_list"}, function( result ) {
|
|
form.find("select[name=groupe]").html(result).val("0");
|
|
}).fail(function() { alert(srvErrorMsg+" (form panier - delete groupe - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnGroupeAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form panier - delete groupe)"); });
|
|
}
|
|
}
|
|
});
|
|
|
|
form.find("button.btnGroupeSecondAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// EDIT
|
|
if($(this).hasClass("edit")) {
|
|
addInputGroupeName(form, "edit");
|
|
switchBtnGroupeAction(form, "save");
|
|
switchBtnGroupeSecondAction(form, "cancel");
|
|
}
|
|
// CANCEL
|
|
else if($(this).hasClass("cancel")) {
|
|
removeInputGroupeName(form);
|
|
// EDIT
|
|
if(parseInt(form.find("select[name=groupe]").val())>0) {
|
|
switchBtnGroupeAction(form, "delete");
|
|
switchBtnGroupeSecondAction(form, "edit");
|
|
}
|
|
// ADD
|
|
else {
|
|
switchBtnGroupeAction(form, "add");
|
|
switchBtnGroupeSecondAction(form, "hide");
|
|
}
|
|
}
|
|
});
|
|
|
|
// VALEUR
|
|
initFloatInput(form.find("input[name=valeur]"));
|
|
form.find("input[name=valeur]").keydown(function(e) {
|
|
if(e.keyCode==13) { e.preventDefault(); $(this).blur(); }
|
|
});
|
|
}
|
|
|
|
function loadDatasInFormPanierDatas(modal,form,id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post(paniersBaseURL, datas, function(jsonTxt) {
|
|
//console.log(jsonTxt);
|
|
var datas = JSON.parse(jsonTxt);
|
|
//console.log(datas);
|
|
|
|
form.find("input[name=nom]").val(datas.nom);
|
|
|
|
if(parseInt(datas.groupe_ref) > 0) {
|
|
form.find("select[name=groupe]").val(datas.groupe_ref);
|
|
switchBtnGroupeAction(form, "delete");
|
|
switchBtnGroupeSecondAction(form, "edit");
|
|
}
|
|
else {
|
|
form.find("select[name=groupe]").val("0");
|
|
switchBtnGroupeAction(form, "add");
|
|
switchBtnGroupeSecondAction(form, "hide");
|
|
}
|
|
|
|
form.find("input[name=valeur]").val(datas.valeur);
|
|
|
|
if(modal) modal.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (form panier - load datas)"); });
|
|
}
|
|
|
|
function clearFormPanierDatas(form) {
|
|
form.find("input[name=nom]").val("");
|
|
|
|
form.find("select[name=groupe]").val("0");
|
|
removeInputGroupeName(form);
|
|
switchBtnGroupeAction(form, "add");
|
|
switchBtnGroupeSecondAction(form, "hide");
|
|
|
|
form.find("input[name=valeur]").val("0");
|
|
|
|
// MODALOADER
|
|
form.find("div.modaLoader").removeClass("show");
|
|
}
|
|
|
|
// GROUPE
|
|
|
|
function switchBtnGroupeAction(form, action) { // ADD / SAVE / DELETE
|
|
var btn = form.find("button.btnGroupeAction");
|
|
|
|
if(action == "add") {
|
|
btn.removeClass("save delete").addClass("add").removeClass("btn-danger btn-primary").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-saved glyphicon-trash").addClass("glyphicon-plus");
|
|
}
|
|
else if(action == "save") {
|
|
btn.removeClass("add save_new delete").addClass("save").removeClass("btn-danger btn-default").addClass("btn-primary")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-trash").addClass("glyphicon-floppy-saved");
|
|
}
|
|
else if(action == "delete") {
|
|
btn.removeClass("add save_new save").addClass("delete").removeClass("btn-default btn-primary").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-floppy-saved").addClass("glyphicon-trash");
|
|
}
|
|
}
|
|
|
|
function switchBtnGroupeSecondAction(form, action) { // CANCEL / EDIT
|
|
var btn = form.find("button.btnGroupeSecondAction");
|
|
|
|
if(action == "cancel") {
|
|
btn.removeClass("edit").addClass("cancel").removeClass("btn-default").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-edit").addClass("glyphicon-floppy-remove");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "edit") {
|
|
btn.removeClass("cancel").addClass("edit").removeClass("btn-danger").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-remove").addClass("glyphicon-edit");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "hide") {
|
|
btn.parent().addClass("hide");
|
|
}
|
|
}
|
|
|
|
function addInputGroupeName(form, action) {
|
|
var select = form.find("select[name=groupe]");
|
|
var ipt = $('<input type="text" class="form-control" name="groupeName" placeholder="nom">').attr("action", action);
|
|
select.addClass("hide");
|
|
ipt.insertBefore(select);
|
|
if(action=="edit") {
|
|
ipt.attr("ref", select.val());
|
|
ipt.val( select.find("option:selected").html() );
|
|
}
|
|
else ipt.attr("ref", "new");
|
|
ipt.focus();
|
|
|
|
ipt.keypress(function(e) {
|
|
if(e.keyCode==13) {
|
|
e.preventDefault();
|
|
form.find("button.btnGroupeAction").click();
|
|
}
|
|
});
|
|
}
|
|
|
|
function removeInputGroupeName(form) {
|
|
var ipt = form.find("input[name=groupeName]");
|
|
if(ipt.length>0) ipt.remove();
|
|
form.find("select[name=groupe]").removeClass("hide");
|
|
}
|
|
|
|
// GET FORM DATAS
|
|
|
|
function getPanierFormDatas(form) {
|
|
var datas = {
|
|
'nom' : form.find("input[name=nom]").val(),
|
|
'groupe' : parseInt(form.find("select[name=groupe]").val()),
|
|
'valeur' : parseFloat(form.find("input[name=valeur]").val())
|
|
};
|
|
return datas;
|
|
}
|
|
|
|
/***** ADD PANIER *****/
|
|
function initAddPanier() {
|
|
// console.log("INIT ADD PANIER");
|
|
|
|
btnAddPanier.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
clearFormPanierDatas(formAddPanier);
|
|
modalAddPanier.modal('show');
|
|
});
|
|
|
|
initFormPanier(formAddPanier);
|
|
|
|
// SAVE PANIER
|
|
$(".btnSaveAddPanier").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
var datas = getPanierFormDatas(formAddPanier);
|
|
if(datas.nom=="" || !datas.groupe>0) {
|
|
alert("ERREUR : au minimum, un nom et un groupe doivent être renseigné !");
|
|
return;
|
|
}
|
|
|
|
datas.action = 'add';
|
|
|
|
formAddPanier.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveAddPanier").prop("disabled", true);
|
|
|
|
$.post( paniersBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalAddPanier.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formAddPanier.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddPanier").prop("disabled", false);
|
|
}
|
|
}).fail( function() {
|
|
alert(srvErrorMsg+" (add panier)");
|
|
formAddPanier.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddPanier").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalAddPanier.on('hidden.bs.modal', function (e) { clearFormPanierDatas(formAddPanier); });
|
|
}
|
|
|
|
/***** EDIT PANIER *****/
|
|
function initEditPanier() {
|
|
// console.log("INIT EDIT PANIER");
|
|
|
|
btnEditPanier.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentEditPanier = id;
|
|
clearFormPanierDatas(formEditPanier);
|
|
loadDatasInFormPanierDatas(modalEditPanier,formEditPanier,id);
|
|
});
|
|
|
|
initFormPanier(formEditPanier);
|
|
|
|
// SAVE PANIER
|
|
$(".btnSaveEditPanier").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
if(!currentEditPanier>0) return;
|
|
|
|
var datas = getPanierFormDatas(formEditPanier);
|
|
if(datas.nom=="" || !datas.groupe>0) {
|
|
alert("ERREUR : au minimum, un nom et un groupe doivent être renseigné !");
|
|
return;
|
|
}
|
|
|
|
datas.action = 'edit';
|
|
datas.ref = currentEditPanier;
|
|
|
|
// console.log(datas,"EDIT datas");
|
|
|
|
formEditPanier.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveEditPanier").prop("disabled", true);
|
|
|
|
$.post( paniersBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditPanier.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formEditPanier.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditPanier").prop("disabled", false);
|
|
}
|
|
}).fail( function() {
|
|
alert(srvErrorMsg+" (edit panier)");
|
|
formEditPanier.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditPanier").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalEditPanier.on('hidden.bs.modal', function (e) {
|
|
clearFormPanierDatas(formEditPanier);
|
|
currentEditPanier = 0;
|
|
});
|
|
}
|
|
|
|
/***** DELETE PANIER *****/
|
|
function initDeletePanier() {
|
|
// console.log("INIT DELETE PANIER");
|
|
|
|
btnDeletePanier.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentDeletePanier = id;
|
|
|
|
modalDeletePanier.find('b.name').html( $(this).attr('nom') );
|
|
|
|
modalDeletePanier.modal('show');
|
|
});
|
|
|
|
// DELETE
|
|
$("#btnDeletePanier").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
$(this).blur();
|
|
|
|
if(!currentDeletePanier>0) return;
|
|
|
|
var datas = {
|
|
action : 'delete',
|
|
ref : currentDeletePanier
|
|
};
|
|
|
|
$.post( paniersBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditPanier.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail(function() { alert(srvErrorMsg+" (delete panier)"); });
|
|
});
|
|
|
|
// CANCEL
|
|
modalDeletePanier.on('hidden.bs.modal', function (e) {
|
|
modalDeletePanier.find('b.name').html("");
|
|
currentDeletePanier = 0;
|
|
});
|
|
} |