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