| 1 | /** |
| 2 | * @licstart The following is the entire license notice for the JavaScript code in this page. |
| 3 | * |
| 4 | * FSF Member Dashboard |
| 5 | * Copyright © 2014 Free Software Foundation, Inc. |
| 6 | * |
| 7 | * This file is a part of FSF Member Dashboard. |
| 8 | * |
| 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. |
| 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 |
| 16 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 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/>. |
| 22 | * |
| 23 | * @licend The above is the entire license notice for the JavaScript code in this page |
| 24 | */ |
| 25 | |
| 26 | (function($) { |
| 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 | |
| 33 | function loadProfile(gid, success) { |
| 34 | $.get('/civicrm/profile/edit', { |
| 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) { |
| 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>'))); |
| 55 | }) |
| 56 | .value(); |
| 57 | } |
| 58 | |
| 59 | profile = parseJSONMaybe(profile); |
| 60 | var form = $('<form' + profile.attributes + '></form>'); |
| 61 | |
| 62 | form.append(profile.requirednote) |
| 63 | .append(profile.hidden) |
| 64 | .append.apply(form, renderFields()); |
| 65 | |
| 66 | $('#info-loading').remove(); |
| 67 | $('#info-form').append(form); |
| 68 | } |
| 69 | |
| 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 | |
| 108 | $(document).ready(function() { |
| 109 | loadProfile(memberDashboard.profileId, function(profile) { |
| 110 | renderProfile(profile); |
| 111 | watchForCountryChanges(); |
| 112 | }); |
| 113 | }); |
| 114 | })(jQuery); |