From e36af3085ac299273093a0c926ba5fbca37b5e63 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Mon, 13 Aug 2012 20:53:48 +0200 Subject: [PATCH] New: add possibility to have a confirm dialog box with on/off buttons Qual: move some functions in lib_head.js --- htdocs/core/js/lib_head.js | 128 ++++++++++++++++++++++++++++++++++- htdocs/core/lib/ajax.lib.php | 96 +++++--------------------- 2 files changed, 145 insertions(+), 79 deletions(-) diff --git a/htdocs/core/js/lib_head.js b/htdocs/core/js/lib_head.js index aee93a8f0dc..2f9de0c6e27 100644 --- a/htdocs/core/js/lib_head.js +++ b/htdocs/core/js/lib_head.js @@ -1,5 +1,5 @@ // Copyright (C) 2005-2010 Laurent Destailleur -// Copyright (C) 2005-2009 Regis Houssin +// Copyright (C) 2005-2012 Regis Houssin // // 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 @@ -627,8 +627,134 @@ function hideMessage(fieldId,message) { if (textbox.value == message) textbox.value = ''; } +/* + * + */ +function setConstant(url, code, input, entity) { + $.get( url, { + action: "set", + name: code, + entity: entity + }, + function() { + $("#set_" + code).hide(); + $("#del_" + code).show(); + $.each(input, function(type, data) { + // Enable another element + if (type == "disabled") { + $.each(data, function(key, value) { + $("#" + value).removeAttr("disabled"); + if ($("#" + value).hasClass("butActionRefused") == true) { + $("#" + value).removeClass("butActionRefused"); + $("#" + value).addClass("butAction"); + } + }); + // Show another element + } else if (type == "showhide" || type == "show") { + $.each(data, function(key, value) { + $("#" + value).show(); + }); + // Set another constant + } else if (type == "set") { + $.each(data, function(key, value) { + $("#set_" + key).hide(); + $("#del_" + key).show(); + $.get( url, { + action: "set", + name: key, + value: value, + entity: entity + }); + }); + } + }); + }); +} +/* + * + */ +function delConstant(url, code, input, entity) { + $.get( url, { + action: "del", + name: code, + entity: entity + }, + function() { + $("#del_" + code).hide(); + $("#set_" + code).show(); + $.each(input, function(type, data) { + // Disable another element + if (type == "disabled") { + $.each(data, function(key, value) { + $("#" + value).attr("disabled", true); + if ($("#" + value).hasClass("butAction") == true) { + $("#" + value).removeClass("butAction"); + $("#" + value).addClass("butActionRefused"); + } + }); + // Hide another element + } else if (type == "showhide" || type == "hide") { + $.each(data, function(key, value) { + $("#" + value).hide(); + }); + // Delete another constant + } else if (type == "del") { + $.each(data, function(key, value) { + $("#del_" + value).hide(); + $("#set_" + value).show(); + $.get( url, { + action: "del", + name: value, + entity: entity + }); + }); + } + }); + }); +} +/* + * + */ +function confirmConstantAction(action, url, code, input, box, entity, yesButton, noButton) { + $("#confirm_" + code) + .attr("title", box.title) + .html(box.content) + .dialog({ + resizable: false, + height: 170, + width: 500, + modal: true, + buttons: [ + { + text : yesButton, + click : function() { + if (action == "set") { + setConstant(url, code, input, entity); + } else if (action == "del") { + delConstant(url, code, input, entity); + } + // Close dialog + $(this).dialog("close"); + // Execute another function + if (box.function) { + var fnName = box.function; + if (window.hasOwnProperty(fnName)) { + window[fnName](); + } + } + } + }, + { + text : noButton, + click : function() { + $(this).dialog("close"); + } + } + ] + }); +} /* This is to allow to transform all select box into ajax autocomplete box * with just one line: $(function() { $( "#listmotifcons" ).combobox(); }); diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index ebfb2cfdd8d..70a88760adf 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -277,7 +277,7 @@ function ajax_dialog($title,$message,$w=350,$h=150) modal: true, buttons: { Ok: function() { - jQuery(this ).dialog(\'close\'); + jQuery(this).dialog(\'close\'); } } }); @@ -360,92 +360,32 @@ function ajax_constantonoff($code,$input=array(),$entity=false) $(function() { var input = '.json_encode($input).'; var url = \''.DOL_URL_ROOT.'/core/ajax/constantonoff.php\'; + var code = \''.$code.'\'; + var entity = \''.$entity.'\'; + var yesButton = "'.dol_escape_js($langs->transnoentities("Yes")).'"; + var noButton = "'.dol_escape_js($langs->transnoentities("No")).'"; // Set constant - $("#set_'.$code.'").click(function() { - $.get( url, { - action: \'set\', - name: \''.$code.'\', - entity: \''.$entity.'\' - }, - function() { - $("#set_'.$code.'").hide(); - $("#del_'.$code.'").show(); - $.each(input, function(type, data) { - // Enable another element - if (type == "disabled") { - $.each(data, function(key, value) { - $("#" + value).removeAttr("disabled"); - if ($("#" + value).hasClass("butActionRefused") == true) { - $("#" + value).removeClass("butActionRefused"); - $("#" + value).addClass("butAction"); - } - }); - // Show another element - } else if (type == "showhide" || type == "show") { - $.each(data, function(key, value) { - $("#" + value).show(); - }); - // Set another constant - } else if (type == "set") { - $.each(data, function(key, value) { - $("#set_" + key).hide(); - $("#del_" + key).show(); - $.get( url, { - action: \'set\', - name: key, - value: value, - entity: \''.$entity.'\' - }); - }); - } - }); - }); + $("#set_" + code).click(function() { + if (input.alert && input.alert.set) { + confirmConstantAction("set", url, code, input, input.alert.set, entity, yesButton, noButton); + } else { + setConstant(url, code, input, entity); + } }); // Del constant - $("#del_'.$code.'").click(function() { - $.get( url, { - action: \'del\', - name: \''.$code.'\', - entity: \''.$entity.'\' - }, - function() { - $("#del_'.$code.'").hide(); - $("#set_'.$code.'").show(); - $.each(input, function(type, data) { - // Disable another element - if (type == "disabled") { - $.each(data, function(key, value) { - $("#" + value).attr("disabled", true); - if ($("#" + value).hasClass("butAction") == true) { - $("#" + value).removeClass("butAction"); - $("#" + value).addClass("butActionRefused"); - } - }); - // Hide another element - } else if (type == "showhide" || type == "hide") { - $.each(data, function(key, value) { - $("#" + value).hide(); - }); - // Delete another constant - } else if (type == "del") { - $.each(data, function(key, value) { - $("#del_" + value).hide(); - $("#set_" + value).show(); - $.get( url, { - action: \'del\', - name: value, - entity: \''.$entity.'\' - }); - }); - } - }); - }); + $("#del_" + code).click(function() { + if (input.alert && input.alert.del) { + confirmConstantAction("del", url, code, input, input.alert.del, entity, yesButton, noButton); + } else { + delConstant(url, code, input, entity); + } }); }); '; + $out.= ''; $out.= ''.img_picto($langs->trans("Disabled"),'switch_off').''; $out.= ''.img_picto($langs->trans("Enabled"),'switch_on').'';