includes/clientside/static/expander.js
author Dan
Mon, 07 Jul 2008 02:48:14 -0400
changeset 601 9c1f07ae26d5
parent 582 a38876c0793c
child 699 c7d737202d59
permissions -rw-r--r--
Fixed $use_cache bug in plugins.php

/*
 * Expandable fieldsets
 */

var expander_onload = function()
{
  var sets = document.getElementsByTagName('fieldset');
  if ( sets.length < 1 )
    return false;
  var init_us = [];
  for ( var index = 0; index < sets.length; index++ )
  {
    var mode = sets[index].getAttribute('enano:expand');
    if ( mode == 'closed' || mode == 'open' )
    {
      init_us.push(sets[index]);
    }
  }
  for ( var k = 0; k < init_us.length; k++ )
  {
    expander_init_element(init_us[k]);
  }
}

function expander_init_element(el)
{
  // get the legend tag
  var legend = el.getElementsByTagName('legend')[0];
  if ( !legend )
    return false;
  // existing content
  var existing_inner = legend.innerHTML;
  // blank the innerHTML and replace it with a link
  legend.innerHTML = '';
  var button = document.createElement('a');
  button.className = 'expander expander-open';
  button.innerHTML = existing_inner;
  button.href = '#';
  
  legend.appendChild(button);
  
  button.onclick = function()
  {
    try
    {
      expander_handle_click(this);
    }
    catch(e)
    {
      console.debug('Exception caught: ', e);
    }
    return false;
  }
  
  if ( el.getAttribute('enano:expand') == 'closed' )
  {
    expander_close(el);
  }
}

function expander_handle_click(el)
{
  if ( el.parentNode.parentNode.tagName != 'FIELDSET' )
    return false;
  var parent = el.parentNode.parentNode;
  if ( parent.getAttribute('enano:expand') == 'closed' )
  {
    expander_open(parent);
  }
  else
  {
    expander_close(parent);
  }
}

function expander_close(el)
{
  var children = el.childNodes;
  for ( var i = 0; i < children.length; i++ )
  {
    var child = children[i];
    if ( child.tagName == 'LEGEND' )
    {
      var a = child.getElementsByTagName('a')[0];
      $(a).rmClass('expander-open');
      $(a).addClass('expander-closed');
      continue;
    }
    if ( child.style )
    {
      child.expander_meta_old_state = child.style.display;
      child.style.display = 'none';
    }
  }
  el.expander_meta_padbak = el.style.padding;
  el.setAttribute('enano:expand', 'closed');
}

function expander_open(el)
{
  var children = el.childNodes;
  for ( var i = 0; i < children.length; i++ )
  {
    var child = children[i];
    if ( child.tagName == 'LEGEND' )
    {
      var a = child.getElementsByTagName('a')[0];
      $(a).rmClass('expander-closed');
      $(a).addClass('expander-open');
      continue;
    }
    if ( child.expander_meta_old_state && child.style )
    {
      child.style.display = child.expander_meta_old_state;
      child.expander_meta_old_state = null;
    }
    else
    {
      if ( child.style )
      {
        child.style.display = null;
      }
    }
  }
  if ( el.expander_meta_padbak )
  {
    el.style.padding = el.expander_meta_padbak;
    el.expander_meta_padbak = null;
  }
  else
  {
    el.style.padding = null;
  }
  el.setAttribute('enano:expand', 'open');
}

addOnloadHook(expander_onload);