Commit | Line | Data |
---|---|---|
444f2d43 | 1 | /** |
c8c0f9a5 | 2 | * @licstart The following is the entire license notice for the JavaScript code in this page. |
24e57837 | 3 | * |
444f2d43 DT |
4 | * FSF Member Dashboard |
5 | * Copyright © 2014 Free Software Foundation, Inc. | |
6 | * | |
7 | * This file is a part of FSF Member Dashboard. | |
8 | * | |
c8c0f9a5 DT |
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. | |
444f2d43 DT |
13 | * |
14 | * FSF Member Dashboard is distributed in the hope that it will be | |
15 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
c8c0f9a5 DT |
16 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU Affero General Public License for more details. | |
444f2d43 DT |
18 | * |
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/>. | |
24e57837 | 22 | * |
c8c0f9a5 | 23 | * @licend The above is the entire license notice for the JavaScript code in this page |
444f2d43 DT |
24 | */ |
25 | ||
5bf8e499 | 26 | (function($) { |
59b55e97 DT |
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; | |
31 | } | |
32 | ||
444f2d43 | 33 | function loadProfile(gid, success) { |
5bf8e499 | 34 | $.get('/civicrm/profile/edit', { |
444f2d43 DT |
35 | gid: gid, |
36 | json: 1 | |
37 | }, success); | |
38 | } | |
39 | ||
40 | function renderProfile(profile) { | |
41 | function renderFields() { | |
42 | return _.chain(profile) | |
43 | .values() | |
44 | .filter(function(field) { | |
45 | return _.isString(field.label) && _.isString(field.html); | |
46 | }) | |
47 | .map(function (field) { | |
0ad4e96b DT |
48 | return $('<div class="crm-section"></div>') |
49 | .append($('<div class="form-item"></div>') | |
50 | .append($('<div class="label"></div>') | |
51 | .append(field.label)) | |
52 | .append($('<div class="content"></div>') | |
53 | .append(field.html)) | |
54 | .append($('<div class="clear"></div>'))); | |
444f2d43 DT |
55 | }) |
56 | .value(); | |
57 | } | |
58 | ||
59b55e97 | 59 | profile = parseJSONMaybe(profile); |
444f2d43 DT |
60 | var form = $('<form' + profile.attributes + '></form>'); |
61 | ||
62 | form.append(profile.requirednote) | |
63 | .append(profile.hidden) | |
64 | .append.apply(form, renderFields()); | |
65 | ||
473ce7cb | 66 | $('#info-loading').remove(); |
5bf8e499 | 67 | $('#info-form').append(form); |
444f2d43 DT |
68 | } |
69 | ||
59b55e97 DT |
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"; | |
76 | ||
77 | $(countryId).change(function() { | |
78 | $.get(url, { | |
79 | _id: $(this).attr('id'), | |
80 | _name: $(this).attr('name'), | |
81 | _value: $(this).val() | |
82 | }, function(states) { | |
83 | var stateSelect = $(stateProvinceId); | |
84 | ||
85 | function addOption(key, value) { | |
86 | stateSelect.get(0).add(new Option(key, value)); | |
87 | } | |
88 | ||
89 | states = parseJSONMaybe(states); | |
90 | stateSelect.empty(); | |
91 | ||
92 | if(_.isEmpty(states)) { | |
93 | addOption('- select a country -', ''); | |
94 | } else { | |
95 | _.each(states, function(state) { | |
96 | // Civi 4.4 use value, Civi 4.5+ uses key. | |
97 | if(state.key) { | |
98 | addOption(state.value, state.key); | |
99 | } else { | |
100 | addOption(state.name, state.value); | |
101 | } | |
102 | }); | |
103 | } | |
104 | }); | |
105 | }); | |
106 | } | |
107 | ||
444f2d43 | 108 | $(document).ready(function() { |
59b55e97 DT |
109 | loadProfile(memberDashboard.profileId, function(profile) { |
110 | renderProfile(profile); | |
111 | watchForCountryChanges(); | |
112 | }); | |
444f2d43 | 113 | }); |
5bf8e499 | 114 | })(jQuery); |