| 1 |
// $Id: cck.js,v 1.5 2009/02/03 19:53:47 karens Exp $
|
| 2 |
(function($) {
|
| 3 |
|
| 4 |
Drupal.behaviors.cckManageFields = {
|
| 5 |
attach: function(context) {
|
| 6 |
attachUpdateSelects(context);
|
| 7 |
}
|
| 8 |
};
|
| 9 |
|
| 10 |
function attachUpdateSelects(context) {
|
| 11 |
var widgetTypes = Drupal.settings.cckWidgetTypes;
|
| 12 |
var fields = Drupal.settings.cckFields;
|
| 13 |
|
| 14 |
// Store the default text of widget selects.
|
| 15 |
$('#cck-field-overview .cck-widget-type-select', context).each(function() {
|
| 16 |
this.initialValue = this.options[0].text;
|
| 17 |
});
|
| 18 |
|
| 19 |
// 'Field type' select updates its 'Widget' select.
|
| 20 |
$('#cck-field-overview .cck-field-type-select', context).each(function() {
|
| 21 |
this.targetSelect = $('.cck-widget-type-select', $(this).parents('tr').eq(0));
|
| 22 |
|
| 23 |
$(this).change(function() {
|
| 24 |
var selectedFieldType = this.options[this.selectedIndex].value;
|
| 25 |
var options = (selectedFieldType in widgetTypes) ? widgetTypes[selectedFieldType] : [ ];
|
| 26 |
this.targetSelect.cckPopulateOptions(options);
|
| 27 |
});
|
| 28 |
|
| 29 |
// Trigger change on initial pageload to get the right widget options
|
| 30 |
// when field type comes pre-selected (on failed validation).
|
| 31 |
$(this).trigger('change');
|
| 32 |
});
|
| 33 |
|
| 34 |
// 'Existing field' select updates its 'Widget' select and 'Label' textfield.
|
| 35 |
$('#cck-field-overview .cck-field-select', context).each(function() {
|
| 36 |
this.targetSelect = $('.cck-widget-type-select', $(this).parents('tr').eq(0));
|
| 37 |
this.targetTextfield = $('.cck-label-textfield', $(this).parents('tr').eq(0));
|
| 38 |
|
| 39 |
$(this).change(function(e, updateText) {
|
| 40 |
var updateText = (typeof(updateText) == 'undefined') ? true : updateText;
|
| 41 |
var selectedField = this.options[this.selectedIndex].value;
|
| 42 |
var selectedFieldType = (selectedField in fields) ? fields[selectedField].type : null;
|
| 43 |
var selectedFieldWidget = (selectedField in fields) ? fields[selectedField].widget : null
|
| 44 |
var options = (selectedFieldType && (selectedFieldType in widgetTypes)) ? widgetTypes[selectedFieldType] : [ ];
|
| 45 |
this.targetSelect.cckPopulateOptions(options, selectedFieldWidget);
|
| 46 |
|
| 47 |
if (updateText) {
|
| 48 |
$(this.targetTextfield).attr('value', (selectedField in fields) ? fields[selectedField].label : '');
|
| 49 |
}
|
| 50 |
});
|
| 51 |
|
| 52 |
// Trigger change on initial pageload to get the right widget options
|
| 53 |
// and label when field type comes pre-selected (on failed validation).
|
| 54 |
$(this).trigger('change', false);
|
| 55 |
});
|
| 56 |
}
|
| 57 |
|
| 58 |
jQuery.fn.cckPopulateOptions = function(options, selected) {
|
| 59 |
return this.each(function() {
|
| 60 |
var disabled = false;
|
| 61 |
if (options.length == 0) {
|
| 62 |
options = [this.initialValue];
|
| 63 |
disabled = true;
|
| 64 |
}
|
| 65 |
|
| 66 |
// If possible, keep the same widget selected when changing field type.
|
| 67 |
// This is based on textual value, since the internal value might be
|
| 68 |
// different (options_buttons vs. nodereference_buttons).
|
| 69 |
var previousSelectedText = this.options[this.selectedIndex].text;
|
| 70 |
|
| 71 |
var html = '';
|
| 72 |
jQuery.each(options, function(value, text) {
|
| 73 |
// Figure out which value should be selected. The 'selected' param
|
| 74 |
// takes precedence.
|
| 75 |
var is_selected = ((typeof(selected) !== 'undefined' && value == selected) || (typeof(selected) == 'undefined' && text == previousSelectedText));
|
| 76 |
html += '<option value="' + value + '"' + (is_selected ? ' selected="selected"' : '') +'>' + text + '</option>';
|
| 77 |
});
|
| 78 |
|
| 79 |
$(this)
|
| 80 |
.html(html)
|
| 81 |
.attr('disabled', disabled ? 'disabled' : '');
|
| 82 |
});
|
| 83 |
}
|
| 84 |
})(jQuery);
|