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
);
11 else if (el
.selectionStart
!= null) {
13 el
.setSelectionRange(index
, index
);
17 //The following methods are queued under fx for more
18 //flexibility when combining with $.fn.delay() and
21 //Set caret to a particular index
22 $.fn
.caretTo = function (index
, offset
) {
23 return this.queue(function (next
) {
25 var i
= $(this).val().indexOf(index
);
26 if (offset
=== true) {
35 $.caretTo(this, index
);
42 * Display a personalized message containing the contact's name
43 * and a variable from the server
45 function assignAutoComplete(select_field
, id_field
, url
, varmax
, profileids
, autocomplete
) {
46 if(varmax
=== undefined) {varmax
= 10;}
47 if(profileids
=== undefined) {profileids
= [];}
49 if(url
=== undefined) {
50 url
= CRM
.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1');
53 var customObj
= $('#' + select_field
);
54 var customIdObj
= $('#' + id_field
);
56 if (!customObj
.hasClass('ac_input')) {
57 customObj
.autocomplete(url
,
58 { width
: 250, selectFirst
: false, matchContains
: true, max
: varmax
}).result(
59 function (event
, data
) {
60 var contactID
= data
[1];
61 customIdObj
.val(contactID
);
63 var namefields
= ['first_name', 'last_name', 'middle_name'];
64 CRM
.api('profile', 'get', {'profile_id' : profileids
, 'contact_id' : contactID
}, {
65 success: function(result
) {
66 $.each(result
.values
, function (id
, value
){
67 $.each(value
, function (fieldname
, fieldvalue
) {
68 $('#' + fieldname
).val(fieldvalue
);
75 customObj
.click(function () {
80 if(autocomplete
.show_hide
) {
82 showHideAutoComplete(select_field
, id_field
,
83 autocomplete
.show_text
,
84 autocomplete
.hide_text
,
91 * Show or hide the autocomplete and change the text
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
);
101 $('#crm-contact-toggle-text-' + id_field
).text(hidden_text
);
102 $('#' + id_field
).val('');
103 $('#' + name_field
).val('');
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
);
118 var autocompletes
= CRM
.form
.autocompletes
;
119 var url
= CRM
.url(autocompletes
.url
[0], autocompletes
.url
[1]);
121 $(autocompletes
).each(function (index
, autocomplete
) {
122 assignAutoComplete(autocomplete
.name_field
, autocomplete
.id_field
, url
, autocomplete
.max
, CRM
.ids
.profile
, autocomplete
);