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