1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary Radiobutton list  
  5  */
  6 
  7 goog.provide('Banana.Controls.DataControls.ListControls.RadioButtonList');
  8 
  9 goog.require('Banana.Controls.DataControls.ListControls.CustomListControl');
 10 goog.require('Banana.Controls.DataControls.RadioButton');
 11 
 12 /** @namespace Banana.Controls.RadioButtonList */
 13 namespace('Banana.Controls').RadioButtonList = Banana.Controls.CustomListControl.extend(
 14 /** @lends Banana.Controls.RadioButtonList.prototype */	
 15 {
 16 	/**
 17 	 * Creates a Radiobutton list.
 18 	 * 
 19 	 * use setDataSource() to create a group of radiobuttons
 20 	 * use setData() to select radiobuttons
 21 	 * use SetGroupName() to make a group of radiobuttons (only 1 of a group can be selected)
 22 	 * 
 23 	 * Example:
 24 	 
 25 	 var rbl = new Banana.Controls.RadioButtonList();
 26 	 
 27 	 this.addControl(rbl);
 28 	 
 29 	 rbl.setDataSource([1,2,3,4,5,6,7,8,9]);
 30 	 rbl.setData([5,6,2]);
 31 	 
 32 	 
 33 	 * @constructs
 34 	 * @extends Banana.Controls.CustomListControl
 35 	 */	
 36 	init : function()
 37 	{
 38 		this._super();
 39 		this.addCssClass('BRadioButtonList');
 40 	}
 41 	
 42 });
 43 
 44 /**
 45  * Invoked after calling setData or setDataSource
 46  * @ignore
 47  */
 48 Banana.Controls.RadioButtonList.prototype.createControls = function()
 49 {
 50 	this.panel = new Banana.Controls.Panel();
 51 	this.panel.addCssClass('BRadioButtonListItemContainer');
 52 
 53 	for(var prop in this.datasource)
 54 	{
 55 		if (typeof(this.datasource[prop]) == 'function') continue;
 56 		
 57 		var radio = new Banana.Controls.RadioButton();
 58 		radio.setId(prop);
 59 		radio.setValue(prop);
 60 
 61 		if (this.getGroupName())
 62 		{
 63 			radio.setName(this.getGroupName());
 64 		}
 65 
 66 		radio.bind('change',this.getProxy(function(sender)
 67 		{
 68 			this.onCheckBoxChanged(sender.data);
 69 			this.triggerEvent('dataChanged',this.getData());
 70 
 71 		}),radio);
 72 
 73 		if (this.data)
 74 		{
 75 			if (prop == this.data)
 76 			{
 77 				radio.setData(true);
 78 			}
 79 		}
 80 
 81 		var control = new Banana.Controls.Decorators.LabelDecoratorRight(radio).setData(this.datasource[prop]);
 82 
 83 		this.panel.addControl(control);
 84 	}
 85 
 86 	this.addControl(this.panel);
 87 	//prevent parent auto height issue
 88 	this.addControl('<div style="clear:both;"></div>');
 89 };
 90 
 91 /**
 92  * A groupname ensures that only 1 radio button can be selected from a list of radio's
 93  *
 94  * @param {String} name of the group, must be unique
 95  * @return {this}
 96  */
 97 Banana.Controls.RadioButtonList.prototype.setGroupName = function(name)
 98 {
 99 	this.groupName = name;
100 	return this;
101 };
102 
103 /**
104  * @return {String}
105  */
106 Banana.Controls.RadioButtonList.prototype.getGroupName = function()
107 {
108 	return this.groupName;
109 };
110 
111 /**
112  * callback invoked after chaning a radiobutton.
113  *
114  * @param {event} sender
115  * @ignore
116  */
117 Banana.Controls.RadioButtonList.prototype.onCheckBoxChanged = function(sender)
118 {
119 	this.data = sender.getValue();
120 
121 	this.isChanged = true;
122 };