scripts/domutils.js
changeset 58 05a69bab5f10
parent 11 0faea3a6c881
equal deleted inserted replaced
57:31ce64a3ff6c 58:05a69bab5f10
    10 {
    10 {
    11   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
    11   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
    12   if ( !this.object )
    12   if ( !this.object )
    13   {
    13   {
    14     this.object = false;
    14     this.object = false;
    15     return this;
    15     return false;
    16   }
    16   }
       
    17   if ( this.object.Dynano )
       
    18   {
       
    19     return this.object.Dynano;
       
    20   }
       
    21   this.object.Dynano = this;
    17   this.height = __DNObjGetHeight(this.object);
    22   this.height = __DNObjGetHeight(this.object);
    18   this.width = __DNObjGetWidth(this.object);
    23   this.width = __DNObjGetWidth(this.object);
       
    24   // fixme: make more accurate?
       
    25   this.object.DN_opac = 100;
       
    26   
       
    27   return true;
    19 }
    28 }
    20 function __DNObjGetHeight(o) {
    29 function __DNObjGetHeight(o) {
    21   return o.offsetHeight;
    30   return o.offsetHeight;
    22 }
    31 }
    23 
    32 
    80 }
    89 }
    81 
    90 
    82 DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
    91 DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
    83 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
    92 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
    84 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
    93 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
    85 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
    94 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); };
    86 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
    95 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); };
    87 DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
    96 DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); };
    88 DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }
    97 DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); };
       
    98 DNobj.prototype.insertText = function(text)  { this.object.appendChild(document.createTextNode(text)); return this; };
       
    99 DNobj.prototype.insertBR = function()        { this.object.appendChild(document.createElement('br')); return this; };
       
   100 DNobj.prototype.attr     = function(attribute, value) { this.object.setAttribute(attribute, value); return this; };
       
   101 DNobj.prototype.css      = function(attribute, value)
       
   102 {
       
   103   if ( attribute == 'float' )
       
   104   {
       
   105     this.object.style.cssFloat = value;
       
   106     this.object.style.styleFloat = value;
       
   107   }
       
   108   else
       
   109   {
       
   110     // convert attribute to CamelCase format (quick and easy version)
       
   111     var i;
       
   112     while ( (i = attribute.indexOf('-')) > -1 )
       
   113     {
       
   114       attribute = attribute.substr(0, i) +
       
   115                   attribute.substr(i + 1, 1).toUpperCase() +
       
   116                   attribute.substr(i + 2);
       
   117     }
       
   118     this.object.style[attribute] = value;
       
   119   }
       
   120   return this;
       
   121 }
       
   122 DNobj.prototype.opacity = function(opacity)
       
   123 {
       
   124   var object = this.object.style;
       
   125   object.opacity = (opacity / 100);
       
   126   object.MozOpacity = (opacity / 100);
       
   127   object.KhtmlOpacity = (opacity / 100);
       
   128   object.filter = "alpha(opacity=" + opacity + ")";
       
   129   this.object.DN_opac = opacity;
       
   130 }
       
   131 DNobj.prototype.fade = function(to, time, done)
       
   132 {
       
   133   var from = this.object.DN_opac;
       
   134   if ( to == from )
       
   135   {
       
   136     return this;
       
   137   }
       
   138   time = time || 500;
       
   139   var op = to > from ? 1 : -1;
       
   140   var timestep = time / ( op * (to - from) );
       
   141   var i = from, tick = 0, o = this.object;
       
   142   this.object.id = this.object.id || 'dynano_autofade_' + Math.floor(Math.random() * 1000000);
       
   143   
       
   144   while ( true )
       
   145   {
       
   146     i += op;
       
   147     tick += timestep;
       
   148     
       
   149     if ( ( op == -1 && i < to ) || ( op == 1 && i > to ) )
       
   150       break;
       
   151     
       
   152     setTimeout('$("' + this.object.id + '").opacity(' + i + ');', tick);
       
   153   }
       
   154   if ( typeof(done) == 'function' )
       
   155   {
       
   156     setTimeout(function()
       
   157       {
       
   158         done(o);
       
   159       }, tick);
       
   160   }
       
   161   return this;
       
   162 }
       
   163 DNobj.prototype.fadeIn = function(time, done)
       
   164 {
       
   165   return this.fade(100, time, done);
       
   166 }
       
   167 DNobj.prototype.fadeOut = function(time, done)
       
   168 {
       
   169   return this.fade(0, time, done);
       
   170 }
    89 
   171 
    90 // Equivalent to PHP trim() function
   172 // Equivalent to PHP trim() function
    91 function trim(text)
   173 function trim(text)
    92 {
   174 {
    93   text = text.replace(/^([\s]+)/, '');
   175   text = text.replace(/^([\s]+)/, '');