scripts/domutils.js
author Dan
Sun, 23 Mar 2008 22:03:57 -0400
changeset 4 cde92f6ec29f
parent 2 860ba7141641
child 11 0faea3a6c881
permissions -rw-r--r--
Should be completely iPhone/iPod Touch-friendly now :)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     1
// The "Dynano" Javascript framework. Similar in syntax to JQuery but only has what Enano needs.
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     2
// License = GPLv2
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     3
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     4
var $ = function(id)
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     5
{
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     6
  return new DNobj(id);
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     7
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     8
var $dynano = $;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
     9
function DNobj(id)
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    10
{
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    11
  this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    12
  if ( !this.object )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    13
  {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    14
    this.object = false;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    15
    return this;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    16
  }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    17
  this.height = __DNObjGetHeight(this.object);
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    18
  this.width = __DNObjGetWidth(this.object);
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    19
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    20
function __DNObjGetHeight(o) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    21
  return o.offsetHeight;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    22
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    23
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    24
function __DNObjGetWidth(o) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    25
  return o.offsetWidth;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    26
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    27
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    28
function addClass(obj, clsname)
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    29
{
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    30
  var cnt = obj.className;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    31
  var space = ( (cnt + '').length > 0 ) ? ' ' : '';
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    32
  var cls = cnt + space + clsname;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    33
  obj.className = cls;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    34
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    35
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    36
function rmClass(obj, clsname)
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    37
{
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    38
  var cnt = obj.className;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    39
  if ( cnt == clsname )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    40
  {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    41
    obj.className = '';
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    42
  }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    43
  else
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    44
  {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    45
    cnt = cnt.replace(clsname, '');
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    46
    cnt = trim(cnt);
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    47
    obj.className = cnt;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    48
  }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    49
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    50
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    51
function hasClass(obj, clsname)
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    52
{
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    53
  var cnt = obj.className;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    54
  if ( !cnt )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    55
    return false;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    56
  if ( cnt == clsname )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    57
    return true;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    58
  cnt = cnt.split(' ');
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    59
  
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    60
  for ( var i in cnt )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    61
    if ( cnt[i] == clsname )
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    62
      return true;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    63
    
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    64
  return false;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    65
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    66
function __DNObjGetLeft(obj) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    67
  var left_offset = obj.offsetLeft;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    68
  while ((obj = obj.offsetParent) != null) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    69
    left_offset += obj.offsetLeft;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    70
  }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    71
  return left_offset;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    72
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    73
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    74
function __DNObjGetTop(obj) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    75
  var left_offset = obj.offsetTop;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    76
  while ((obj = obj.offsetParent) != null) {
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    77
    left_offset += obj.offsetTop;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    78
  }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    79
  return left_offset;
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    80
}
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    81
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    82
DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    83
DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    84
DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    85
DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    86
DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    87
DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    88
DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents:
diff changeset
    89
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    90
// Equivalent to PHP trim() function
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    91
function trim(text)
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    92
{
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    93
  text = text.replace(/^([\s]+)/, '');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    94
  text = text.replace(/([\s]+)$/, '');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    95
  return text;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    96
}
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    97