home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip_2003-11_cd1.bin / chplus / chlapark / class_magic.js < prev    next >
Text File  |  2003-09-29  |  2KB  |  99 lines

  1. var classMagic = {};
  2.  
  3. // prida objektu novy class
  4. classMagic.add = function (elm, newCl) {
  5.     if (elm && newCl) {
  6.         elm = this.fixElm(elm);
  7.         for (var i = 0; i < elm.length; i++) {
  8.             elm[i].className += (this.get(elm[i])) ? " " + newCl : newCl;
  9.         }
  10.         return true;
  11.     }
  12.     return false;
  13. };
  14.  
  15. // zisti, ci dany objekt ma priradeny dany class
  16. classMagic.has = function (elm, cl) {
  17.     if (elm && cl) {
  18.         if (actCl = this.get(elm)) {
  19.             for (var i = 0; i < actCl.length; i++) {
  20.                 if (actCl[i] == cl) {
  21.                     return true;
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     return false;
  27. }
  28.  
  29. // nahradi class(-y) objektu novym classom
  30. classMagic.set = function (elm, newCl) {
  31.     if (elm && newCl) {
  32.         elm = this.fixElm(elm);
  33.         for (var i = 0; i < elm.length; i++) {
  34.             elm[i].className = newCl;
  35.         }
  36.         return true;
  37.     }
  38.     return false;
  39. };
  40.  
  41. // nahradi stary class objektu novym classom
  42. classMagic.replace = function (elm, newCl, oldCl) {
  43.     if (elm && newCl && oldCl) {
  44.         elm = this.fixElm(elm);
  45.         for (var i = 0; i < elm.length; i++) {
  46.             var cl;
  47.             var replCl = "";
  48.             if (cl = this.get(elm[i])) {
  49.                 for (var j = 0; j < cl.length; j++) {
  50.                     replCl += (j > 0) ? " " : "";
  51.                     replCl += (cl[j] == oldCl) ? newCl : cl[j];
  52.                 }
  53.                 elm[i].className = replCl;
  54.             }
  55.         }
  56.         return true;
  57.     }
  58.     return false;
  59. };
  60.  
  61. // odstrani stary class
  62. classMagic.remove = function (elm, oldCl) {
  63.     if (elm && oldCl) {
  64.         elm = this.fixElm(elm);
  65.         for (var i = 0; i < elm.length; i++) {
  66.             var cl;
  67.             var replCl = "";
  68.             if (cl = this.get(elm[i])) {
  69.                 for (var j = 0; j < cl.length; j++) {
  70.                     replCl += (j > 0) ? " " : "";
  71.                     replCl += (cl[j] == oldCl) ? "" : cl[j];
  72.                 }
  73.                 elm[i].className = replCl;
  74.             }
  75.         }
  76.         return true;
  77.     }
  78.     return false;
  79. };
  80.  
  81. // vrati pole vsetkych classov, ktore objekt ma
  82. // (toto je pomocna funkcia, ktoru vyuzivaju dalsie funkcie, ale moze sa pouzit aj samostatne)
  83. classMagic.get = function (elm) {
  84.     if (elm) {
  85.         return (elm.className == "") ? false : elm.className.split(" ");
  86.     }
  87.     return false;
  88. };
  89.  
  90. // pomocna funkcia, ktore prevedie samostatny element na pole
  91. classMagic.fixElm = function (elm) {
  92.     if (elm) {
  93.         if (!elm.sort) {
  94.             elm = [elm];
  95.         }
  96.         return elm;
  97.     }
  98.     return false;
  99. }