scripts/domutils.js
author Dan
Fri, 15 Aug 2008 23:31:37 -0400
changeset 33 3b4aef1efff6
parent 11 0faea3a6c881
child 58 05a69bab5f10
permissions -rw-r--r--
Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now

// The "Dynano" Javascript framework. Similar in syntax to JQuery but only has what Enano needs.
// License = GPLv2

var $ = function(id)
{
  return new DNobj(id);
}
var $dynano = $;
function DNobj(id)
{
  this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
  if ( !this.object )
  {
    this.object = false;
    return this;
  }
  this.height = __DNObjGetHeight(this.object);
  this.width = __DNObjGetWidth(this.object);
}
function __DNObjGetHeight(o) {
  return o.offsetHeight;
}

function __DNObjGetWidth(o) {
  return o.offsetWidth;
}

function addClass(obj, clsname)
{
  var cnt = obj.className;
  var space = ( (cnt + '').length > 0 ) ? ' ' : '';
  var cls = cnt + space + clsname;
  obj.className = cls;
}

function rmClass(obj, clsname)
{
  var cnt = obj.className;
  if ( cnt == clsname )
  {
    obj.className = '';
  }
  else
  {
    cnt = cnt.replace(clsname, '');
    cnt = trim(cnt);
    obj.className = cnt;
  }
}

function hasClass(obj, clsname)
{
  var cnt = obj.className;
  if ( !cnt )
    return false;
  if ( cnt == clsname )
    return true;
  cnt = cnt.split(' ');
  
  for ( var i in cnt )
    if ( cnt[i] == clsname )
      return true;
    
  return false;
}
function __DNObjGetLeft(obj) {
  var left_offset = obj.offsetLeft;
  while ((obj = obj.offsetParent) != null) {
    left_offset += obj.offsetLeft;
  }
  return left_offset;
}

function __DNObjGetTop(obj) {
  var left_offset = obj.offsetTop;
  while ((obj = obj.offsetParent) != null) {
    left_offset += obj.offsetTop;
  }
  return left_offset;
}

DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }

// Equivalent to PHP trim() function
function trim(text)
{
  text = text.replace(/^([\s]+)/, '');
  text = text.replace(/([\s]+)$/, '');
  return text;
}

// Tell which elements have the specified CSS class
// Parameters:
//   * object - HTMLElement
//   * string - class name
//   * string - tag name, if omitted will test all elements (slow)
function getElementsByClassName(oRoot, className, tagName)
{
  tagName = ( tagName ) ? tagName : '*';
  var arrEls = document.getElementsByTagName(tagName);
  var arrResult = [];
  for ( var i = 0; i < arrEls.length; i++ )
  {
    if ( $(arrEls[i]).hasClass(className) )
    {
      arrResult.push(arrEls[i]);
    }
  }
  return arrResult;
}

// shortcut :)
document.getElementsByClassName = function(a, b)
{
  return getElementsByClassName(document, a, b);
}