work in progress, listview still broken
[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   has '+value' => (isa => 'ArrayRef');
13
14   does 'Reaction::UI::ViewPort::Field::Role::Mutable';
15   does 'Reaction::UI::ViewPort::Field::Role::Choices';
16
17   around value => sub {
18     my $orig = shift;
19     my $self = shift;
20     return $orig->($self) unless @_;
21     my $value = $listify->(shift);
22     $_ = $self->str_to_ident($_) for @$value;
23     my $checked = $self->attribute->check_valid_value($self->action, $value);
24     # i.e. fail if any of the values fail
25     confess "Not a valid set of values"
26       if (@$checked < @$value || grep { !defined($_) } @$checked);
27     $orig->($self, $checked);
28   };
29
30   #XXX go away!
31   override _build_value => sub {
32     return super() || [];
33   };
34
35   implements is_current_value => as {
36     my ($self, $check_value) = @_;
37     my @our_values = @{$self->value||[]};
38     $check_value = $self->obj_to_str($check_value) if ref($check_value);
39     return grep { $self->obj_to_str($_) eq $check_value } @our_values;
40   };
41
42   implements current_value_choices => as {
43     my $self = shift;
44     my @all = grep { $self->is_current_value($_->{value}) } @{$self->value_choices};
45     return [ @all ];
46   };
47
48   implements available_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   around handle_events => sub {
55     my $orig = shift;
56     my ($self, $events) = @_;
57     my $ev_value = $listify->($events->{value});
58     if (delete $events->{add_all_values}) {
59       $events->{value} = [map {$self->obj_to_str($_)} @{$self->valid_values}];
60     } elsif (exists $events->{add_values} && delete $events->{do_add_values}) {
61       my $add = $listify->(delete $events->{add_values});
62       $events->{value} = [ @{$ev_value}, @$add ];
63     } elsif (delete $events->{remove_all_values}) {
64       $events->{value} = [];
65     }elsif (exists $events->{remove_values} && delete $events->{do_remove_values}) {
66       my $remove = $listify->(delete $events->{remove_values});
67       my %r = map { ($_ => 1) } @$remove;
68       $events->{value} = [ grep { !$r{$_} } @{$ev_value} ];
69     }
70     return $orig->(@_);
71   };
72
73 };
74
75 1;
76
77 =head1 NAME
78
79 Reaction::UI::ViewPort::Field::ChooseMany
80
81 =head1 DESCRIPTION
82
83 =head1 METHODS
84
85 =head2 is_current_value
86
87 =head2 current_values
88
89 =head2 available_values
90
91 =head2 available_value_names
92
93 =head1 SEE ALSO
94
95 =head2 L<Reaction::UI::ViewPort::Field>
96
97 =head1 AUTHORS
98
99 See L<Reaction::Class> for authors.
100
101 =head1 LICENSE
102
103 See L<Reaction::Class> for the license.
104
105 =cut