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