File Coverage

lib/HTML/Object/EventListener.pm
Criterion Covered Total %
statement 41 42 97.6
branch 2 4 50.0
condition 4 8 50.0
subroutine 14 15 93.3
pod 8 8 100.0
total 69 77 89.6


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## HTML Object - ~/lib/HTML/Object/EventListener.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2021/12/11
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::EventListener;
15             BEGIN
16             {
17 29     29   278 use strict;
  29         124  
  29         1005  
18 29     29   375 use warnings;
  29         135  
  29         1053  
19 29     29   258 use parent qw( Module::Generic );
  29         94  
  29         303  
20 29     29   2045 use vars qw( $VERSION );
  29         92  
  29         1558  
21 29     29   807 our $VERSION = 'v0.2.0';
22             };
23              
24 29     29   197 use strict;
  29         115  
  29         894  
25 29     29   224 use warnings;
  29         124  
  29         14356  
26              
27             sub init
28             {
29 5     5 1 417 my $self = shift( @_ );
30 5         136 $self->{element} = undef;
31 5         17 $self->{type} = undef;
32 5         12 $self->{code} = undef;
33 5         16 $self->{options} = {};
34 5         15 $self->{_init_strict_use_sub} = 1;
35 5 50       38 $self->SUPER::init( @_ ) || return( $self->pass_error );
36 5 50       238 return( $self->error( "No callback has been set for this event listener." ) ) if( !$self->{code} );
37 5         15 return( $self );
38             }
39              
40 6     6 1 25 sub capture { return( shift->options->{capture} ); }
41              
42 14     14 1 5073 sub code { return( shift->_set_get_code( 'code', @_ ) ); }
43              
44 7     7 1 7990 sub element { return( shift->_set_get_object_without_init( 'element', 'HTML::Object::Element', @_ ) ); }
45              
46 0     0 1 0 sub handleEvent { return( shift->code( @_ ) ); }
47              
48 13     13 1 4549 sub options { return( shift->_set_get_hash_as_mix_object( 'options', @_ ) ); }
49              
50             sub remove
51             {
52 2     2 1 1241 my $self = shift( @_ );
53 2   50     8 my $elem = $self->element ||
54             return( $self->error({
55             message => "No element object found in our event listener!",
56             class => 'HTML::Object::SyntaxError',
57             }) );
58 2   50     56 my $type = $self->type ||
59             return( $self->error({
60             message => "No event type found in our event listener!",
61             class => 'HTML::Object::SyntaxError',
62             }) );
63 2   50     1852 my $code = $self->code ||
64             return( $self->error({
65             message => "No event callabck found in our event listener!",
66             class => 'HTML::Object::SyntaxError',
67             }) );
68 2         1750 my $opts = $self->options;
69 2   50     1672 $opts->{capture} //= 0;
70 2         51 $elem->removeEventListener( $type, $code, { capture => $opts->{capture} });
71 2         7 return( $self );
72             }
73              
74 7     7 1 905 sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
75              
76             1;
77             # NOTE: POD
78             __END__
79              
80             =encoding utf-8
81              
82             =head1 NAME
83              
84             HTML::Object::EventListener - HTML Object Event Listener Class
85              
86             =head1 SYNOPSIS
87              
88             use HTML::Object::EventListener;
89             my $handler = HTML::Object::EventListener->new ||
90             die( HTML::Object::EventListener->error, "\n" );
91              
92             =head1 VERSION
93              
94             v0.2.0
95              
96             =head1 DESCRIPTION
97              
98             This module implements an L<HTML::Object> event listener. It is instantiated by the L<addEventListener|HTML::Object::EventTarget/addEventListener> method
99              
100             =head1 CONSTRUCTOR
101              
102             =head2 new
103              
104             Provided with an hash or hash reference of options and this will instantiate a new even listener and return the object.
105              
106             It takes the same options as the methods listed below.
107              
108             =head1 METHODS
109              
110             =head2 capture
111              
112             Sets or get the C<capture> boolean value for this event listener.
113              
114             =head2 code
115              
116             Same as L</handleEvent>
117              
118             =head2 element
119              
120             Sets or gets the L<HTML::Object::Element> object to which is attached this event listener.
121              
122             =head2 handleEvent
123              
124             Set or get an anonymous subroutine or a reference to a subroutine that is called whenever an event of the specified type occurs.
125              
126             =head2 options
127              
128             Set or get an hash reference of options for this event handler. Those options are the same as the ones passed to L<HTML::Object::EventTarget/addEventListener>
129              
130             =head2 remove
131              
132             A convenient alternative method to L<HTML::Object::EventTarget/removeEventListener>. It takes no parameter and calls L<HTML::Object::EventTarget/removeEventListener> with all the necessary parameters, hassle free.
133              
134             It returns the current event listener object upon success, and C<undef> upon error and then set an L<error|Module::Generic/error>
135              
136             =head2 type
137              
138             Set or get the type of event this object is for.
139              
140             =head1 AUTHOR
141              
142             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
143              
144             =head1 SEE ALSO
145              
146             L<HTML::Object::EventTarget>, L<HTML::Object::Event>, L<HTML::Object::EventListener>
147              
148             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/Events/Event_handlers>
149              
150             =head1 COPYRIGHT & LICENSE
151              
152             Copyright(c) 2021 DEGUEST Pte. Ltd.
153              
154             All rights reserved
155              
156             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
157              
158             =cut