includes/clientside/static/expander.js
changeset 582 a38876c0793c
child 699 c7d737202d59
equal deleted inserted replaced
581:5e8fd89c02ea 582:a38876c0793c
       
     1 /*
       
     2  * Expandable fieldsets
       
     3  */
       
     4 
       
     5 var expander_onload = function()
       
     6 {
       
     7   var sets = document.getElementsByTagName('fieldset');
       
     8   if ( sets.length < 1 )
       
     9     return false;
       
    10   var init_us = [];
       
    11   for ( var index = 0; index < sets.length; index++ )
       
    12   {
       
    13     var mode = sets[index].getAttribute('enano:expand');
       
    14     if ( mode == 'closed' || mode == 'open' )
       
    15     {
       
    16       init_us.push(sets[index]);
       
    17     }
       
    18   }
       
    19   for ( var k = 0; k < init_us.length; k++ )
       
    20   {
       
    21     expander_init_element(init_us[k]);
       
    22   }
       
    23 }
       
    24 
       
    25 function expander_init_element(el)
       
    26 {
       
    27   // get the legend tag
       
    28   var legend = el.getElementsByTagName('legend')[0];
       
    29   if ( !legend )
       
    30     return false;
       
    31   // existing content
       
    32   var existing_inner = legend.innerHTML;
       
    33   // blank the innerHTML and replace it with a link
       
    34   legend.innerHTML = '';
       
    35   var button = document.createElement('a');
       
    36   button.className = 'expander expander-open';
       
    37   button.innerHTML = existing_inner;
       
    38   button.href = '#';
       
    39   
       
    40   legend.appendChild(button);
       
    41   
       
    42   button.onclick = function()
       
    43   {
       
    44     try
       
    45     {
       
    46       expander_handle_click(this);
       
    47     }
       
    48     catch(e)
       
    49     {
       
    50       console.debug('Exception caught: ', e);
       
    51     }
       
    52     return false;
       
    53   }
       
    54   
       
    55   if ( el.getAttribute('enano:expand') == 'closed' )
       
    56   {
       
    57     expander_close(el);
       
    58   }
       
    59 }
       
    60 
       
    61 function expander_handle_click(el)
       
    62 {
       
    63   if ( el.parentNode.parentNode.tagName != 'FIELDSET' )
       
    64     return false;
       
    65   var parent = el.parentNode.parentNode;
       
    66   if ( parent.getAttribute('enano:expand') == 'closed' )
       
    67   {
       
    68     expander_open(parent);
       
    69   }
       
    70   else
       
    71   {
       
    72     expander_close(parent);
       
    73   }
       
    74 }
       
    75 
       
    76 function expander_close(el)
       
    77 {
       
    78   var children = el.childNodes;
       
    79   for ( var i = 0; i < children.length; i++ )
       
    80   {
       
    81     var child = children[i];
       
    82     if ( child.tagName == 'LEGEND' )
       
    83     {
       
    84       var a = child.getElementsByTagName('a')[0];
       
    85       $(a).rmClass('expander-open');
       
    86       $(a).addClass('expander-closed');
       
    87       continue;
       
    88     }
       
    89     if ( child.style )
       
    90     {
       
    91       child.expander_meta_old_state = child.style.display;
       
    92       child.style.display = 'none';
       
    93     }
       
    94   }
       
    95   el.expander_meta_padbak = el.style.padding;
       
    96   el.setAttribute('enano:expand', 'closed');
       
    97 }
       
    98 
       
    99 function expander_open(el)
       
   100 {
       
   101   var children = el.childNodes;
       
   102   for ( var i = 0; i < children.length; i++ )
       
   103   {
       
   104     var child = children[i];
       
   105     if ( child.tagName == 'LEGEND' )
       
   106     {
       
   107       var a = child.getElementsByTagName('a')[0];
       
   108       $(a).rmClass('expander-closed');
       
   109       $(a).addClass('expander-open');
       
   110       continue;
       
   111     }
       
   112     if ( child.expander_meta_old_state && child.style )
       
   113     {
       
   114       child.style.display = child.expander_meta_old_state;
       
   115       child.expander_meta_old_state = null;
       
   116     }
       
   117     else
       
   118     {
       
   119       if ( child.style )
       
   120       {
       
   121         child.style.display = null;
       
   122       }
       
   123     }
       
   124   }
       
   125   if ( el.expander_meta_padbak )
       
   126   {
       
   127     el.style.padding = el.expander_meta_padbak;
       
   128     el.expander_meta_padbak = null;
       
   129   }
       
   130   else
       
   131   {
       
   132     el.style.padding = null;
       
   133   }
       
   134   el.setAttribute('enano:expand', 'open');
       
   135 }
       
   136 
       
   137 addOnloadHook(expander_onload);