1 package Reaction::UI::ViewPort::Field::Mutable::ChooseMany;
6 return [] unless defined($_[0]);
7 return ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]];
10 class ChooseMany is 'Reaction::UI::ViewPort::Field', which {
12 does 'Reaction::UI::ViewPort::Field::Role::Mutable';
13 does 'Reaction::UI::ViewPort::Field::Role::Choices';
15 #MUST BE HERE, BELOW THE 'does', OR THE TRIGGER WILL NOT HAPPEN!
16 has '+value' => (isa => 'ArrayRef');
21 return $orig->($self) unless @_;
22 my $value = $listify->(shift);
23 $_ = $self->str_to_ident($_) for @$value;
24 my $checked = $self->attribute->check_valid_value($self->model, $value);
25 # i.e. fail if any of the values fail
26 confess "Not a valid set of values"
27 if (@$checked < @$value || grep { !defined($_) } @$checked);
28 $orig->($self, $checked);
32 around _value_string_from_value => sub {
35 join(", ", (map {$self->obj_to_name($_->{value}) } @{ $self->current_value_choices }));
38 implements is_current_value => as {
39 my ($self, $check_value) = @_;
40 return unless $self->_model_has_value;
41 my @our_values = @{$self->value || []};
42 $check_value = $self->obj_to_str($check_value) if ref($check_value);
43 return grep { $self->obj_to_str($_) eq $check_value } @our_values;
46 implements current_value_choices => as {
48 my @all = grep { $self->is_current_value($_->{value}) } @{$self->value_choices};
52 implements available_value_choices => as {
54 my @all = grep { !$self->is_current_value($_->{value}) } @{$self->value_choices};
58 around handle_events => sub {
60 my ($self, $events) = @_;
61 $events->{value} = [] if $events->{no_current_value};
62 my $ev_value = $listify->($events->{value});
63 if (delete $events->{add_all_values}) {
64 $events->{value} = [map {$self->obj_to_str($_)} @{$self->valid_values}];
65 } elsif (exists $events->{add_values} && delete $events->{do_add_values}) {
66 my $add = $listify->(delete $events->{add_values});
67 $events->{value} = [ @{$ev_value}, @$add ];
68 } elsif (delete $events->{remove_all_values}) {
69 $events->{value} = [];
70 }elsif (exists $events->{remove_values} && delete $events->{do_remove_values}) {
71 my $remove = $listify->(delete $events->{remove_values});
72 my %r = map { ($_ => 1) } @$remove;
73 $events->{value} = [ grep { !$r{$_} } @{$ev_value} ];
85 Reaction::UI::ViewPort::Field::ChooseMany
91 =head2 is_current_value
95 =head2 available_values
97 =head2 available_value_names
101 =head2 L<Reaction::UI::ViewPort::Field>
105 See L<Reaction::Class> for authors.
109 See L<Reaction::Class> for the license.