/* * fase4.js * Funzioni per visualizzare (ed eventualmente popolare) i dialog e per gestire le chiamate ajax, usando json e il disegno in locale * * Copyright 2018 salvi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ /* Funzioni helper per AJAX */ function jsonAjaxCall (action, formData, errorPanel, callback) { formData.append('action', action); setHtml('DialogError',""); setHtml('TableError',""); var ajax = new XMLHttpRequest(); ajax.open('POST', 'fase4.php', true); ajax.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { try { callback(JSON.parse(this.responseText)); } catch(e) { setHtml(errorPanel, "ERRORE: " + e.message + "
" + this.responseText); } } }; ajax.send(formData); } function jsonAjaxCallReloadAndClose(action, formData) { jsonAjaxCall (action, formData, 'DialogError', function(data) { pareseData(data.table); closeDialog() }); } function jsonAjaxCallAndReload(action, formData) { jsonAjaxCall (action, formData, 'TableError', function(data) { pareseData(data.table); }); } /* Fine funzioni helper per AJAX */ /* Funzioni di aiuto */ function hideElement(element) { var el = document.getElementById(element); el.style.visibility="hidden"; el.style.position="absolute"; } function showElement(element) { var el = document.getElementById(element); el.style.visibility="visible"; el.style.position="relative"; return el; } function setHtml(element, html) { document.getElementById(element).innerHTML=html; } function setValue(element, value) { var el = document.getElementById(element); el.value=value; return el; } function setonClick(element, func) { var el = document.getElementById(element).onclick=func; } function getValue(element) { return document.getElementById(element).value; } /* Fine funzioni di aiuto */ /* Funzioni di base per il dialog */ function openDialog() { document.getElementById("isf_overlay").style.visibility = "visible"; document.getElementById("isf_overlay_close_button").style.position = "relative"; // Per riposizionarlo davanti al dialog aperto } function closeDialog() { document.getElementById("isf_overlay").style.visibility = "hidden"; hideElement("eld_elterm_error"); hideElement("el_dialog"); hideElement("el_delete"); hideElement("atd_atelement_error"); hideElement("atd_atvalue_error"); hideElement("attr_dialog"); hideElement("attr_delete"); } /* Fine funzioni di base per il dialog */ var elementi = null; var edit = false; function Attr(at, el) { this.fromElement = el; this.atcode = at.atcode; this.atorder = at.atorder; this.atelement = at.atelement; this.atvalue = at.atvalue; this.render = function() { var html = "
  • "; if (edit) { html += "["; html += "\"Cancella\""; html += "\"Modifica\""; if (this.atorder == 0) { html += "\"Non"; } else { html += "\"Alza\""; } if (this.atorder == this.fromElement.attrs.length - 1) { html += "\"Non"; } else { html += "\"Abbassa\""; } html += "] "; } html += "" + this.atelement + " : " + this.atvalue + "
  • \n"; return html; }; } function Element(el) { var i; this.elcode = el.elcode; this.elorder = el.elorder; this.elterm = el.elterm; this.elurl = el.elurl; this.attrs = []; for (i = 0; i < el.attrs.length; i++) { this.attrs.push(new Attr(el.attrs[i], this)); } this.render = function() { var html = "
    "; if (edit) { html += " ["; if (this.attrs.length) { html += "\"Non"; } else { html += "\"Cancella\""; } html += "\"Aggiungi"; html += "\"Modifica\""; if (this.elorder == 0) { html += "\"Non"; } else { html += "\"Alza\""; } if (this.elorder == this.attrs.length - 1) { html += "\"Non"; } else { html += "\"Abbassa\""; } html += "] "; } if (this.elurl) { html += "" + this.elterm + "
    \n"; } else { html += this.elterm + "\n"; } if (this.attrs.length) { var i; html += "
    \n"; } return html; }; } function pareseData(data) { var i; elementi = []; for (i = 0; i < data.length; i++) { elementi.push(new Element(data[i])); } render (); } function render () { var i; var html = "
    \n"; if (edit) { html += "
    ["; html += "\"Aggiungi"; html += "] Aggiungi una nuova voce
    \n"; } for (i = 0;i < elementi.length; i++) { html += elementi[i].render(); } html += "
    \n"; setHtml("tablediv", html); } function showAddEl () { showElement("el_dialog"); hideElement("eld_elterm_error"); setHtml("eld_titolo", "Aggiungo"); setValue("eld_submit", "Aggiungi").onclick=function() { addEl(0); }; setValue("eld_elterm", ''); setValue("eld_elurl", ''); openDialog() } function showModifyEl (elcode, elterm, elurl) { showElement("el_dialog"); hideElement("eld_elterm_error"); setHtml("eld_titolo", "Modifico"); setValue("eld_submit", "Modifica").onclick=function() { modifyEl(elcode); }; setValue("eld_elterm", elterm); setValue("eld_elurl", elurl); openDialog() } function addEl(elorder) { elDialog('elorder', elorder, 'addel'); } function modifyEl (elcode) { elDialog('elcode', elcode, 'editel'); } function elDialog(field, value, action) { var formData = new FormData(); formData.append(field, value); formData.append('elterm', getValue('eld_elterm')); formData.append('elurl', getValue('eld_elurl')); jsonAjaxCall (action, formData, 'DialogError', function (data) { hideElement("eld_elterm_error"); if (data.errcode) { if ('elterm' in data.errmsg) { showElement("eld_elterm_error").innerHTML = data.errmsg.elterm; } } else { pareseData(data.table); closeDialog() } }); } function showDelElConfirm (elcode, elorder, elterm) { showElement("el_delete"); setHtml("elq_elterm",elterm); setonClick("elq_submit", function () {delEl(elorder, elcode); }); openDialog() } function delEl(elorder, elcode) { var formData = new FormData(); formData.append('elorder', elorder); formData.append('elcode', elcode); jsonAjaxCallReloadAndClose ('delel', formData); } function showAddAttr(atterm, elterm) { showElement("attr_dialog"); hideElement("atd_atelement_error"); hideElement("atd_atvalue_error"); setHtml("atd_titolo", "Aggiungo"); setHtml("atd_elterm", elterm); setValue("atd_atelement", ""); setValue("atd_atvalue", ""); setValue("atd_submit", "Aggiungi").onclick=function() { addAttr(atterm); }; openDialog() } function showModifyAttr (elterm, atcode, atelement, atvalue) { showElement("attr_dialog"); hideElement("atd_atelement_error"); hideElement("atd_atvalue_error"); setHtml("atd_titolo", "Modifico"); setHtml("atd_elterm", elterm); setValue("atd_atelement", atelement); setValue("atd_atvalue", atvalue); setValue("atd_submit", "Modifica").onclick=function() { modifyAttr(atcode); }; openDialog() } function addAttr(atterm) { attrDialog('atterm', atterm, 'addattr'); } function modifyAttr(atcode) { attrDialog('atcode', atcode, 'editattr'); } function attrDialog(field, value, action) { var formData = new FormData(); formData.append(field, value); if (field == 'atterm') { formData.append('atorder', 0); } formData.append('atelement', getValue('atd_atelement')); formData.append('atvalue', getValue('atd_atvalue')); jsonAjaxCall (action, formData, 'DialogError', function (data) { hideElement("atd_atelement_error"); hideElement("atd_atvalue_error"); if (data.errcode) { if ('atelement' in data.errmsg) { showElement("atd_atelement_error").innerHTML = data.errmsg.atelement; } if ('atvalue' in data.errmsg) { showElement("atd_atvalue_error").innerHTML = data.errmsg.atvalue; } } else { pareseData(data.table); closeDialog() } }); } function showDelAttrConfrm (elterm, atcode, atorder, atterm, atelement) { showElement("attr_delete"); setHtml("arq_atelement", atelement); setHtml("atq_elterm", elterm); setonClick("atq_submit", function () {delAttr(atcode, atorder, atterm); }); openDialog() } function delAttr(atcode, atorder, atterm) { var formData = new FormData(); formData.append('atcode', atcode); formData.append('atorder', atorder); formData.append('atterm', atterm); jsonAjaxCallReloadAndClose ('delattr', formData); } function swithcEdit() { edit = !edit; if (edit) { setValue('editswitch', 'Termina la modifica'); } else { setValue('editswitch', 'Modifica'); } render(); } function loadBody(newEdit) { var formData = new FormData(); jsonAjaxCallAndReload ('table', formData); } function riseel(elorder, elcode) { var formData = new FormData(); formData.append('elorder', elorder); formData.append('elcode', elcode); jsonAjaxCallAndReload ('riseel', formData); } function lowerel(elorder, elcode) { var formData = new FormData(); formData.append('elorder', elorder); formData.append('elcode', elcode); jsonAjaxCallAndReload ('lowerel', formData); } function riseattr(atorder, atcode, atterm) { var formData = new FormData(); formData.append('atorder', atorder); formData.append('atcode', atcode); formData.append('atterm', atterm); jsonAjaxCallAndReload ('riseattr', formData); } function lowerattr(atorder, atcode, atterm) { var formData = new FormData(); formData.append('atorder', atorder); formData.append('atcode', atcode); formData.append('atterm', atterm); jsonAjaxCallAndReload ('lowerattr', formData); }