From c651e7c81dd6a02113d09f4a84e0f5d3b339d383 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Fri, 5 Jul 2019 17:10:20 +0200 Subject: [PATCH] Gestione AJAX dei form --- .gitignore | 1 + add.php | 28 +++++-------- ajax.php | 7 ++++ editor.php | 2 + lib/functions.js | 87 ++++++++++++++++++++++++++++++++++++++++ lib/functions.php | 2 +- modules/fatture/edit.php | 33 ++++----------- package.json | 11 ++++- 8 files changed, 124 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 04341c68a..44aa37e47 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,7 @@ node_modules/ vendor/ *.phar *.lock +package-lock.json # Project # ###################### diff --git a/add.php b/add.php index ec69d1472..3af3e16c9 100644 --- a/add.php +++ b/add.php @@ -80,27 +80,19 @@ $(document).ready(function(){ echo ' - $("#form_'.$id_module.'-'.$id_plugin.'").find("form").ajaxForm({ - url: globals.rootdir + "/actions.php", - beforeSubmit: function(arr, $form, options) { - return $form.parsley().validate(); - }, - data: data, - type: "post", - success: function(data){ - data = data.trim(); - - if(data && $("#'.get('select').'").val() !== undefined ) { - result = JSON.parse(data); - $("#'.get('select').'").selectSetNew(result.id, result.text); + $("#form_'.$id_module.'-'.$id_plugin.'").find("form").submit(function () { + submitAjax(this, data, function(response) { + // Selezione automatica nuovo valore per il select + select = "#'.get('select').'"; + if ($(select).val() !== undefined) { + $(select).selectSetNew(response.id, response.text); } $("#bs-popup2").modal("hide"); - }, - error: function(data) { - alert("'.tr('Errore').': " + data); - } - }); + }); + + return false; + }) }); '; } diff --git a/ajax.php b/ajax.php index 88c554958..29e4a42ba 100644 --- a/ajax.php +++ b/ajax.php @@ -107,5 +107,12 @@ switch (get('op')) { echo json_encode($response); + break; + + case 'flash': + $response = flash()->getMessages(); + + echo json_encode($response); + break; } diff --git a/editor.php b/editor.php index 3a91829ea..e6ccde6f7 100755 --- a/editor.php +++ b/editor.php @@ -130,6 +130,8 @@ if (empty($record) || !$has_access) { form.prepend(\'\'); $("#save").click(function(){ + //submitAjax(form); + $("#submit").trigger("click"); });'; diff --git a/lib/functions.js b/lib/functions.js index 7678d1216..a25a9fa6e 100644 --- a/lib/functions.js +++ b/lib/functions.js @@ -40,6 +40,25 @@ $(document).ready(function () { moment.locale(globals.locale); globals.timestampFormat = moment.localeData().longDateFormat('L') + ' ' + moment.localeData().longDateFormat('LT'); + // Standard per i popup + toastr.options = { + "closeButton": true, + "debug": false, + "newestOnTop": false, + "progressBar": true, + "positionClass": "toast-top-right", + "preventDuplicates": true, + "onclick": null, + "showDuration": "300", + "hideDuration": "1000", + "timeOut": "12000", + "extendedTimeOut": "8000", + "showEasing": "swing", + "hideEasing": "linear", + "showMethod": "fadeIn", + "hideMethod": "fadeOut" + }; + // Imposta lo standard per la conversione dei numeri numeral.register('locale', 'it', { delimiters: { @@ -1591,3 +1610,71 @@ function executeHook(hook, length){ }, }); } + +function submitAjax(form, data = {}, callback = null, errorCallback = null) { + valid = $(form).parsley().validate(); + + if(valid) { + $("#main_loading").show(); + + content_was_modified = false; + + // Fix per gli id di default + data.id_module = data.id_module ? data.id_module : globals.id_module; + data.id_record = data.id_record ? data.id_record : globals.id_record; + data.id_plugin = data.id_plugin ? data.id_plugin : globals.id_plugin; + + // Invio dei dati + $(form).ajaxSubmit({ + url: globals.rootdir + "/actions.php", + data: data, + type: "post", + success: function (data) { + data = data.trim(); + + if (data) { + response = JSON.parse(data); + callback(response); + } + + $("#main_loading").fadeOut(); + + // Visualizzazione messaggi + $.ajax({ + url: globals.rootdir + '/ajax.php', + type: 'get', + data: { + op: 'flash', + }, + success: function (flash) { + messages = JSON.parse(flash); + + info = messages.info ? messages.info : {}; + Object.keys(info).forEach(function (element) { + toastr["success"](info[element]); + }); + + warning = messages.warning ? messages.warning : {}; + Object.keys(warning).forEach(function (element) { + toastr["warning"](warning[element]); + }); + + error = messages.error ? messages.error : {}; + Object.keys(error).forEach(function (element) { + toastr["error"](error[element]); + }); + } + }); + }, + error: function (data) { + $("#main_loading").fadeOut(); + + toastr["error"](data); + + errorCallback(data); + } + }); + } + + return valid; +} diff --git a/lib/functions.php b/lib/functions.php index 3d7c91f32..e8d125713 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -231,7 +231,7 @@ function translateTemplate() // Annullo le notifiche (AJAX) if (isAjaxRequest()) { - flash()->clearMessage('info'); + //flash()->clearMessage('info'); } echo $template; diff --git a/modules/fatture/edit.php b/modules/fatture/edit.php index bd7786afe..932c0aa59 100644 --- a/modules/fatture/edit.php +++ b/modules/fatture/edit.php @@ -697,34 +697,15 @@ $(".btn-sm[data-toggle=\"tooltip\"]").each(function() { var restore = buttonLoading(btn); + valid = submitAjax(form, {}, function() { + buttonRestore(btn, restore); + }, function() { + buttonRestore(btn, restore); + }); // Procedo al salvataggio solo se tutti i campi obbligatori sono compilati, altrimenti mostro avviso - if (form.parsley().isValid()) { - content_was_modified = false; + //form.find("input:disabled, select:disabled").removeAttr("disabled"); - form.find("input:disabled, select:disabled").removeAttr("disabled"); - - $.ajax({ - url: globals.rootdir + "/actions.php?id_module=" + globals.id_module , - cache: false, - type: "POST", - processData: false, - dataType : "html", - data: form.serialize(), - success: function(data) { - $("#main_loading").fadeOut(); - - buttonRestore(btn, restore); - }, - error: function(data) { - $("#main_loading").fadeOut(); - - swal("'.tr('Errore').'", "'.tr('Errore durante il salvataggio').'", "error"); - - buttonRestore(btn, restore); - } - }); - - } else { + if(!valid) { swal({ type: "error", title: "'.tr('Errore').'", diff --git a/package.json b/package.json index b4e3e85f1..1b4145b13 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "jquery": "^3.2.1", "jquery-form": "^4.2.1", "jquery-ui-touch-punch": "^0.2.3", + "jquery.shorten": "^1.0.0", "moment": "^2.18.1", "numeral": "^2.0.6", "parsleyjs": "^2.7.2", @@ -33,8 +34,8 @@ "signature_pad": "^2.1.1", "smartwizard": "^4.2.2", "sweetalert2": "^6.11.4", - "tooltipster": "^4.2.5", - "jquery.shorten": "^1.0.0" + "toastr": "^2.1.4", + "tooltipster": "^4.2.5" }, "devDependencies": { "archiver": "^2.0.0", @@ -173,6 +174,12 @@ "main": [ "src/jquery.shorten.js" ] + }, + "toastr": { + "main": [ + "build/toastr.min.js", + "build/toastr.min.css" + ] } } }