From 3cea91884b28b52df4e38f2ba88c00b65071a81f Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Sat, 25 Oct 2008 04:11:30 +0000 Subject: Bug 291433: Ability to have custom fields whose visibility depends on the values of other fields Patch By Max Kanat-Alexander r=bbaetz, a=mkanat --- js/field.js | 36 ++++++++++++++++++++++++++++++++++ js/util.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) (limited to 'js') diff --git a/js/field.js b/js/field.js index 497c9d2c1..fb8716872 100644 --- a/js/field.js +++ b/js/field.js @@ -323,3 +323,39 @@ function boldOnChange(e, field_id){ } } } + +/** + * Says that a field should only be displayed when another field has + * a certain value. May only be called after the controller has already + * been added to the DOM. + */ +function showFieldWhen(controlled_id, controller_id, value) { + var controller = document.getElementById(controller_id); + // Note that we don't get an object for "controlled" here, because it + // might not yet exist in the DOM. We just pass along its id. + YAHOO.util.Event.addListener(controller, 'change', + handleVisControllerValueChange, [controlled_id, controller, value]); +} + +/** + * Called by showFieldWhen when a field's visibility controller + * changes values. + */ +function handleVisControllerValueChange(e, args) { + var controlled_id = args[0]; + var controller = args[1]; + var value = args[2]; + + var label_container = + document.getElementById('field_label_' + controlled_id); + var field_container = + document.getElementById('field_container_' + controlled_id); + if (bz_valueSelected(controller, value)) { + YAHOO.util.Dom.removeClass(label_container, 'bz_hidden_field'); + YAHOO.util.Dom.removeClass(field_container, 'bz_hidden_field'); + } + else { + YAHOO.util.Dom.addClass(label_container, 'bz_hidden_field'); + YAHOO.util.Dom.addClass(field_container, 'bz_hidden_field'); + } +} diff --git a/js/util.js b/js/util.js index ce7ea4cae..feef8fe41 100644 --- a/js/util.js +++ b/js/util.js @@ -154,3 +154,68 @@ function bz_isValueInArray(aArray, aValue) return false; } + +/** + * Create wanted options in a select form control. + * + * @param aSelect Select form control to manipulate. + * @param aValue Value attribute of the new option element. + * @param aTextValue Value of a text node appended to the new option + * element. + * @return Created option element. + */ +function bz_createOptionInSelect(aSelect, aTextValue, aValue) { + var myOption = new Option(aTextValue, aValue); + aSelect.appendChild(myOption); + return myOption; +} + +/** + * Clears all options from a select form control. + * + * @param aSelect Select form control of which options to clear. + */ +function bz_clearOptions(aSelect) { + + var length = aSelect.options.length; + + for (var i = 0; i < length; i++) { + aSelect.removeChild(aSelect.options[0]); + } +} + +/** + * Takes an array and moves all the values to an select. + * + * @param aSelect Select form control to populate. Will be cleared + * before array values are created in it. + * @param aArray Array with values to populate select with. + */ +function bz_populateSelectFromArray(aSelect, aArray) { + // Clear the field + bz_clearOptions(aSelect); + + for (var i = 0; i < aArray.length; i++) { + var item = aArray[i]; + bz_createOptionInSelect(aSelect, item[1], item[0]); + } +} + +/** + * Tells you whether or not a particular value is selected in a select, + * whether it's a multi-select or a single-select. The check is + * case-sensitive. + * + * @param aSelect The select you're checking. + * @param aValue The value that you want to know about. + */ +function bz_valueSelected(aSelect, aValue) { + var options = aSelect.options; + for (var i = 0; i < options.length; i++) { + if (options[i].selected && options[i].value == aValue) { + return true; + } + } + return false; +} + -- cgit v1.2.3-24-g4f1b