Newer
Older
Yves Chedemois
committed
// $Id$
Yves Chedemois
committed
Drupal.behaviors.cckManageFields = function(context) {
attachUpdateSelects(context);
Yves Chedemois
committed
};
Yves Chedemois
committed
function attachUpdateSelects(context) {
var widgetTypes = Drupal.settings.contentWidgetTypes;
var fields = Drupal.settings.contentFields;
// Store the default text of widget selects.
$('#content-field-overview .content-widget-type-select', context).each(function() {
this.initialValue = this.options[0].text;
});
Yves Chedemois
committed
// 'Field type' select updates its 'Widget' select.
Yves Chedemois
committed
$('#content-field-overview .content-field-type-select', context).each(function() {
Karen Stevenson
committed
this.targetSelect = $('.content-widget-type-select', $(this).parents('tr').eq(0));
Yves Chedemois
committed
$(this).change(function() {
var selectedFieldType = this.options[this.selectedIndex].value;
var options = (selectedFieldType in widgetTypes) ? widgetTypes[selectedFieldType] : [ ];
Yves Chedemois
committed
this.targetSelect.contentPopulateOptions(options);
Yves Chedemois
committed
});
Yves Chedemois
committed
// Trigger change on initial pageload to get the right widget options
// when field type comes pre-selected (on failed validation).
Yves Chedemois
committed
$(this).trigger('change');
});
Yves Chedemois
committed
// 'Existing field' select updates its 'Widget' select and 'Label' textfield.
Yves Chedemois
committed
$('#content-field-overview .content-field-select', context).each(function() {
Karen Stevenson
committed
this.targetSelect = $('.content-widget-type-select', $(this).parents('tr').eq(0));
this.targetTextfield = $('.content-label-textfield', $(this).parents('tr').eq(0));
Yves Chedemois
committed
$(this).change(function(e, updateText) {
var updateText = (typeof(updateText) == 'undefined') ? true : updateText;
var selectedField = this.options[this.selectedIndex].value;
var selectedFieldType = (selectedField in fields) ? fields[selectedField].type : null;
var selectedFieldWidget = (selectedField in fields) ? fields[selectedField].widget : null
var options = (selectedFieldType && (selectedFieldType in widgetTypes)) ? widgetTypes[selectedFieldType] : [ ];
Yves Chedemois
committed
this.targetSelect.contentPopulateOptions(options, selectedFieldWidget);
Yves Chedemois
committed
if (updateText) {
Yves Chedemois
committed
$(this.targetTextfield).attr('value', (selectedField in fields) ? fields[selectedField].label : '');
Yves Chedemois
committed
}
});
Yves Chedemois
committed
// Trigger change on initial pageload to get the right widget options
// and label when field type comes pre-selected (on failed validation).
Yves Chedemois
committed
$(this).trigger('change', false);
});
}
Yves Chedemois
committed
jQuery.fn.contentPopulateOptions = function(options, selected) {
Yves Chedemois
committed
return this.each(function() {
var disabled = false;
if (options.length == 0) {
options = [this.initialValue];
disabled = true;
}
Yves Chedemois
committed
// If possible, keep the same widget selected when changing field type.
// This is based on textual value, since the internal value might be
// different (optionwidgets_buttons vs. nodereference_buttons).
var previousSelectedText = this.options[this.selectedIndex].text;
Yves Chedemois
committed
var html = '';
jQuery.each(options, function(value, text) {
Yves Chedemois
committed
// Figure out which value should be selected. The 'selected' param
// takes precedence.
var is_selected = ((typeof(selected) !== 'undefined' && value == selected) || (typeof(selected) == 'undefined' && text == previousSelectedText));
html += '<option value="' + value + '"' + (is_selected ? ' selected="selected"' : '') +'>' + text + '</option>';
Yves Chedemois
committed
});
$(this)
.html(html)
.attr('disabled', disabled ? 'disabled' : '');
});
}