2 * @licstart The following is the entire license notice for the JavaScript code in this page.
5 * Copyright © 2014 Free Software Foundation, Inc.
7 * This file is a part of FSF Member Dashboard.
9 * FSF Member Dashboard is free software: you can redistribute it
10 * and/or modify it under the terms of the GNU Affero General Public
11 * License as published by the Free Software Foundation, either
12 * version 3 of the License, or (at your option) any later version.
14 * FSF Member Dashboard is distributed in the hope that it will be
15 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FSF Member Dashboard. If not, see
21 * <http://www.gnu.org/licenses/>.
23 * @licend The above is the entire license notice for the JavaScript code in this page
27 // Helper function to support older versions of jQuery that don't
28 // parse the JSON returned from AJAX calls by default.
29 function parseJSONMaybe(obj
) {
30 return _
.isString(obj
) ? JSON
.parse(obj
): obj
;
33 function loadProfile(gid
, success
) {
34 $.get('/civicrm/profile/edit', {
40 function renderProfile(profile
) {
41 function renderFields() {
42 return _
.chain(profile
)
44 .filter(function(field
) {
45 return _
.isString(field
.label
) && _
.isString(field
.html
);
47 .map(function (field
) {
48 return $('<div class="crm-section"></div>')
49 .append($('<div class="form-item"></div>')
50 .append($('<div class="label"></div>')
52 .append($('<div class="content"></div>')
54 .append($('<div class="clear"></div>')));
59 profile
= parseJSONMaybe(profile
);
60 var form
= $('<form' + profile
.attributes
+ '></form>');
62 form
.append(profile
.requirednote
)
63 .append(profile
.hidden
)
64 .append
.apply(form
, renderFields());
66 $('#info-loading').remove();
67 $('#info-form').append(form
);
70 // Fetch states/provinces via AJAX.
71 function watchForCountryChanges() {
72 // TODO: Fetch these ids from the profile JSON.
73 var countryId
= "#country-Primary, #country-1";
74 var stateProvinceId
= "#state_province-Primary, #state_province-1";
75 var url
= "/civicrm/ajax/jqState";
77 $(countryId
).change(function() {
79 _id
: $(this).attr('id'),
80 _name
: $(this).attr('name'),
83 var stateSelect
= $(stateProvinceId
);
85 function addOption(key
, value
) {
86 stateSelect
.get(0).add(new Option(key
, value
));
89 states
= parseJSONMaybe(states
);
92 if(_
.isEmpty(states
)) {
93 addOption('- select a country -', '');
95 _
.each(states
, function(state
) {
96 // Civi 4.4 use value, Civi 4.5+ uses key.
98 addOption(state
.value
, state
.key
);
100 addOption(state
.name
, state
.value
);
108 $(document
).ready(function() {
109 loadProfile(memberDashboard
.profileId
, function(profile
) {
110 renderProfile(profile
);
111 watchForCountryChanges();