Commit 6b4c7995 authored by ccarigna's avatar ccarigna
Browse files

Added javascript for getting location data from a central location, removed form AJAX calls

parent 2d10ef5f
/** Adds the ability to select a pre-existing location, making an AJAX call to populate the data from a central location
*/
(function ($) {
Drupal.behaviors.uw_ct_event = {
attach: function (context, settings) {
$loader = $('<div>').addClass('ajax-progress ajax-progress-throbber')
.append($('<div>').addClass('throbber'))
.append($('<div>Please wait...</div>').addClass('message'));
$locations = $('<select>').addClass('form-select').attr('id', settings.uw_ct_event.selectid);
//get list of possible locations
$.getJSON(settings.uw_ct_event.url + '/list', function(data) {
if (data && !data.error) {
$locations.append($('<option>').text(settings.uw_ct_event.custom).attr('value', 'custom'));
$.each(data, function(key, value) {
$locations.append($('<option>').text(value.title).attr('value', value.nid));
});
//add the list to the page
$locations.insertBefore('#uw_event_location', context)
$locations.wrap($('<div>').addClass('form-item'));
$locations.before($('<label>').text(settings.uw_ct_event.label).attr('for', settings.uw_ct_event.selectid));
$locations.after($('<div>').text(settings.uw_ct_event.description).addClass('description'));
//get location data when the list changes
$locations.change(function() {
if ($locations.val() != 'custom') {
// enable throbber
$loader.insertAfter($locations);
$locations.addClass('progress-disabled').attr('disabled', 'disabled');
$.getJSON(settings.uw_ct_event.url + '/get/' + $locations.val(), function(data) {
// success, disable throbber
$loader.remove();
$locations.removeClass('progress-disabled').removeAttr('disabled');
if (data && !data.error) {
_populate_location_data(data);
}
});
}
else {
_populate_location_data(
{
name: '',
additional: '',
street: '',
city: '',
province: '',
postal_code: '',
country: 'ca'
}
);
}
});
}
});
}
};
function _populate_location_data(data) {
$('#edit-field-event-location-und-0-name').val(data.name);
$('#edit-field-event-location-und-0-additional').val(data.additional);
$('#edit-field-event-location-und-0-street').val(data.street);
$('#edit-field-event-location-und-0-city').val(data.city);
$('#edit-field-event-location-und-0-province').val(data.province);
$('#edit-field-event-location-und-0-postal-code').val(data.postal_code);
$('#edit-field-event-location-und-0-country').val(data.country);
}
}(jQuery));
......@@ -70,10 +70,29 @@ function uw_ct_event_form_alter(&$form, &$form_state, $form_id) {
*/
function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_id) {
/* Add javascript
//* Add javascript for adding location data through AJAX
// there needs to be a central website to get location data from (that has the uw_ct_location feature enabled, populated with content)
// the location of that site is in a Drupal variable, uw_location_data_json_url, and defaults to the current site if it is not set
// (see the $settings array below)
$form['#attached']['js'] = array(
drupal_get_path('module', 'uw_ct_event') . '/js/location-ajax.js',
);
// settings used in location-ajax.js
global $base_url;
$settings = array(
'label' => t('Location lookup'),
'description' => t('Choose a location or enter location information below.'),
'custom' => t('- Custom location -'),
'url' => variable_get('uw_location_data_json_url', $base_url . '/location/json'),
'selectid' => 'populate-location-data',
);
$form['#attached']['js'][] = array(
'data' => array('uw_ct_event' => $settings),
'type' => 'setting',
);
// */
// Remove the 'delete this location' checkbox on the edit form
......@@ -84,6 +103,8 @@ function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_i
$form['field_event_location']['#prefix'] = '<div id="uw_event_location">';
$form['field_event_location']['#suffix'] = '</div>';
/* no longer use Drupal internal AJAX
// Have to set the AJAX callback parameter on the field language instance value
// Also see this issue for some extra insight: http://drupal.org/node/1024962
// Specifically on setting another form field value - as we need to below.
......@@ -125,17 +146,19 @@ function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_i
$form['field_event_location']['und'][0]['#default_value']['locpick']['user_latitude'] = '49.001';
//dsm($form);
// */
}
/* AJAX Callback function defined in uw_ct_contact_form_contact_node_form_alter
* Must not modify the form in the AJAX callback - just return what to replace
*/
*
function uw_ct_event_load_location_details($form, $form_state) {
// The form has already been submitted and updated. We can return the replaced
// item as it is.
return $form['field_event_location'];
}
// */
/**
* Remove the delete checkbox from location element.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment