| 1 | package Tags::HTML::Image; | ||
| 2 | |||
| 3 | 3 | ▶ | use base qw(Tags::HTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | 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 Error::Pure qw(err); | ||
| 9 | use Mo::utils 0.12 qw(check_code); | ||
| 10 | use Mo::utils::CSS 0.02 qw(check_css_class); | ||
| 11 | use Scalar::Util qw(blessed); | ||
| 12 | |||
| 13 | our $VERSION = 0.04; | ||
| 14 | |||
| 15 | # Constructor. | ||
| 16 | sub new { | ||
| 17 | my ($class, @params) = @_; | ||
| 18 | |||
| 19 | # Create object. | ||
| 20 | my ($object_params_ar, $other_params_ar) = split_params( | ||
| 21 | ['css_class', 'css_comment_height', 'fit_minus', | ||
| 22 | 'img_comment_cb', 'img_select_cb', 'img_src_cb', 'img_width', 'title'], | ||
| 23 | @params); | ||
| 24 | my $self = $class->SUPER::new(@{$other_params_ar}); | ||
| 25 | |||
| 26 | # Image CSS class. | ||
| 27 | $self->{'css_class'} = 'image'; | ||
| 28 | |||
| 29 | # Image comment height (in pixels). | ||
| 30 | $self->{'css_comment_height'} = '50'; | ||
| 31 | |||
| 32 | # Length to minus of image fit. | ||
| 33 | $self->{'fit_minus'} = undef; | ||
| 34 | |||
| 35 | # Image comment callback. | ||
| 36 | $self->{'img_comment_cb'} = undef; | ||
| 37 | |||
| 38 | # Image select callback. | ||
| 39 | $self->{'img_select_cb'} = undef; | ||
| 40 | |||
| 41 | # Image src callback across data object. | ||
| 42 | $self->{'img_src_cb'} = undef; | ||
| 43 | |||
| 44 | # Image width in pixels. | ||
| 45 | $self->{'img_width'} = undef; | ||
| 46 | |||
| 47 | # Image title. | ||
| 48 | $self->{'title'} = undef; | ||
| 49 | |||
| 50 | # Process params. | ||
| 51 | set_params($self, @{$object_params_ar}); | ||
| 52 | |||
| 53 | check_css_class($self, 'css_class'); | ||
| 54 | |||
| 55 | # Check callback codes. | ||
| 56 | check_code($self, 'img_comment_cb'); | ||
| 57 | check_code($self, 'img_select_cb'); | ||
| 58 | check_code($self, 'img_src_cb'); | ||
| 59 | |||
| 60 | $self->_cleanup; | ||
| 61 | |||
| 62 | # Object. | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _cleanup { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | delete $self->{'_image'}; | ||
| 70 | $self->{'_image_comment_tags'} = []; | ||
| 71 | $self->{'_image_comment_css'} = []; | ||
| 72 | $self->{'_image_select_tags'} = []; | ||
| 73 | $self->{'_image_select_css'} = []; | ||
| 74 | delete $self->{'_image_url'}; | ||
| 75 | |||
| 76 | return; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _init { | ||
| 80 | my ($self, $image, @params) = @_; | ||
| 81 | |||
| 82 | if (! defined $image) { | ||
| 83 | err 'Image object is required.'; | ||
| 84 | } | ||
| 85 | if (! blessed($image) || ! $image->isa('Data::Image')) { | ||
| 86 | err "Image object must be a instance of 'Data::Image'."; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->{'_image'} = $image; | ||
| 90 | |||
| 91 | # Process image URL. | ||
| 92 | if (defined $self->{'_image'}->url) { | ||
| 93 | $self->{'_image_url'} = $self->{'_image'}->url; | ||
| 94 | } elsif (defined $self->{'_image'}->url_cb) { | ||
| 95 | $self->{'_image_url'} = $self->{'_image'}->url_cb->($self->{'_image'}); | ||
| 96 | } elsif (defined $self->{'img_src_cb'}) { | ||
| 97 | $self->{'_image_url'} = $self->{'img_src_cb'}->($self->{'_image'}); | ||
| 98 | } else { | ||
| 99 | err 'No image URL.'; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Process comment. | ||
| 103 | if (defined $self->{'img_comment_cb'}) { | ||
| 104 | ($self->{'_image_comment_tags'}, $self->{'_image_comment_css'}) | ||
| 105 | = $self->{'img_comment_cb'}->($self, $image, @params); | ||
| 106 | } else { | ||
| 107 | if (defined $image->comment) { | ||
| 108 | $self->{'_image_comment_tags'} = [ | ||
| 109 | ['d', $image->comment], | ||
| 110 | ]; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | if (@{$self->{'_image_comment_tags'}}) { | ||
| 114 | my $comment_font_size = $self->{'css_comment_height'} / 2; | ||
| 115 | my $comment_vertical_padding = $self->{'css_comment_height'} / 4; | ||
| 116 | push @{$self->{'_image_comment_css'}}, ( | ||
| 117 | ['s', '.'.$self->{'css_class'}.' figcaption'], | ||
| 118 | ['d', 'position', 'absolute'], | ||
| 119 | ['d', 'bottom', 0], | ||
| 120 | ['d', 'background', 'rgb(0, 0, 0)'], | ||
| 121 | ['d', 'background', 'rgba(0, 0, 0, 0.5)'], | ||
| 122 | ['d', 'color', '#f1f1f1'], | ||
| 123 | ['d', 'width', '100%'], | ||
| 124 | ['d', 'transition', '.5s ease'], | ||
| 125 | ['d', 'opacity', 0], | ||
| 126 | ['d', 'font-size', $comment_font_size.'px'], | ||
| 127 | ['d', 'padding', $comment_vertical_padding.'px 5px'], | ||
| 128 | ['d', 'text-align', 'center'], | ||
| 129 | ['e'], | ||
| 130 | |||
| 131 | ['s', 'figure.'.$self->{'css_class'}.':hover figcaption'], | ||
| 132 | ['d', 'opacity', 1], | ||
| 133 | ['e'], | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | if (defined $self->{'img_select_cb'}) { | ||
| 138 | my $select_hr = $self->{'img_select_cb'}->($self, $image, @params); | ||
| 139 | if (ref $select_hr eq 'HASH' && exists $select_hr->{'value'}) { | ||
| 140 | $select_hr->{'css_background_color'} ||= 'lightgreen'; | ||
| 141 | $self->{'_image_select_tags'} = [ | ||
| 142 | ['b', 'i'], | ||
| 143 | ['a', 'class', 'selected'], | ||
| 144 | ['a', 'style', 'background-color: '.$select_hr->{'css_background_color'}.';'], | ||
| 145 | exists $select_hr->{'value'} ? ( | ||
| 146 | ['d', $select_hr->{'value'}], | ||
| 147 | ) : (), | ||
| 148 | ['e', 'i'], | ||
| 149 | ]; | ||
| 150 | } | ||
| 151 | |||
| 152 | push @{$self->{'_image_select_css'}}, ( | ||
| 153 | ['s', '.'.$self->{'css_class'}.' .selected'], | ||
| 154 | ['d', 'border', '1px solid black'], | ||
| 155 | ['d', 'border-radius', '0.5em'], | ||
| 156 | ['d', 'color', 'black'], | ||
| 157 | ['d', 'padding', '0.5em'], | ||
| 158 | ['d', 'position', 'absolute'], | ||
| 159 | ['d', 'right', '10px'], | ||
| 160 | ['d', 'top', '10px'], | ||
| 161 | ['e'], | ||
| 162 | ); | ||
| 163 | } | ||
| 164 | |||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Process 'Tags'. | ||
| 169 | sub _process { | ||
| 170 | my $self = shift; | ||
| 171 | |||
| 172 | if (! exists $self->{'_image'}) { | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | |||
| 176 | # Begin of figure. | ||
| 177 | $self->{'tags'}->put( | ||
| 178 | ['b', 'figure'], | ||
| 179 | ['a', 'class', $self->{'css_class'}], | ||
| 180 | ); | ||
| 181 | |||
| 182 | # Begin of image title. | ||
| 183 | if (defined $self->{'title'}) { | ||
| 184 | $self->{'tags'}->put( | ||
| 185 | ['b', 'fieldset'], | ||
| 186 | ['b', 'legend'], | ||
| 187 | ['d', $self->{'title'}], | ||
| 188 | ['e', 'legend'], | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | # Select information. | ||
| 193 | if (@{$self->{'_image_select_tags'}}) { | ||
| 194 | $self->{'tags'}->put( | ||
| 195 | @{$self->{'_image_select_tags'}}, | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | my @alt; | ||
| 200 | if ($self->{'_image'}->comment) { | ||
| 201 | push @alt, ['a', 'alt', $self->{'_image'}->comment]; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Image. | ||
| 205 | $self->{'tags'}->put( | ||
| 206 | ['b', 'img'], | ||
| 207 | @alt, | ||
| 208 | ['a', 'src', $self->{'_image_url'}], | ||
| 209 | ['e', 'img'], | ||
| 210 | ); | ||
| 211 | |||
| 212 | # Image comment. | ||
| 213 | if (@{$self->{'_image_comment_tags'}}) { | ||
| 214 | $self->{'tags'}->put( | ||
| 215 | ['b', 'figcaption'], | ||
| 216 | @{$self->{'_image_comment_tags'}}, | ||
| 217 | ['e', 'figcaption'], | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | |||
| 221 | # End of image title. | ||
| 222 | if (defined $self->{'title'}) { | ||
| 223 | $self->{'tags'}->put( | ||
| 224 | ['e', 'fieldset'], | ||
| 225 | ); | ||
| 226 | } | ||
| 227 | |||
| 228 | # End of figure. | ||
| 229 | $self->{'tags'}->put( | ||
| 230 | ['e', 'figure'], | ||
| 231 | ); | ||
| 232 | |||
| 233 | return; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _process_css { | ||
| 237 | my $self = shift; | ||
| 238 | |||
| 239 | if (! exists $self->{'_image'}) { | ||
| 240 | return; | ||
| 241 | } | ||
| 242 | |||
| 243 | my $calc; | ||
| 244 | if (! defined $self->{'img_width'}) { | ||
| 245 | $calc .= 'calc(100vh'; | ||
| 246 | if (defined $self->{'fit_minus'}) { | ||
| 247 | $calc .= ' - '.$self->{'fit_minus'}; | ||
| 248 | } | ||
| 249 | $calc .= ')'; | ||
| 250 | } | ||
| 251 | |||
| 252 | $self->{'css'}->put( | ||
| 253 | ['s', '.'.$self->{'css_class'}.' img'], | ||
| 254 | ['d', 'display', 'block'], | ||
| 255 | ['d', 'height', '100%'], | ||
| 256 | ['d', 'width', '100%'], | ||
| 257 | ['d', 'object-fit', 'contain'], | ||
| 258 | ['e'], | ||
| 259 | |||
| 260 | ['s', '.'.$self->{'css_class'}], | ||
| 261 | defined $self->{'img_width'} ? ( | ||
| 262 | ['d', 'width', $self->{'img_width'}], | ||
| 263 | ) : ( | ||
| 264 | ['d', 'height', $calc], | ||
| 265 | ), | ||
| 266 | ['e'], | ||
| 267 | |||
| 268 | @{$self->{'_image_comment_css'}}, | ||
| 269 | |||
| 270 | @{$self->{'_image_select_css'}}, | ||
| 271 | ); | ||
| 272 | |||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | 1; | ||
| 277 | |||
| 278 | __END__ | ||