1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary Switch button 
  5  */
  6 
  7 goog.provide('Banana.Controls.SwitchButton');
  8 
  9 goog.require('Banana.Controls.DataControls');
 10 
 11 /** @namespace Banana.Controls.SwitchButton */
 12 namespace('Banana.Controls').SwitchButton = Banana.Controls.Panel.extend(
 13 /** @lends Banana.Controls.SwitchButton.prototype */
 14 {
 15 	/**
 16 	 * Creates a switch button. Default state = off (false)
 17 	 * @constructs
 18 	 * @extends Banana.Controls.Panel
 19 	 */
 20 	init : function()
 21 	{
 22 		this._super();
 23 		this.status = false;
 24 	},
 25 	
 26 	/**
 27 	 * @override
 28 	 */
 29 	createComponents : function()
 30 	{
 31 		this.image = new Banana.Controls.Image();
 32 		this.addControl(this.image);
 33 		
 34 		this.bind('click',this.getProxy(function()
 35 		{
 36 			this.status = this.status ? false : true; 
 37 		
 38 			this.triggerEvent('switched',this.status);
 39 			this.bindImage();
 40 			this.image.invalidateDisplay();
 41 			
 42 		}));
 43 		
 44 		this.bindImage();
 45 	},
 46 	
 47 	/**
 48 	 * Binds image
 49 	 * @ignore
 50 	 */
 51 	bindImage : function()
 52 	{
 53 		if (this.status)
 54 		{
 55 			this.image.setImage(this.onImage);
 56 		}
 57 		else
 58 		{
 59 			this.image.setImage(this.offImage);
 60 		}
 61 	},
 62 	
 63 	/**
 64 	 * Sets image representing the on status
 65 	 * @param {String} image name
 66 	 * @return {this}
 67 	 */
 68 	setOnImage : function(image)
 69 	{
 70 		this.onImage = image;
 71 		
 72 		if (this.isRendered)
 73 		{
 74 			this.bindImage();
 75 		}
 76 		
 77 		return this;
 78 	},
 79 	
 80 	/**
 81 	 * Sets image representing the off status
 82 	 * @param {String} image name
 83 	 * @return {this}
 84 	 */
 85 	setOffImage : function(image)
 86 	{
 87 		this.offImage = image;
 88 		
 89 		if (this.isRendered)
 90 		{
 91 			this.bindImage();
 92 		}
 93 		
 94 		return this;
 95 	},
 96 	
 97 	/**
 98 	 * Switches on button
 99 	 * @return {this}
100 	 */
101 	on : function()
102 	{
103 		this.status = true;
104 		
105 		if (this.isRendered)
106 		{
107 			this.bindImage();
108 		}
109 		
110 		return this;
111 		
112 	},
113 	
114 	/**
115 	 * Switches off button
116 	 * @return {this} 
117 	 */
118 	off : function()
119 	{
120 		this.status = false;
121 		
122 		if (this.isRendered)
123 		{
124 			this.bindImage();
125 		}
126 		
127 		return this;
128 	}
129 });