From 8a2c791be3a4bd528e3f0ac920c7c98af8e30ffb Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Tue, 18 Jun 2013 20:55:13 +0000 Subject: [PATCH] For radiobox customfields, implement "empty" first value = extra group label --- etemplate/js/et2_extension_customfields.js | 8 ++- etemplate/js/et2_widget_radiobox.js | 68 ++++++++++++++++++++-- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/etemplate/js/et2_extension_customfields.js b/etemplate/js/et2_extension_customfields.js index 9938ce137a..1cb4fef435 100644 --- a/etemplate/js/et2_extension_customfields.js +++ b/etemplate/js/et2_extension_customfields.js @@ -411,9 +411,15 @@ var et2_customfields_list = et2_valueWidget.extend([et2_IDetachedDOM, et2_IInput return true; }, _setup_radio: function(field_name, field, attrs) { - // No label on the widget itself + // 'Empty' label will be first delete(attrs.label); + if(field.values && field.values['']) + { + attrs.label = field.values['']; + delete field.values['']; + } + field.type = 'radiogroup'; attrs.options = field.values; return true; diff --git a/etemplate/js/et2_widget_radiobox.js b/etemplate/js/et2_widget_radiobox.js index 8c1e34712b..4206b35e34 100644 --- a/etemplate/js/et2_widget_radiobox.js +++ b/etemplate/js/et2_widget_radiobox.js @@ -172,6 +172,7 @@ var et2_radiobox_ro = et2_valueWidget.extend([et2_IDetachedDOM], this.span.text(this.options.ro_false); } }, + /** * Code for implementing et2_IDetachedDOM */ @@ -199,11 +200,18 @@ et2_register_widget(et2_radiobox_ro, ["radio_ro"]); /** * A group of radio buttons * - * @augments et2_box + * @augments et2_valueWidget */ -var et2_radioGroup = et2_box.extend( +var et2_radioGroup = et2_valueWidget.extend( { attributes: { + "label": { + "name": "Label", + "default": "", + "type": "string", + "description": "The label is displayed above the list of radio buttons. The label can contain variables, as descript for name. If the label starts with a '@' it is replaced by the value of the content-array at this index (with the '@'-removed and after expanding the variables).", + "translate": true + }, "value": { "name": "Value", "type": "string", @@ -240,8 +248,15 @@ var et2_radioGroup = et2_box.extend( * @memberOf et2_radioGroup */ init: function(parent, attrs) { - attrs.type = "vbox"; this._super.apply(this, arguments); + this.node = $j(document.createElement("div")) + .addClass("et2_vbox") + .addClass("et2_box_widget"); + this.setDOMNode(this.node[0]); + + // The supported widget classes array defines a whitelist for all widget + // classes or interfaces child widgets have to support. + this.supportedWidgetClasses = [et2_radiobox]; }, set_value: function(_value) { @@ -253,6 +268,10 @@ var et2_radioGroup = et2_box.extend( } }, + getValue: function() { + return jQuery("input:checked", this.getDOMNode()).val(); + }, + /** * Set a bunch of radio buttons * Options should be {value: label, ...} @@ -270,10 +289,51 @@ var et2_radioGroup = et2_box.extend( readonly: this.options.readonly }; var radio = et2_createWidget("radio", attrs, this); -// radio.set_name(this.id); } this.set_value(this.value); + }, + + /** + * Set a label on the group of radio buttons + */ + set_label: function(_value) { + // Abort if ther was no change in the label + if (_value == this.label) + { + return; + } + + if (_value) + { + // Create the label container if it didn't exist yet + if (this._labelContainer == null) + { + this._labelContainer = $j(document.createElement("label")) + this.getSurroundings().insertDOMNode(this._labelContainer[0]); + } + + // Clear the label container. + this._labelContainer.empty(); + + // Create the placeholder element and set it + var ph = document.createElement("span"); + this.getSurroundings().setWidgetPlaceholder(ph); + + this._labelContainer + .append(document.createTextNode(_value)) + .append(ph); + } + else + { + // Delete the labelContainer from the surroundings object + if (this._labelContainer) + { + this.getSurroundings().removeDOMNode(this._labelContainer[0]); + } + this._labelContainer = null; + } } + }); // No such tag as 'radiogroup', but it needs something et2_register_widget(et2_radioGroup, ["radiogroup"]);