It is starting to look like this may actually work after all. Listview is the only...
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / ChooseMany.pm
1 package Reaction::UI::ViewPort::Field::ChooseMany;
2
3 use Reaction::Class;
4
5 class ChooseMany is 'Reaction::UI::ViewPort::Field::ChooseOne', which {
6
7   #has '+layout' => (default => 'dual_select_group');
8   has '+value' => (isa => 'ArrayRef');
9
10   my $listify = sub {                  # quick utility function, $listify->($arg)
11     return (defined($_[0])
12              ? (ref($_[0]) eq 'ARRAY'
13                  ? $_[0]               # \@arr => \@arr
14                  : [$_[0]])            # $scalar => [$scalar]
15              : []);                    # undef => []
16   };
17
18   around value => sub {
19     my $orig = shift;
20     my $self = shift;
21     if (@_) {
22       my $value = $listify->(shift);
23       if (defined $value) {
24         $_ = $self->str_to_ident($_) for @$value;
25         my $checked = $self->attribute->check_valid_value($self->action, $value);
26         # i.e. fail if any of the values fail
27         confess "Not a valid set of values"
28           if (@$checked < @$value || grep { !defined($_) } @$checked);
29
30         $value = $checked;
31       }
32       $orig->($self, $value);
33     } else {
34       $orig->($self);
35     }
36   };
37
38   override build_value => sub {
39     return super() || [];
40   };
41
42   implements is_current_value => as {
43     my ($self, $check_value) = @_;
44     my @our_values = @{$self->value||[]};
45     $check_value = $self->obj_to_str($check_value) if ref($check_value);
46     return grep { $self->obj_to_str($_) eq $check_value } @our_values;
47   };
48
49   implements current_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   implements available_value_choices => as {
56     my $self = shift;
57     my @all = grep { !$self->is_current_value($_->{value}) } @{$self->value_choices};
58     return [ @all ];
59   };
60
61   around handle_events => sub {
62     my $orig = shift;
63     my ($self, $events) = @_;
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     }
77     return $orig->(@_);
78   };
79
80 };
81
82 1;
83
84 =head1 NAME
85
86 Reaction::UI::ViewPort::Field::ChooseMany
87
88 =head1 DESCRIPTION
89
90 =head1 METHODS
91
92 =head2 is_current_value
93
94 =head2 current_values
95
96 =head2 available_values
97
98 =head2 available_value_names
99
100 =head1 SEE ALSO
101
102 =head2 L<Reaction::UI::ViewPort::Field>
103
104 =head1 AUTHORS
105
106 See L<Reaction::Class> for authors.
107
108 =head1 LICENSE
109
110 See L<Reaction::Class> for the license.
111
112 =cut