Improve CRM.confirm signature
authorColeman Watts <coleman@civicrm.org>
Tue, 25 Mar 2014 16:15:05 +0000 (12:15 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 25 Mar 2014 16:15:05 +0000 (12:15 -0400)
js/Common.js
templates/CRM/Admin/Page/Tag.tpl
templates/CRM/Case/Form/ActivityChangeStatusJs.tpl
templates/CRM/Case/Form/CaseView.js
templates/CRM/Form/attachmentjs.tpl
templates/CRM/Mailing/Page/Tab.tpl
templates/CRM/common/enableDisableApi.tpl

index 819cff3df78f457c503bdded6a6e2123bf628ff3..3cf1ffb093469b8580153c05cca034928f97af21 100644 (file)
@@ -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 = $('<div class="crm-confirm-dialog"></div>').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 = $('<div class="crm-confirm-dialog"></div>')
-      .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 = $('<div class="crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
-
-        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: '<div class="crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>',
+          options: null
+        });
+        e.preventDefault();
       })
 
       .on('click', function (event) {
index 8e17b11f248864992ca9ae478dd3ad8050cc931a..9dd1066e7f5aac4779b30aacc4652b1f92a5df59 100644 (file)
 <script type="text/javascript">
 cj(function($) {
   var tag;
-  $('.merge_tag').click(function() {
+  $('.merge_tag').click(function(e) {
     tag = $(this).crmEditableEntity();
     mergeTagDialog();
-    return false;
+    e.preventDefault();
   });
 
   function mergeTagDialog() {
     var tagUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTagList' h=0}"{literal};
     var title = {/literal}'{ts escape="js" 1="%1"}Merge tag %1 into:{/ts}'{literal};
-    CRM.confirm(doMergeTag, {
+    CRM.confirm({
       title: ts(title, {1: tag.name}),
       message: '<input name="select_merge_tag" class="big" />',
       open: function() {
@@ -136,34 +136,33 @@ cj(function($) {
               $(dialog).append('<div class="messages status">{/literal}{ts escape='js'}Note: the selected tag is used by additional entities.{/ts}{literal}</div>');
             }
           });
-      },
-      close: function() {
-        $('input[name=select_merge_tag]', this).select2('destroy');
-        $(this).dialog('destroy').remove();
       }
-    });
-  }
-
-  function doMergeTag() {
-    var toId = $("input[name=select_merge_tag]").val();
-    if (!toId) {
-      return false;
-    }
-    /* send synchronous request so that disabling any actions for slow servers*/
-    var postUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTags' h=0 }"{literal};
-    var data = {fromId: tag.id, toId: toId, key:{/literal}"{crmKey name='civicrm/ajax/mergeTags'}"{literal}};
-    cj.ajax({
-      type: "POST",
-      url: postUrl,
-      data: data,
-      dataType: "json",
-      success: function(values) {
-        if ( values.status == true ) {
-          $('#tag-' + toId).children('td.crm-tag-used_for').text(values.tagB_used_for);
-          $('#tag-' + tag.id).html('<td colspan="8"><div class="status message"><div class="icon inform-icon"></div>' + values.message + '</div></td>');
+    })
+      .on('dialogclose', function() {
+        $('input[name=select_merge_tag]', this).select2('destroy');
+      })
+      .on('crmConfirm:yes', function() {
+        var toId = $("input[name=select_merge_tag]", this).val();
+        if (!toId) {
+          $("input[name=select_merge_tag]", this).crmError(ts('Select a tag'));
+          return false;
         }
-      }
-    });
+        /* send synchronous request so that disabling any actions for slow servers*/
+        var postUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTags' h=0 }"{literal};
+        var data = {fromId: tag.id, toId: toId, key:{/literal}"{crmKey name='civicrm/ajax/mergeTags'}"{literal}};
+        cj.ajax({
+          type: "POST",
+          url: postUrl,
+          data: data,
+          dataType: "json",
+          success: function(values) {
+            if ( values.status == true ) {
+              $('#tag-' + toId).children('td.crm-tag-used_for').text(values.tagB_used_for);
+              $('#tag-' + tag.id).html('<td colspan="8"><div class="status message"><div class="icon inform-icon"></div>' + values.message + '</div></td>');
+            }
+          }
+        });
+      });
   }
 });
 </script>
index eca6d42e62762de202febba855a2aafb976ef619..43c8df5f0909c44ab6997684e589a28034e418a5 100644 (file)
           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) {
               }
             });
             CRM.status({}, request);
-          }
-          ,{
-            title: {/literal}'{ts escape='js'}Change Activity Status{/ts}'{literal},
-            message: $el
-          }
-        );
+          });
         return false;
       });
     });
index 3d982312953b30c44ea43124b2f865d9c1285303..48562ba167c13d264c2cc159c82980bcf8b62168 100644 (file)
       })
       .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: '<em>' + $('option:selected', this).text() + '</em>'})
           })
-            .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', '');
             });
         }
           $(this).select2('val', '');
         }
       })
-      .on('click', 'a.case-miniform', function() {
+      .on('click', 'a.case-miniform', function(e) {
         var dialog,
           $el = $(this),
           target = $el.attr('href');
           }
           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();
index a3e44e60b112919a072f6fdc135ce1efecb26199..75345c5813f456bf1774b7ba36673d0e04a9158e 100644 (file)
@@ -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: '<em>' + $el.data('filename') + '</em>'})
-      }).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);
index f34a3d3b4270d62a990ebd1b00151a3243220f4d..c9e7022b357aa001738f95af0b7a2e6809ee37fd 100644 (file)
           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;
       });
     }
index 7dd9811a00da9686bfd766c91e79138e8fc8c58e..64f33e4aa136578f4d16c540ee52650c7c25da84 100644 (file)
       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: '<div class="crm-loading-element">{ts escape="js"}Loading{/ts}...</div>',
           {* 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 {