
var RadioGroup = require ('./radio-group');

module.exports = new Class
({
	Extends: Htk.Field
	,Tag: 'htk-radio'
	,Properties:
	{
		tip:
		{
			type: String
			,set: function (x)
			{
				if (x)
					this.node.title = _(x);
			}
		},
		radioGroup:
		{
			type: RadioGroup
			,set: function (x)
			{
				this.link ({_radioGroup: x}, {changed: this._onRadioGroupChange});
				this.node.name = x.radioName
				this._onRadioGroupChange ();
			}
			,get: function ()
			{
				return this._radioGroup;
			}
		}
	}

	,initialize: function (props)
	{
		Object.assign (this, {
			_radioGroup: null
		});
		this.parent (props);
	}

	,render: function ()
	{
		var radio = this.createRoot ('input');
		radio.type = 'radio';
		radio.name = '';
		radio.checked = false;
		radio.addEventListener ('change', this._onChange.bind (this));
		this._node = radio;
	}

	,_onChange: function ()
	{
		if (this.node.checked && this._radioGroup)
			this._radioGroup.value = this.value;
	}
	
	,_onRadioGroupChange: function ()
	{
		if (this._radioGroup.value && this._radioGroup.value == this.value)
			this.node.checked = true;
		else
			this.node.checked = false;
	}

	,putValue: function (value)
	{
		if (!value)
			this.node.value = '';
		else
			this.node.value = value;
	}

	,setEditable: function (editable)
	{
		this.node.disabled = !editable;
	}
});
