Merge in 5.20
[civicrm-core.git] / templates / CRM / Contribute / Form / PCP.js.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
4 | |
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
8 +--------------------------------------------------------------------+
9 *}
10 {literal}
11 <script type="text/javascript">
12 CRM.$(function($) {
13 var $form = $("form.{/literal}{$form.formClass}{literal}");
14
15 // FIXME: This could be much simpler as an entityRef field but pcp doesn't have a searchable api :(
16 var pcpURL = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1');
17 $('input[name=pcp_made_through_id]', $form).crmSelect2({
18 minimumInputLength: 1,
19 ajax: {
20 url: pcpURL,
21 data: function(term, page) {
22 return {term: term, page_num: page};
23 },
24 results: function(response) {
25 return response;
26 }
27 },
28 initSelection: function(el, callback) {
29 callback({id: $(el).val(), text: $('[name=pcp_made_through]', $form).val()});
30 }
31 })
32 // This is just a cheap trick to store the name when the form reloads
33 .on('change', function() {
34 var fieldNameVal = $(this).select2('data');
35 if (!fieldNameVal) {
36 fieldNameVal = '';
37 }
38 $('[name=pcp_made_through]', $form).val(fieldNameVal.text);
39 });
40 });
41 </script>
42 {/literal}