PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/DOMAssistantCSS.js

http://domassistant.googlecode.com/
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 */
  3. DOMAssistant.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. removeClass : function (className) {
  15. return this.replaceClass(className);
  16. },
  17. replaceClass : function (className, newClass) {
  18. var classToRemove = new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i");
  19. this.className = this.className.replace(classToRemove, function (match, p1, p2) {
  20. return newClass? (p1 + newClass + p2) : " ";
  21. }).replace(/^\s+|\s+$/g, "");
  22. return this;
  23. },
  24. hasClass : function (className) {
  25. return (" " + this.className + " ").indexOf(" " + className + " ") > -1;
  26. },
  27. setStyle : function (style, value) {
  28. var css = this.style;
  29. if ("filters" in this && (typeof style === "string"? /opacity/i.test(style) : def(style.opacity))) {
  30. css.zoom = 1;
  31. css.filter = (css.filter || "").replace(/alpha\([^)]*\)/, "") + "alpha(opacity=" + (def(style.opacity)? style.opacity : value) * 100 + ")";
  32. }
  33. if (def(css.cssText)) {
  34. var styleToSet = css.cssText;
  35. if (typeof style === "object") {
  36. for (var i in style) {
  37. if (typeof i === "string") {
  38. if (direct[i]) { css[i] = style[i]; }
  39. styleToSet += ";" + i + ":" + style[i];
  40. }
  41. }
  42. }
  43. else {
  44. if (direct[style]) { css[style] = value; }
  45. styleToSet += ";" + style + ":" + value;
  46. }
  47. css.cssText = styleToSet;
  48. }
  49. return this;
  50. },
  51. getStyle : function (cssRule) {
  52. var val = "", f;
  53. cssRule = cssRule.toLowerCase();
  54. if (window.getComputedStyle) {
  55. val = document.defaultView.getComputedStyle(this, null).getPropertyValue(cssRule);
  56. }
  57. else if (this.currentStyle) {
  58. if ("filters" in this && cssRule === "opacity") {
  59. val = (f = this.style.filter || this.currentStyle.filter) && f.indexOf("opacity=") >= 0? parseFloat(f.match(/opacity=([^)]*)/)[1]) / 100 : 1;
  60. }
  61. else {
  62. cssRule = cssRule.replace(/^float$/, "styleFloat").replace(/\-(\w)/g, function (match, p1) {
  63. return p1.toUpperCase();
  64. });
  65. val = this.currentStyle[cssRule];
  66. }
  67. if (val === "auto" && /^(width|height)$/.test(cssRule) && this.currentStyle.display !== "none") {
  68. val = this["offset" + cssRule.charAt(0).toUpperCase() + cssRule.substr(1)] + "px";
  69. }
  70. }
  71. return val;
  72. }
  73. };
  74. }();
  75. DOMAssistant.attach(DOMAssistant.CSS);