info: Fix state select box not updating when country is changed.
[org.fsf.memberdashboard.git] / assets / js / member-info.js
1 /**
2 * FSF Member Dashboard
3 * Copyright © 2014 Free Software Foundation, Inc.
4 *
5 * This file is a part of FSF Member Dashboard.
6 *
7 * FSF Member Dashboard is free software; you can copy, modify, and
8 * distribute it under the terms of the GNU Affero General Public
9 * License Version 3, 19 November 2007 and the CiviCRM Licensing
10 * Exception.
11 *
12 * FSF Member Dashboard is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FSF Member Dashboard. If not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 (function($) {
23 // Helper function to support older versions of jQuery that don't
24 // parse the JSON returned from AJAX calls by default.
25 function parseJSONMaybe(obj) {
26 return _.isString(obj) ? JSON.parse(obj): obj;
27 }
28
29 function loadProfile(gid, success) {
30 $.get('/civicrm/profile/edit', {
31 gid: gid,
32 json: 1
33 }, success);
34 }
35
36 function renderProfile(profile) {
37 function renderFields() {
38 return _.chain(profile)
39 .values()
40 .filter(function(field) {
41 return _.isString(field.label) && _.isString(field.html);
42 })
43 .map(function (field) {
44 return $('<div class="crm-section"></div>')
45 .append($('<div class="form-item"></div>')
46 .append($('<div class="label"></div>')
47 .append(field.label))
48 .append($('<div class="content"></div>')
49 .append(field.html))
50 .append($('<div class="clear"></div>')));
51 })
52 .value();
53 }
54
55 profile = parseJSONMaybe(profile);
56 var form = $('<form' + profile.attributes + '></form>');
57
58 form.append(profile.requirednote)
59 .append(profile.hidden)
60 .append.apply(form, renderFields());
61
62 $('#info-loading').remove();
63 $('#info-form').append(form);
64 }
65
66 // Fetch states/provinces via AJAX.
67 function watchForCountryChanges() {
68 // TODO: Fetch these ids from the profile JSON.
69 var countryId = "#country-Primary, #country-1";
70 var stateProvinceId = "#state_province-Primary, #state_province-1";
71 var url = "/civicrm/ajax/jqState";
72
73 $(countryId).change(function() {
74 $.get(url, {
75 _id: $(this).attr('id'),
76 _name: $(this).attr('name'),
77 _value: $(this).val()
78 }, function(states) {
79 var stateSelect = $(stateProvinceId);
80
81 function addOption(key, value) {
82 stateSelect.get(0).add(new Option(key, value));
83 }
84
85 states = parseJSONMaybe(states);
86 stateSelect.empty();
87
88 if(_.isEmpty(states)) {
89 addOption('- select a country -', '');
90 } else {
91 _.each(states, function(state) {
92 // Civi 4.4 use value, Civi 4.5+ uses key.
93 if(state.key) {
94 addOption(state.value, state.key);
95 } else {
96 addOption(state.name, state.value);
97 }
98 });
99 }
100 });
101 });
102 }
103
104 $(document).ready(function() {
105 loadProfile(memberDashboard.profileId, function(profile) {
106 renderProfile(profile);
107 watchForCountryChanges();
108 });
109 });
110 })(jQuery);