Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[civicrm-core.git] / templates / CRM / Contact / Form / Search / Builder.js
index d5d19e97714682a3eb86d47f7f4d2dfe8e72fada..5a572419b398650d2b382d2570b0fe1eeb9cb3b4 100644 (file)
@@ -62,7 +62,7 @@
     $('.crm-search-value select', row).remove();
     $('input[id^=value]', row)
       .hide()
-      .after('<select class="form-select required" ' + multiSelect + '><option value="">' + ts('Loading') + '...</option></select>');
+      .after('<select class="crm-form-' + multiSelect.substr(0, 5) + 'select required" ' + multiSelect + '><option value="">' + ts('Loading') + '...</option></select>');
     
     fetchOptions(row, field);
   }
 
   // Initialize display: Hide empty blocks & fields
   var newBlock = CRM.searchBuilder && CRM.searchBuilder.newBlock || 0;
-  $('.crm-search-block', '#Builder').each(function(blockNo) {
-    var block = $(this);
-    var empty = blockNo + 1 > newBlock;
-    var skippedRow = false;
-    $('tr:not(.crm-search-builder-add-row)', block).each(function(rowNo) {
-      var row = $(this);
-      if ($('select:first', row).val() === '') {
-        if (!skippedRow && (rowNo == 0 || blockNo + 1 == newBlock)) {
-          skippedRow = true;
+  function initialize() {
+    $('.crm-search-block', '#Builder').each(function(blockNo) {
+      var block = $(this);
+      var empty = blockNo + 1 > newBlock;
+      var skippedRow = false;
+      $('tr:not(.crm-search-builder-add-row)', block).each(function(rowNo) {
+        var row = $(this);
+        if ($('select:first', row).val() === '') {
+          if (!skippedRow && (rowNo == 0 || blockNo + 1 == newBlock)) {
+            skippedRow = true;
+          }
+          else {
+            row.hide();
+          }
         }
         else {
-          row.hide();
+          empty = false;
         }
-      }
-      else {
-        empty = false;
+      });
+      if (empty) {
+        block.hide();
       }
     });
-    if (empty) {
-      block.hide();
-    }
-  });
+  }
 
-  $('#Builder')
+  $('#crm-main-content-wrapper')
     // Reset and hide row
     .on('click', '.crm-reset-builder-row', function() {
       var row = $(this).closest('tr');
       }
       $(this).siblings('input').val(value);
     })
-  ;
+    .on('crmLoad', function() {
+      initialize();
+      $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+    });
+
+  initialize();
 
-  $().crmAccordions();
-  $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+  // Fetch initial options during page refresh - it's more efficient to bundle them in a single ajax request
+  var initialFields = {}, fetchFields = false;
+  $('select[id^=mapper][id$="_1"] option:selected', '#Builder').each(function() {
+    var field = $(this).attr('value');
+    if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') {
+      initialFields[field] = [CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}];
+      fetchFields = true;
+    }
+  });
+  if (fetchFields) {
+    CRM.api3(initialFields).done(function(data) {
+      $.each(data, function(field, result) {
+        CRM.searchBuilder.fieldOptions[field] = result.values;
+      });
+      $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+    });
+  } else {
+    $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+  }
 })(cj, CRM);