###############################################################################
#
#  WookeeExpandable.pm
#
#  (C) 2004-2007 Michael Buschbeck <michael@buschbeck.net>
#  $Id$
#
#  Provides block markup for expandable sections: Hidden by default, they
#  appear when the user clicks an anchor tag and can be re-hidden by clicking
#  the anchor again.
#
#    <+> Click to open...
#    Text appears only when section is opened.
#    Expandable sections can be nested.
#    </+>
#

package WookeeExpandable;

use version; our $VERSION = qv('1.103');


###############################################################################
#
#  BlockWikiExpandable
#
#  Implements expandable block markup.
#

package BlockWikiExpandable;

use base 'BlockWiki';

use strict;
use warnings;

use constant TRUE  => 1;
use constant FALSE => 0;

BlockWikiExpandable->register();


###########################################################
#
#  Static
#

use constant propTag => '+';

our $iIdentifier = 0;


###########################################################
#
#  parseHeader  $text
#  parseHeader \$text
#

sub parseHeader
{
  my $self = shift;
  my $text = shift;

  my $refText = (ref($text) ? $text : \$text);
  $$refText =~ s/<[-+]>\s*//;
  
  $self->SUPER::parseHeader($text);
}


###########################################################
#
#  parseBlock  $text
#  parseBlock \$text
#
#  Adds HTML markup around the emitted block content.
#

sub parseBlock
{
  my $self = shift;
  my $text = shift;

  my $result = $self->SUPER::parseBlock($text);

  $iIdentifier++;
  my $identifier = "wookee-expandable-$iIdentifier";
  
  return qq[<table border=0 cellspacing=0 cellpadding=0 class="wookee-expandable">]
       . qq[<tr>]
       . qq[<td valign=top>] . $self->{anchortext} . qq[</td>]
       . qq[<td valign=top>&nbsp;]
       . qq[<a class="wookee-expandable-label" labelfor="$identifier" onclick="toggleElementExpanded(event);">]
       . qq[&gt;&gt;]
       . qq[</a>]
       . qq[</td>]
       . qq[</tr>]
       . qq[</table>]
       . qq[<div id="$identifier" class="wookee-expandable-body expandable">$result</div>];
}


###########################################################
#
#  paraAddFirst $chunk
#
#  Saves the parsed and formatted content of the first
#  paragraph as the expandable block's anchor text.
#

sub paraAddFirst
{
  my $self  = shift;
  my $chunk = shift;

  if (not defined($self->{anchortext})) {
    $self->paraCloseAll();
    $self->{anchortext} = $self->{result};
    undef $self->{result};
  }

  $self->SUPER::paraAddFirst($chunk);
}


1;

