| 1 | package Tags::HTML::Tree; | ||
| 2 | |||
| 3 | 6 | ▶ | use base qw(Tags::HTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 6 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Class::Utils qw(set_params split_params); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use English; | ||
| 9 | use Error::Pure qw(err); | ||
| 10 | use Mo::utils 0.12 qw(check_code check_required); | ||
| 11 | use Mo::utils::CSS 0.07 qw(check_css_class check_css_unit); | ||
| 12 | use Scalar::Util qw(blessed); | ||
| 13 | use Unicode::UTF8 qw(decode_utf8); | ||
| 14 | |||
| 15 | our $VERSION = 0.07; | ||
| 16 | |||
| 17 | # Constructor. | ||
| 18 | sub new { | ||
| 19 | my ($class, @params) = @_; | ||
| 20 | |||
| 21 | # Create object. | ||
| 22 | my ($object_params_ar, $other_params_ar) = split_params( | ||
| 23 | ['cb_value', 'css_class', 'indent'], @params); | ||
| 24 | my $self = $class->SUPER::new(@{$other_params_ar}); | ||
| 25 | |||
| 26 | # Callback for tree value. | ||
| 27 | $self->{'cb_value'} = sub { | ||
| 28 | my ($self, $tree) = @_; | ||
| 29 | |||
| 30 | $self->{'tags'}->put( | ||
| 31 | ['d', $tree->value], | ||
| 32 | ); | ||
| 33 | |||
| 34 | return; | ||
| 35 | }; | ||
| 36 | |||
| 37 | # CSS class. | ||
| 38 | $self->{'css_class'} = 'tree'; | ||
| 39 | |||
| 40 | # Tree indent. | ||
| 41 | $self->{'indent'} = '2em'; | ||
| 42 | |||
| 43 | # Process params. | ||
| 44 | set_params($self, @{$object_params_ar}); | ||
| 45 | |||
| 46 | check_code($self, 'cb_value'); | ||
| 47 | |||
| 48 | check_required($self, 'css_class'); | ||
| 49 | check_css_class($self, 'css_class'); | ||
| 50 | |||
| 51 | check_css_unit($self, 'indent'); | ||
| 52 | |||
| 53 | # Object. | ||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _cleanup { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | delete $self->{'_tree'}; | ||
| 61 | |||
| 62 | return; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _init { | ||
| 66 | my ($self, $tree) = @_; | ||
| 67 | |||
| 68 | if (! defined $tree | ||
| 69 | || ! blessed($tree) | ||
| 70 | || ! $tree->isa('Tree')) { | ||
| 71 | |||
| 72 | err "Data object must be a 'Tree' instance."; | ||
| 73 | } | ||
| 74 | |||
| 75 | $self->{'_tree'} = $tree; | ||
| 76 | |||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _prepare { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | $self->script_js([<<"END"]); | ||
| 84 | window.addEventListener('load', (event) => { | ||
| 85 | let toggler = document.getElementsByClassName("caret"); | ||
| 86 | for (let i = 0; i < toggler.length; i++) { | ||
| 87 | toggler[i].addEventListener("click", function() { | ||
| 88 | this.parentElement.querySelector(".nested").classList.toggle("active"); | ||
| 89 | this.classList.toggle("caret-down"); | ||
| 90 | }); | ||
| 91 | } | ||
| 92 | }); | ||
| 93 | END | ||
| 94 | |||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | # Process 'Tags'. | ||
| 99 | sub _process { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | if (! exists $self->{'_tree'}) { | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | $self->{'tags'}->put( | ||
| 107 | ['b', 'ul'], | ||
| 108 | ['a', 'class', $self->{'css_class'}], | ||
| 109 | ); | ||
| 110 | $self->_li($self->{'_tree'}); | ||
| 111 | $self->{'tags'}->put( | ||
| 112 | ['e', 'ul'], | ||
| 113 | ); | ||
| 114 | |||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _process_css { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | $self->{'css'}->put( | ||
| 122 | ['s', 'ul, .'.$self->{'css_class'}], | ||
| 123 | ['d', 'list-style-type', 'none'], | ||
| 124 | ['d', 'padding-left', $self->{'indent'}], | ||
| 125 | ['e'], | ||
| 126 | |||
| 127 | ['s', '.caret'], | ||
| 128 | ['d', 'cursor', 'pointer'], | ||
| 129 | ['d', '-webkit-user-select', 'none'], | ||
| 130 | ['d', '-moz-user-select', 'none'], | ||
| 131 | ['d', '-ms-user-select', 'none'], | ||
| 132 | ['d', 'user-select', 'none'], | ||
| 133 | ['e'], | ||
| 134 | |||
| 135 | ['s', '.caret::before'], | ||
| 136 | ['d', 'content', decode_utf8('"⯈"')], | ||
| 137 | ['d', 'color', 'black'], | ||
| 138 | ['d', 'display', 'inline-block'], | ||
| 139 | ['d', 'margin-right', '6px'], | ||
| 140 | ['e'], | ||
| 141 | |||
| 142 | ['s', '.caret-down::before'], | ||
| 143 | ['d', 'transform', 'rotate(90deg)'], | ||
| 144 | ['e'], | ||
| 145 | |||
| 146 | ['s', '.nested'], | ||
| 147 | ['d', 'display', 'none'], | ||
| 148 | ['e'], | ||
| 149 | |||
| 150 | ['s', '.active'], | ||
| 151 | ['d', 'display', 'block'], | ||
| 152 | ['e'], | ||
| 153 | ); | ||
| 154 | |||
| 155 | return; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _li { | ||
| 159 | my ($self, $tree) = @_; | ||
| 160 | |||
| 161 | my $meta_hr = $tree->meta; | ||
| 162 | $self->{'tags'}->put( | ||
| 163 | ['b', 'li'], | ||
| 164 | ); | ||
| 165 | my @children = $tree->children; | ||
| 166 | if (@children) { | ||
| 167 | $self->{'tags'}->put( | ||
| 168 | ['b', 'span'], | ||
| 169 | ['a', 'class', 'caret'], | ||
| 170 | ); | ||
| 171 | $self->{'cb_value'}->($self, $tree); | ||
| 172 | $self->{'tags'}->put( | ||
| 173 | ['e', 'span'], | ||
| 174 | |||
| 175 | ['b', 'ul'], | ||
| 176 | ['a', 'class', 'nested'], | ||
| 177 | ); | ||
| 178 | foreach my $child (@children) { | ||
| 179 | $self->_li($child); | ||
| 180 | } | ||
| 181 | $self->{'tags'}->put( | ||
| 182 | ['e', 'ul'], | ||
| 183 | ); | ||
| 184 | } else { | ||
| 185 | $self->{'cb_value'}->($self, $tree); | ||
| 186 | } | ||
| 187 | $self->{'tags'}->put( | ||
| 188 | ['e', 'li'], | ||
| 189 | ); | ||
| 190 | |||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | 1; | ||
| 195 | |||
| 196 | __END__ | ||