Commit | Line | Data |
---|---|---|
d154efb9 TO |
1 | (function($, CRM) { |
2 | ||
3 | /** | |
4 | * Setup a contact widget for use in CRUD forms. The form element stores a contact ID | |
5 | * but displays a name/label. | |
6 | * | |
7 | * Usage: | |
8 | * <INPUT type="text" name="my_contact_id" value="123" /> | |
9 | * <SCRIPT type="text/javascript"> | |
10 | * $('[name=my_contact_id]').crmContactField(); | |
11 | * $('[name=my_contact_id]').on('change', function(){ | |
12 | * console.log("the new contact id is ", $(this).val()); | |
13 | * }); | |
14 | * </SCRIPT> | |
15 | * | |
16 | * Note: The given form element is the canonical representation of the selected contact-ID. | |
17 | * To display/enter the contact by name, the contact-ID field will be hidden, and a helper | |
18 | * widget will be inserted. The values will be synced between the two. | |
19 | * | |
20 | * Cases to consider/test: | |
21 | * - When initializing, the read the contact-ID and set the contact-label | |
22 | * - When unsetting(blanking) the contact-label, also unset (blank) the contact-ID | |
23 | * - If third party code updates the hidden value, then one must trigger a 'change' | |
24 | * event to update the visible widget. | |
25 | */ | |
26 | $.fn.crmContactField = function() { | |
27 | return this.each(function(){ | |
8133bcd0 | 28 | var urlParams = 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'; |
d154efb9 TO |
29 | var hiddenEl = this; |
30 | var widgetEl = $('<input type="text" />'); | |
31 | ||
32 | var activeContactId = null; | |
8133bcd0 | 33 | // For organization autocomplete |
593c1267 | 34 | if($(this).attr('urlParam')) { |
44b74e45 DC |
35 | var param = $(this).attr('urlParam'); |
36 | var urlParams = urlParams + '&' + param; | |
8133bcd0 DC |
37 | } |
38 | var contactUrl = CRM.url('civicrm/ajax/rest', urlParams); | |
d154efb9 TO |
39 | var setContactId = function(newContactId) { |
40 | if (newContactId != $(hiddenEl).val()) { | |
41 | $(hiddenEl).val(newContactId); | |
42 | $(hiddenEl).trigger('change'); | |
43 | } | |
44 | if (activeContactId != newContactId) { | |
45 | activeContactId = newContactId; | |
46 | ||
47 | if (activeContactId) { | |
48 | // lookup the name | |
49 | $(widgetEl).css({visibility: 'hidden'}); // don't allow input during ajax | |
50 | $.ajax({ | |
51 | url : contactUrl + '&id=' + newContactId, | |
52 | async : false, | |
53 | success : function(html){ | |
54 | var htmlText = html.split( '|' , 2); | |
55 | $(widgetEl).val(htmlText[0]); | |
56 | $(widgetEl).css({visibility: 'visible'}); | |
57 | } | |
58 | }); | |
59 | } else { | |
60 | // there is no name to lookup - just show a blank | |
61 | $(widgetEl).val(''); | |
62 | } | |
63 | } | |
64 | }; | |
65 | ||
66 | $(hiddenEl).after(widgetEl); | |
67 | $(hiddenEl).hide(); | |
68 | $(widgetEl).autocomplete(contactUrl, { | |
69 | width: 200, | |
70 | selectFirst: false, | |
71 | minChars: 1, | |
72 | matchContains: true, | |
73 | delay: 400 | |
74 | }).result(function(event, data) { | |
75 | activeContactId = data[1]; | |
76 | setContactId(activeContactId); | |
77 | }).bind('change blur', function() { | |
78 | if (! $(widgetEl).val()) { | |
79 | activeContactId = ''; | |
80 | setContactId(activeContactId); | |
81 | } | |
82 | }); | |
83 | ||
84 | $(hiddenEl).bind('change', function(){ | |
85 | setContactId($(this).val()); | |
86 | }); | |
87 | setContactId($(hiddenEl).val()); | |
88 | }); | |
89 | }; | |
90 | ||
91 | })(jQuery, CRM); |