| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/List.pm | ||
| 3 | ## Version v0.3.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/28 | ||
| 7 | ## Modified 2025/10/16 | ||
| 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::List; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::EventTarget ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::Event; | ||
| 23 | use Wanted; | ||
| 24 | our $VERSION = 'v0.3.0'; | ||
| 25 | }; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | sub init | ||
| 31 | { | ||
| 32 | my $self = shift( @_ ); | ||
| 33 | $self->{children} = []; | ||
| 34 | $self->{_init_strict_use_sub} = 1; | ||
| 35 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 36 | return( $self ); | ||
| 37 | } | ||
| 38 | |||
| 39 | # This method is called from the package inheriting from us | ||
| 40 | # $def is a dictionary hash reference providing specifics in a crisp way about the types of events and related properties concerned | ||
| 41 | # Example: | ||
| 42 | # { | ||
| 43 | # addtrack => { | ||
| 44 | # add => { property => 'children', type => 'add', event => 'addtrack' }, | ||
| 45 | # }, | ||
| 46 | # change => { | ||
| 47 | # add => { property => 'selected', type => 'add', event => 'change' }, | ||
| 48 | # remove => { property => 'selected', type => 'remove', event => 'change' }, | ||
| 49 | # }, | ||
| 50 | # removetrack => { | ||
| 51 | # remove => { property => 'children', type => 'remove', event => 'removetrack' }, | ||
| 52 | # }, | ||
| 53 | # } | ||
| 54 | sub addEventListener | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | return( $self->error( "I was expecting 3 arguments, but got only ", scalar( @_ ), "." ) ) if( scalar( @_ ) < 3 ); | ||
| 58 | my $def = shift( @_ ); | ||
| 59 | my $type = shift( @_ ); | ||
| 60 | my $code = shift( @_ ); | ||
| 61 | return( $self->error( "Dictionary provided is not an hash reference." ) ) if( !defined( $def ) || ref( $def ) ne 'HASH' ); | ||
| 62 | return( $self->error( "Event type provided contains illegal characters." ) ) if( !defined( $type ) || $type !~ /^\w+#/ ); | ||
| 63 | return( $self->error( "Callback is not a code reference." ) ) if( !defined( $code ) || !length( "$code" ) || ref( $code ) ne 'CODE' ); | ||
| 64 | # Before we enable the event listener we must make sure we are listening on events on relevant array or scalar | ||
| 65 | if( CORE::exists( $def->{ $type } ) ) | ||
| 66 | { | ||
| 67 | my $ref = $def->{ $type }; | ||
| 68 | # add or remove | ||
| 69 | OP: foreach my $op ( keys( %$ref ) ) | ||
| 70 | { | ||
| 71 | my $this = $ref->{ $op }; | ||
| 72 | for( qw( property type ) ) | ||
| 73 | { | ||
| 74 | if( !CORE::exists( $this->{ $_ } ) || !defined( $this->{ $_ } ) || !CORE::length( $this->{ $_ } ) ) | ||
| 75 | { | ||
| 76 | warn( "Dictionary property \"$_\" is missing or empty.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 77 | next OP; | ||
| 78 | } | ||
| 79 | elsif( $_ ne 'add' && $_ ne 'remove' ) | ||
| 80 | { | ||
| 81 | warnings::warn( "Unknown data listener type \"$_\"\n" ); | ||
| 82 | next OP; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | my $subref = $self->can( $this->{property} ); | ||
| 87 | if( !defined( $subref ) ) | ||
| 88 | { | ||
| 89 | warn( "This object class \"" . ( ref( $self ) || $self ) . "\" does not support method \"" . $this->{property} . "\".\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 90 | next; | ||
| 91 | } | ||
| 92 | my $data = $subref->( $self ); | ||
| 93 | if( !$self->_is_object( $data ) || !$data->can( 'callback' ) ) | ||
| 94 | { | ||
| 95 | warn( "Object from class \"" . ( ref( $data ) || $data ) . "\" does not have a \"callback\" method.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 96 | next; | ||
| 97 | } | ||
| 98 | my $cb = $data->callback( $this->{type} ); | ||
| 99 | # Callback already exists | ||
| 100 | next if( defined( $cb ) && ref( $cb ) ); | ||
| 101 | $data->callback( $this->{type} => sub | ||
| 102 | { | ||
| 103 | my $hash = shift( @_ ); | ||
| 104 | my $event = $self->_make_event( $type ); | ||
| 105 | if( CORE::exists( $this->{callback} ) && ref( $this->{callback} ) eq 'CODE' ) | ||
| 106 | { | ||
| 107 | $this->{callback}->( $self, { event => $event, added => $hash->{added}, removed => $hash->{removed}, type => $hash->{type} }); | ||
| 108 | } | ||
| 109 | $self->dispatchEvent( $event ); | ||
| 110 | }); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | return( $self->SUPER::addEventListener( $type => $code ) ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub forEach { return( shift->children->foreach( @_ ) ); } | ||
| 117 | |||
| 118 | sub length { return( shift->children->length ); } | ||
| 119 | |||
| 120 | sub push { return( shift->children->push( @_ ) ); } | ||
| 121 | |||
| 122 | sub _make_event | ||
| 123 | { | ||
| 124 | my $self = shift( @_ ); | ||
| 125 | my $type = shift( @_ ); | ||
| 126 | my $event = HTML::Object::Event->new( $type, | ||
| 127 | bubbles => 0, | ||
| 128 | cancellable => 0, | ||
| 129 | target => $self, | ||
| 130 | ) || return( $self->pass_error ); | ||
| 131 | return( $event ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub AUTOLOAD | ||
| 135 | { | ||
| 136 | my( $name ) = our $AUTOLOAD =~ /([^:]+)$/; | ||
| 137 | my $self = shift( @_ ); | ||
| 138 | my $children = $self->children; | ||
| 139 | die( "No method \"\$name\" in class \"", ( ref( $self ) || $self ), "\".\n" ) if( !$children ); | ||
| 140 | my $code = $children->can( $name ); | ||
| 141 | die( "No method \"\$name\" in class \"", ( ref( $children ) || $children ), "\".\n" ) if( !$code ); | ||
| 142 | eval( "sub $name { return( shift->children->$name( \@_ ) ); }\n\n" ); | ||
| 143 | return( $code->( $children, @_ ) ); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | # NOTE: POD | ||
| 148 | __END__ |