596bff78 |
1 | cj(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) { |
60 | customIdObj.val(data[1]); |
61 | customObj.caretTo(0); |
62 | var namefields = ['first_name', 'last_name', 'middle_name']; |
63 | CRM.api('profile', 'get', {'profile_id' : profileids, 'contact_id' : data[1]}, { |
64 | success: function(result) { |
65 | $.each(result.values, function (id, values){ |
66 | $.each(values, function (fieldname, fieldvalue) { |
67 | $('#' + fieldname).val(fieldvalue); |
68 | }); |
69 | }); |
70 | } |
71 | }); |
72 | } |
73 | ); |
74 | customObj.click(function () { |
75 | customIdObj.val(''); |
76 | }); |
77 | } |
78 | |
79 | if(autocomplete.show_hide) { |
80 | customObj.hide(); |
81 | showHideAutoComplete(select_field, id_field, |
82 | autocomplete.show_text, |
83 | autocomplete.hide_text, |
84 | profileids |
85 | ); |
86 | } |
87 | } |
88 | |
89 | /** |
90 | * Show or hide the autocomplete and change the text |
91 | */ |
92 | function showHideAutoComplete(name_field, id_field, hidden_text, shown_text, profileids) { |
93 | $('#crm-contact-toggle-' + id_field).on('click', function(event) { |
94 | event.preventDefault(); |
95 | $('#' + name_field).toggle(); |
96 | if($('#' + name_field).is(":visible")) { |
97 | $('#crm-contact-toggle-text-' + id_field).text(shown_text); |
98 | } |
99 | else{ |
100 | $('#crm-contact-toggle-text-' + id_field).text(hidden_text); |
101 | $('#' + id_field).val(''); |
40a60af6 |
102 | $('#' + name_field).val(''); |
596bff78 |
103 | CRM.api('profile', 'get', {'profile_id' : profileids}, { |
104 | success: function(result) { |
105 | $.each(result.values, function (id, values){ |
106 | $.each(values, function (fieldname, fieldvalue) { |
107 | $('#' + fieldname).val(fieldvalue); |
108 | }); |
109 | }); |
110 | } |
111 | }); |
112 | } |
113 | |
114 | }); |
596bff78 |
115 | } |
116 | |
117 | var autocompletes = CRM.form.autocompletes; |
118 | var url = CRM.url(autocompletes.url[0], autocompletes.url[1]); |
119 | |
120 | $(autocompletes).each(function (index, autocomplete) { |
121 | assignAutoComplete(autocomplete.name_field, autocomplete.id_field, url, autocomplete.max, CRM.ids.profile, autocomplete); |
122 | } |
123 | ); |
124 | |
125 | }); |
126 | |