SYNOPSIS
use HTML::PopupTreeSelect;
# setup your tree as a hash structure. This one sets up a tree like:
#
# - Root
# - Top Category 1
# - Sub Category 1
# - Sub Category 2
# - Top Category 2
my $data = { label => "Root",
value => 0,
children => [
{ label => "Top Category 1",
value => 1,
children => [
{ label => "Sub Category 1",
value => 2
},
{ label => "Sub Category 2",
value => 3
},
],
},
{ label => "Top Category 2",
value => 4
},
]
};
# create your HTML tree select widget. This one will call a
# javascript function 'select_category(value)' when the user selects
# a category.
my $select = HTML::PopupTreeSelect->new(name => 'category',
data => $data,
title => 'Select a Category',
button_label => 'Choose',
onselect => 'select_category');
# include it in your HTML page, for example using HTML::Template:
$template->param(category_select => $select->output);
DESCRIPTION
This module creates an HTML popup tree selector. The HTML and Javascript produced will work in Mozilla 1+ (Netscape 6+) on all operating systems, Microsoft IE 5+ and Safari 1.0. For an example, visit this page:
http://sam.tregar.com/html-popuptreeselect/example.html
I based the design for this widget on the xTree widget from WebFX. You can find it here:
http://webfx.eae.net/dhtml/xtree/
This module is used to provide the category chooser in Krang, an open source content management system. You can find out more about Krang here:
http://krang.sf.net
INSTALLATION
To use this module you'll need to copy the contents of the images/ directory in the module distribution into a place where your webserver can serve them. If that's not the same place your CGI will run from then you need to set the image_path parameter when you call new(). See below for details.INTERFACE
new()
new(), is used to build a new HTML selector. You call it with a description of the tree to display and get back an object. Call it with following parameters:- name
- A unique name for the tree selector. You can have multiple tree selectors on a page, but they must have unique names. Must be alpha-numeric and begin with a letter.
- data
-
This must be a hash reference (or an array reference of these hash
references, if there are multiple ``root'' categories) containing
the following keys:
-
- label (required)
- The textual label for this node.
- value (required)
- The value passed to the onselect handler or set in the form_field when the user selects this node.
- open (optional)
- If set to 1 this node will start open (showing its children). By default all nodes start closed.
- inactive (optional)
- If set to 1 this node will not be selectable. It will not appear as a link in the widget and clicking on the label will have no effect. However, if it has children they will still be accessible.
- children (optional)
- The 'children' key may point to an array of hashes with the same keys. This is the tree structure which will be displayed in the tree selector.
-
See SYNOPSIS above for an example of a valid data structure.
-
- title
- The title of the window which pops up.
- button_label (optional)
- The widget pops up when the user presses a button. This field gives the label for the button. Defaults to ``Choose''.
- onselect (optional)
- Specifies a Javascript function that will be called when an item in the tree is selected. Recieves the value of the item as a single argument. The default is for nothing to happen.
- form_field (optional)
- Specifies a form field to recieve the value of the selected item. This provides a no-javascript means to use this widget (although the widget itself, of course, uses great gobs of javascript).
- form_field_form (optional)
-
Specifies the form in which to find the "form_field" specified. If
not included the first form on the page will be used. - include_css (optional)
-
Set this to 0 and the default CSS will not be included in the widget
output. This allows you to include your own CSS which will be used by
your widget. Modifying the CSS will allow you to control the fonts,
colors and spacing in the output widget.
If you run the widget with include_css set to 1 then you can use that output as a base on which to make changes.
- resizable (optional)
- Set this to 1 and the default widget output will not be resizable. If you run the widget with resizable set to 1 then default output will have a bar at the bottom which allows it to be resized by dragging. Defaults to 0.
- image_path (optional)
- Set this to the URL to the images for the widget. These files should be copied from the images directory in the module distribution into a place where your webserver can reach them. By default this is empty and the widget expects to find images in the current directory.
- width (optional)
- Set this to the width of the popup window. Defaults to 200.
- height (optional)
- Set this to the height of the tree box inside the window. This defaults to 0 which allows the chooser to grow as the tree expands. If you set this option you'll probably want to set the "use_scrollbars" option as well.
- scrollbars (optional)
- If set to 1 the chooser will have a fixed size (specified by width and height) and show scrollbars inside the tree area.
- hide_selects (optional)
-
This option will cause the chooser to dynamically hide select boxes on
the page when the chooser opens. This is necessary in order to avoid
the select boxes showing through the chooser under Windows in both IE
and Mozilla (to a lesser extent). This defaults to 1. For a detailed
explanation of the problem, see this page:
http://www.webreference.com/dhtml/diner/seethru/
- hide_textareas (optional)
- This option will cause the chooser to dynamically hide textareas on the page when the chooser opens. This is necessary to workaround a bug in Netscape 6.0 through 7.0 in which buttons hovering over textareas are not clickable. This defect is fixed in version 7.1 and later. This option defaults to 0, since this problem only affects older browsers.
- parent_var (optional)
- This option includes a 'parent' loop in the template data used to construct the widget's HTML. It's not used by the default template, so it defaults to 0. Set to 1 to use this variable in your own template via sub-classing.
output()
Call output() to get HTML from the widget object to include in your page.SUBCLASSING
HTML::PopupTreeSelect can be subclassed, for the purposes of --- for example --- using a different template engine to generate the HTML. Here's one brief example, using the Template engine:
package My::PopupTreeSelect; use Template; use base 'HTML::PopupTreeSelect'; sub output { my($self) = @_; return $self->SUPER::output(Template->new); } sub _output_generate { my($self, $template, $param) = @_; my $output; $template->process(\$MY_TEMPLATE_SRC, $param, \$output); return $output; }
Of course, $MY_TEMPLATE_SRC will need to be provided, too. $HTML::PopupTreeSelect::TEMPLATE_SRC is a global variable, so it may be modified to your liking, or your own template data can be provided to your own template generator method.
CAVEATS
- The javascript used to implement the widget needs control over the global document.onmousedown, document.onmousemove and document.onmouseup handlers. This means that it's unlikely to play nice with other DHTML on the same page.
TODO
Here are some possible directions for future development. Send me a patch for one of these and you're guaranteed a place in Changes.- Allow each node to specify its own icon. Right now every node uses "closed_node.png" and "open_node.png".
BUGS
I know of no bugs in this module. If you find one, please file a bug report at:
http://rt.cpan.org
Alternately you can email me directly at "[email protected]". Please include the version of the module and a complete test case that demonstrates the bug.
COPYRIGHT AND LICENSE
Copyright (C) 2003, 2004 Sam TregarThis program is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.