container support built into fields and an example of usage in ComponentUI
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action / Role / Close.pm
1 package Reaction::UI::ViewPort::Action::Role::Close;
2
3 use Reaction::Role;
4 use MooseX::Types::Moose qw/Str CodeRef/;
5 with 'Reaction::UI::ViewPort::Action::Role::Apply';
6
7 has close_label => (is => 'rw', isa => Str, lazy_build => 1);
8 has on_close_callback => (is => 'rw', isa => CodeRef);
9 has close_label_close => (is => 'rw', isa => Str, lazy_build => 1);
10 has close_label_cancel => (is => 'rw', isa => Str, lazy_build => 1);
11
12 sub _build_close_label { shift->_build_close_label_close }
13 sub _build_close_label_close { 'close' }
14 sub _build_close_label_cancel { 'cancel' }
15
16 sub can_close { 1 }
17
18 sub close {
19   my $self = shift;
20   return unless $self->has_on_close_callback;
21   $self->on_close_callback->($self);
22 }
23
24 around apply => sub {
25   my $orig = shift;
26   my $self = shift;
27   my $success = $self->$orig(@_);
28   $self->close_label( $self->close_label_cancel ) unless $success;
29   return $success;
30 };
31
32 # can't do a close-type operation if there's nowhere to go afterwards
33 around accept_events => sub {
34   my $orig = shift;
35   my $self = shift;
36   ( ($self->has_on_close_callback ? ('close') : ()), $self->$orig(@_) );
37 };
38
39 1;
40
41 __END__
42
43 =head1 NAME
44
45 Reaction::UI::ViewPort::Action::Role::Close
46
47 =head1 ATTRIBUTES
48
49 =head2 close_label
50
51 Default: C<close_label_close>
52
53 =head2 close_label_close
54
55 Default: 'close'
56
57 =head2 close_label_cancel
58
59 This label is only shown when C<changed> is true.
60
61 Default: 'cancel'
62
63 =head2 on_close_callback
64
65 CodeRef.
66
67 =head1 METHODS
68
69 =head2 close
70
71 =head2 can_close
72
73 =head1 SEE ALSO
74
75 L<Reaction::UI::ViewPort::Action::Role::Apply>
76
77 L<Reaction::UI::ViewPort::Action::Role::OK>
78
79 =head1 AUTHORS
80
81 See L<Reaction::Class> for authors.
82
83 =head1 LICENSE
84
85 See L<Reaction::Class> for the license.
86
87 =cut