1 /**
  2  * @author Gillis Haasnoot <gillis.haasnoot@gmail.com>
  3  * @package Banana.Util
  4  * @summary Dom Helper. dom writer for various banana actions
  5  */
  6 
  7 /** @namespace Banana.Util.DomHelper */
  8 goog.provide('Banana.Util.DomHelper');
  9 
 10 /**
 11  * responsible for writing dom elements
 12  */
 13 /** 
 14  * @class 
 15  * @name Banana.Util.DomHelper
 16  * @constructor
 17  */
 18 namespace('Banana.Util').DomHelper = 
 19 /** @lends Banana.Util.DomHelper.prototype */
 20 {	
 21 	/**
 22 	 * renders control to target
 23 	 * 
 24 	 * @param {String} data
 25 	 * @param {mixed} target Banana.Control or dom id
 26 	 * 
 27 	 */
 28 	render : function(data,target)
 29 	{
 30 		if (typeof(target) === 'string')
 31 		{		
 32 			jQuery('#'+target).append(data);			
 33 		}
 34 		else
 35 		{				
 36 			jQuery('#'+target.getClientId()).append(data);				
 37 		}	
 38 	},
 39 	
 40 	/**
 41 	 * replaces content with another
 42 	 * 
 43 	 * @param {String} data
 44 	 * @param {mixed} target Banana.Control or dom id
 45 	 */
 46 	replace : function(data,target)
 47 	{
 48 		if (typeof(target) === 'string')
 49 		{		
 50 			jQuery(target).replaceWith(data);			
 51 		}
 52 		else
 53 		{
 54 			jQuery('#'+target.getClientId()).replaceWith(data);				
 55 		}
 56 	},
 57 	
 58 	/**
 59 	 * renders plain html to target
 60 	 * 
 61 	 * @param {String} data
 62 	 * @param {mixed} target Banana.Control or dom id
 63 	 * @param {boolean} dontAppend If true we replace existing data. Otherwise append
 64 	 */
 65 	renderHtml : function(html,target,dontAppend)
 66 	{
 67 		if (target.getClientId() === 'body')
 68 		{
 69 			if (dontAppend)
 70 			{
 71 				jQuery('body').html(html);
 72 			}
 73 			else
 74 			{
 75 				jQuery('body').append(html);	
 76 			}
 77 		}
 78 		else
 79 		{
 80 			if (dontAppend)
 81 			{
 82 				jQuery('#'+target.getClientId()).html(html);
 83 			}
 84 			else
 85 			{
 86 				jQuery('#'+target.getClientId()).append(html);
 87 			}
 88 		}	
 89 	},
 90 	
 91 	/**
 92 	 * removes all child elements from control
 93 	 * 
 94 	 * @param {Banana.Control} control
 95 	 */
 96 	clear : function(control)
 97 	{
 98 		jQuery('#'+control.getClientId()).empty();
 99 	},
100 	
101 	/**
102 	 * removes control from dom. removing also removes all child dom elements
103 	 * 
104 	 * @param {Banana.Control} control
105 	 */
106 	remove : function(control)
107 	{
108 		if (typeof(control) === 'string')
109 		{
110 			jQuery('#'+control).remove();
111 		}
112 		else
113 		{
114 			//seems that first clearing it and then removing it is a lot faster see http://api.jquery.com/remove/#expr but doesnt work somehow find out why
115 			//jQuery('#'+control.getClientId()).clear().remove();
116 			jQuery('#'+control.getClientId()).remove();
117 		}
118 	},
119 	
120 	/** 
121 	 * sets data on dom element. 
122 	 * 
123 	 * @param {String} value
124 	 * @param {Banana.Control} control
125 	 */
126 	setData : function(value, control )
127 	{	
128 		jQuery('#'+control.getClientId()).val(value);	
129 	},
130 	
131 	/** 
132 	 * Dedicated function to set text data on labels 
133 	 * 
134 	 * @param {String} value
135 	 * @param {Banana.Controls.Label} control
136 	 */
137 	setTextData : function(value,control)
138 	{
139 		jQuery('#'+control.getClientId()).text(value);
140 	},
141 	
142 	/** 
143 	 * Dedictated function to set data on checkboxes
144 	 * 
145 	 * @param {String} value
146 	 * @param {Banana.Controls.Checkbox} control
147 	 */
148 	setCheckBoxData : function(value,control)
149 	{
150 		if (value)
151 		{
152 			jQuery('#'+control.getClientId()).prop({'checked':true});
153 		}
154 		else
155 		{
156 			jQuery('#'+control.getClientId()).prop('checked',false);
157 		}		
158 	},
159 	
160 	/**
161 	 * gets data from dom data control
162 	 * @param {Banana.Control} control
163 	 * @return {String}
164 	 */
165 	getData : function(control)
166 	{		
167 		return jQuery('#'+control.getClientId()).val();		
168 	},
169 	
170 	/**
171 	 * dedicated function to retreive data from checkboxes
172 	 * @param {Banana.Controls.Checkbox} control
173 	 * @return {String}
174 	 */
175 	getCheckBoxData : function(control)
176 	{
177 		return jQuery('#'+control.getClientId()).prop("checked");
178 	},
179 
180 	/**
181 	 * Mark all controls as cleared.
182 	 * Method is used during page transitions where new pages are rendered first, before the old one
183 	 * is removed.
184 	 * TODO: potentialy dangerous if user already used 'cleared' id somewhere. I suggest different id
185 	 * @param {String} id used to mark dom elements
186 	 */
187 	clearIdsFrom : function(id)
188 	{
189 		jQuery('#'+id + ' *').attr({'id':'cleared'});
190 		jQuery('#'+id).attr({'id':'cleared'});
191 	}
192 	
193 };