From 7553cf23165041ec99568565b48809be004030c4 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 13 Mar 2013 14:14:56 -0700 Subject: [PATCH] Improve convenience/flexibility of CRM.confirm --- js/Common.js | 60 +++++++++++++++++++++-------------------- js/view/crm.designer.js | 10 +++---- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/js/Common.js b/js/Common.js index eb801f56bd..275c0c2ecc 100644 --- a/js/Common.js +++ b/js/Common.js @@ -42,6 +42,7 @@ var cj = jQuery; * @return string the translated string */ function ts(text, params) { + "use strict"; text = CRM.strings[text] || text; if (params && typeof(params) === 'object') { for (var i in params) { @@ -525,6 +526,7 @@ CRM.validate = CRM.validate || { }; (function($, undefined) { + "use strict"; $(document).ready(function() { advmultiselectResize(); $().crmtooltip(); @@ -667,40 +669,40 @@ CRM.validate = CRM.validate || { /** * Prompt the user for confirmation. * - * @param {Object} with keys "title", "message", "onContinue", "onCancel", "continueButton", "cancelButton" + * @param buttons {object|function} key|value pairs where key == button label and value == callback function + * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue" + * @param options {object|void} Override defaults, keys include 'title', 'message', + * see jQuery.dialog for full list of available params */ - CRM.confirm = function(options) { - var isContinue = false; - options.title = options.title || ts('Confirm Action'); - options.message = options.message || ts('Are you sure you want to continue?'); - options.continueButton = options.continueButton || ts('Continue'); - options.cancelButton = options.cancelButton || ts('Cancel'); - var dialog = $('
') - .attr('title', options.title) - .html(options.message) - .appendTo('body'); - var buttons = {}; - buttons[options.continueButton] = function() { - isContinue = true; - $(dialog).dialog('close'); - }; - buttons[options.cancelButton] = function() { - $(dialog).dialog('close'); - }; - $(dialog).dialog({ + CRM.confirm = function(buttons, options) { + var dialog, callbacks = {}; + var settings = { + title: ts('Confirm Action'), + message: ts('Are you sure you want to continue?'), resizable: false, modal: true, - buttons: buttons, - close: function() { - if (isContinue) { - options.onContinue && options.onContinue(); - } else { - options.onCancel && options.onCancel(); - } - $(dialog).remove(); + close: function() {$(dialog).remove();}, + buttons: {} + }; + settings.buttons[ts('Cancel')] = function() {dialog.dialog('close');}; + options = options || {}; + $.extend(settings, options); + if (typeof(buttons) === 'function') { + callbacks[ts('Continue')] = buttons; + } else { + callbacks = buttons; + } + $.each(callbacks, function(label, callback) { + settings.buttons[label] = function() { + callback.call(dialog); + dialog.dialog('close'); } }); - + dialog = $('
') + .html(options.message) + .appendTo('body') + .dialog(settings); + return dialog; } /** diff --git a/js/view/crm.designer.js b/js/view/crm.designer.js index 8d1509c3f1..14b2e26346 100644 --- a/js/view/crm.designer.js +++ b/js/view/crm.designer.js @@ -396,14 +396,14 @@ if (paletteView.hideAddFieldAlert) { openAddNewWindow(); } else { - CRM.confirm({ - title: ts('Add Field'), - message: ts('A new window or tab will open. Use the new window to add your field, and then return to this window and click "Refresh."'), - onContinue: function() { + CRM.confirm(function() { paletteView.hideAddFieldAlert = true; openAddNewWindow(); + }, { + title: ts('Add Field'), + message: ts('A new window or tab will open. Use the new window to add your field, and then return to this window and click "Refresh."') } - }); + ); } return false; }, -- 2.25.1