1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Controls
  4  * @summary Status bar 
  5  */
  6 
  7 goog.provide('Banana.Controls.StatusBar');
  8 
  9 /** @namespace Banana.Controls.StatusBar */
 10 namespace('Banana.Controls').StatusBar = Banana.Controls.Panel.extend(
 11 /** @lends Banana.Controls.StatusBar.prototype */
 12 {
 13 	/**
 14 	 * Creates a status bar. It can display 4 types. Ok, Info, Warning or Error
 15 	 * Call setMessage({text:'some info',type:'info'}) to show info text.
 16 	 * you can also call shortcut method showInfo('some info');
 17 	 * Optionally you can specify the timeout. This determines how long the message
 18 	 * will be visible on the screen. By default it will stay 10 seconds.
 19 	 * @constructs
 20 	 * @extends Banana.Controls.Panel
 21 	 */
 22 	init : function()
 23 	{
 24 		this._super();
 25 		this.addCssClass('BStatusBar');
 26 		
 27 		this.defaultTimeout = 10000;
 28 	},
 29 
 30 	/**
 31 	 * @override
 32 	 */
 33 	updateDisplay : function()
 34 	{
 35 		var el = jQuery('#'+this.getClientId()).detach();
 36 		jQuery('body').append(el);
 37 	},
 38 
 39 	/**
 40 	 * @param {String} message
 41 	 * @return {this}
 42 	 */
 43 	setSuccesMessage : function(message)
 44 	{
 45 		var m = {};
 46 		m.type = 'ok';
 47 		m.message = message;
 48 		this.setMessage(m);
 49 		return this;
 50 	},
 51 	
 52 	/**
 53 	 * Shows error message. Optionally you can specify a list of errors which will be shown on the screen
 54 	 * @param {String} message
 55 	 * @param {int} timeout
 56 	 * @param {Array} errors
 57 	 * @return {this}
 58 	 */
 59 	setErrorMessage : function(message, timeout, errors)
 60 	{
 61 		var m = {};
 62 		m.type = 'error';
 63 		m.message = message;
 64 		m.timeout = timeout;	
 65 		m.errors = errors;
 66 		this.setMessage(m);
 67 		return this;
 68 	},
 69 
 70 	/**
 71 	 * @param {String} message
 72 	 * @param {int} timeout
 73 	 * @return {this}
 74 	 */
 75 	setWarningMessage : function(message,timeout)
 76 	{
 77 		var m = {};
 78 		m.type = 'warning';
 79 		m.message = message;
 80 		m.timeout = timeout;		
 81 		
 82 		this.setMessage(m);
 83 		return this;
 84 	},
 85 
 86 	/**
 87 	 * @param {String} message
 88 	 * @param {int} timeout 
 89 	 * @return {this}
 90 	 */
 91 	setInfoMessage : function(message,timeout)
 92 	{
 93 		var m = {};
 94 		m.type = 'info';
 95 		m.message = message;
 96 		m.timeout = timeout;
 97 		
 98 		this.setMessage(m);
 99 		return this;
100 	},
101 	/**
102 	 *
103 	 * @param {Object} message structure like {type:'warning/ok/error/info',message:'a message',timeout:undefined||1000,errors:[]}
104 	 * @return {this}
105 	 */
106 	setMessage : function(message)
107 	{
108 		if (this.timeout)
109 		{
110 			clearTimeout(this.timeout);
111 		}
112 		this.setCss({'display':'block'});
113 		this.clear();
114 		
115 		// remove possible css classes
116 		this.removeCssClass('BStatusBarError');
117 		this.removeCssClass('BStatusBarSuccess');
118 		this.removeCssClass('BStatusBarWarning');
119 		this.removeCssClass('BStatusBarInfo');		
120 		
121 		this.messageBar = new Banana.Controls.Panel();
122 
123 		switch (message.type)
124 		{
125 			case 'ok':
126 				this.addCssClass('BStatusBarSuccess');
127 			break;
128 			
129 			case 'warning':
130 				this.addCssClass('BStatusBarWarning');
131 			break;
132 			
133 			case 'error':
134 				this.addCssClass('BStatusBarError');
135 			break;
136 			
137 			case 'info':
138 				this.addCssClass('BStatusBarInfo');
139 			break;
140 		}
141 
142 		var l = new Banana.Controls.Label();
143 		l.addCssClass('title');
144 		l.setData(message.message);
145 		this.messageBar.addControl(l);
146 		
147 		this.addControl(this.messageBar);
148 		
149 		var messages = new Banana.Controls.Panel();
150 		messages.addCssClass('messages');
151 		if (message.errors && message.errors.length)
152 		{
153 			for (var x = 0; x < message.errors.length; x++)
154 			{
155 				if (message.errors[x].message)
156 					messages.addControl("<li>" + message.errors[x].message + " </li>");
157 			}
158 		}
159 		
160 		this.messageBar.addControl(messages);
161 		this.invalidateDisplay();
162 
163 		if (message.timeout !== 0 && message.timeout !== undefined)
164 		{
165 			this.timeout = setTimeout(this.getProxy(this.hide),
166 					message.timeout || this.defaultTimeout);
167 		}
168 		
169 		return this;
170 	},
171 	
172 	/**
173 	 * @return {this}
174 	 */
175 	hide : function()
176 	{
177 		this.setCss({'display':'none'});
178 		if (this.messageBar) this.messageBar.setVisible(false);
179 		
180 		return this;
181 	}
182 });
183