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