File Coverage

blib/lib/Tree/Simple/View/DHTML.pm
Criterion Covered Total %
statement 148 149 99.3
branch 47 48 97.9
condition 6 6 100.0
subroutine 25 25 100.0
pod 4 4 100.0
total 230 232 99.1


line stmt bran cond sub pod time code
1              
2             package Tree::Simple::View::DHTML;
3              
4 1     1   13443 use strict;
  1         1  
  1         24  
5 1     1   3 use warnings;
  1         1  
  1         33  
6              
7             our $VERSION = '0.180001';
8              
9 1     1   3 use base 'Tree::Simple::View::HTML';
  1         0  
  1         407  
10              
11 1     1   4 use Tree::Simple::View::Exceptions;
  1         1  
  1         14  
12              
13 1     1   3 use constant OPEN_TAG => 1;
  1         1  
  1         37  
14 1     1   4 use constant CLOSE_TAG => 2;
  1         1  
  1         30  
15 1     1   3 use constant EXPANDED => 3;
  1         1  
  1         864  
16              
17             ## private methods
18              
19             sub _init {
20 17     17   30 my ($self, @args) = @_;
21 17         35 $self->SUPER::_init(@args);
22 17         15 $self->{list_counter_id} = 0;
23 17         131 ($self->{obj_id}) = ("$self" =~ /\((.*?)\)$/);
24             }
25              
26             sub _createUID {
27 85     85   66 my ($self, $tree) = @_;
28 85 100       142 return $tree->getUID() if $self->{config}->{use_tree_uids};
29 84         61 $self->{list_counter_id}++;
30 84         149 return join "_" => ($self->{obj_id}, $self->{list_counter_id});
31             }
32              
33             ## public methods
34              
35             sub expandPathSimple {
36 3     3 1 5 my ($self, $tree, $current_path, @path) = @_;
37 3         4 my @results = ("
    ");
38 3         6 my $root_depth = $tree->getDepth() + 1;
39 3         8 my $last_depth = -1;
40             my $traversal_sub = sub {
41 46     46   338 my ($t) = @_;
42 46         35 my $display_style = "none";
43 46 100 100     70 if (defined $current_path && $self->_compareNodeToPath($current_path, $t)) {
44 5         20 $display_style = "block";
45 5         5 $current_path = shift @path;
46             }
47 46         57 my $current_depth = $t->getDepth();
48 46 100       105 push @results => ("" x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
49 46 100       53 unless ($t->isLeaf()) {
50 16         63 my $uid = $self->_createUID($t);
51 16         28 push @results => ("
  • " .
  • 52             $t->getNodeValue() . "");
    53 16         57 push @results => "
      ";
    54             }
    55             else {
    56 30         117 push @results => ("
  • " . $t->getNodeValue() . "
  • ");
    57             }
    58 46         99 $last_depth = $current_depth;
    59 3         12 };
    60 3 100       7 $traversal_sub->($self->{tree}) if $self->{include_trunk};
    61 3         7 $self->{tree}->traverse($traversal_sub);
    62 3         24 $last_depth -= $root_depth;
    63 3 100       6 $last_depth++ if $self->{include_trunk};
    64 3         5 push @results => ("" x ($last_depth + 1));
    65            
    66 3         31 return (join "\n" => @results);
    67             }
    68              
    69             sub expandPathComplex {
    70 5     5 1 8 my ($self, $tree, $config, $current_path, @path) = @_;
    71            
    72 5         13 my ($list_func, $list_item_func) = $self->_processConfig($config);
    73            
    74 5         109 my @results = $list_func->(OPEN_TAG);
    75 5         16 my $root_depth = $tree->getDepth() + 1;
    76 5         13 my $last_depth = -1;
    77             my $traversal_sub = sub {
    78 76     76   598 my ($t) = @_;
    79 76         57 my $display_style = "none";
    80 76 100 100     158 if (defined $current_path && $self->_compareNodeToPath($current_path, $t)) {
    81 9         48 $display_style = "block";
    82 9         9 $current_path = shift @path;
    83             }
    84 76         144 my $current_depth = $t->getDepth();
    85 76 100       507 push @results => ($list_func->(CLOSE_TAG) x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
    86 76 100       100 unless ($t->isLeaf()) {
    87 26         124 my $uid = $self->_createUID($t);
    88 26         465 push @results => ($list_item_func->($t, EXPANDED, $uid));
    89 26         521 push @results => $list_func->(OPEN_TAG, $uid, $display_style);
    90             }
    91             else {
    92 50         1049 push @results => ($list_item_func->($t));
    93             }
    94 76         249 $last_depth = $current_depth;
    95 5         19 };
    96 5 100       12 $traversal_sub->($self->{tree}) if $self->{include_trunk};
    97 5         13 $self->{tree}->traverse($traversal_sub);
    98 5         42 $last_depth -= $root_depth;
    99 5 100       10 $last_depth++ if $self->{include_trunk};
    100 5         86 push @results => ($list_func->(CLOSE_TAG) x ($last_depth + 1));
    101 5         198 return (join "\n" => @results);
    102             }
    103              
    104             sub expandAllSimple {
    105 2     2 1 1 my ($self) = @_;
    106 2         4 my @results = ("
      ");
    107 2         5 my $root_depth = $self->{tree}->getDepth() + 1;
    108 2         7 my $last_depth = -1;
    109             my $traversal_sub = sub {
    110 31     31   219 my ($t) = @_;
    111 31         37 my $current_depth = $t->getDepth();
    112 31 100       75 push @results => ("" x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
    113 31 100       56 unless ($t->isLeaf()) {
    114 11         42 my $uid = $self->_createUID($t);
    115 11         20 push @results => ("
  • " .
  • 116             $t->getNodeValue() . "");
    117 11         37 push @results => "
      ";
    118             }
    119             else {
    120 20         78 push @results => ("
  • " . $t->getNodeValue() . "
  • ");
    121             }
    122 31         64 $last_depth = $current_depth;
    123 2         9 };
    124 2 100       4 $traversal_sub->($self->{tree}) if $self->{include_trunk};
    125 2         5 $self->{tree}->traverse($traversal_sub);
    126 2         16 $last_depth -= $root_depth;
    127 2 100       5 $last_depth++ if $self->{include_trunk};
    128 2         5 push @results => ("" x ($last_depth + 1));
    129 2         18 return (join "\n" => @results);
    130             }
    131              
    132             sub expandAllComplex {
    133 7     7 1 8 my ($self, $config) = @_;
    134            
    135 7         22 my ($list_func, $list_item_func) = $self->_processConfig($config);
    136            
    137 7         126 my @results = $list_func->(OPEN_TAG);
    138 7         23 my $root_depth = $self->{tree}->getDepth() + 1;
    139 7         18 my $last_depth = -1;
    140             my $traversal_sub = sub {
    141 94     94   752 my ($t) = @_;
    142 94         120 my $current_depth = $t->getDepth();
    143 94 100       606 push @results => ($list_func->(CLOSE_TAG) x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
    144 94 100       120 unless ($t->isLeaf()) {
    145 32         146 my $uid = $self->_createUID($t);
    146 32         565 push @results => ($list_item_func->($t, EXPANDED, $uid));
    147 32         802 push @results => $list_func->(OPEN_TAG, $uid);
    148             }
    149             else {
    150 62         1274 push @results => ($list_item_func->($t));
    151             }
    152 94         647 $last_depth = $current_depth;
    153 7         23 };
    154 7 100       14 $traversal_sub->($self->{tree}) if $self->{include_trunk};
    155 7         15 $self->{tree}->traverse($traversal_sub);
    156 7         62 $last_depth -= $root_depth;
    157 7 100       12 $last_depth++ if $self->{include_trunk};
    158 7         135 push @results => ($list_func->(CLOSE_TAG) x ($last_depth + 1));
    159 7         260 return (join "\n" => @results);
    160             }
    161              
    162             # code strings
    163              
    164 1         257 use constant LIST_FUNCTION_CODE_STRING => q|
    165             sub {
    166             my ($tag_type, $list_id, $display_style) = @_;
    167             # allow this to be found quickly
    168             return "" if ($tag_type == CLOSE_TAG);
    169             # test the most functional first
    170             if ($tag_type == OPEN_TAG && $list_id && $display_style) {
    171             my $temp_list_css;
    172             if ($list_css && $list_css !~ /CLASS/) {
    173             # in case someone has already set the list_css
    174             # property, we need to add out display property
    175             # to it, so we need to do a little text mangling
    176             $temp_list_css = $list_css;
    177             chop($temp_list_css);
    178             $temp_list_css .= " display: $display_style;'";
    179             }
    180             elsif ($list_css) {
    181             $temp_list_css = "${list_css} STYLE='display: $display_style;'"
    182             }
    183             else {
    184             $temp_list_css = " STYLE='display: $display_style;'"
    185             }
    186             return "<${list_type}${temp_list_css} ID='${list_id}'>"
    187             }
    188             # next...
    189             return "<${list_type}${list_css} ID='${list_id}'>"
    190             if ($tag_type == OPEN_TAG && $list_id);
    191             # and finally, something that does nothing really
    192             return "<${list_type}${list_css}>" if ($tag_type == OPEN_TAG);
    193             }
    194 1     1   5 |;
      1         1  
    195              
    196             sub _buildListItemFunction {
    197 12     12   22 my ($self, %config) = @_;
    198             # process the configuration directives
    199 12         30 my ($list_item_css, $expanded_item_css, $node_formatter) = $self->_processListItemConfig(%config);
    200            
    201 12         14 my $link_css = "";
    202 12 100       22 if (exists $config{link_css}) {
        100          
    203 2         3 $link_css = " STYLE='" . $config{link_css}. "'";
    204             }
    205             elsif (exists $config{link_css_class}) {
    206 3         5 $link_css = " CLASS='" . $config{link_css_class} . "'";
    207             }
    208            
    209 12         11 my $form_element_formatter;
    210 12 50       16 if (exists $config{form_element_formatter}) {
    211 0         0 $form_element_formatter = $config{form_element_formatter};
    212             }
    213             else {
    214 12 100       23 if (exists $config{radio_button}) {
        100          
    215 1         4 $form_element_formatter = $self->_makeRadioButtonFormatter($config{radio_button});
    216             }
    217             elsif (exists $config{checkbox}) {
    218 1         4 $form_element_formatter = $self->_makeCheckBoxFormatter($config{checkbox});
    219             }
    220             }
    221            
    222             # now compile the subroutine in the current environment
    223 12         1608 return eval $self->LIST_ITEM_FUNCTION_CODE_STRING;
    224             }
    225              
    226             sub _makeRadioButtonFormatter {
    227 1     1   2 my ($self, $radio_button_id) = @_;
    228             return sub {
    229 15     15   8 my ($t) = @_;
    230 15         28 return "";
    231             }
    232 1         3 }
    233              
    234             sub _makeCheckBoxFormatter {
    235 1     1   2 my ($self, $checkbox_id) = @_;
    236             return sub {
    237 15     15   11 my ($t) = @_;
    238 15         26 return "";
    239             }
    240 1         4 }
    241              
    242 1         46 use constant LIST_ITEM_FUNCTION_CODE_STRING => q|;
    243             sub {
    244             my ($t, $is_expanded, $tree_id) = @_;
    245             my $item_css = $list_item_css;
    246             if ($is_expanded) {
    247             $item_css = $expanded_item_css if $expanded_item_css;
    248             return "" .
    249             (($form_element_formatter) ? $form_element_formatter->($t) : "") .
    250             "" .
    251             (($node_formatter) ? $node_formatter->($t) : $t->getNodeValue()) .
    252             "";
    253             }
    254             return "" .
    255             (($form_element_formatter) ? $form_element_formatter->($t) : "") .
    256             (($node_formatter) ? $node_formatter->($t) : $t->getNodeValue()) .
    257             "";
    258             }
    259 1     1   7 |;
      1         1  
    260              
    261 1         34 use constant javascript => q|
    262            
    275 1     1   3 |;
      1         1  
    276              
    277             1;
    278              
    279             __END__