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