scripts/domutils.js
author Dan
Mon, 01 Sep 2008 13:06:50 -0400
changeset 40 bd3372a2afc1
parent 11 0faea3a6c881
child 58 05a69bab5f10
permissions -rw-r--r--
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
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
11
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
    98
// Tell which elements have the specified CSS class
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
    99
// Parameters:
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   100
//   * object - HTMLElement
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   101
//   * string - class name
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   102
//   * string - tag name, if omitted will test all elements (slow)
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   103
function getElementsByClassName(oRoot, className, tagName)
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   104
{
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   105
  tagName = ( tagName ) ? tagName : '*';
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   106
  var arrEls = document.getElementsByTagName(tagName);
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   107
  var arrResult = [];
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   108
  for ( var i = 0; i < arrEls.length; i++ )
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   109
  {
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   110
    if ( $(arrEls[i]).hasClass(className) )
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   111
    {
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   112
      arrResult.push(arrEls[i]);
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   113
    }
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   114
  }
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   115
  return arrResult;
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   116
}
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   117
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   118
// shortcut :)
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   119
document.getElementsByClassName = function(a, b)
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   120
{
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   121
  return getElementsByClassName(document, a, b);
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   122
}
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 2
diff changeset
   123