CRM-10760 - Autocomplte - Convert quicksearch bar to use jQuery.ui + CRM.api
authorColeman Watts <coleman@civicrm.org>
Wed, 29 Jan 2014 20:53:49 +0000 (12:53 -0800)
committerColeman Watts <coleman@civicrm.org>
Thu, 30 Jan 2014 19:03:16 +0000 (11:03 -0800)
js/Common.js
templates/CRM/common/Navigation.tpl

index 4dc1b47074135d6f6576611ab868d9909b5abd22..322bd8e8dc2c03af094cda1d6bd32bfb1c0218c1 100644 (file)
@@ -843,6 +843,11 @@ CRM.validate = CRM.validate || {
     catch (e) {}
   });
 
+  /**
+   * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin
+   */
+  $.widget('civi.crmAutocomplete', $.ui.autocomplete, {});
+
   $(function () {
     // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content.
     $('.crm-container').trigger('crmLoad');
index 265215a9bf6cf928f3837fcd2c3579b56a8dd102..148dfe0f976b741845f458fa82fba182fbdb77cd 100644 (file)
@@ -88,29 +88,34 @@ $('#civicrm-menu').ready(function() {
     $(this).attr("tabIndex",i+2);
   });
 
-  var contactUrl = {/literal}"{crmURL p='civicrm/ajax/rest' q='className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=navigation' h=0 }"{literal};
-  $('#sort_name_navigation').autocomplete(contactUrl, {
-    width: 200,
-    selectFirst: false,
-    minChars: 1,
-    matchContains: true,
-    delay: 400,
-    max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal},
-    extraParams:{
-      fieldName:function () {
-        return  $('input[name=quickSearchField]:checked').val();
+  $('#sort_name_navigation')
+    .crmAutocomplete({
+      source: function(request, response) {
+        var
+          option = $('input[name=quickSearchField]:checked'),
+          params = {
+            name: request.term,
+            field_name: option.val(),
+            table_name: option.attr("data-tablename")
+          };
+        CRM.api3('contact', 'getquick', params).done(function(result) {
+          var ret = [];
+          if (result.values) {
+            $.each(result.values, function(k, v) {
+              ret.push({value: v.id, label: v.data});
+            })
+          }
+          response(ret);
+        })
       },
-      tableName:function () {
-        return  $('input[name=quickSearchField]:checked').attr("data-tablename");
-      }
-    }
-  }).result(function(event, data, formatted) {
-        document.location = CRM.url('civicrm/contact/view', {reset: 1, cid: data[1]});
+      select: function (event, ui) {
+        document.location = CRM.url('civicrm/contact/view', {reset: 1, cid: ui.item.value});
         return false;
-      });
-  $('#sort_name_navigation').keydown(function() {
-    $.Menu.closeAll();
-  });
+      }
+    })
+    .keydown(function() {
+      $.Menu.closeAll();
+    });
   $('.crm-hidemenu').click(function() {
     $.Menu.closeAll();
     $('#civicrm-menu').slideUp();
@@ -131,7 +136,7 @@ $('#civicrm-menu').ready(function() {
     if (value === 'first_name' || value === 'last_name') {
       value = 'sort_name';
     }
-    $('#sort_name_navigation').attr({name: value, placeholder: label}).flushCache().focus();
+    $('#sort_name_navigation').attr({name: value, placeholder: label}).focus();
   });
   // check if there is only one contact and redirect to view page
   $('#id_search_block').on('submit', function() {