3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
10 Ext.Element.addMethods(function(){
11 // local style camelizing for speed
13 camelRe = /(-[a-z])/gi,
15 view = document.defaultView,
16 propFloat = Ext.isIE ? 'styleFloat' : 'cssFloat',
17 opacityRe = /alpha\(opacity=(.*)\)/i,
18 trimRe = /^\s+|\s+$/g,
30 ISCLIPPED = 'isClipped',
31 OVERFLOW = 'overflow',
32 OVERFLOWX = 'overflow-x',
33 OVERFLOWY = 'overflow-y',
34 ORIGINALCLIP = 'originalClip',
35 // special markup used throughout Ext when box wrapping elements
36 borders = {l: BORDER + LEFT + WIDTH, r: BORDER + RIGHT + WIDTH, t: BORDER + TOP + WIDTH, b: BORDER + BOTTOM + WIDTH},
37 paddings = {l: PADDING + LEFT, r: PADDING + RIGHT, t: PADDING + TOP, b: PADDING + BOTTOM},
38 margins = {l: MARGIN + LEFT, r: MARGIN + RIGHT, t: MARGIN + TOP, b: MARGIN + BOTTOM},
39 data = Ext.Element.data;
43 function camelFn(m, a) {
44 return a.charAt(1).toUpperCase();
47 function chkCache(prop) {
48 return propCache[prop] || (propCache[prop] = prop == 'float' ? propFloat : prop.replace(camelRe, camelFn));
52 // private ==> used by Fx
53 adjustWidth : function(width) {
55 var isNum = Ext.isNumber(width);
56 if(isNum && me.autoBoxAdjust && !me.isBorderBox()){
57 width -= (me.getBorderWidth("lr") + me.getPadding("lr"));
59 return (isNum && width < 0) ? 0 : width;
62 // private ==> used by Fx
63 adjustHeight : function(height) {
65 var isNum = Ext.isNumber(height);
66 if(isNum && me.autoBoxAdjust && !me.isBorderBox()){
67 height -= (me.getBorderWidth("tb") + me.getPadding("tb"));
69 return (isNum && height < 0) ? 0 : height;
74 * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out.
75 * @param {String/Array} className The CSS class to add, or an array of classes
76 * @return {Ext.Element} this
78 addClass : function(className){
80 Ext.each(className, function(v) {
81 me.dom.className += (!me.hasClass(v) && v ? " " + v : "");
87 * Adds one or more CSS classes to this element and removes the same class(es) from all siblings.
88 * @param {String/Array} className The CSS class to add, or an array of classes
89 * @return {Ext.Element} this
91 radioClass : function(className){
92 Ext.each(this.dom.parentNode.childNodes, function(v) {
94 Ext.fly(v, '_internal').removeClass(className);
97 return this.addClass(className);
101 * Removes one or more CSS classes from the element.
102 * @param {String/Array} className The CSS class to remove, or an array of classes
103 * @return {Ext.Element} this
105 removeClass : function(className){
107 if (me.dom && me.dom.className) {
108 Ext.each(className, function(v) {
109 me.dom.className = me.dom.className.replace(
110 classReCache[v] = classReCache[v] || new RegExp('(?:^|\\s+)' + v + '(?:\\s+|$)', "g"),
118 * Toggles the specified CSS class on this element (removes it if it already exists, otherwise adds it).
119 * @param {String} className The CSS class to toggle
120 * @return {Ext.Element} this
122 toggleClass : function(className){
123 return this.hasClass(className) ? this.removeClass(className) : this.addClass(className);
127 * Checks if the specified CSS class exists on this element's DOM node.
128 * @param {String} className The CSS class to check for
129 * @return {Boolean} True if the class exists, else false
131 hasClass : function(className){
132 return className && (' '+this.dom.className+' ').indexOf(' '+className+' ') != -1;
136 * Replaces a CSS class on the element with another. If the old name does not exist, the new name will simply be added.
137 * @param {String} oldClassName The CSS class to replace
138 * @param {String} newClassName The replacement CSS class
139 * @return {Ext.Element} this
141 replaceClass : function(oldClassName, newClassName){
142 return this.removeClass(oldClassName).addClass(newClassName);
145 isStyle : function(style, val) {
146 return this.getStyle(style) == val;
150 * Normalizes currentStyle and computedStyle.
151 * @param {String} property The style property whose value is returned.
152 * @return {String} The current value of the style property for this element.
154 getStyle : function(){
155 return view && view.getComputedStyle ?
161 if(el == document) return null;
162 prop = chkCache(prop);
163 out = (v = el.style[prop]) ? v :
164 (cs = view.getComputedStyle(el, "")) ? cs[prop] : null;
166 // Webkit returns rgb values for transparent.
167 if(Ext.isWebKit && out == 'rgba(0, 0, 0, 0)'){
177 if(el == document) return null;
178 if (prop == 'opacity') {
179 if (el.style.filter.match) {
180 if(m = el.style.filter.match(opacityRe)){
181 var fv = parseFloat(m[1]);
183 return fv ? fv / 100 : 0;
189 prop = chkCache(prop);
190 return el.style[prop] || ((cs = el.currentStyle) ? cs[prop] : null);
195 * Return the CSS color for the specified CSS attribute. rgb, 3 digit (like #fff) and valid values
196 * are convert to standard 6 digit hex color.
197 * @param {String} attr The css attribute
198 * @param {String} defaultValue The default value to use when a valid color isn't found
199 * @param {String} prefix (optional) defaults to #. Use an empty string when working with
202 getColor : function(attr, defaultValue, prefix){
203 var v = this.getStyle(attr),
204 color = Ext.isDefined(prefix) ? prefix : '#',
207 if(!v || /transparent|inherit/.test(v)){
211 Ext.each(v.slice(4, v.length -1).split(','), function(s){
213 color += (h < 16 ? '0' : '') + h.toString(16);
216 v = v.replace('#', '');
217 color += v.length == 3 ? v.replace(/^(\w)(\w)(\w)$/, '$1$1$2$2$3$3') : v;
219 return(color.length > 5 ? color.toLowerCase() : defaultValue);
223 * Wrapper for setting style properties, also takes single object parameter of multiple styles.
224 * @param {String/Object} property The style property to be set, or an object of multiple styles.
225 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
226 * @return {Ext.Element} this
228 setStyle : function(prop, value){
232 if (!Ext.isObject(prop)) {
237 for (style in prop) {
240 this.setOpacity(value) :
241 this.dom.style[chkCache(style)] = value;
247 * Set the opacity of the element
248 * @param {Float} opacity The new opacity. 0 = transparent, .5 = 50% visibile, 1 = fully visible, etc
249 * @param {Boolean/Object} animate (optional) a standard Element animation config object or <tt>true</tt> for
250 * the default animation (<tt>{duration: .35, easing: 'easeIn'}</tt>)
251 * @return {Ext.Element} this
253 setOpacity : function(opacity, animate){
257 if(!animate || !me.anim){
259 var opac = opacity < 1 ? 'alpha(opacity=' + opacity * 100 + ')' : '',
260 val = s.filter.replace(opacityRe, '').replace(trimRe, '');
263 s.filter = val + (val.length > 0 ? ' ' : '') + opac;
268 me.anim({opacity: {to: opacity}}, me.preanim(arguments, 1), null, .35, 'easeIn');
274 * Clears any opacity settings from this element. Required in some cases for IE.
275 * @return {Ext.Element} this
277 clearOpacity : function(){
278 var style = this.dom.style;
280 if(!Ext.isEmpty(style.filter)){
281 style.filter = style.filter.replace(opacityRe, '').replace(trimRe, '');
284 style.opacity = style['-moz-opacity'] = style['-khtml-opacity'] = '';
290 * Returns the offset height of the element
291 * @param {Boolean} contentHeight (optional) true to get the height minus borders and padding
292 * @return {Number} The element's height
294 getHeight : function(contentHeight){
297 hidden = Ext.isIE && me.isStyle('display', 'none'),
298 h = MATH.max(dom.offsetHeight, hidden ? 0 : dom.clientHeight) || 0;
300 h = !contentHeight ? h : h - me.getBorderWidth("tb") - me.getPadding("tb");
301 return h < 0 ? 0 : h;
305 * Returns the offset width of the element
306 * @param {Boolean} contentWidth (optional) true to get the width minus borders and padding
307 * @return {Number} The element's width
309 getWidth : function(contentWidth){
312 hidden = Ext.isIE && me.isStyle('display', 'none'),
313 w = MATH.max(dom.offsetWidth, hidden ? 0 : dom.clientWidth) || 0;
314 w = !contentWidth ? w : w - me.getBorderWidth("lr") - me.getPadding("lr");
315 return w < 0 ? 0 : w;
319 * Set the width of this Element.
320 * @param {Mixed} width The new width. This may be one of:<div class="mdetail-params"><ul>
321 * <li>A Number specifying the new width in this Element's {@link #defaultUnit}s (by default, pixels).</li>
322 * <li>A String used to set the CSS width style. Animation may <b>not</b> be used.
324 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
325 * @return {Ext.Element} this
327 setWidth : function(width, animate){
329 width = me.adjustWidth(width);
330 !animate || !me.anim ?
331 me.dom.style.width = me.addUnits(width) :
332 me.anim({width : {to : width}}, me.preanim(arguments, 1));
337 * Set the height of this Element.
339 // change the height to 200px and animate with default configuration
340 Ext.fly('elementId').setHeight(200, true);
342 // change the height to 150px and animate with a custom configuration
343 Ext.fly('elId').setHeight(150, {
344 duration : .5, // animation will have a duration of .5 seconds
345 // will change the content to "finished"
346 callback: function(){ this.{@link #update}("finished"); }
349 * @param {Mixed} height The new height. This may be one of:<div class="mdetail-params"><ul>
350 * <li>A Number specifying the new height in this Element's {@link #defaultUnit}s (by default, pixels.)</li>
351 * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>
353 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
354 * @return {Ext.Element} this
356 setHeight : function(height, animate){
358 height = me.adjustHeight(height);
359 !animate || !me.anim ?
360 me.dom.style.height = me.addUnits(height) :
361 me.anim({height : {to : height}}, me.preanim(arguments, 1));
366 * Gets the width of the border(s) for the specified side(s)
367 * @param {String} side Can be t, l, r, b or any combination of those to add multiple values. For example,
368 * passing <tt>'lr'</tt> would get the border <b><u>l</u></b>eft width + the border <b><u>r</u></b>ight width.
369 * @return {Number} The width of the sides passed added together
371 getBorderWidth : function(side){
372 return this.addStyles(side, borders);
376 * Gets the width of the padding(s) for the specified side(s)
377 * @param {String} side Can be t, l, r, b or any combination of those to add multiple values. For example,
378 * passing <tt>'lr'</tt> would get the padding <b><u>l</u></b>eft + the padding <b><u>r</u></b>ight.
379 * @return {Number} The padding of the sides passed added together
381 getPadding : function(side){
382 return this.addStyles(side, paddings);
386 * Store the current overflow setting and clip overflow on the element - use <tt>{@link #unclip}</tt> to remove
387 * @return {Ext.Element} this
393 if(!data(dom, ISCLIPPED)){
394 data(dom, ISCLIPPED, true);
395 data(dom, ORIGINALCLIP, {
396 o: me.getStyle(OVERFLOW),
397 x: me.getStyle(OVERFLOWX),
398 y: me.getStyle(OVERFLOWY)
400 me.setStyle(OVERFLOW, HIDDEN);
401 me.setStyle(OVERFLOWX, HIDDEN);
402 me.setStyle(OVERFLOWY, HIDDEN);
408 * Return clipping (overflow) to original clipping before <tt>{@link #clip}</tt> was called
409 * @return {Ext.Element} this
415 if(data(dom, ISCLIPPED)){
416 data(dom, ISCLIPPED, false);
417 var o = data(dom, ORIGINALCLIP);
419 me.setStyle(OVERFLOW, o.o);
422 me.setStyle(OVERFLOWX, o.x);
425 me.setStyle(OVERFLOWY, o.y);
432 addStyles : function(sides, styles){
435 Ext.each(sides.match(/\w/g), function(s) {
436 if (s = parseInt(this.getStyle(styles[s]), 10)) {