X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2FAutoComplete.js;h=7a1237e02688cb92c13a836bffca2b72edb04212;hb=1789c269b1c68178c1f3de38d751a4e37f94db4c;hp=8304d0e047cdb2055781dfeb834b93d1459c4cb3;hpb=95b0090202aa538612981aa3cda2c71154397fec;p=civicrm-core.git diff --git a/js/AutoComplete.js b/js/AutoComplete.js index 8304d0e047..7a1237e026 100644 --- a/js/AutoComplete.js +++ b/js/AutoComplete.js @@ -1,4 +1,4 @@ -cj(function ($) { +CRM.$(function($) { 'use strict'; // Behind the scenes method deals with browser for setting cursor position @@ -14,83 +14,35 @@ cj(function ($) { } }; - //The following methods are queued under fx for more - //flexibility when combining with $.fn.delay() and - //jQuery effects. - - //Set caret to a particular index - $.fn.caretTo = function (index, offset) { - return this.queue(function (next) { - if (isNaN(index)) { - var i = $(this).val().indexOf(index); - if (offset === true) { - i += index.length; - } - else if (offset) { - i += offset; - } - $.caretTo(this, i); - } - else { - $.caretTo(this, index); - } - next(); - }); -}; - /** * Display a personalized message containing the contact's name * and a variable from the server */ - function assignAutoComplete(select_field, id_field, url, varmax, profileids, autocomplete) { - if(varmax === undefined) {varmax = 10;} + function assignAutoComplete( id_field, profileids, autocomplete) { if(profileids === undefined) {profileids = [];} - if(url === undefined) { - url = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'); - } - - var customObj = $('#' + select_field); var customIdObj = $('#' + id_field); - if (!customObj.hasClass('ac_input')) { - customObj.autocomplete(url, - { width : 250, selectFirst : false, matchContains: true, max: varmax }).result( - function (event, data) { - var contactID = data[1]; - customIdObj.val(contactID); - customObj.caretTo(0); - var namefields = ['first_name', 'last_name', 'middle_name']; - CRM.api('profile', 'get', {'profile_id' : profileids, 'contact_id' : contactID}, { - success: function(result) { - $.each(result.values, function (id, value){ + customIdObj.on('change', function (event, data) { + var contactID = $(this).val(); + var namefields = ['first_name', 'last_name', 'middle_name']; + CRM.api('profile', 'get', {'profile_id': profileids, 'contact_id': contactID}, { + success: function (result) { + $.each(result.values, function (id, value) { $.each(value, function (fieldname, fieldvalue) { $('#' + fieldname).val(fieldvalue); }); - }); - } - }); - } - ); - customObj.click(function () { - customIdObj.val(''); - }); - } - - if(autocomplete.show_hide) { - customObj.hide(); - showHideAutoComplete(select_field, id_field, - autocomplete.show_text, - autocomplete.hide_text, - profileids - ); + }); + } + }); + } + ) } - } - + /** * Show or hide the autocomplete and change the text */ - function showHideAutoComplete(name_field, id_field, hidden_text, shown_text, profileids) { + function showHideAutoComplete(id_field, hidden_text, shown_text, profileids) { $('#crm-contact-toggle-' + id_field).on('click', function(event) { event.preventDefault(); $('#' + name_field).toggle(); @@ -110,15 +62,12 @@ cj(function ($) { } }); } - }); } var autocompletes = CRM.form.autocompletes; - var url = CRM.url(autocompletes.url[0], autocompletes.url[1]); - $(autocompletes).each(function (index, autocomplete) { - assignAutoComplete(autocomplete.name_field, autocomplete.id_field, url, autocomplete.max, CRM.ids.profile, autocomplete); + assignAutoComplete(autocomplete.id_field, CRM.ids.profile, autocomplete); } );