1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary Button control   
  5  */
  6 
  7 goog.provide('Banana.Controls.Button');
  8 
  9 goog.require('Banana.Controls.Panel');
 10 
 11 /** @namespace Banana.Controls.Button */
 12 namespace('Banana.Controls').Button = Banana.Controls.Panel.extend(
 13 /** @lends Banana.Controls.Button.prototype */
 14 {
 15 	/**
 16 	 * Creates a simple button
 17 	 * 
 18 	 * Example:
 19 	 
 20 	   var button = new Banana.Controls.Button();
 21 	   
 22 	   this.addControl(button); //add button to a collection
 23 	   
 24 	   button.bind('click',function(){
 25 	   		//do something here
 26 	   });
 27 	 *
 28 	 * @constructs
 29 	 * @extends Banana.Controls.Panel
 30 	 */
 31 	init : function()
 32 	{
 33 		this._super(); //call parent constructor
 34 
 35 		this.addCssClass('BButton');
 36 		this.addCssClass('BButtonHover');
 37 
 38 		this.bind('click',this.getProxy(function(e)
 39 		{
 40 			if (this.enabled !== undefined && this.enabled === false)
 41 			{
 42 				e.stopImmediatePropagation();
 43 				e.stopPropagation();//needed?? dont think so
 44 			}
 45 
 46 			return false;
 47 		}));
 48 	},
 49 	
 50 	/**
 51 	 * @return {String}
 52 	 */
 53 	getTagName : function()
 54 	{
 55 		return 'button';
 56 	},
 57 
 58 	/**
 59 	 * @ignore
 60 	 * @override
 61 	 */
 62 	createComponents : function()
 63 	{
 64 		if (this.imageUrl)
 65 		{
 66 			this.image = new Banana.Controls.Image();
 67 			this.image.setImage(this.imageUrl);
 68 			this.addControl(this.image);
 69 		}
 70 		else
 71 		{
 72 			this.label = new Banana.Controls.Label().addCssClass('BButtonLabel');
 73 			if (this.labelStyle)
 74 			{
 75 				this.label.setStyle(this.labelStyle);
 76 			}
 77 			if (this.labelClass)
 78 			{
 79 				this.label.addCssClass(this.labelClass);
 80 			}
 81 			this.label.setData(this.text);
 82 			this.addControl(this.label);
 83 		}
 84 	},
 85 
 86 	/**
 87 	 *@param {boolean} enables or disables button
 88 	 *@returns {Banana.Controls.Button}
 89 	 */
 90 	setEnabled : function(enabled)
 91 	{
 92 		if (!enabled)
 93 		{
 94 			this.addCssClass('BButtonDisabled');
 95 			this.removeCssClass('BButtonHover');
 96 		}
 97 		else
 98 		{
 99 			this.removeCssClass('BButtonDisabled');
100 			this.addCssClass('BButtonHover');			
101 		}
102 		
103 		this.enabled = enabled;
104 		return this;
105 	},
106 	
107 	/**
108 	 * sets style on the text label
109 	 * @param {String} style
110 	 * @returns {this}
111 	 */
112 	setLabelStyle : function(style)
113 	{
114 		this.labelStyle = style;
115 		return this;
116 	},
117 
118 	/**
119 	 * sets css class on the label of the button
120 	 * @param {String} clas
121 	 * @returns {this}
122 	 */
123 	setLabelCssClass : function(clas)
124 	{
125 		this.labelClass = clas;
126 		return this;
127 	},
128 
129 	/**
130 	 * sets the text on the button
131 	 *
132 	 * @param {String} text
133 	 * @return {this} 
134 	 */
135 	setText : function(text)
136 	{
137 		this.text = text;
138 		if (this.isRendered && this.label)
139 		{
140 			this.label.setData(this.text);
141 		}
142 		return this;
143 	},
144 	
145 	/**
146 	 * @param {String} image
147 	 * @returns {this}
148 	 */
149 	setImage : function(image)
150 	{
151 		this.imageUrl = image;
152 		if (this.isRendered)
153 		{
154 			this.image.setImage(this.imageUrl);
155 		}
156 		return this;
157 	}
158 });
159