| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/VideoTrackList.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/28 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::DOM::VideoTrackList; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::List ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub init | ||
| 29 | { | ||
| 30 | my $self = shift( @_ ); | ||
| 31 | $self->{selectedindex} = -1; | ||
| 32 | $self->{_init_strict_use_sub} = 1; | ||
| 33 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 34 | return( $self ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub addEventListener { return( shift->SUPER::addEventListener({ | ||
| 38 | addtrack => { | ||
| 39 | add => { property => 'children', type => 'add', event => 'addtrack', callback => sub | ||
| 40 | { | ||
| 41 | my( $event, $ref ) = @_; | ||
| 42 | $event->track( $ref->{added} ) if( $ref->{added} ); | ||
| 43 | }}, | ||
| 44 | }, | ||
| 45 | change => { | ||
| 46 | add => { property => 'selected', type => 'add', event => 'change' }, | ||
| 47 | remove => { property => 'selected', type => 'remove', event => 'change' }, | ||
| 48 | }, | ||
| 49 | removetrack => { | ||
| 50 | remove => { property => 'children', type => 'remove', event => 'removetrack', callback => sub | ||
| 51 | { | ||
| 52 | my( $event, $ref ) = @_; | ||
| 53 | $event->track( $ref->{removed} ) if( $ref->{removed} ); | ||
| 54 | }}, | ||
| 55 | }, | ||
| 56 | }, @_ ) ); } | ||
| 57 | |||
| 58 | sub children { return( shift->reset(@_)->_set_get_object_array_object({ | ||
| 59 | field => 'children', | ||
| 60 | callbacks => | ||
| 61 | { | ||
| 62 | add => '_on_children_add', | ||
| 63 | remove => '_on_children_remove', | ||
| 64 | } | ||
| 65 | }, 'HTML::Object::Element', @_ ) ); } | ||
| 66 | |||
| 67 | sub forEach { return( shift->children->foreach( @_ ) ); } | ||
| 68 | |||
| 69 | sub getTrackById | ||
| 70 | { | ||
| 71 | my $self = shift( @_ ); | ||
| 72 | my $id = shift( @_ ); | ||
| 73 | return if( !defined( $id ) || !CORE::length( "$id" ) ); | ||
| 74 | foreach my $e ( @$self ) | ||
| 75 | { | ||
| 76 | if( Scalar::Util::blessed( $e ) && | ||
| 77 | $e->isa( 'HTML::Object::DOM::Element::Track' ) ) | ||
| 78 | { | ||
| 79 | my $e_id = $e->attr( 'id' ); | ||
| 80 | return( $e ) if( defined( $e_id ) && $id eq $e_id ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Note: property length is inherited | ||
| 87 | |||
| 88 | sub onaddtrack : lvalue { return( shift->on( 'addtrack', @_ ) ); } | ||
| 89 | |||
| 90 | sub onchange : lvalue { return( shift->on( 'change', @_ ) ); } | ||
| 91 | |||
| 92 | sub onremovetrack : lvalue { return( shift->on( 'removetrack', @_ ) ); } | ||
| 93 | |||
| 94 | # Note: property | ||
| 95 | sub selectedIndex : lvalue { return( shift->_set_get_number({ | ||
| 96 | field => 'selectedindex', | ||
| 97 | callbacks => | ||
| 98 | { | ||
| 99 | add => '_on_selectedindex_change', | ||
| 100 | remove => '_on_selectedindex_change', | ||
| 101 | } | ||
| 102 | }, @_ ) ); } | ||
| 103 | |||
| 104 | # Note: property selectedindex | ||
| 105 | sub selectedindex : lvalue { return( shift->selectedIndex( @_ ) ); } | ||
| 106 | |||
| 107 | sub _on_children_add { return( shift->_trigger_event_for( addtrack => 'HTML::Object::DOM::TrackEvent' ) ); } | ||
| 108 | |||
| 109 | sub _on_children_remove { return( shift->_trigger_event_for( removetrack => 'HTML::Object::DOM::TrackEvent' ) ); } | ||
| 110 | |||
| 111 | sub _on_selectedindex_change { return( shift->_trigger_event_for( change => 'HTML::Object::DOM::TrackEvent' ) ); } | ||
| 112 | |||
| 113 | # Get called by HTML::Object::DOM::VideoTrack->selected | ||
| 114 | sub _update_selected | ||
| 115 | { | ||
| 116 | my $self = shift( @_ ); | ||
| 117 | my $track = shift( @_ ); | ||
| 118 | my $pos = $self->children->index( $track ); | ||
| 119 | $self->selectedIndex( $pos ) if( defined( $pos ) ); | ||
| 120 | return( $self ); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | # NOTE: POD | ||
| 125 | __END__ | ||
| 126 | |||
| 127 | =encoding utf-8 | ||
| 128 | |||
| 129 | =head1 NAME | ||
| 130 | |||
| 131 | HTML::Object::DOM::VideoTrackList - HTML Object DOM VideoTrackList Class | ||
| 132 | |||
| 133 | =head1 SYNOPSIS | ||
| 134 | |||
| 135 | use HTML::Object::DOM::VideoTrackList; | ||
| 136 | my $list = HTML::Object::DOM::VideoTrackList->new || | ||
| 137 | die( HTML::Object::DOM::VideoTrackList->error, "\n" ); | ||
| 138 | |||
| 139 | =head1 VERSION | ||
| 140 | |||
| 141 | v0.2.0 | ||
| 142 | |||
| 143 | =head1 DESCRIPTION | ||
| 144 | |||
| 145 | The C<VideoTrackList> interface is used to represent a list of the video tracks contained within a <video> element, with each track represented by a separate C<VideoTrack> object in the list. | ||
| 146 | |||
| 147 | It inherits from L<HTML::Object::EventTarget>. | ||
| 148 | |||
| 149 | =head1 INHERITANCE | ||
| 150 | |||
| 151 | +-----------------------+ +---------------------------+ +-------------------------+ +-----------------------------------+ | ||
| 152 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::List | --> | HTML::Object::DOM::VideoTrackList | | ||
| 153 | +-----------------------+ +---------------------------+ +-------------------------+ +-----------------------------------+ | ||
| 154 | |||
| 155 | =head1 PROPERTIES | ||
| 156 | |||
| 157 | Inherits properties from its parent L<HTML::Object::DOM::List> | ||
| 158 | |||
| 159 | =head2 length | ||
| 160 | |||
| 161 | The number of tracks in the list. | ||
| 162 | |||
| 163 | Example: | ||
| 164 | |||
| 165 | my $videoElem = $doc->querySelector( 'video' ); | ||
| 166 | my $numVideoTracks = 0; | ||
| 167 | if( $videoElem->videoTracks ) | ||
| 168 | { | ||
| 169 | $numVideoTracks = $videoElem->videoTracks->length; | ||
| 170 | } | ||
| 171 | |||
| 172 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/length> | ||
| 173 | |||
| 174 | =head2 selectedIndex | ||
| 175 | |||
| 176 | Sets or gets a number. Under perl, this is not set automatically. It is up to you to set this to whatever number you see fit. | ||
| 177 | |||
| 178 | Under JavaScript, this is the index of the currently selected track, if any, or −1 otherwise. | ||
| 179 | |||
| 180 | It returns the number as an L<object|Module::Generic::Number> | ||
| 181 | |||
| 182 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/selectedIndex> | ||
| 183 | |||
| 184 | =head2 selectedindex | ||
| 185 | |||
| 186 | Alias for L</selectedIndex> | ||
| 187 | |||
| 188 | =head1 METHODS | ||
| 189 | |||
| 190 | Inherits methods from its parent L<HTML::Object::DOM::List> | ||
| 191 | |||
| 192 | =head2 addEventListener | ||
| 193 | |||
| 194 | Calls and returns the value from addEventListenerCalls in the ancestor class L<HTML::Object::DOM::List> | ||
| 195 | |||
| 196 | =head2 children | ||
| 197 | |||
| 198 | Returns an L<array object|Module::Generic::Array> of this element's children. | ||
| 199 | |||
| 200 | =head2 forEach | ||
| 201 | |||
| 202 | This is an alias for L<Module::Generic::Array/foreach> | ||
| 203 | |||
| 204 | =head2 getTrackById | ||
| 205 | |||
| 206 | Returns the C<VideoTrack> found within the C<VideoTrackList> whose id matches the specified string. If no match is found, C<undef> is returned. | ||
| 207 | |||
| 208 | Example: | ||
| 209 | |||
| 210 | my $theTrack = $VideoTrackList->getTrackById( $id ); | ||
| 211 | |||
| 212 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/getTrackById> | ||
| 213 | |||
| 214 | =head1 EVENTS | ||
| 215 | |||
| 216 | Events fired are of class L<HTML::Object::DOM::TrackEvent> | ||
| 217 | |||
| 218 | =head2 addtrack | ||
| 219 | |||
| 220 | Fired when a new video track has been added to the L<media element|HTML::Object::DOM::Element::Video>. Also available via the L</onaddtrack> property. | ||
| 221 | |||
| 222 | Example: | ||
| 223 | |||
| 224 | my $videoElement = $doc->querySelector('video'); | ||
| 225 | |||
| 226 | $videoElement->videoTracks->addEventListener( addtrack => sub | ||
| 227 | { | ||
| 228 | my $event = shift( @_ ); | ||
| 229 | say( "Video track: ", $event->track->label, " added" ); | ||
| 230 | }); | ||
| 231 | |||
| 232 | my $videoElement = $doc->querySelector('video'); | ||
| 233 | |||
| 234 | $videoElement->videoTracks->onaddtrack = sub | ||
| 235 | { | ||
| 236 | my $event = shift( @_ ); | ||
| 237 | say( "Video track: ", $event->track->label, " added" ); | ||
| 238 | }; | ||
| 239 | |||
| 240 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/addtrack_event> | ||
| 241 | |||
| 242 | =head2 change | ||
| 243 | |||
| 244 | Fired when a video track has been made active or inactive. Also available via the L</onchange> property. | ||
| 245 | |||
| 246 | Example: | ||
| 247 | |||
| 248 | my $videoElement = $doc->querySelector( 'video' ); | ||
| 249 | $videoElement->videoTracks->addEventListener( change => sub | ||
| 250 | { | ||
| 251 | say( "'", $event->type, "' event fired" ); | ||
| 252 | }); | ||
| 253 | |||
| 254 | # changing the value of 'selected' will trigger the 'change' event | ||
| 255 | my $toggleTrackButton = $doc->querySelector( '.toggle-$track' ); | ||
| 256 | $toggleTrackButton->addEventListener( click => sub | ||
| 257 | { | ||
| 258 | my $track = $videoElement->videoTracks->[0]; | ||
| 259 | $track->selected = !$track->selected; | ||
| 260 | }); | ||
| 261 | |||
| 262 | my $videoElement = $doc->querySelector( 'video' ); | ||
| 263 | $videoElement->videoTracks->onchange = sub | ||
| 264 | { | ||
| 265 | my $event = shift( @_ ); | ||
| 266 | say( "'", $event->type, "' event fired" ); | ||
| 267 | }; | ||
| 268 | |||
| 269 | # changing the value of 'selected' will trigger the 'change' event | ||
| 270 | my $toggleTrackButton = $doc->querySelector( '.toggle-$track' ); | ||
| 271 | $toggleTrackButton->addEventListener( click => sub | ||
| 272 | { | ||
| 273 | my $track = $videoElement->videoTracks->[0]; | ||
| 274 | $track->selected = !$track->selected; | ||
| 275 | }); | ||
| 276 | |||
| 277 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/change_event> | ||
| 278 | |||
| 279 | =head2 removetrack | ||
| 280 | |||
| 281 | Fired when a new video track has been removed from the media element. Also available via the L</onremovetrack> property. | ||
| 282 | |||
| 283 | Example: | ||
| 284 | |||
| 285 | my $videoElement = $doc->querySelector( 'video' ); | ||
| 286 | |||
| 287 | $videoElement->videoTracks->addEventListener( removetrack => sub | ||
| 288 | { | ||
| 289 | my $event = shift( @_ ); | ||
| 290 | say( "Video track: ", $event->track->label, " removed" ); | ||
| 291 | }); | ||
| 292 | |||
| 293 | my $videoElement = $doc->querySelector( 'video' ); | ||
| 294 | |||
| 295 | $videoElement->videoTracks->onremovetrack = sub | ||
| 296 | { | ||
| 297 | my $event = shift( @_ ); | ||
| 298 | say( "Video track: ", $event->track->label, " removed" ); | ||
| 299 | }; | ||
| 300 | |||
| 301 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/removetrack_event> | ||
| 302 | |||
| 303 | =head1 EVENT HANDLERS | ||
| 304 | |||
| 305 | =head2 onaddtrack | ||
| 306 | |||
| 307 | An event handler to be called when the L</addtrack> event is fired, indicating that a new video track has been added to the media element. | ||
| 308 | |||
| 309 | Example: | ||
| 310 | |||
| 311 | $doc->querySelector('video')->videoTracks->onaddtrack = sub | ||
| 312 | { | ||
| 313 | my $event = shift( @_ ); | ||
| 314 | addToTrackList( $event->track ); | ||
| 315 | }; | ||
| 316 | |||
| 317 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/onaddtrack> | ||
| 318 | |||
| 319 | =head2 onchange | ||
| 320 | |||
| 321 | An event handler to be called when the change event occurs - that is, when the value of the selected property for a track has changed, due to the track being made active or inactive. | ||
| 322 | |||
| 323 | Example: | ||
| 324 | |||
| 325 | my $trackList = $doc->querySelector( 'video' )->videoTracks; | ||
| 326 | $trackList->onchange = sub | ||
| 327 | { | ||
| 328 | my $event = shift( @_ ); | ||
| 329 | $trackList->forEach(sub | ||
| 330 | { | ||
| 331 | my $track = shift( @_ ); | ||
| 332 | updateTrackSelectedButton( $track->id, $track->selected ); | ||
| 333 | }); | ||
| 334 | }; | ||
| 335 | |||
| 336 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/onchange> | ||
| 337 | |||
| 338 | =head2 onremovetrack | ||
| 339 | |||
| 340 | An event handler to call when the removetrack event is sent, indicating that a video track has been removed from the media element. | ||
| 341 | |||
| 342 | Example: | ||
| 343 | |||
| 344 | $doc->querySelector( 'my-video' )->videoTracks->onremovetrack = sub | ||
| 345 | { | ||
| 346 | $myTrackCount = $doc->querySelector( 'my-video' )->videoTracks->length; | ||
| 347 | }; | ||
| 348 | |||
| 349 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList/onremovetrack> | ||
| 350 | |||
| 351 | =head1 AUTHOR | ||
| 352 | |||
| 353 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 354 | |||
| 355 | =head1 SEE ALSO | ||
| 356 | |||
| 357 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/VideoTrackList> | ||
| 358 | |||
| 359 | =head1 COPYRIGHT & LICENSE | ||
| 360 | |||
| 361 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 362 | |||
| 363 | All rights reserved | ||
| 364 | |||
| 365 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 366 | |||
| 367 | =cut |