File Coverage

lib/HTML/Object/DOM/VideoTrackList.pm
Criterion Covered Total %
statement 19 52 36.5
branch 0 14 0.0
condition 0 9 0.0
subroutine 7 23 30.4
pod 10 10 100.0
total 36 108 33.3


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