| 1 |
/* $Id$ */
|
| 2 |
|
| 3 |
if (!Drupal.ConditionalFields) {
|
| 4 |
Drupal.ConditionalFields = {};
|
| 5 |
}
|
| 6 |
|
| 7 |
Drupal.ConditionalFields.switchField = function(id, values, onPageReady) {
|
| 8 |
/* For each controlling field: find the controlled fields */
|
| 9 |
$.each(Drupal.settings.ConditionalFields.controlling_fields, function(controllingField, controlledFields) {
|
| 10 |
if (controllingField == id) {
|
| 11 |
/* Find the settings of the controlled field */
|
| 12 |
$.each(controlledFields, function(i, fieldSettings) {
|
| 13 |
Drupal.ConditionalFields.doAnimation(fieldSettings, 'hide', onPageReady);
|
| 14 |
/* Find the trigger values of the controlled field (for this controlling field) */
|
| 15 |
$.each(fieldSettings.trigger_values, function(ii, val) {
|
| 16 |
if (jQuery.inArray(val, values) != -1) {
|
| 17 |
Drupal.ConditionalFields.doAnimation(fieldSettings, 'show', onPageReady);
|
| 18 |
/* Stop searching in this field */
|
| 19 |
return false;
|
| 20 |
}
|
| 21 |
});
|
| 22 |
/* To do: Feature: Multiple controlling fields on the same field, are
|
| 23 |
not supported for now. Test: other controlling fields types and widgets. */
|
| 24 |
});
|
| 25 |
}
|
| 26 |
});
|
| 27 |
}
|
| 28 |
|
| 29 |
Drupal.ConditionalFields.doAnimation = function(fieldSettings, showOrHide, onPageReady) {
|
| 30 |
/* Multiple fields are enclosed in a wrapper */
|
| 31 |
if ($(fieldSettings.field_id).parents('#' + fieldSettings.field_id.substring(13) + '-add-more-wrapper').length == 1) {
|
| 32 |
var toSwitch = $('#' + fieldSettings.field_id.substring(13) + '-add-more-wrapper');
|
| 33 |
} else {
|
| 34 |
var toSwitch = $(fieldSettings.field_id);
|
| 35 |
}
|
| 36 |
|
| 37 |
if (Drupal.settings.ConditionalFields.ui_settings == 'disable') {
|
| 38 |
var disabled = '';
|
| 39 |
if (showOrHide == 'hide') {
|
| 40 |
disabled = '';
|
| 41 |
}
|
| 42 |
toSwitch.find('textarea, input, select').attr('disabled', disabled);
|
| 43 |
}
|
| 44 |
/* Avoid flickering */
|
| 45 |
else if (onPageReady == true) {
|
| 46 |
/* Setting css instead of simply hiding to avoid interference from collapse.js */
|
| 47 |
showOrHide == 'show' ? toSwitch.show() : toSwitch.css('display', 'none');
|
| 48 |
}
|
| 49 |
else {
|
| 50 |
switch (Drupal.settings.ConditionalFields.ui_settings.animation) {
|
| 51 |
case 0:
|
| 52 |
showOrHide == 'show' ? toSwitch.show() : toSwitch.hide();
|
| 53 |
case 1:
|
| 54 |
showOrHide == 'show' ? toSwitch.slideDown(Drupal.settings.ConditionalFields.ui_settings.anim_speed) :
|
| 55 |
toSwitch.slideUp(Drupal.settings.ConditionalFields.ui_settings.anim_speed);
|
| 56 |
case 2:
|
| 57 |
showOrHide == 'show' ? toSwitch.fadeIn(Drupal.settings.ConditionalFields.ui_settings.anim_speed) :
|
| 58 |
toSwitch.fadeOut(Drupal.settings.ConditionalFields.ui_settings.anim_speed);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
Drupal.ConditionalFields.findValues = function(field) {
|
| 64 |
var values = [];
|
| 65 |
field.find('option:selected, input:checked').each( function() {
|
| 66 |
if ($(this)[0].selected || $(this)[0].checked) {
|
| 67 |
values[values.length] = this.value;
|
| 68 |
}
|
| 69 |
});
|
| 70 |
return values;
|
| 71 |
}
|
| 72 |
|
| 73 |
Drupal.ConditionalFields.fieldChange = function() {
|
| 74 |
var values = Drupal.ConditionalFields.findValues($(this));
|
| 75 |
var id = '#' + $(this).attr('id');
|
| 76 |
Drupal.ConditionalFields.switchField(id, values, false);
|
| 77 |
}
|
| 78 |
|
| 79 |
Drupal.behaviors.ConditionalFields = function (context) {
|
| 80 |
$('.node-form, #user-register').find('.controlling-field:not(.conditional-field-processed)', context).addClass('conditional-field-processed').each(function () {
|
| 81 |
/* Set default state */
|
| 82 |
Drupal.ConditionalFields.switchField('#' + $(this).attr('id'), Drupal.ConditionalFields.findValues($(this)), true);
|
| 83 |
/* Add events. Apparently, Explorer doesn't catch the change event? */
|
| 84 |
$.browser.msie == true ? $(this).click(Drupal.ConditionalFields.fieldChange) : $(this).change(Drupal.ConditionalFields.fieldChange);
|
| 85 |
});
|
| 86 |
};
|