X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FField%2FMutable%2FChooseMany.pm;h=1b7a4113501313650ecf914d3990c919ddea3009;hb=7b5e71adcc00ae151a7908c2cddcd7323408efc7;hp=e6702f7e308d9ca9f0d56ce5190a256719e58c90;hpb=656d19e97472bf2bb06bf13e1eefa6ecd3b70b2b;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseMany.pm b/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseMany.pm index e6702f7..1b7a411 100644 --- a/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseMany.pm +++ b/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseMany.pm @@ -7,84 +7,82 @@ my $listify = sub{ return ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]]; }; -class ChooseMany is 'Reaction::UI::ViewPort::Field', which { - - does 'Reaction::UI::ViewPort::Field::Role::Mutable'; - does 'Reaction::UI::ViewPort::Field::Role::Choices'; - - #MUST BE HERE, BELOW THE 'does', OR THE TRIGGER WILL NOT HAPPEN! - has '+value' => (isa => 'ArrayRef'); - - around value => sub { - my $orig = shift; - my $self = shift; - return $orig->($self) unless @_; - my $value = $listify->(shift); - $_ = $self->str_to_ident($_) for @$value; - my $checked = $self->attribute->check_valid_value($self->model, $value); - # i.e. fail if any of the values fail - confess "Not a valid set of values" - if (@$checked < @$value || grep { !defined($_) } @$checked); - $orig->($self, $checked); - }; - - #XXX go away! - override _build_value => sub { - return super() || []; - }; - - around _value_string_from_value => sub { - my $self = shift; - join ", ", (map {$self->obj_to_name($_->{value}) } @{ $self->current_value_choices }) - }; - - implements is_current_value => as { - my ($self, $check_value) = @_; - my @our_values = @{$self->value || []}; - $check_value = $self->obj_to_str($check_value) if ref($check_value); - return grep { $self->obj_to_str($_) eq $check_value } @our_values; - }; - - implements current_value_choices => as { - my $self = shift; - my @all = grep { $self->is_current_value($_->{value}) } @{$self->value_choices}; - return [ @all ]; - }; - - implements available_value_choices => as { - my $self = shift; - my @all = grep { !$self->is_current_value($_->{value}) } @{$self->value_choices}; - return [ @all ]; - }; - - around handle_events => sub { - my $orig = shift; - my ($self, $events) = @_; - $events->{value} = [] if $events->{no_current_value}; - my $ev_value = $listify->($events->{value}); - if (delete $events->{add_all_values}) { - $events->{value} = [map {$self->obj_to_str($_)} @{$self->valid_values}]; - } elsif (exists $events->{add_values} && delete $events->{do_add_values}) { - my $add = $listify->(delete $events->{add_values}); - $events->{value} = [ @{$ev_value}, @$add ]; - } elsif (delete $events->{remove_all_values}) { - $events->{value} = []; - }elsif (exists $events->{remove_values} && delete $events->{do_remove_values}) { - my $remove = $listify->(delete $events->{remove_values}); - my %r = map { ($_ => 1) } @$remove; - $events->{value} = [ grep { !$r{$_} } @{$ev_value} ]; - } - - return $orig->(@_); - }; +use namespace::clean -except => [ qw(meta) ]; +use MooseX::Types::Moose qw/ArrayRef/; +extends 'Reaction::UI::ViewPort::Field'; + +with 'Reaction::UI::ViewPort::Field::Role::Mutable'; +with 'Reaction::UI::ViewPort::Field::Role::Choices'; + +#MUST BE HERE, BELOW THE 'does', OR THE TRIGGER WILL NOT HAPPEN! +has '+value' => (isa => ArrayRef); + +around value => sub { + my $orig = shift; + my $self = shift; + return $orig->($self) unless @_; + my $value = $listify->(shift); + $_ = $self->str_to_ident($_) for @$value; + my $checked = $self->attribute->check_valid_value($self->model, $value); + # i.e. fail if any of the values fail + confess "Not a valid set of values" + if (@$checked < @$value || grep { !defined($_) } @$checked); + $orig->($self, $checked); +}; + +around _value_string_from_value => sub { + my $orig = shift; + my $self = shift; + join(", ", (map {$self->obj_to_name($_->{value}) } @{ $self->current_value_choices })); +}; +sub is_current_value { + my ($self, $check_value) = @_; + return unless $self->_model_has_value; + my @our_values = @{$self->value || []}; + $check_value = $self->obj_to_str($check_value) if ref($check_value); + return grep { $self->obj_to_str($_) eq $check_value } @our_values; +}; +sub current_value_choices { + my $self = shift; + my @all = grep { $self->is_current_value($_->{value}) } @{$self->value_choices}; + return [ @all ]; +}; +sub available_value_choices { + my $self = shift; + my @all = grep { !$self->is_current_value($_->{value}) } @{$self->value_choices}; + return [ @all ]; }; +around handle_events => sub { + my $orig = shift; + my ($self, $events) = @_; + $events->{value} = [] if $events->{no_current_value}; + my $ev_value = $listify->($events->{value}); + if (delete $events->{add_all_values}) { + $events->{value} = [map {$self->obj_to_str($_)} @{$self->valid_values}]; + } elsif (exists $events->{add_values} && delete $events->{do_add_values}) { + my $add = $listify->(delete $events->{add_values}); + $events->{value} = [ @{$ev_value}, @$add ]; + } elsif (delete $events->{remove_all_values}) { + $events->{value} = []; + }elsif (exists $events->{remove_values} && delete $events->{do_remove_values}) { + my $remove = $listify->(delete $events->{remove_values}); + my %r = map { ($_ => 1) } @$remove; + $events->{value} = [ grep { !$r{$_} } @{$ev_value} ]; + } + + return $orig->(@_); +}; + +__PACKAGE__->meta->make_immutable; + + 1; =head1 NAME -Reaction::UI::ViewPort::Field::ChooseMany +Reaction::UI::ViewPort::Field::Mutable::ChooseMany =head1 DESCRIPTION