Merge pull request #15829 from eileenmcnaughton/dedupe3
[civicrm-core.git] / js / crm.searchForm.js
index c1105213301532c0c3b47a0f74f3ba1af8c5bdcd..42a371dcecf42d18e90d97dae3038d2992d098b7 100644 (file)
         params.name = $('input.select-row').map(function() {return $(this).attr('id');}).get().join('-');
       }
     }
-    $.getJSON(url, params, function(data) {
+    $.post(url, params, function(data) {
       if (data && data.getCount !== undefined) {
         selected = data.getCount;
         displayCount();
         enableTaskMenu();
       }
-    });
+    }, 'json');
   }
 
   /**
         }
       })
       // When selecting a task
-      .on('change', 'select#task', function() {
+      .on('change', 'select#task', function(e) {
         var $form = $(this).closest('form'),
-          $go = $('input.crm-search-go-button', $form);
-        if (1) {
+        $go = $('input.crm-search-go-button', $form);
+        var $selectedOption = $(this).find(':selected');
+        if (!$selectedOption.val()) {
+          // do not blank refresh the empty option.
+          return;
+        }
+        if ($selectedOption.data('is_confirm')) {
+          var confirmed = false;
+          var refresh_fields = $selectedOption.data('confirm_refresh_fields');
+          var $message = '<tr>' + (($selectedOption.data('confirm_message') !== undefined) ? $selectedOption.data('confirm_message') : '') + '</tr>';
+          if (refresh_fields === undefined) {
+            refresh_fields = {};
+          }
+          $.each(refresh_fields, function (refreshIndex, refreshValue) {
+            var $refresh_field = $(refreshValue.selector);
+            var prependText = (refreshValue.prepend !== undefined) ? refreshValue.prepend : '';
+            var existingInput = $refresh_field.find('input').val();
+            $message = $message + '<tr>' + $refresh_field.html().replace(existingInput, prependText + existingInput) + '</tr>';
+          });
+
+          CRM.confirm({
+            title: $selectedOption.data('confirm_title') ? $selectedOption.data('confirm_title') : ts('Confirm action'),
+            message: '<table class="form-layout">' + $message + '</table>'
+          })
+          .on('crmConfirm:yes', function() {
+            confirmed = true;
+            $.each(refresh_fields, function (refreshIndex, refreshValue) {
+              $('#' + refreshIndex).val($('.crm-confirm #' + refreshIndex).val());
+            });
+            $go.click();
+          })
+          .on('crmConfirm:no', function() {
+            $('#task').val('').change();
+            return;
+          });
+        }
+        else if (!$(this).find(':selected').data('supports_modal')) {
           $go.click();
+          $('#task').val('').select2('val', '');
         }
         // The following code can load the task in a popup, however not all tasks function correctly with this
-        // So it's disabled pending a per-task opt-in mechanism
+        // So it's a per-task opt-in mechanism.
         else {
           var data = $form.serialize() + '&' + $go.attr('name') + '=' + $go.attr('value');
           var url = $form.attr('action');