1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary Base data control. Provides functionality to act as a data control 
  5  */
  6 
  7 goog.provide('Banana.Controls.DataControls.DataControl');
  8 
  9 goog.require('Banana.Controls.Panel');
 10 
 11 /** @namespace Banana.Controls.DataControl */
 12 namespace('Banana.Controls').DataControl = Banana.Controls.Panel.extend(
 13 /** @lends Banana.Controls.DataControl.prototype */
 14 {
 15 	/**
 16 	 * Creates a base data control. Most Data controls in Banana are derived from this base data control.
 17 	 * It adds set/get data support. And as well support to bind to datasets. @see Banana.Data.DataSet
 18 	 * Changes made in controls are automatically redirected to the data property to ensure always up to date data.
 19 	 * Still you can as a user bind on the 'dataChanged' event to listen to data changes.
 20 	 * Depending on the type of the control you can also use dom events to detect changes.  
 21 	 * 
 22 	 * @constructs
 23 	 * @extends Banana.Controls.Panel
 24 	 */
 25 	init : function()
 26 	{
 27 		this._super();
 28 		this.addCssClass('BDataControl')
 29 		this.isChanged = false;
 30 	},
 31 
 32 	/**
 33 	 * @override
 34 	 */
 35 	updateDisplay : function()
 36 	{
 37 		this._super();
 38 		this.setDomData(this.data);
 39 	}
 40 });
 41 
 42 /**
 43  *	Sets data on control.
 44  *	when this function is called we also trigger a datachanged event and update the dom.
 45  *
 46  *	@param {mixed} data for control
 47  *	@param {boolean} ignoreEvent when true no datachanged is triggered. This is useful when you are running in a circle or performance issues.
 48  *	@param {boolean} ignoreDom when true setDomData function is not called. Useful in cases of optimizing performance.
 49  *  @return {this}
 50  *
 51  */
 52 Banana.Controls.DataControl.prototype.setData = function(data,ignoreEvent,ignoreDom)
 53 {
 54 	this.data = data;
 55 
 56 	if (!ignoreEvent)
 57 	{
 58 		this.triggerEvent('dataChanged',data);
 59 	}
 60 
 61 	if (!ignoreDom)
 62 	{
 63 		this.setDomData(data);
 64 	}
 65 
 66 	return this;
 67 };
 68 
 69 /**
 70  * returns the most up to date data from the control
 71  *
 72  * @return {mixed} data
 73  */
 74 Banana.Controls.DataControl.prototype.getData = function()
 75 {
 76 	return this.data;
 77 };
 78 
 79 /**
 80  * binds data to the Dom element
 81  * 
 82  * @ignore
 83  * @param {mixed} data
 84  *
 85  */
 86 Banana.Controls.DataControl.prototype.setDomData = function(data)
 87 {
 88 	if (this.isRendered && this.data !== undefined)
 89 	{
 90 		Banana.Util.DomHelper.setData(data, this);
 91 	}
 92 };
 93 
 94 /**
 95  * @ignore
 96  * @return data from dom element
 97  */
 98 Banana.Controls.DataControl.prototype.getDomData = function()
 99 {
100 	if (this.isRendered)
101 	{
102 		return Banana.Util.DomHelper.getData(this);
103 	}
104 };
105 
106 /**
107  * binds a value from data from dataset to data in this control
108  *
109  * @param {mixed} Banana.Data.DataSet || name of the dataset
110  * @param {string} bind property of the data. can be like value.subvalue.subsubvalue
111  * @return {this}
112  */
113 Banana.Controls.DataControl.prototype.dataSetBind = function(set,bind)
114 {	
115 	this.bindedData = [set,bind];
116 	
117 	if (set instanceof Banana.Data.DataSet)
118 	{
119 		set.bindControlToData(this);
120 	}
121 	else if (this.isRendered)
122 	{	
123 		if (this.getPage().getDataSet(bd[0]))
124 		{
125 				this.getPage().getDataSet(bd[0]).bindControlToData(this);
126 		}
127 	}
128 	return this;
129 };
130 
131 /**
132  * Unbinds this control from dataset
133  * @param {mixed} Banana.Data.DataSet || name of the dataset
134  *
135  */
136 Banana.Controls.DataControl.prototype.unDataSetBind = function(set)
137 {
138 	this.bindedData = null;
139 
140 	if (set instanceof Banana.Data.DataSet)
141 	{
142 		set.bindControlToData(this);
143 		set.unBindControl(this);
144 	}
145 	else
146 	{
147 		set = this.getPage().getDataSet(set);
148 		set.unBindControl(this);
149 	}
150 	return this;
151 };
152