#!/usr/bin/perl
# (c) 2007 James Robson



=pod

	Demonstrates Trillbox::Treeview control

=cut

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);


# Load our TR widgets from here
use lib './Trillbox';
use Trillbox::Treeview;



print header;
print start_html;


my $tv = Trillbox::Treeview->new();

$tv->add_node( 'one' );
$tv->add_node( 'two' );
$tv->add_node( 'node3' );

$tv->add_node( [ 'one', 'one_l2' ] );
$tv->add_node( [ 'one', 'one_l2', 'one_l3' ] );
$tv->add_node( [ 'one', 'one_l2', 'one_l3', 'one_l4' ] );
$tv->add_node( [ 'one', 'two_l2' ] );


$tv->add_node( [ 'two', 'one_l2' ] );
$tv->add_node( [ 'two', 'two_l2' ] );
$tv->add_node( [ 'two', 'two_l2', 'one_l3' ] );
#$tv->add_node( [ 'two', 'two_l2', 'one_l3' ] ); # Can add duplicate named nodes... bad?
$tv->add_node( [ 'two', 'two_l2', 'one_l3', 'one_l4' ] );
$tv->add_node( [ 'two', 'two_l2', 'one_l3', 'two_l4' ] );
$tv->add_node( [ 'two', 'two_l2', 'one_l3', 'three_l4' ] );


$tv->set( attrs => { style=>"margin:3px;margin-left:3em;font-family:sans-serif" } );

print "
<p>
This shows the entire tree built by add_node():
</p>
";

print $tv->output();

print "

<hr>
<p>
This demonstrates traversing the tree one node-level at a time. You can verify
the traversal by the expanded output above:
</p>

<p>
Go to <a href=\"treeview.cgi\">Root</a>
</p>
";

my $exp_node_pat = '-_exp_-';

my @node_path = (defined param('exp_node')) ? get_node_path( param('exp_node') ) : ();


my $opref = $tv->output_children( @node_path );

if ( defined($opref) ) {

	for (@{$opref}) {
		my @ta = ( @node_path, ${$_}{'text'} );
		my $p = join($exp_node_pat, @ta);
		

		print "

	<div style=\"margin-left:3em;float:left;width:200px\">${$_}{'text'}</div><a href=\"?exp_node=$p\">expand</a><br />

	";

	}

}




my $tv2 = Trillbox::Treeview->new();

$tv2->set( attrs => { style=>"margin:3px;margin-left:3em;font-family:sans-serif" } );

print "<hr>

<p>
This structure comes explicity from an actual directory tree:
</p>
";

my @path;
$tv2->add_node('Treeview');
get_folders( 'Treeview' );

print $tv2->output();







print "<hr>

<p>
Test using template defined in __DATA__ section of treeview.cgi (uses
&lt;blockquote&gt; rather than &lt;div&gt;):
</p>
";


undef $tv2;
undef @path;

my $tstr;
for(<DATA>) { $tstr .= $_ }


# Use DATA section
$tv2 =  Trillbox::Treeview->new( template_str => $tstr );
$tv2->add_node('Treeview');
get_folders( 'Treeview' );

print $tv2->output();


print end_html;










sub get_node_path {
	my @a = split(/$exp_node_pat/, shift);

	my @trl = @a;
	my $trail = join('/', @trl);

	print "<div style=\"font-weight:bold\">$trail</div>";

	return @a;
}



# Return directory content using simple recursion
sub get_folders {

	my $dir = shift;



    opendir(DIR, $dir) || die "$!";
	my @dirs;
	my @fils;
	for (readdir(DIR)) {
		next if /^\./;
		my $pth = "$dir/$_";

		# Directory nodes
		if ( -d $pth ) {
			push(@dirs, $pth);
			my @t = split(/\//, $pth);
			$tv2->add_node( \@t );
		}


		# Handle file nodes
		if ( -f $pth ) {
			my @t = split(/\//, $pth);
			
			if ($t[$#t] =~ /gif$|jpg$/i) {
				$t[$#t] = "<img src=\"$pth\" alt=\"\" /> <a href=\"$pth\">$t[$#t]</a>";
			}
			else {
				$t[$#t] = "<a href=\"$pth\">$t[$#t]</a>";
			}

			$tv2->add_node( \@t );
		}

	}
	close(DIR);
	


	for (@dirs) {
		get_folders($_);
	}
	shift(@path);

}





# Template definition:

__DATA__
<%=== open ===%>
<blockquote><%text%>
<%=== close ===%>
</blockquote>




syntax highlighted by Code2HTML, v. 0.9.1