Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-28-20-20-34
[civicrm-core.git] / js / AutoComplete.js
CommitLineData
596bff78 1cj(function ($) {
2 'use strict';
3
596bff78 4 // Behind the scenes method deals with browser for setting cursor position
5 $.caretTo = function (el, index) {
6 if (el.createTextRange) {
7 var range = el.createTextRange();
8 range.move("character", index);
9 range.select();
10 }
11 else if (el.selectionStart != null) {
12 el.focus();
13 el.setSelectionRange(index, index);
14 }
15 };
16
17 //The following methods are queued under fx for more
18 //flexibility when combining with $.fn.delay() and
19 //jQuery effects.
20
21 //Set caret to a particular index
22 $.fn.caretTo = function (index, offset) {
23 return this.queue(function (next) {
24 if (isNaN(index)) {
25 var i = $(this).val().indexOf(index);
26 if (offset === true) {
27 i += index.length;
28 }
29 else if (offset) {
30 i += offset;
31 }
32 $.caretTo(this, i);
33 }
34 else {
35 $.caretTo(this, index);
36 }
37 next();
38 });
39};
40
41 /**
42 * Display a personalized message containing the contact's name
43 * and a variable from the server
44 */
45 function assignAutoComplete(select_field, id_field, url, varmax, profileids, autocomplete) {
46 if(varmax === undefined) {varmax = 10;}
47 if(profileids === undefined) {profileids = [];}
48
49 if(url === undefined) {
50 url = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1');
51 }
52
53 var customObj = $('#' + select_field);
54 var customIdObj = $('#' + id_field);
55
56 if (!customObj.hasClass('ac_input')) {
57 customObj.autocomplete(url,
58 { width : 250, selectFirst : false, matchContains: true, max: varmax }).result(
59 function (event, data) {
239dd9c6 60 var contactID = data[1];
61 customIdObj.val(contactID);
596bff78 62 customObj.caretTo(0);
63 var namefields = ['first_name', 'last_name', 'middle_name'];
239dd9c6 64 CRM.api('profile', 'get', {'profile_id' : profileids, 'contact_id' : contactID}, {
596bff78 65 success: function(result) {
239dd9c6 66 $.each(result.values, function (id, value){
67 $.each(value, function (fieldname, fieldvalue) {
596bff78 68 $('#' + fieldname).val(fieldvalue);
69 });
70 });
71 }
72 });
73 }
74 );
75 customObj.click(function () {
76 customIdObj.val('');
77 });
78 }
79
80 if(autocomplete.show_hide) {
81 customObj.hide();
82 showHideAutoComplete(select_field, id_field,
83 autocomplete.show_text,
84 autocomplete.hide_text,
85 profileids
86 );
87 }
88 }
89
90 /**
91 * Show or hide the autocomplete and change the text
92 */
93 function showHideAutoComplete(name_field, id_field, hidden_text, shown_text, profileids) {
94 $('#crm-contact-toggle-' + id_field).on('click', function(event) {
95 event.preventDefault();
96 $('#' + name_field).toggle();
97 if($('#' + name_field).is(":visible")) {
98 $('#crm-contact-toggle-text-' + id_field).text(shown_text);
99 }
100 else{
101 $('#crm-contact-toggle-text-' + id_field).text(hidden_text);
102 $('#' + id_field).val('');
40a60af6 103 $('#' + name_field).val('');
596bff78 104 CRM.api('profile', 'get', {'profile_id' : profileids}, {
105 success: function(result) {
106 $.each(result.values, function (id, values){
107 $.each(values, function (fieldname, fieldvalue) {
108 $('#' + fieldname).val(fieldvalue);
109 });
110 });
111 }
112 });
113 }
114
115 });
596bff78 116 }
117
118 var autocompletes = CRM.form.autocompletes;
119 var url = CRM.url(autocompletes.url[0], autocompletes.url[1]);
120
121 $(autocompletes).each(function (index, autocomplete) {
122 assignAutoComplete(autocomplete.name_field, autocomplete.id_field, url, autocomplete.max, CRM.ids.profile, autocomplete);
123 }
124 );
125
126});
127