paniers/public_html_admin/js/legumes.js

363 lines
13 KiB
JavaScript

// SETTINGS
var legumesConf = {
type : "legume",
baseURL : "legumes.php",
// LIST
list_selector : "#legumesList",
// SEARCH
search_selector : "#legumesSearch .searchGrp",
filtres_selector : "#legumesSearch select",
// VIEW
view_modalSelector : "#modalViewLegume",
view_btnSelector : ".btnViewLegume",
// ADD
add_modalSelector : "#modalAddLegume",
add_btnSelector : "#btnAddLegume",
// EDIT
edit_modalSelector : "#modalEditLegume",
edit_btnSelector : ".btnEditLegume",
// DELETE
delete_modalSelector : "#modalDeleteLegume",
delete_btnSelector : ".btnDeleteLegume",
}
// MODALS
var modalViewLegume = false;
var modalAddLegume = false;
var modalEditLegume = false;
var modalDeleteLegume = false;
$(document).ready( function() {
// LIST LOADER
initListProgressLoad($(legumesConf.list_selector), legumesConf.baseURL, legumesConf.type+" list");
// SEARCH
$(legumesConf.search_selector).initSearchGroup();
// VIEW
modalViewLegume = $(legumesConf.view_modalSelector);
if(modalViewLegume.length>0) {
initViewLegume();
// VIEW REQUEST
var id = getUrlParameter("ref");
if(id!==false && parseInt(id)>0 && current_page == "legumes") {
modalViewLegume_clear(modalViewLegume);
modalViewLegume_loadDatas(modalViewLegume, id);
}
}
// ADD
modalAddLegume = $(legumesConf.add_modalSelector);
if(modalAddLegume.length>0) initAddLegume();
// EDIT
modalEditLegume = $(legumesConf.edit_modalSelector);
if(modalEditLegume.length>0) initEditLegume();
// DELETE
modalDeleteLegume = $(legumesConf.delete_modalSelector);
if(modalDeleteLegume.length>0) initDeleteLegume();
});
/***** MODAL VIEW *****/
function initViewLegume() {
// INIT VIEW BTNs
var initBtnFct = function() { modalForm_initBtnView(
$(legumesConf.view_btnSelector), // BTNs
modalViewLegume, // MODAL
modalViewLegume_clear, // CLEAR FUNCTION
modalViewLegume_loadDatas // LOAD DATAS FUNCTION
)};
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
initBtnFct();
// CANCEL
modalViewLegume.on('hidden.bs.modal', function() { modalViewLegume_clear(modalViewLegume); });
}
function modalViewLegume_loadDatas(modal, id) {
$.post(legumesConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
var datas = JSON.parse(jsonTxt);
modal.find("small.db_ref > span").html(datas.ref);
modal.find("td.nom").html(datas.nom);
// TARIFS
if(Object.keys(datas.tarifs_list).length>0) {
modal.find("table.modal-body tbody tr.nullchild").addClass("hide");
var tarifs_courant = {};
$.each(datas.tarifs, function(n,e) { tarifs_courant[e.ref] = e.print });
$.each(datas.tarifs_list, function(n,e) { modalViewLegume_addTarif(modal, e, tarifs_courant); });
}
modal.modal('show');
}).fail(function() { alert("("+legumesConf.type+" - load modal view)"); });
}
function modalViewLegume_clear(modal) {
modal.find("small.db_ref > span").html("");
modal.find("td.nom").html("");
modal.find("table.modal-body tbody tr.nullchild").removeClass("hide");
modal.find("table.modal-body tbody tr.tarif").remove();
modal.find("table.modal-body tfoot").addClass("hide").find("tr:not(.header)").remove();
}
function modalViewLegume_addTarif(modal, datas, tarifs_courant) {
var tr = $("<tr class='tarif'></tr>")
.append( $("<td></td>").html(datas.date_print) );
if(tarifs_courant.hasOwnProperty(datas.ref)) tr.addClass("courant");
var grps = modal.find("table.modal-body tbody th.titre.groupe");
if(parseInt(datas.groupe_ref)>0) {
grps.each(function(n,e) {
if(parseInt($(this).attr('ref')) == parseInt(datas.groupe_ref)) {
tr.append( $("<td class='tarif'></td>").html(datas.print) );
}
else tr.append( $("<td class='tarif groupe-null'>.</td>") );
});
}
else {
tr.append( $("<td class='tarif'></td>").attr("colspan", grps.length).html(datas.print) );
}
modal.find("table.modal-body tbody").append(tr);
}
function modalViewLegume_addTarifLegumeHist(modal, datas) {
var tr = $("<tr></tr>").attr("ref", datas.ref);
tr.append($("<td></td>").html(datas.tarif_print));
tr.append($("<td></td>").html(datas.tarif_periode));
btnDel = $('<button class="btn btn-xs btn-link glyphicon glyphicon-trash btnDeleteTarifLegume"></button>').attr('ref', datas.ref).attr('tarif', datas.tarif_print).attr('periode', datas.tarif_periode);
tr.append($("<td class='td_btn_action td_btn_delete'></td>").append(btnDel));
modal.find("table.modal-body tfoot").append(tr);
btnDel.click(function(e) {
e.preventDefault(); $(this).blur();
tr = $(this).parent().parent();
id = parseInt( $(this).attr("ref") );
if(!id>0) return;
msg = "Êtes vous sûr de vouloir supprimer le tarif de "+$(this).attr("tarif")+" en application "+$(this).attr("periode")+" ?";
if(confirm(msg)) {
datas = { 'action' : 'deleteTarif', 'tarif' : id };
$.post( legumesConf.baseURL, datas, function( result ) {
if(parseInt(result)>0) {
tr.remove();
if(modal.find("table.modal-body tfoot tr:not(.header)").length == 0) modal.find("table.modal-body tfoot").addClass("hide");
}
else { console.error(result); alert(result); }
}).fail(function() { alert("("+legumesConf.type+" - delete tarif)"); });
}
})
}
/***** MODAL FORM ADD/EDIT *****/
function modalFormLegume_init(modal) {
modal.find("form").on("submit", function(e) { e.preventDefault(); });
// BTN ADD TARIF
modal.find("button.btnAddTarif").click(function(e) {
e.preventDefault();
iptGrp = modalFormLegume_addTarif(modal, false);
iptGrp.find("input[name=prix]").focus();
});
// PRIX
initFloatInput(modal.find("input[name=prix]"));
}
function modalFormLegume_loadDatas(modal,id) {
$.post(legumesConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
var datas = JSON.parse(jsonTxt);
modal.find("input[name=nom]").val(datas.nom);
$.each(datas.tarifs, function(n,e) {
if(e.ref>0) {
e.is_next = false;
modalFormLegume_addTarif(modal, e);
}
});
$.each(datas.next_tarifs, function(n,e) {
if(e.ref>0) {
e.is_next = true;
modalFormLegume_addTarif(modal, e);
}
});
if(modal) modal.modal('show');
}).fail(function() { alert("("+legumesConf.type+" - load datas in modal form)"); });
}
function modalFormLegume_clear(modal) {
modal.removeAttr("edit_id");
modal.find("div.modaLoader").removeClass("show");
modal.find("input[name=nom]").val("");
modal.find("div.tarifs div.tarif").remove();
}
function modalFormLegume_addTarif(modal, datas) {
if(modal.find("div.tarifs div.tarif[ref="+datas.ref+"]").length>0) return;
var iptGrp = $( modal.find("div.tarif_template").html() ).addClass("tarif").attr('ref', 'new');
if(isDefined(datas) && typeof(datas)==="object") {
iptGrp.attr('ref', datas.ref);
iptGrp.find("select[name=groupe]").val(datas.tarif_groupe).prop("disabled", true);
iptGrp.find("input[name=prix]").val(datas.prix).prop("disabled", true);
iptGrp.find("select[name=unite]").val(datas.unite).prop("disabled", true);
iptGrp.find("input[name=date]").val(datas.date).prop("disabled", true);
if(datas.is_next) iptGrp.find("button.btnDel").prop("disabled", false);
}
else {
iptGrp.find("input[name=date]").val(getTodayDate("yyyy-mm-dd"));
iptGrp.find("button.btnDel").prop("disabled", false);
}
modal.find("div.tarifs").append(iptGrp);
// INIT
initFloatInput( iptGrp.find("input[name=prix]") );
iptGrp.find("button.btnDel").click(function(e) {
e.preventDefault(); $(this).blur();
if(parseInt(iptGrp.attr("ref"))>0) {
if(iptGrp.hasClass("deleted")) {
iptGrp.removeClass("deleted");
$(this).removeClass("btn-info").addClass("btn-danger")
.find("i").removeClass("glyphicon-refresh").addClass("glyphicon-trash");
}
else {
iptGrp.addClass("deleted");
$(this).removeClass("btn-danger").addClass("btn-info")
.find("i").removeClass("glyphicon-trash").addClass("glyphicon-refresh");
}
}
else iptGrp.remove();
});
return iptGrp;
}
function modalFormLegume_getDatas(modal) {
var datas = {
'nom' : modal.find("input[name=nom]").val()
};
// TARIFS
var tarifs = [];
modal.find("div.tarifs div.input-group.tarif").each(function(n,e) {
tarifs.push({
'ref' : $(this).attr('ref'),
'groupe' : parseInt( $(this).find("select[name=groupe]").val() ),
'prix' : parseFloat( $(this).find("input[name=prix]").val() ),
'unite' : $(this).find("select[name=unite]").val(),
'date' : $(this).find("input[name=date]").val(),
'del' : $(this).hasClass("deleted") ? 1 : 0
});
});
datas.tarifs = JSON.stringify(tarifs);
return datas;
}
function modalFormLegume_checkDatas(datas) {
if(datas.nom=="" || JSON.parse(datas.tarifs).length==0) {
alert("ERREUR : au minimum, un nom et un tarif doivent être renseigné !");
return false;
}
return true;
}
/***** ADD *****/
function initAddLegume() {
// INIT FORM
modalFormLegume_init(modalAddLegume);
// INIT ADD BTN
modalForm_initBtnAdd(
$(legumesConf.add_btnSelector), // BTN ADD
modalAddLegume, // MODAL
modalFormLegume_clear // CLEAR FORM FUNCTION
);
// INIT SAVE BTN
modalForm_initBtnSaveAdd(
modalAddLegume.find(".btnSave"), // BTN SAVE
legumesConf.type, // ADD TYPE
modalAddLegume, // MODAL
modalFormLegume_getDatas, // GET FORM DATAS FUNCTION
modalFormLegume_checkDatas, // CHECK FORM DATAS FUNCTION
legumesConf.baseURL // SAVE URL
);
// CANCEL
modalAddLegume.on('hidden.bs.modal', function (e) { modalFormLegume_clear(modalAddLegume); });
}
/***** EDIT *****/
function initEditLegume() {
// INIT FORM
modalFormLegume_init(modalEditLegume);
// INIT EDIT BTNs
var initBtnFct = function() { modalForm_initBtnEdit(
$(legumesConf.edit_btnSelector), // BTNs
modalEditLegume, // MODAL
modalFormLegume_clear, // CLEAR FUNCTION
modalFormLegume_loadDatas // LOAD DATAS FUNCTION
)};
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
initBtnFct();
// INIT SAVE EDIT BTN
modalForm_initBtnSaveEdit(
modalEditLegume.find(".btnSave"), // BTN SAVE
legumesConf.type, // EDIT TYPE
modalEditLegume, // MODAL
modalFormLegume_getDatas, // GET FORM DATAS FUNCTION
modalFormLegume_checkDatas, // CHECK FORM DATAS FUNCTION
legumesConf.baseURL // SAVE URL
);
// CANCEL
modalEditLegume.on('hidden.bs.modal', function (e) { modalFormLegume_clear(modalEditLegume); });
}
/***** DELETE *****/
function initDeleteLegume() {
// INIT DELETE BTNs
var initBtnFct = function() { modalForm_initBtnDelete(
$(legumesConf.delete_btnSelector), // BTNs
modalDeleteLegume, // MODAL
modalDeleteLegume_clear, // CLEAR MODAL FUNCTION
modalDeleteLegume_loadDatas // LOAD DATAS FUNCTION
)};
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
initBtnFct();
// INIT SAVE DELETE BTN
modalForm_initBtnSaveDelete(
modalDeleteLegume.find(".btnSave"), // BTN SAVE
legumesConf.type, // DELETE TYPE
modalDeleteLegume, // MODAL
false, // GET FORM DATAS FUNCTION
false, // CHECK FORM DATAS FUNCTION
legumesConf.baseURL // SAVE URL
);
// CANCEL
modalDeleteLegume.on('hidden.bs.modal', function (e) { modalDeleteLegume_clear(); });
}
function modalDeleteLegume_loadDatas(btn, id) {
modalDeleteLegume.find('b.name').html( btn.attr('nom') );
modalDeleteLegume.modal('show');
}
function modalDeleteLegume_clear() {
modalDeleteLegume.removeAttr("delete_id");
modalDeleteLegume.find('b.name').html("");
}