1 package Reaction::UI::ViewPort::Action;
5 use aliased 'Reaction::UI::ViewPort::Object';
7 BEGIN { *DEBUG_EVENTS = \&Reaction::UI::ViewPort::DEBUG_EVENTS; }
9 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Text';
10 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Array';
11 use aliased 'Reaction::UI::ViewPort::Field::Mutable::String';
12 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Number';
13 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Integer';
14 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Boolean';
15 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Password';
16 use aliased 'Reaction::UI::ViewPort::Field::Mutable::DateTime';
17 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseOne';
18 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseMany';
20 use aliased 'Reaction::UI::ViewPort::Field::Mutable::File';
21 #use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange';
23 use Reaction::Types::Core qw/NonEmptySimpleStr/;
25 use namespace::clean -except => [ qw(meta) ];
29 has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
30 #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
31 has method => ( isa => NonEmptySimpleStr, is => 'rw', default => sub { 'post' } );
33 has on_apply_callback => (is => 'rw', isa => 'CodeRef');
34 has on_close_callback => (is => 'rw', isa => 'CodeRef');
36 has ok_label => (is => 'rw', isa => 'Str', lazy_build => 1);
37 has apply_label => (is => 'rw', isa => 'Str', lazy_build => 1);
38 has close_label => (is => 'rw', isa => 'Str', lazy_fail => 1);
39 has close_label_close => (is => 'rw', isa => 'Str', lazy_build => 1);
40 has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
42 has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
45 $self->close_label($self->close_label_close);
47 sub _build_ok_label { 'ok' };
48 sub _build_apply_label { 'apply' };
49 sub _build_close_label_close { 'close' };
50 sub _build_close_label_cancel { 'cancel' };
53 foreach my $field ( @{ $self->fields } ) {
54 if ($field->needs_sync) {
56 $self->ctx->log->debug(
57 "Failing out of can_apply on ${\ref($self)} at ${\$self->location}"
58 ." because field for ${\$field->attribute->name} needs sync"
63 # if e.g. a datetime field has an invalid value that can't be re-assembled
64 # into a datetime object, the action may be in a consistent state but
65 # not synchronized from the fields; in this case, we must not apply
68 my $ret = $self->model->can_apply;
69 $self->ctx->log->debug(
70 "model can_apply returned ${ret}"
71 ." on ${\ref($self)} at ${\$self->location}"
75 return $self->model->can_apply;
78 shift->model->do_apply;
82 $self->close(@_) if $self->apply(@_);
86 if ($self->can_apply && (my $result = $self->do_apply)) {
88 $self->close_label($self->close_label_close);
89 $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
93 $self->close_label($self->close_label_cancel);
99 return unless $self->has_on_close_callback;
100 $self->on_close_callback->($self);
104 override accept_events => sub {
105 (($_[0]->has_on_close_callback ? ('ok', 'close') : ()), 'apply', super());
106 }; # can't do a close-type operation if there's nowhere to go afterwards
108 after apply_child_events => sub {
109 # interrupt here because fields will have been updated
111 $self->sync_action_from_fields;
113 sub sync_action_from_fields {
115 foreach my $field (@{$self->fields}) {
116 $field->sync_to_action; # get the field to populate the $action if possible
118 $self->model->sync_all;
119 foreach my $field (@{$self->fields}) {
120 $field->sync_from_action; # get errors from $action if applicable
123 sub _build_fields_for_type_Num {
124 my ($self, $attr, $args) = @_;
125 $self->_build_simple_field(attribute => $attr, class => Number, %$args);
127 sub _build_fields_for_type_Int {
128 my ($self, $attr, $args) = @_;
129 $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
131 sub _build_fields_for_type_Bool {
132 my ($self, $attr, $args) = @_;
133 $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
135 sub _build_fields_for_type_Reaction_Types_Core_SimpleStr {
136 my ($self, $attr, $args) = @_;
137 $self->_build_simple_field(attribute => $attr, class => String, %$args);
139 sub _build_fields_for_type_Reaction_Types_File_File {
140 my ($self, $attr, $args) = @_;
141 $self->_build_simple_field(attribute => $attr, class => File, %$args);
143 sub _build_fields_for_type_Str {
144 my ($self, $attr, $args) = @_;
145 if ($attr->has_valid_values) { # There's probably a better way to do this
146 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
148 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
151 sub _build_fields_for_type_Reaction_Types_Core_Password {
152 my ($self, $attr, $args) = @_;
153 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
155 sub _build_fields_for_type_Reaction_Types_DateTime_DateTime {
156 my ($self, $attr, $args) = @_;
157 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
159 sub _build_fields_for_type_Enum {
160 my ($self, $attr, $args) = @_;
161 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
164 #this needs to be fixed. somehow. beats the shit our of me. really.
165 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
166 sub _build_fields_for_type_DBIx_Class_Row {
167 my ($self, $attr, $args) = @_;
168 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
170 sub _build_fields_for_type_ArrayRef {
171 my ($self, $attr, $args) = @_;
172 if ($attr->has_valid_values) {
173 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
175 $self->_build_simple_field
179 layout => 'field/mutable/hidden_array',
184 #implements _build_fields_for_type_DateTime_Spanset => as {
185 # my ($self, $attr, $args) = @_;
186 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
189 __PACKAGE__->meta->make_immutable;
196 Reaction::UI::ViewPort::Action
200 use aliased 'Reaction::UI::ViewPort::Action';
202 $self->push_viewport(Action,
203 layout => 'register',
205 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
208 qw / contact_title company_name email address1 address2 address3
209 city country post_code telephone mobile fax/ ],
214 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
215 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
221 L<Reaction::InterfaceModel::Action>
231 =head2 close_label_close
235 =head2 close_label_cancel
237 This label is only shown when C<changed> is true.
249 Returns true if a field has been edited.
253 =head2 on_apply_callback
261 Calls C<apply>, and then C<close> if successful.
265 Pop viewport and proceed to C<next_action>.
269 Attempt to save changes and update C<changed> attribute if required.
273 L<Reaction::UI::ViewPort::Object>
275 L<Reaction::UI::ViewPort>
277 L<Reaction::InterfaceModel::Action>
281 See L<Reaction::Class> for authors.
285 See L<Reaction::Class> for the license.