1 /** 2 * @fileoverview An abstract class which represents 3 * a component of the model-view-controller. 4 */ 5 6 goog.provide('xrx.component'); 7 8 9 goog.require('goog.events.EventHandler'); 10 goog.require('goog.ui.IdGenerator'); 11 12 13 14 /** 15 * @constructor 16 */ 17 xrx.component = function(element) { 18 19 20 21 this.element_ = element; 22 }; 23 24 25 26 xrx.component.prototype.createDom = goog.abstractMethod; 27 28 29 30 /** 31 * Generator for unique IDs. 32 * @type {goog.ui.IdGenerator} 33 * @private 34 */ 35 xrx.component.prototype.idGenerator_ = goog.ui.IdGenerator.getInstance(); 36 37 38 39 /** 40 * Unique ID of the component, lazily initialized in {@link 41 * xrx.component#getId} if needed. This property is strictly private and 42 * must not be accessed directly outside of this class! 43 * @type {?string} 44 * @private 45 */ 46 xrx.component.prototype.id_ = null; 47 48 49 50 /** 51 * Event handler. 52 * @type {goog.events.EventHandler} 53 * @private 54 */ 55 xrx.component.prototype.handler_; 56 57 58 59 60 /** 61 * Gets the component's element. 62 * @return {Element} The element for the component. 63 */ 64 xrx.component.prototype.getElement = function() { 65 return this.element_; 66 }; 67 68 69 70 /** 71 * Gets the unique ID for the instance of this component. If the instance 72 * doesn't already have an ID, generates one on the fly. 73 * @return {string} Unique component ID. 74 */ 75 xrx.component.prototype.getId = function() { 76 return this.id_ || this.element_.getAttribute('id') || 77 (this.id_ = this.idGenerator_.getNextUniqueId()); 78 }; 79 80 81 82 /** 83 * Gets the XPath expression found in the component's data-xrx-ref attribute. 84 * @return {?string} The expression. 85 */ 86 xrx.component.prototype.getRefExpression = function() { 87 return this.getElement().getAttribute('data-xrx-ref'); 88 }; 89 90 91 92 /** 93 * Gets the bind ID found in the component's data-xrx-bind attribute. 94 * @return {?string} The bind ID. 95 */ 96 xrx.component.prototype.getBindId = function() { 97 return this.getElement().getAttribute('data-xrx-bind'); 98 }; 99 100 101 102 /** 103 * Gets the bind referenced by the component. 104 * @return {?xrx.bind} The bind. 105 */ 106 xrx.component.prototype.getBind = function() { 107 return xrx.model.getComponent(this.getBindId()); 108 }; 109 110 111 112 /** 113 * Returns the event handler for this component, lazily created the first time 114 * this method is called. 115 * @return {!goog.events.EventHandler} Event handler for this component. 116 * @protected 117 */ 118 xrx.component.prototype.getHandler = function() { 119 return this.handler_ || 120 (this.handler_ = new goog.events.EventHandler(this)); 121 }; 122 123