CRM-15172 - Add chainSelect method to CRM_Core_Form
[civicrm-core.git] / templates / CRM / common / stateCountry.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2014 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}
26 {if $config->stateCountryMap}
27 <script type="text/javascript">
28 {literal}
29 CRM.$(function($) {
30 var $form = $('form.{/literal}{$form.formClass}{literal}');
31 function chainSelect(e) {
32 var info = $(this).data('chainSelect');
33 var val = info.target.val();
34 var multiple = info.target.attr('multiple');
35 var placeholder = $(this).val() ? "{/literal}{ts escape='js'}Loading{/ts}{literal}..." : info.placeholder;
36 if (multiple) {
37 info.target.html('').prop('disabled', true).crmSelect2({placeholder: placeholder});
38 }
39 else {
40 info.target.html('<option value="">' + placeholder + '</option>').prop('disabled', true).crmSelect2();
41 }
42 if ($(this).val()) {
43 $.getJSON(info.callback, {_value: $(this).val()}, function(data) {
44 var options = '';
45 function buildOptions(data) {
46 $.each(data, function() {
47 if (this.children) {
48 options += '<optgroup label="' + this.name + '">';
49 buildOptions(this.children);
50 options += '</optgroup>';
51 }
52 else if (this.value || !multiple) {
53 options += '<option value="' + this.value + '">' + this.name + '</option>';
54 }
55 else {
56 info.target.crmSelect2({placeholder: this.name});
57 }
58 });
59 }
60 buildOptions(data);
61 info.target.html(options).val(val).prop('disabled', false).trigger('change');
62 });
63 }
64 else {
65 info.target.trigger('change');
66 }
67 }
68 function initField(selector, removePlaceholder) {
69 var $el = $(selector, $form);
70 if (removePlaceholder !== false) {
71 $el.removeAttr('placeholder');
72 }
73 return $el.css('min-width', '20em').crmSelect2();
74 }
75 {/literal}
76 {foreach from=$config->stateCountryMap item=stateCountryMap}
77 {if $stateCountryMap.state_province && $stateCountryMap.county}
78 $('select[name="{$stateCountryMap.state_province}"], select#{$stateCountryMap.state_province}', $form).data('chainSelect', {ldelim}
79 callback: CRM.url('civicrm/ajax/jqCounty'),
80 target: initField('select[name="{$stateCountryMap.county}"], #{$stateCountryMap.county}'),
81 placeholder: "{ts escape='js'}Choose state first{/ts}"
82 {rdelim}).on('change', chainSelect);
83 {/if}
84 {if $stateCountryMap.country && $stateCountryMap.state_province}
85 initField('select[name="{$stateCountryMap.country}"], select#{$stateCountryMap.country}', false).data('chainSelect', {ldelim}
86 callback: CRM.url('civicrm/ajax/jqState'),
87 target: initField('select[name="{$stateCountryMap.state_province}"], #{$stateCountryMap.state_province}'),
88 placeholder: "{ts escape='js'}Choose country first{/ts}"
89 {rdelim}).on('change', chainSelect).change();
90 {/if}
91 {/foreach}
92 {literal}
93 });
94 {/literal}
95 </script>
96 {/if}