summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authormkanat%bugzilla.org <>2008-10-25 06:11:30 +0200
committermkanat%bugzilla.org <>2008-10-25 06:11:30 +0200
commit3cea91884b28b52df4e38f2ba88c00b65071a81f (patch)
treec0f451235176b2b542a38b0c935dcddf0453a1ee /js
parente0da20baba17b7f068946c8647fb6d67e77c39b7 (diff)
downloadbugzilla-3cea91884b28b52df4e38f2ba88c00b65071a81f.tar.gz
bugzilla-3cea91884b28b52df4e38f2ba88c00b65071a81f.tar.xz
Bug 291433: Ability to have custom fields whose visibility depends on the values of other fields
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=bbaetz, a=mkanat
Diffstat (limited to 'js')
-rw-r--r--js/field.js36
-rw-r--r--js/util.js65
2 files changed, 101 insertions, 0 deletions
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;
+}
+