From: Coleman Watts Date: Tue, 25 Mar 2014 16:15:05 +0000 (-0400) Subject: Improve CRM.confirm signature X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5fb83680d13b069618ff14071f5d6c573b63d4de;p=civicrm-core.git Improve CRM.confirm signature --- diff --git a/js/Common.js b/js/Common.js index 819cff3df7..3cf1ffb093 100644 --- a/js/Common.js +++ b/js/Common.js @@ -631,48 +631,44 @@ CRM.validate = CRM.validate || { /** * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference */ - CRM.confirm = function (buttons, options, cancelLabel) { - var dialog, callbacks = {}; - cancelLabel = cancelLabel || ts('Cancel'); - var settings = { + CRM.confirm = function (options) { + var dialog, settings = { title: ts('Confirm Action'), message: ts('Are you sure you want to continue?'), - resizable: false, - modal: true, width: 'auto', + modal: true, + dialogClass: 'crm-container crm-confirm', close: function () { - $(dialog).dialog('destroy').remove(); + $(this).dialog('destroy').remove(); }, - buttons: {} - }; - - settings.buttons[cancelLabel] = function () { - dialog.trigger('crmConfirmNo').dialog('close'); + options: { + no: ts('Cancel'), + yes: ts('Continue') + } }; - options = options || {}; - $.extend(settings, options); - if ($.isFunction(buttons)) { - callbacks[ts('Continue')] = buttons; - } - else if (_.isString(buttons) || !buttons) { - callbacks[buttons || ts('Continue')] = function() {}; + $.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {}); + if (!settings.buttons && $.isPlainObject(settings.options)) { + settings.buttons = []; + $.each(settings.options, function(key, label) { + settings.buttons.push({ + text: label, + click: function() { + var event = $.Event('crmConfirm:' + key); + $(this).trigger(event); + if (!event.isDefaultPrevented()) { + dialog.dialog('close'); + } + } + }); + }); } - else { - callbacks = buttons; + dialog = $('
').html(settings.message); + delete settings.options; + delete settings.message; + if ($.isFunction(options)) { + dialog.on('crmConfirm:yes', options); } - $.each(callbacks, function (label, callback) { - settings.buttons[label] = function () { - dialog.trigger('crmConfirmYes'); - if (callback.call(dialog) !== false) { - dialog.dialog('close'); - } - }; - }); - dialog = $('
') - .html(options.message) - .dialog(settings) - .trigger('crmLoad'); - return dialog; + return dialog.dialog(settings).trigger('crmLoad'); }; /** @@ -738,11 +734,6 @@ CRM.validate = CRM.validate || { }); // Handle qf form errors $('form :input.error', this).one('blur', function() { - // ignore autocomplete fields - if ($(this).is('.ac_input')) { - return; - } - $('.ui-notify-message.error a.ui-notify-close').click(); $(this).removeClass('error'); $(this).next('span.crm-error').remove(); @@ -784,19 +775,15 @@ CRM.validate = CRM.validate || { messagesFromMarkup.call($('#crm-container')); } - // bind the event for image popup $('body') - .on('click', 'a.crm-image-popup', function() { - var o = $('
'); - - CRM.confirm('', - { - title: ts('Preview'), - message: o - }, - ts('Done') - ); - return false; + // bind the event for image popup + .on('click', 'a.crm-image-popup', function(e) { + CRM.confirm({ + title: ts('Preview'), + message: '
', + options: null + }); + e.preventDefault(); }) .on('click', function (event) { diff --git a/templates/CRM/Admin/Page/Tag.tpl b/templates/CRM/Admin/Page/Tag.tpl index 8e17b11f24..9dd1066e7f 100644 --- a/templates/CRM/Admin/Page/Tag.tpl +++ b/templates/CRM/Admin/Page/Tag.tpl @@ -102,16 +102,16 @@ diff --git a/templates/CRM/Case/Form/ActivityChangeStatusJs.tpl b/templates/CRM/Case/Form/ActivityChangeStatusJs.tpl index eca6d42e62..43c8df5f09 100644 --- a/templates/CRM/Case/Form/ActivityChangeStatusJs.tpl +++ b/templates/CRM/Case/Form/ActivityChangeStatusJs.tpl @@ -44,7 +44,11 @@ cj("#activity_change_status").val(current_status_id); }); - CRM.confirm(function() { + CRM.confirm({ + title: {/literal}'{ts escape='js'}Change Activity Status{/ts}'{literal}, + message: $el + }) + .on('crmConfirm:yes', function() { // update the status var status_id = $("#activity_change_status").val(); if (status_id === current_status_id) { @@ -83,12 +87,7 @@ } }); CRM.status({}, request); - } - ,{ - title: {/literal}'{ts escape='js'}Change Activity Status{/ts}'{literal}, - message: $el - } - ); + }); return false; }); }); diff --git a/templates/CRM/Case/Form/CaseView.js b/templates/CRM/Case/Form/CaseView.js index 3d98231295..48562ba167 100644 --- a/templates/CRM/Case/Form/CaseView.js +++ b/templates/CRM/Case/Form/CaseView.js @@ -150,14 +150,14 @@ }) .on('change', 'select[name=timeline_id]', function() { if ($(this).val()) { - CRM.confirm(ts('Add'), { + CRM.confirm({ title: $('option:first', this).text(), message: ts('Add the %1 set of scheduled activities to this case?', {1: '' + $('option:selected', this).text() + ''}) }) - .on('crmConfirmYes', function() { + .on('crmConfirm:yes', function() { $('[name=_qf_CaseView_next]').click(); }) - .on('crmConfirmNo', function() { + .on('crmConfirm:no', function() { $('select[name=timeline_id]').select2('val', ''); }); } @@ -174,7 +174,7 @@ $(this).select2('val', ''); } }) - .on('click', 'a.case-miniform', function() { + .on('click', 'a.case-miniform', function(e) { var dialog, $el = $(this), target = $el.attr('href'); @@ -204,16 +204,16 @@ } return submission; } - dialog = CRM.confirm(submit, { + dialog = CRM.confirm({ title: $(this).attr('title') || $(this).text(), message: detached[target], - close: function() { - detached[target] = $(target, dialog).detach(); - $(dialog).dialog('destroy').remove(); - }, open: miniForms[target].pre - }); - return false; + }) + .on('dialogclose', function() { + detached[target] = $(target, dialog).detach(); + }) + .on('crmConfirm:yes', submit); + e.preventDefault(); }); $().crmAccordions(); diff --git a/templates/CRM/Form/attachmentjs.tpl b/templates/CRM/Form/attachmentjs.tpl index a3e44e60b1..75345c5813 100644 --- a/templates/CRM/Form/attachmentjs.tpl +++ b/templates/CRM/Form/attachmentjs.tpl @@ -5,10 +5,10 @@ var $el = $(this), $row = $el.closest('.crm-attachment-wrapper'), msg = '{/literal}{ts escape="js" 1="%1"}This will immediately delete the file %1. This action cannot be undone.{/ts}{literal}'; - CRM.confirm(null, { + CRM.confirm({ title: $el.attr('title'), message: ts(msg, {1: '' + $el.data('filename') + ''}) - }).on('crmConfirmYes', function() { + }).on('crmConfirm:yes', function() { var postUrl = {/literal}"{crmURL p='civicrm/file/delete' h=0 }"{literal}; var request = $.post(postUrl, $el.data('args')); CRM.status({success: '{/literal}{ts escape="js"}Removed{/ts}{literal}'}, request); diff --git a/templates/CRM/Mailing/Page/Tab.tpl b/templates/CRM/Mailing/Page/Tab.tpl index f34a3d3b42..c9e7022b35 100644 --- a/templates/CRM/Mailing/Page/Tab.tpl +++ b/templates/CRM/Mailing/Page/Tab.tpl @@ -98,15 +98,12 @@ o.unblock(); }); - CRM.confirm( '' - ,{ - title: ts('Email Message'), - message: o, - width : "680px", // don't remove px - height: "560" - }, - ts('Done') - ); + CRM.confirm({ + title: ts('Email Message'), + message: o, + width : 680, + height: 560 + }); return false; }); } diff --git a/templates/CRM/common/enableDisableApi.tpl b/templates/CRM/common/enableDisableApi.tpl index 7dd9811a00..64f33e4aa1 100644 --- a/templates/CRM/common/enableDisableApi.tpl +++ b/templates/CRM/common/enableDisableApi.tpl @@ -76,11 +76,12 @@ fieldLabel = info.label || info.title || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal}; enabled = !$row.hasClass('disabled'); if (enabled) { - CRM.confirm({}, {{/literal} + CRM.confirm({{/literal} message: '
{ts escape="js"}Loading{/ts}...
', {* client-side variable substitutions in smarty are AWKWARD! *} title: ts('{ts escape="js" 1='%1'}Disable %1{/ts}{literal}', {1: fieldLabel}), width: 300, + options: null, open: confirmation }); } else {