From be878e3e79ecca3a87903f6d8b19067dcc787b6e Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Fri, 23 Mar 2012 18:57:13 +0000 Subject: [PATCH] Get radio customfield working in popup + nm --- etemplate/js/et2_extension_customfields.js | 9 ++ etemplate/js/et2_widget_radiobox.js | 171 +++++++++++++++++++-- 2 files changed, 171 insertions(+), 9 deletions(-) diff --git a/etemplate/js/et2_extension_customfields.js b/etemplate/js/et2_extension_customfields.js index 3367bb8d88..bd195bf186 100644 --- a/etemplate/js/et2_extension_customfields.js +++ b/etemplate/js/et2_extension_customfields.js @@ -304,6 +304,15 @@ var et2_customfields_list = et2_baseWidget.extend([et2_IDetachedDOM], { return true; }, + _setup_radio: function(field_name, field, attrs) { + // No label on the widget itself + delete(attrs.label); + + field.type = 'radiogroup'; + attrs.options = field.values; + return true; + }, + _setup_checkbox: function(field_name, field, attrs) { // Read-only checkbox is just text if(attrs.readonly) diff --git a/etemplate/js/et2_widget_radiobox.js b/etemplate/js/et2_widget_radiobox.js index 2d5ac01d23..71e4edaf04 100644 --- a/etemplate/js/et2_widget_radiobox.js +++ b/etemplate/js/et2_widget_radiobox.js @@ -54,23 +54,31 @@ var et2_radiobox = et2_inputWidget.extend({ }, createInputWidget: function() { - this.input = $j(document.createElement("input")).attr("type", "radio"); + this.input = $j(document.createElement("input")) + .val(this.options.set_value) + .attr("type", "radio"); this.input.addClass("et2_radiobox"); this.setDOMNode(this.input[0]); }, + set_name: function(_name) { + if(_name.substr(_name.length-2) != "[]") + { + _name += "[]"; + } + this.input.attr("name", _name); + }, + /** * Override default to match against set/unset value */ set_value: function(_value) { - if(_value != this.value) { - if(_value == this.set_value) { - this.input.attr("checked", "checked"); - } else { - this.input.removeAttr("checked"); - } + if(_value == this.options.set_value) { + this.input.attr("checked", "checked"); + } else { + this.input.removeAttr("checked"); } }, @@ -78,11 +86,156 @@ var et2_radiobox = et2_inputWidget.extend({ * Override default to return unchecked value */ getValue: function() { - if(this.input.attr("checked")) { - return this.set_value; + if(jQuery("input:checked", this._parent.getDOMNode()).val() == this.options.set_value) { + return this.options.set_value; } + return null; } }); et2_register_widget(et2_radiobox, ["radio"]); +var et2_radiobox_ro = et2_valueWidget.extend([et2_IDetachedDOM], { + + attributes: { + "set_value": { + "name": "Set value", + "type": "string", + "default": "true", + "description": "Value when selected" + }, + "ro_true": { + "name": "Read only selected", + "type": "string", + "default": "x", + "description": "What should be displayed when readonly and selected" + }, + "ro_false": { + "name": "Read only unselected", + "type": "string", + "default": "", + "description": "What should be displayed when readonly and not selected" + }, + "label": { + "name": "Label", + "default": "", + "type": "string", + } + }, + init: function() { + this._super.apply(this, arguments); + + this.value = ""; + this.span = $j(document.createElement("span")) + .addClass("et2_radiobox"); + + this.setDOMNode(this.span[0]); + }, + + /** + * Override default to match against set/unset value + */ + set_value: function(_value) { + if(_value == this.options.set_value) { + this.span.text(this.options.ro_true); + } else { + this.span.text(this.options.ro_false); + } + }, + /** + * Code for implementing et2_IDetachedDOM + */ + getDetachedAttributes: function(_attrs) + { + // Show label in nextmatch instead of just x + this.options.ro_true = this.options.label; + _attrs.push("value"); + }, + + getDetachedNodes: function() + { + return [this.span[0]]; + }, + + setDetachedAttributes: function(_nodes, _values) + { + this.span = jQuery(_nodes[0]); + this.set_value(_values["value"]); + } +}); + +et2_register_widget(et2_radiobox_ro, ["radio_ro"]); + + +/** + * A group of radio buttons + */ +var et2_radioGroup = et2_box.extend({ + + attributes: { + "value": { + "name": "Value", + "type": "string", + "default": "true", + "description": "Value for each radio button" + }, + "ro_true": { + "name": "Read only selected", + "type": "string", + "default": "x", + "description": "What should be displayed when readonly and selected" + }, + "ro_false": { + "name": "Read only unselected", + "type": "string", + "default": "", + "description": "What should be displayed when readonly and not selected" + }, + "options": { + "name": "Radio options", + "type": "any", + "default": {}, + "description": "Options for radio buttons. Should be {value: label, ...}" + } + }, + + createNamespace: false, + + init: function(parent, attrs) { + attrs.type = "vbox"; + this._super.apply(this, arguments); + }, + + set_value: function(_value) { + this.value = _value; + for (var i = 0; i < this._children.length; i++) + { + var radio = this._children[i]; + radio.set_value(_value); + } + }, + + /** + * Set a bunch of radio buttons + * Options should be {value: label, ...} + */ + set_options: function(_options) { + for(var key in _options) + { + var attrs = { + // Add index so radios work properly + "id": (this.options.readonly ? this.id : this.id + "[" + "]"), + set_value: key, + label: _options[key], + ro_true: this.options.ro_true, + ro_false: this.options.ro_false, + readonly: this.options.readonly + } + var radio = et2_createWidget("radio", attrs, this); +// radio.set_name(this.id); + } + this.set_value(this.value); + } +}); +// No such tag as 'radiogroup', but it needs something +et2_register_widget(et2_radioGroup, ["radiogroup"]);