3 * Copyright(c) 2006-2010 Ext JS, Inc.
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,
32 ISCLIPPED = 'isClipped',
33 OVERFLOW = 'overflow',
34 OVERFLOWX = 'overflow-x',
35 OVERFLOWY = 'overflow-y',
36 ORIGINALCLIP = 'originalClip',
37 // special markup used throughout Ext when box wrapping elements
38 borders = {l: BORDER + LEFT + WIDTH, r: BORDER + RIGHT + WIDTH, t: BORDER + TOP + WIDTH, b: BORDER + BOTTOM + WIDTH},
39 paddings = {l: PADDING + LEFT, r: PADDING + RIGHT, t: PADDING + TOP, b: PADDING + BOTTOM},
40 margins = {l: MARGIN + LEFT, r: MARGIN + RIGHT, t: MARGIN + TOP, b: MARGIN + BOTTOM},
41 data = Ext.Element.data;
45 function camelFn(m, a) {
46 return a.charAt(1).toUpperCase();
49 function chkCache(prop) {
50 return propCache[prop] || (propCache[prop] = prop == 'float' ? propFloat : prop.replace(camelRe, camelFn));
54 // private ==> used by Fx
55 adjustWidth : function(width) {
57 var isNum = (typeof width == "number");
58 if(isNum && me.autoBoxAdjust && !me.isBorderBox()){
59 width -= (me.getBorderWidth("lr") + me.getPadding("lr"));
61 return (isNum && width < 0) ? 0 : width;
64 // private ==> used by Fx
65 adjustHeight : function(height) {
67 var isNum = (typeof height == "number");
68 if(isNum && me.autoBoxAdjust && !me.isBorderBox()){
69 height -= (me.getBorderWidth("tb") + me.getPadding("tb"));
71 return (isNum && height < 0) ? 0 : height;
76 * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out.
77 * @param {String/Array} className The CSS class to add, or an array of classes
78 * @return {Ext.Element} this
80 addClass : function(className){
86 // Separate case is for speed
87 if (!Ext.isArray(className)) {
88 if (typeof className == 'string' && !this.hasClass(className)) {
89 me.dom.className += " " + className;
93 for (i = 0, len = className.length; i < len; i++) {
95 if (typeof v == 'string' && (' ' + me.dom.className + ' ').indexOf(' ' + v + ' ') == -1) {
100 me.dom.className += " " + cls.join(" ");
107 * Removes one or more CSS classes from the element.
108 * @param {String/Array} className The CSS class to remove, or an array of classes
109 * @return {Ext.Element} this
111 removeClass : function(className){
118 if (!Ext.isArray(className)){
119 className = [className];
121 if (me.dom && me.dom.className) {
122 elClasses = me.dom.className.replace(trimRe, '').split(spacesRe);
123 for (i = 0, len = className.length; i < len; i++) {
125 if (typeof cls == 'string') {
126 cls = cls.replace(trimRe, '');
127 idx = elClasses.indexOf(cls);
129 elClasses.splice(idx, 1);
133 me.dom.className = elClasses.join(" ");
139 * Adds one or more CSS classes to this element and removes the same class(es) from all siblings.
140 * @param {String/Array} className The CSS class to add, or an array of classes
141 * @return {Ext.Element} this
143 radioClass : function(className){
144 var cn = this.dom.parentNode.childNodes,
148 className = Ext.isArray(className) ? className : [className];
149 for (i = 0, len = cn.length; i < len; i++) {
151 if (v && v.nodeType == 1) {
152 Ext.fly(v, '_internal').removeClass(className);
155 return this.addClass(className);
159 * Toggles the specified CSS class on this element (removes it if it already exists, otherwise adds it).
160 * @param {String} className The CSS class to toggle
161 * @return {Ext.Element} this
163 toggleClass : function(className){
164 return this.hasClass(className) ? this.removeClass(className) : this.addClass(className);
168 * Checks if the specified CSS class exists on this element's DOM node.
169 * @param {String} className The CSS class to check for
170 * @return {Boolean} True if the class exists, else false
172 hasClass : function(className){
173 return className && (' '+this.dom.className+' ').indexOf(' '+className+' ') != -1;
177 * Replaces a CSS class on the element with another. If the old name does not exist, the new name will simply be added.
178 * @param {String} oldClassName The CSS class to replace
179 * @param {String} newClassName The replacement CSS class
180 * @return {Ext.Element} this
182 replaceClass : function(oldClassName, newClassName){
183 return this.removeClass(oldClassName).addClass(newClassName);
186 isStyle : function(style, val) {
187 return this.getStyle(style) == val;
191 * Normalizes currentStyle and computedStyle.
192 * @param {String} property The style property whose value is returned.
193 * @return {String} The current value of the style property for this element.
195 getStyle : function(){
196 return view && view.getComputedStyle ?
209 prop = chkCache(prop);
210 // Fix bug caused by this: https://bugs.webkit.org/show_bug.cgi?id=13343
211 if(wk && /marginRight/.test(prop)){
212 display = this.getStyle('display');
213 el.style.display = 'inline-block';
215 out = (v = el.style[prop]) ? v :
216 (cs = view.getComputedStyle(el, "")) ? cs[prop] : null;
218 // Webkit returns rgb values for transparent.
220 if(out == 'rgba(0, 0, 0, 0)'){
223 el.style.display = display;
233 if(el == document) return null;
234 if (prop == 'opacity') {
235 if (el.style.filter.match) {
236 if(m = el.style.filter.match(opacityRe)){
237 var fv = parseFloat(m[1]);
239 return fv ? fv / 100 : 0;
245 prop = chkCache(prop);
246 return el.style[prop] || ((cs = el.currentStyle) ? cs[prop] : null);
251 * Return the CSS color for the specified CSS attribute. rgb, 3 digit (like #fff) and valid values
252 * are convert to standard 6 digit hex color.
253 * @param {String} attr The css attribute
254 * @param {String} defaultValue The default value to use when a valid color isn't found
255 * @param {String} prefix (optional) defaults to #. Use an empty string when working with
258 getColor : function(attr, defaultValue, prefix){
259 var v = this.getStyle(attr),
260 color = (typeof prefix != 'undefined') ? prefix : '#',
263 if(!v || /transparent|inherit/.test(v)){
267 Ext.each(v.slice(4, v.length -1).split(','), function(s){
269 color += (h < 16 ? '0' : '') + h.toString(16);
272 v = v.replace('#', '');
273 color += v.length == 3 ? v.replace(/^(\w)(\w)(\w)$/, '$1$1$2$2$3$3') : v;
275 return(color.length > 5 ? color.toLowerCase() : defaultValue);
279 * Wrapper for setting style properties, also takes single object parameter of multiple styles.
280 * @param {String/Object} property The style property to be set, or an object of multiple styles.
281 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
282 * @return {Ext.Element} this
284 setStyle : function(prop, value){
288 if (typeof prop != 'object') {
293 for (style in prop) {
296 this.setOpacity(value) :
297 this.dom.style[chkCache(style)] = value;
303 * Set the opacity of the element
304 * @param {Float} opacity The new opacity. 0 = transparent, .5 = 50% visibile, 1 = fully visible, etc
305 * @param {Boolean/Object} animate (optional) a standard Element animation config object or <tt>true</tt> for
306 * the default animation (<tt>{duration: .35, easing: 'easeIn'}</tt>)
307 * @return {Ext.Element} this
309 setOpacity : function(opacity, animate){
313 if(!animate || !me.anim){
315 var opac = opacity < 1 ? 'alpha(opacity=' + opacity * 100 + ')' : '',
316 val = s.filter.replace(opacityRe, '').replace(trimRe, '');
319 s.filter = val + (val.length > 0 ? ' ' : '') + opac;
324 me.anim({opacity: {to: opacity}}, me.preanim(arguments, 1), null, .35, 'easeIn');
330 * Clears any opacity settings from this element. Required in some cases for IE.
331 * @return {Ext.Element} this
333 clearOpacity : function(){
334 var style = this.dom.style;
336 if(!Ext.isEmpty(style.filter)){
337 style.filter = style.filter.replace(opacityRe, '').replace(trimRe, '');
340 style.opacity = style['-moz-opacity'] = style['-khtml-opacity'] = '';
346 * Returns the offset height of the element
347 * @param {Boolean} contentHeight (optional) true to get the height minus borders and padding
348 * @return {Number} The element's height
350 getHeight : function(contentHeight){
353 hidden = Ext.isIE && me.isStyle('display', 'none'),
354 h = MATH.max(dom.offsetHeight, hidden ? 0 : dom.clientHeight) || 0;
356 h = !contentHeight ? h : h - me.getBorderWidth("tb") - me.getPadding("tb");
357 return h < 0 ? 0 : h;
361 * Returns the offset width of the element
362 * @param {Boolean} contentWidth (optional) true to get the width minus borders and padding
363 * @return {Number} The element's width
365 getWidth : function(contentWidth){
368 hidden = Ext.isIE && me.isStyle('display', 'none'),
369 w = MATH.max(dom.offsetWidth, hidden ? 0 : dom.clientWidth) || 0;
370 w = !contentWidth ? w : w - me.getBorderWidth("lr") - me.getPadding("lr");
371 return w < 0 ? 0 : w;
375 * Set the width of this Element.
376 * @param {Mixed} width The new width. This may be one of:<div class="mdetail-params"><ul>
377 * <li>A Number specifying the new width in this Element's {@link #defaultUnit}s (by default, pixels).</li>
378 * <li>A String used to set the CSS width style. Animation may <b>not</b> be used.
380 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
381 * @return {Ext.Element} this
383 setWidth : function(width, animate){
385 width = me.adjustWidth(width);
386 !animate || !me.anim ?
387 me.dom.style.width = me.addUnits(width) :
388 me.anim({width : {to : width}}, me.preanim(arguments, 1));
393 * Set the height of this Element.
395 // change the height to 200px and animate with default configuration
396 Ext.fly('elementId').setHeight(200, true);
398 // change the height to 150px and animate with a custom configuration
399 Ext.fly('elId').setHeight(150, {
400 duration : .5, // animation will have a duration of .5 seconds
401 // will change the content to "finished"
402 callback: function(){ this.{@link #update}("finished"); }
405 * @param {Mixed} height The new height. This may be one of:<div class="mdetail-params"><ul>
406 * <li>A Number specifying the new height in this Element's {@link #defaultUnit}s (by default, pixels.)</li>
407 * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>
409 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
410 * @return {Ext.Element} this
412 setHeight : function(height, animate){
414 height = me.adjustHeight(height);
415 !animate || !me.anim ?
416 me.dom.style.height = me.addUnits(height) :
417 me.anim({height : {to : height}}, me.preanim(arguments, 1));
422 * Gets the width of the border(s) for the specified side(s)
423 * @param {String} side Can be t, l, r, b or any combination of those to add multiple values. For example,
424 * passing <tt>'lr'</tt> would get the border <b><u>l</u></b>eft width + the border <b><u>r</u></b>ight width.
425 * @return {Number} The width of the sides passed added together
427 getBorderWidth : function(side){
428 return this.addStyles(side, borders);
432 * Gets the width of the padding(s) for the specified side(s)
433 * @param {String} side Can be t, l, r, b or any combination of those to add multiple values. For example,
434 * passing <tt>'lr'</tt> would get the padding <b><u>l</u></b>eft + the padding <b><u>r</u></b>ight.
435 * @return {Number} The padding of the sides passed added together
437 getPadding : function(side){
438 return this.addStyles(side, paddings);
442 * Store the current overflow setting and clip overflow on the element - use <tt>{@link #unclip}</tt> to remove
443 * @return {Ext.Element} this
449 if(!data(dom, ISCLIPPED)){
450 data(dom, ISCLIPPED, true);
451 data(dom, ORIGINALCLIP, {
452 o: me.getStyle(OVERFLOW),
453 x: me.getStyle(OVERFLOWX),
454 y: me.getStyle(OVERFLOWY)
456 me.setStyle(OVERFLOW, HIDDEN);
457 me.setStyle(OVERFLOWX, HIDDEN);
458 me.setStyle(OVERFLOWY, HIDDEN);
464 * Return clipping (overflow) to original clipping before <tt>{@link #clip}</tt> was called
465 * @return {Ext.Element} this
471 if(data(dom, ISCLIPPED)){
472 data(dom, ISCLIPPED, false);
473 var o = data(dom, ORIGINALCLIP);
475 me.setStyle(OVERFLOW, o.o);
478 me.setStyle(OVERFLOWX, o.x);
481 me.setStyle(OVERFLOWY, o.y);
488 addStyles : function(sides, styles){
490 sidesArr = sides.match(wordsRe),
494 len = sidesArr.length;
495 for (i = 0; i < len; i++) {
497 size = side && parseInt(this.getStyle(styles[side]), 10);
499 ttlSize += MATH.abs(size);