sub set_attribute {
my $self = shift;
- my ($name, $value) = $self->_parse_attribute_args(@_);
+ my $attr = $self->_parse_attribute_args(@_);
sub {
my $a = (my $evt = $_[0])->{attrs};
- my $e = exists $a->{$name};
+ my @kadd = grep {!exists $a->{$_}} keys %$attr;
+{ %$evt, raw => undef, raw_attrs => undef,
- attrs => { %$a, $name => $value },
- ($e # add to name list if not present
- ? ()
- : (attr_names => [ @{$evt->{attr_names}}, $name ]))
+ attrs => { %$a, %$attr },
+ @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
}
};
}
sub _parse_attribute_args {
my $self = shift;
- # allow ->add_to_attribute(name => 'value')
- # or ->add_to_attribute({ name => 'name', value => 'value' })
- die "WARNING: Long form arg (name => 'class', value => 'x') is deprecated"
- if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
- my ($name, $value) = @_ > 1 ? @_ : @{$_[0]}{qw(name value)};
- return ($name, $self->_zconfig->parser->html_escape($value));
+ #die "Long form arg (name => 'class', value => 'x') is no longer supported"
+ #if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
+
+ my $opts = ref($_[0]) eq 'HASH' ? $_[0] : {$_[0] => $_[1]};
+ for (values %{$opts}) { $self->_zconfig->parser->html_escape($_); }
+ return $opts;
}
sub add_attribute {
sub add_class { shift->add_to_attribute('class',@_) }
-sub remove_class { shift->remove_attribute('class',@_) }
+sub remove_class { shift->remove_from_attribute('class',@_) }
sub set_class { shift->set_attribute('class',@_) }
sub add_to_attribute {
my $self = shift;
- my ($name, $value) = $self->_parse_attribute_args(@_);
+ my $attr = $self->_parse_attribute_args(@_);
+ sub {
+ my $a = (my $evt = $_[0])->{attrs};
+ my @kadd = grep {!exists $a->{$_}} keys %$attr;
+ +{ %$evt, raw => undef, raw_attrs => undef,
+ attrs => {
+ %$a,
+ map {$_ => join(' ', (exists $a->{$_} ? $a->{$_} : ()), $attr->{$_}) }
+ keys %$attr
+ },
+ @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
+ }
+ };
+}
+
+sub remove_from_attribute {
+ my $self = shift;
+ my $attr = $self->_parse_attribute_args(@_);
sub {
my $a = (my $evt = $_[0])->{attrs};
- my $e = exists $a->{$name};
+{ %$evt, raw => undef, raw_attrs => undef,
attrs => {
%$a,
- $name => join(' ', ($e ? $a->{$name} : ()), $value)
+ #TODO needs to support multiple removes
+ map { my $tar = $_; $_ => join ' ',
+ map {$attr->{$tar} ne $_} split ' ', $a->{$_} }
+ grep {exists $a->{$_}} keys %$attr
},
- ($e # add to name list if not present
- ? ()
- : (attr_names => [ @{$evt->{attr_names}}, $name ]))
}
};
}
$self->repeat($repeat_for, { %{$options||{}}, content => 1 })
}
+sub extract_names {
+ my ($self, $to) = @_;
+ sub {
+ my ($evt) = @_;
+ push @$to, $evt->{'attrs'}->{'name'};
+ $evt;
+ }
+};
+
+sub validate_form {
+ my ($self,$to) = @_;
+ $self->collect({
+ filter => sub {
+ return
+ $_->select('input')->validation_rules($to)
+ ->select('select')->validation_rules($to);
+ },
+ passthrough => 1,
+ });
+}
+
+sub fill_form {
+ my ($self,$val) = @_;
+ $self->collect({
+ filter => sub {
+ return
+ $_->select('input')->val($val)
+ #->select('select')->val($val)
+ ;
+ },
+ passthrough => 1,
+ });
+}
+
+sub validation_rules {
+ my ($self, $to) = @_;
+ sub {
+ my ($evt) = @_;
+ $to->{$evt->{'attrs'}->{'name'}}
+ = [split ' ', $evt->{'attrs'}->{'data-validate'}||""];
+ $evt;
+ }
+}
+
+sub val {
+ #if val is a hashref automatically match to name, otherwise fill as is.
+ my ($self, $val) = @_;
+ sub {
+ my ($evt) = @_;
+ my $attrs = $evt->{'attrs'};
+ my $nm = $attrs->{'name'};
+ my $tar = defined $val && ref $val eq 'HASH' ? $val->{$nm} : $val;
+ if(defined $tar) {
+ if($evt->{'name'} eq 'select') {
+ #if we are select do something more complicated
+ warn "Can't do selects yet";
+ } else {
+ $evt->{'raw'} = undef;
+ $evt->{'raw_attrs'} = undef;
+ push @{$evt->{'attr_names'}}, 'value' unless exists $attrs->{'value'};
+ $attrs->{'value'} = $tar;
+ #check if we are a checkbox
+ if(exists $attrs->{'type'} && $attrs->{'type'} eq 'checkbox') {
+ if($tar) {
+ push @{$evt->{'attr_names'}}, 'selected' unless exists $attrs->{'selected'};
+ $attrs->{'selected'} = $tar ? 'selected' : '';
+ } else {
+ delete $attrs->{'selected'};
+ $evt->{'attr_names'} = [ grep $_ ne 'selected', @{$evt->{'attr_names'}} ];
+ }
+ }
+ }
+ }
+ $evt;
+ }
+}
+
+
1;
=head1 NAME
->select('p')
->set_attribute(class=>'paragraph')
->select('div')
- ->set_attribute({name=>'class', value=>'divider'});
-
+ ->set_attribute({class=>'paragraph', name=>'divider'});
Overrides existing values, if such exist. When multiple L</set_attribute>
calls are made against the same or overlapping selection sets, the final
Adds a value to an existing attribute, or creates one if the attribute does not
yet exist. You may call this method with either an Array or HashRef of Args.
-Here's the 'long form' HashRef:
-
$html_zoom
->select('p')
- ->set_attribute(class=>'paragraph')
- ->then
- ->add_to_attribute({name=>'class', value=>'divider'});
-
-And the exact same effect using the 'short form' Array:
-
- $html_zoom
- ->select('p')
- ->set_attribute(class=>'paragraph')
+ ->set_attribute({class => 'paragraph', name => 'test'})
->then
->add_to_attribute(class=>'divider');
->then
->remove_attribute('class');
+=head2 remove_from_attribute
+
+Removes a value from existing attribute
+
+ $html_zoom
+ ->select('p')
+ ->set_attribute(class=>'paragraph lead')
+ ->then
+ ->remove_from_attribute('class' => 'lead');
+
Removes attributes from the original stream or events already added.
+=head2 add_class
+
+Add to a class attribute
+
+=head2 remove_class
+
+Remove from a class attribute
+
=head2 transform_attribute
Transforms (or creates or deletes) an attribute by running the passed