1 /**
  2  * @author Pavel Yaschenko
  3  */
  4 
  5 
  6 (function ($, richfaces, params) {
  7 	
  8 	richfaces.blankFunction = function (){}; //TODO: add it to global library
  9 	
 10     /**
 11      * @class Base class for all components.
 12      * All RichFaces components should use this class as base or another RichFaces class which based on it.
 13 	 *
 14 		<pre><code>
 15 		//Inheritance example:
 16 		(function ($, richfaces, params) {
 17 		
 18 			// Constructor definition
 19 			richfaces.MyComponent = function(componentId, [options]) {
 20 				// call constructor of parent class
 21 				$super.constructor.call(this, componentId, [options]);
 22 			};
 23 			
 24 			// define private method
 25 			var myPrivateMethod = function () {
 26 			}
 27 			
 28 			// create container for protected method's links
 29 			// in this example we link private method as protected
 30 			var $p ={myProtectedMethod: myPrivateMethod};
 31 			
 32 			// Extend component class and add protected methods from parent class to our container
 33 			$p = richfaces.BaseComponent.extend(richfaces.BaseComponent, richfaces.MyComponent, $p);
 34 			
 35 			// define super class link
 36 			var $super = richfaces.MyComponent.$super;
 37 		
 38 			// Add new properties and methods
 39 			$.extend(richfaces.MyComponent.prototype, (function (params) {
 40 				return {
 41 		 				name:"MyComponent", 
 42 						f:function (){alert("hello");
 43 					}
 44 				};
 45 			})(params));
 46 		})(jQuery, RichFaces);
 47 		</code></pre>
 48 	 *
 49      * @memberOf RichFaces
 50      * @name BaseComponent
 51      *
 52      * @constructor
 53      * @param {String} componentId - component id
 54      * */
 55 	richfaces.BaseComponent = function(componentId) {
 56 		this.id = componentId;
 57 	};
 58 	
 59 	/**
 60      * <protected> Attach component object to DOM element by component id, DOM element or jQuery object and returns the element
 61      *
 62      * @function
 63      * @name RichFaces.BaseComponent#attachToDom
 64      * @param {string|DOMElement|jQuery} source - component id, DOM element or DOM elements wrapped by jQuery
 65      *
 66      * @return {DOMElement}
 67      * */
 68 	var attachToDom = function(source) {
 69 		source = source || this.id;
 70 		var element = richfaces.getDomElement(source);
 71 		if (element) {
 72 			element["richfaces"] = element["richfaces"] || {};
 73 			element.richfaces.component = this;
 74 		}
 75 		return element;
 76 	}
 77 	
 78 	var $p = {attachToDom:attachToDom};
 79 	
 80 	/**
 81      * Method extends child class prototype with parent prototype 
 82      * and return the object with parent's protected methods
 83      *
 84      * @function
 85      * @name RichFaces.BaseComponent.extend
 86      *
 87      * @return {object}
 88      * */
 89 	richfaces.BaseComponent.extend = function (parent, child, h) {
 90 		var F = richfaces.blankFunction;
 91 		F.prototype = parent.prototype;
 92 	    child.prototype = new F();
 93 	    child.prototype.constructor = child;
 94 	    child.$super = parent.prototype;
 95 	    if (child.$super == richfaces.BaseComponent.prototype) {
 96 	    	var r = jQuery.extend({}, $p, h||{});
 97 	    }
 98 	    
 99 	    // create wrapper with protected methods and variables
100 	    child.extend = function (_parent, _child, _h) {
101 	    		var _r = jQuery.extend({}, r||h||{}, _h||{});
102 				return richfaces.BaseComponent.extend(_parent, _child, _r);
103 		}
104 	    return r||h;
105 	};
106 	
107 	$.extend(richfaces.BaseComponent.prototype, (function (params) {
108 		return {
109 			/**
110              * Component name.
111              *
112              * @name RichFaces.BaseComponent#name
113              * @type String
114              * */
115 			name: "BaseComponent",
116 			
117 			/**
118              * Method for converting object to string
119              *
120              * @function
121              * @name RichFaces.BaseComponent#toString
122              *
123              * @return {String}
124              * */
125 			toString: function() {
126 				var result = [];
127 				if (this.constructor.$super) {
128 					result[result.length] = this.constructor.$super.toString();
129 				}
130 				result[result.length] = this.name;
131 				return result.join(', ');
132 			},
133 			
134 			/**
135              * Method returns element's id for event handlers binding.
136              * Event API calls this method when binding by component object as selector was used.
137              *
138              * @function
139              * @name RichFaces.BaseComponent#getEventElement
140              *
141              * @return {String}
142              * */			
143 			getEventElement: function() {
144 				return this.id; 
145 			},
146 			
147 			/**
148              * Destroy method. Will be called before remove component from the page 
149              *
150              * @function
151              * @name RichFaces.BaseComponent#destroy
152              *
153              * */	
154 			destroy: function() {
155 			}
156 		};
157 	})(params));
158      
159 })(jQuery, window.RichFaces || (window.RichFaces={}));