fixed _build_value_string for ChooseOne fields
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Mutable / ChooseMany.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Mutable::ChooseMany;
2
3use Reaction::Class;
4
5my $listify = sub{
1734a92a 6 return [] unless defined($_[0]);
ddccc6a2 7 return ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]];
8};
9
10class ChooseMany is 'Reaction::UI::ViewPort::Field', which {
11
ddccc6a2 12 does 'Reaction::UI::ViewPort::Field::Role::Mutable';
13 does 'Reaction::UI::ViewPort::Field::Role::Choices';
14
cc44a337 15 #MUST BE HERE, BELOW THE 'does', OR THE TRIGGER WILL NOT HAPPEN!
16 has '+value' => (isa => 'ArrayRef');
36d54b14 17
ddccc6a2 18 around value => sub {
19 my $orig = shift;
20 my $self = shift;
21 return $orig->($self) unless @_;
22 my $value = $listify->(shift);
23 $_ = $self->str_to_ident($_) for @$value;
36d54b14 24 my $checked = $self->attribute->check_valid_value($self->model, $value);
ddccc6a2 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);
29 };
30
31 #XXX go away!
32 override _build_value => sub {
33 return super() || [];
34 };
35
36d54b14 36 implements _build_value_string => as {
cc44a337 37 my $self = shift;
38 join ", ", (map {$self->obj_to_name($_->{value}) } @{ $self->current_value_choices })
36d54b14 39 };
40
ddccc6a2 41 implements is_current_value => as {
42 my ($self, $check_value) = @_;
cc44a337 43 my @our_values = @{$self->value || []};
ddccc6a2 44 $check_value = $self->obj_to_str($check_value) if ref($check_value);
45 return grep { $self->obj_to_str($_) eq $check_value } @our_values;
46 };
47
48 implements current_value_choices => as {
49 my $self = shift;
50 my @all = grep { $self->is_current_value($_->{value}) } @{$self->value_choices};
51 return [ @all ];
52 };
53
54 implements available_value_choices => as {
55 my $self = shift;
56 my @all = grep { !$self->is_current_value($_->{value}) } @{$self->value_choices};
57 return [ @all ];
58 };
59
60 around handle_events => sub {
61 my $orig = shift;
62 my ($self, $events) = @_;
1734a92a 63 $events->{value} = [] if $events->{no_current_value};
ddccc6a2 64 my $ev_value = $listify->($events->{value});
65 if (delete $events->{add_all_values}) {
66 $events->{value} = [map {$self->obj_to_str($_)} @{$self->valid_values}];
67 } elsif (exists $events->{add_values} && delete $events->{do_add_values}) {
68 my $add = $listify->(delete $events->{add_values});
69 $events->{value} = [ @{$ev_value}, @$add ];
70 } elsif (delete $events->{remove_all_values}) {
71 $events->{value} = [];
72 }elsif (exists $events->{remove_values} && delete $events->{do_remove_values}) {
73 my $remove = $listify->(delete $events->{remove_values});
74 my %r = map { ($_ => 1) } @$remove;
75 $events->{value} = [ grep { !$r{$_} } @{$ev_value} ];
76 }
1734a92a 77
ddccc6a2 78 return $orig->(@_);
79 };
80
81};
82
831;
84
85=head1 NAME
86
87Reaction::UI::ViewPort::Field::ChooseMany
88
89=head1 DESCRIPTION
90
91=head1 METHODS
92
93=head2 is_current_value
94
95=head2 current_values
96
97=head2 available_values
98
99=head2 available_value_names
100
101=head1 SEE ALSO
102
103=head2 L<Reaction::UI::ViewPort::Field>
104
105=head1 AUTHORS
106
107See L<Reaction::Class> for authors.
108
109=head1 LICENSE
110
111See L<Reaction::Class> for the license.
112
113=cut