3 * Copyright © 2014 Free Software Foundation, Inc.
5 * This file is a part of FSF Member Dashboard.
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
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.
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/>.
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
;
29 function loadProfile(gid
, success
) {
30 $.get('/civicrm/profile/edit', {
36 function renderProfile(profile
) {
37 function renderFields() {
38 return _
.chain(profile
)
40 .filter(function(field
) {
41 return _
.isString(field
.label
) && _
.isString(field
.html
);
43 .map(function (field
) {
44 return $('<div class="crm-section"></div>')
45 .append($('<div class="form-item"></div>')
46 .append($('<div class="label"></div>')
48 .append($('<div class="content"></div>')
50 .append($('<div class="clear"></div>')));
55 profile
= parseJSONMaybe(profile
);
56 var form
= $('<form' + profile
.attributes
+ '></form>');
58 form
.append(profile
.requirednote
)
59 .append(profile
.hidden
)
60 .append
.apply(form
, renderFields());
62 $('#info-loading').remove();
63 $('#info-form').append(form
);
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";
73 $(countryId
).change(function() {
75 _id
: $(this).attr('id'),
76 _name
: $(this).attr('name'),
79 var stateSelect
= $(stateProvinceId
);
81 function addOption(key
, value
) {
82 stateSelect
.get(0).add(new Option(key
, value
));
85 states
= parseJSONMaybe(states
);
88 if(_
.isEmpty(states
)) {
89 addOption('- select a country -', '');
91 _
.each(states
, function(state
) {
92 // Civi 4.4 use value, Civi 4.5+ uses key.
94 addOption(state
.value
, state
.key
);
96 addOption(state
.name
, state
.value
);
104 $(document
).ready(function() {
105 loadProfile(memberDashboard
.profileId
, function(profile
) {
106 renderProfile(profile
);
107 watchForCountryChanges();