Commit | Line | Data |
---|---|---|
3cc60a06 | 1 | CRM.$(function($) { |
596bff78 | 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 | ||
596bff78 | 17 | /** |
18 | * Display a personalized message containing the contact's name | |
19 | * and a variable from the server | |
20 | */ | |
dc677c00 | 21 | function assignAutoComplete( id_field, profileids, autocomplete) { |
596bff78 | 22 | if(profileids === undefined) {profileids = [];} |
23 | ||
596bff78 | 24 | var customIdObj = $('#' + id_field); |
25 | ||
dc677c00 EM |
26 | customIdObj.on('change', function (event, data) { |
27 | var contactID = $(this).val(); | |
dc677c00 EM |
28 | var namefields = ['first_name', 'last_name', 'middle_name']; |
29 | CRM.api('profile', 'get', {'profile_id': profileids, 'contact_id': contactID}, { | |
30 | success: function (result) { | |
31 | $.each(result.values, function (id, value) { | |
239dd9c6 | 32 | $.each(value, function (fieldname, fieldvalue) { |
596bff78 | 33 | $('#' + fieldname).val(fieldvalue); |
34 | }); | |
dc677c00 EM |
35 | }); |
36 | } | |
37 | }); | |
38 | } | |
39 | ) | |
596bff78 | 40 | } |
dc677c00 | 41 | |
596bff78 | 42 | /** |
43 | * Show or hide the autocomplete and change the text | |
44 | */ | |
dc677c00 | 45 | function showHideAutoComplete(id_field, hidden_text, shown_text, profileids) { |
596bff78 | 46 | $('#crm-contact-toggle-' + id_field).on('click', function(event) { |
47 | event.preventDefault(); | |
48 | $('#' + name_field).toggle(); | |
49 | if($('#' + name_field).is(":visible")) { | |
50 | $('#crm-contact-toggle-text-' + id_field).text(shown_text); | |
51 | } | |
52 | else{ | |
53 | $('#crm-contact-toggle-text-' + id_field).text(hidden_text); | |
2406ce64 | 54 | $('#' + id_field + ', #' + name_field).val(''); |
596bff78 | 55 | CRM.api('profile', 'get', {'profile_id' : profileids}, { |
56 | success: function(result) { | |
57 | $.each(result.values, function (id, values){ | |
58 | $.each(values, function (fieldname, fieldvalue) { | |
59 | $('#' + fieldname).val(fieldvalue); | |
60 | }); | |
61 | }); | |
62 | } | |
63 | }); | |
64 | } | |
596bff78 | 65 | }); |
596bff78 | 66 | } |
67 | ||
68 | var autocompletes = CRM.form.autocompletes; | |
596bff78 | 69 | $(autocompletes).each(function (index, autocomplete) { |
dc677c00 | 70 | assignAutoComplete(autocomplete.id_field, CRM.ids.profile, autocomplete); |
596bff78 | 71 | } |
72 | ); | |
73 | ||
74 | }); | |
75 |