1 jls.loader.provide('jls.gui.Style'); 2 3 jls.gui.Style = jls.lang.Class.create( /** @lends jls.gui.Style.prototype */ 4 { 5 /** 6 * Creates a style. 7 * 8 * @param {Object} [properties] The style properties to activate. 9 * @constructs 10 * @class This class provides style for Element. 11 */ 12 initialize : function(properties) { 13 this._properties = properties || {}; 14 this._activated = false; 15 this._parent = null; 16 this._handler = jls.lang.Class.emptyFunction; 17 this._inheritedProperties = jls.gui.Style.defaultInherited; 18 this._defaultProperties = jls.gui.Style.defaultAcquired; 19 }, 20 /** 21 * Activates this style. 22 * 23 * @param {Object} [properties] The style properties to activate. 24 */ 25 activate : function() { 26 if (this._activated) { 27 return; 28 } 29 this._activated = true; 30 this.setProperties(this._properties); 31 this._properties = null; 32 }, 33 /** 34 * Sets a handler on this style. 35 * 36 * @param {Function} handler The element to link to this style. 37 * @returns {Function} The previous handler if any. 38 */ 39 setHandler : function(handler) { 40 var previous = this._handler; 41 this._handler = handler; 42 return previous; 43 }, 44 setParent : function(parent) { 45 this._parent = parent; 46 }, 47 setInheritedProperties : function(properties) { 48 this._inheritedProperties = properties; 49 }, 50 setDefaultProperties : function(properties) { 51 this._defaultProperties = properties; 52 }, 53 /** 54 * Returns the value of a property. 55 * 56 * @param {String} key The property key. 57 * @returns {Object} The property value or null. 58 */ 59 getPropertyValue : function(key) { 60 /*var getter = key.camelize('get'); 61 if (Object.isFunction(this[getter])) { 62 return this[getter](); 63 }*/ 64 key = key.camelize(); 65 if (! this._activated) { 66 if (key in this._properties) { 67 this[key] = this._properties[key]; 68 return this._properties[key]; 69 } 70 } 71 if (key in this) { 72 return this[key]; 73 } 74 if (key in this._inheritedProperties) { 75 if (this._parent != null) { 76 var value = this._parent.getPropertyValue(key); 77 if (value != null) { 78 return value; 79 } 80 } 81 return this._inheritedProperties[key]; 82 } 83 if (key in this._defaultProperties) { 84 return this._defaultProperties[key]; 85 } 86 return null; 87 }, 88 /** 89 * Removes a property. 90 * 91 * @param {String} key The property key. 92 * @returns {Object} The previous property value if any. 93 */ 94 removeProperty : function(key) { 95 key = key.camelize(); 96 if (! (key in this)) { 97 return null; 98 } 99 var previous = this[key]; 100 delete this[key]; 101 this._handler(key, previous, null); 102 return previous; 103 }, 104 /** 105 * Sets a property. 106 * 107 * @param {String} key The property key. 108 * @param {String} value The value of the property. 109 * @returns {Object} The previous property value if any. 110 */ 111 setProperty : function(key, value) { 112 if ((typeof value == 'undefined') || (value == null)) { 113 return this.removeProperty(key); 114 } 115 key = key.camelize(); 116 var previous = this[key]; 117 if (value == previous) { 118 return previous; 119 } 120 this[key] = value; 121 this._handler(key, previous, value); 122 return previous; 123 }, 124 /** 125 * Sets properties. 126 * 127 * @param {Object} properties The style properties to set. 128 * @param {Array} [list] The property to set. 129 */ 130 setProperties : function(properties, list) { 131 if (Object.isArray(list)) { 132 for (var i = 0; i < list.length; i++) { 133 var k = list[i]; 134 if (k in properties) { 135 this.setProperty(k, properties[k]); 136 } 137 } 138 } else { 139 for (var k in properties) { 140 this.setProperty(k, properties[k]); 141 } 142 } 143 return this; 144 } 145 }); 146 147 Object.extend(jls.gui.Style, 148 { 149 defaultInherited: { 150 fontFamily: 'Arial', 151 fontSize: 12, 152 marginTop: 0, 153 marginBottom: 0, 154 marginLeft: 0, 155 marginRight: 0, 156 overflow: 'visible', // visible | hidden | scroll | auto 157 direction: 'ltr', // ltr | rtl 158 verticalPosition: 'top', // top | middle | bottom 159 textAlign: 'left', // left | right | center | justify | leading | trailing 160 verticalAlign: 'bottom' // top | middle | bottom 161 }, 162 defaultAcquired: { // noninheritable 163 clear: 'none', // none | left | right | both 164 display: 'inline', // inline | block | none 165 position: 'relative', // static | relative | absolute | fixed 166 visibility: 'visible', // visible | hidden | collapse 167 left: null, 168 top: null, 169 width: null, 170 height: null 171 } 172 }); 173 174