/modules/DOMAssistantCSS.js
JavaScript | 80 lines | 73 code | 5 blank | 2 comment | 22 complexity | 4e4be8ddb987295056e436a6007d9c51 MD5 | raw file
1// Developed by Robert Nyman/DOMAssistant team, code/licensing: http://domassistant.googlecode.com/, documentation: http://www.domassistant.com/documentation 2/*global DOMAssistant */ 3DOMAssistant.CSS = function () { 4 var def = DOMAssistant.def, 5 direct = { display: true }; 6 return { 7 addClass : function (className) { 8 if (!this.hasClass(className)) { 9 var currentClass = this.className; 10 this.className = currentClass + (currentClass.length? " " : "") + className; 11 } 12 return this; 13 }, 14 15 removeClass : function (className) { 16 return this.replaceClass(className); 17 }, 18 19 replaceClass : function (className, newClass) { 20 var classToRemove = new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i"); 21 this.className = this.className.replace(classToRemove, function (match, p1, p2) { 22 return newClass? (p1 + newClass + p2) : " "; 23 }).replace(/^\s+|\s+$/g, ""); 24 return this; 25 }, 26 27 hasClass : function (className) { 28 return (" " + this.className + " ").indexOf(" " + className + " ") > -1; 29 }, 30 31 setStyle : function (style, value) { 32 var css = this.style; 33 if ("filters" in this && (typeof style === "string"? /opacity/i.test(style) : def(style.opacity))) { 34 css.zoom = 1; 35 css.filter = (css.filter || "").replace(/alpha\([^)]*\)/, "") + "alpha(opacity=" + (def(style.opacity)? style.opacity : value) * 100 + ")"; 36 } 37 if (def(css.cssText)) { 38 var styleToSet = css.cssText; 39 if (typeof style === "object") { 40 for (var i in style) { 41 if (typeof i === "string") { 42 if (direct[i]) { css[i] = style[i]; } 43 styleToSet += ";" + i + ":" + style[i]; 44 } 45 } 46 } 47 else { 48 if (direct[style]) { css[style] = value; } 49 styleToSet += ";" + style + ":" + value; 50 } 51 css.cssText = styleToSet; 52 } 53 return this; 54 }, 55 56 getStyle : function (cssRule) { 57 var val = "", f; 58 cssRule = cssRule.toLowerCase(); 59 if (window.getComputedStyle) { 60 val = document.defaultView.getComputedStyle(this, null).getPropertyValue(cssRule); 61 } 62 else if (this.currentStyle) { 63 if ("filters" in this && cssRule === "opacity") { 64 val = (f = this.style.filter || this.currentStyle.filter) && f.indexOf("opacity=") >= 0? parseFloat(f.match(/opacity=([^)]*)/)[1]) / 100 : 1; 65 } 66 else { 67 cssRule = cssRule.replace(/^float$/, "styleFloat").replace(/\-(\w)/g, function (match, p1) { 68 return p1.toUpperCase(); 69 }); 70 val = this.currentStyle[cssRule]; 71 } 72 if (val === "auto" && /^(width|height)$/.test(cssRule) && this.currentStyle.display !== "none") { 73 val = this["offset" + cssRule.charAt(0).toUpperCase() + cssRule.substr(1)] + "px"; 74 } 75 } 76 return val; 77 } 78 }; 79}(); 80DOMAssistant.attach(DOMAssistant.CSS);