1package Reaction::UI::ViewPort::Action::Role::Close;
2
3use Reaction::Role;
4use MooseX::Types::Moose qw/Str CodeRef/;
5with 'Reaction::UI::ViewPort::Action::Role::Apply';
6
7has close_label => (is => 'rw', isa => Str, lazy_build => 1);
8has on_close_callback => (is => 'rw', isa => CodeRef);
9has close_label_close => (is => 'rw', isa => Str, lazy_build => 1);
10has close_label_cancel => (is => 'rw', isa => Str, lazy_build => 1);
11
12sub _build_close_label { shift->_build_close_label_close }
13sub _build_close_label_close { 'close' }
14sub _build_close_label_cancel { 'cancel' }
15
16sub can_close { 1 }
17
18sub close {
19  my $self = shift;
20  return unless $self->has_on_close_callback;
21  $self->on_close_callback->($self);
22}
23
24around 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
33around accept_events => sub {
34  my $orig = shift;
35  my $self = shift;
36  ( ($self->has_on_close_callback ? ('close') : ()), $self->$orig(@_) );
37};
38
391;
40
41__END__
42
43=head1 NAME
44
45Reaction::UI::ViewPort::Action::Role::Close - Integrate Close and Apply events into ViewPort
46
47=head1 SYNOPSIS
48
49  package MyApp::UI::ViewPort::SomeAction;
50  use Reaction::Class;
51
52  use namespace::clean -except => 'meta';
53
54  extends 'Reaction::UI::ViewPort::Object::Mutable';
55  with    'Reaction::UI::ViewPort::Action::Role::Close';
56
57  ...
58  1;
59
60=head1 DESCRIPTION
61
62This role integrates a C<close> event and inherits an
63L<apply|Reaction::UI::ViewPort::Action::Role::Close/apply>
64event into the consuming viewport.
65
66=head1 ATTRIBUTES
67
68=head2 close_label
69
70Defaults to returned string value of L</_build_close_label> (C<close>).
71
72=head2 close_label_close
73
74Defaults to returned string value of L</_build_close_label_close> (C<close>).
75
76=head2 close_label_cancel
77
78This label is only shown when C<changed> is true. It is initialised
79with the returned string value of L</_build_close_label_cancel>.
80
81Default: 'cancel'
82
83=head2 on_close_callback
84
85CodeRef. If set will be called on L</close>.
86
87=head1 METHODS
88
89=head2 close
90
91Calls L</on_close_callback> if one is set.
92
93=head2 can_close
94
95Returns true.
96
97=head2 apply
98
99Extends L<Reaction::UI::ViewPort::Action::Role::Apply/apply> and sets
100the L</close_label> to L</close_label_cancel> if the original call to
101C<apply> was not successfull.
102
103Returns the result of the original C<apply> call.
104
105=head2 accept_events
106
107Extends L<Reaction::UI::ViewPort::Action::Role::Apply/accept_events>
108with the C<close> event if an L</on_close_callback> was provided.
109
110=head1 SEE ALSO
111
112L<Reaction::UI::ViewPort::Action::Role::Apply>
113
114L<Reaction::UI::ViewPort::Action::Role::OK>
115
116=head1 AUTHORS
117
118See L<Reaction::Class> for authors.
119
120=head1 LICENSE
121
122See L<Reaction::Class> for the license.
123
124=cut
125