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 class Action is Object, which {
26 has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
27 #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
28 has method => ( isa => NonEmptySimpleStr, is => 'rw', default => sub { 'post' } );
30 has next_action => (is => 'rw', isa => 'ArrayRef');
31 has on_apply_callback => (is => 'rw', isa => 'CodeRef');
33 has ok_label => (is => 'rw', isa => 'Str', lazy_build => 1);
34 has apply_label => (is => 'rw', isa => 'Str', lazy_build => 1);
35 has close_label => (is => 'rw', isa => 'Str', lazy_fail => 1);
36 has close_label_close => (is => 'rw', isa => 'Str', lazy_build => 1);
37 has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
39 has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
41 implements BUILD => as{
43 $self->close_label($self->close_label_close);
46 implements _build_ok_label => as{ 'ok' };
47 implements _build_apply_label => as{ 'apply' };
48 implements _build_close_label_close => as{ 'close' };
49 implements _build_close_label_cancel => as{ 'cancel' };
51 implements can_apply => as {
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 implements do_apply => as {
79 shift->model->do_apply;
84 $self->close(@_) if $self->apply(@_);
87 implements apply => as {
89 if ($self->can_apply && (my $result = $self->do_apply)) {
91 $self->close_label($self->close_label_close);
92 $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
96 $self->close_label($self->close_label_cancel);
101 implements close => as {
103 my ($controller, $name, @args) = @{$self->next_action};
104 $controller->pop_viewport;
105 $controller->$name($self->ctx, @args);
108 implements can_close => as { 1 };
110 override accept_events => sub {
111 (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
112 }; # can't do a close-type operation if there's nowhere to go afterwards
114 after apply_child_events => sub {
115 # interrupt here because fields will have been updated
117 $self->sync_action_from_fields;
120 implements sync_action_from_fields => as {
122 foreach my $field (@{$self->fields}) {
123 $field->sync_to_action; # get the field to populate the $action if possible
125 $self->model->sync_all;
126 foreach my $field (@{$self->fields}) {
127 $field->sync_from_action; # get errors from $action if applicable
132 implements _build_fields_for_type_Num => as {
133 my ($self, $attr, $args) = @_;
134 $self->_build_simple_field(attribute => $attr, class => Number, %$args);
137 implements _build_fields_for_type_Int => as {
138 my ($self, $attr, $args) = @_;
139 $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
142 implements _build_fields_for_type_Bool => as {
143 my ($self, $attr, $args) = @_;
144 $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
147 implements _build_fields_for_type_Reaction_Types_Core_SimpleStr => as {
148 my ($self, $attr, $args) = @_;
149 $self->_build_simple_field(attribute => $attr, class => String, %$args);
152 implements _build_fields_for_type_Reaction_Types_File_File => as {
153 my ($self, $attr, $args) = @_;
154 $self->_build_simple_field(attribute => $attr, class => File, %$args);
157 implements _build_fields_for_type_Str => as {
158 my ($self, $attr, $args) = @_;
159 if ($attr->has_valid_values) { # There's probably a better way to do this
160 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
162 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
166 implements _build_fields_for_type_Reaction_Types_Core_Password => as {
167 my ($self, $attr, $args) = @_;
168 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
171 implements _build_fields_for_type_Reaction_Types_DateTime_DateTime => as {
172 my ($self, $attr, $args) = @_;
173 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
176 implements _build_fields_for_type_Enum => as {
177 my ($self, $attr, $args) = @_;
178 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
181 #this needs to be fixed. somehow. beats the shit our of me. really.
182 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
183 implements _build_fields_for_type_DBIx_Class_Row => as {
184 my ($self, $attr, $args) = @_;
185 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
188 implements _build_fields_for_type_ArrayRef => as {
189 my ($self, $attr, $args) = @_;
190 if ($attr->has_valid_values) {
191 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
193 $self->_build_simple_field
197 layout => 'field/mutable/hidden_array',
202 #implements _build_fields_for_type_DateTime_Spanset => as {
203 # my ($self, $attr, $args) = @_;
204 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
213 Reaction::UI::ViewPort::Action
217 use aliased 'Reaction::UI::ViewPort::Action';
219 $self->push_viewport(Action,
220 layout => 'register',
222 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
225 qw / contact_title company_name email address1 address2 address3
226 city country post_code telephone mobile fax/ ],
231 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
232 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
238 L<Reaction::InterfaceModel::Action>
248 =head2 close_label_close
252 =head2 close_label_cancel
254 This label is only shown when C<changed> is true.
266 Returns true if a field has been edited.
270 =head2 on_apply_callback
278 Calls C<apply>, and then C<close> if successful.
282 Pop viewport and proceed to C<next_action>.
286 Attempt to save changes and update C<changed> attribute if required.
290 L<Reaction::UI::ViewPort::Object>
292 L<Reaction::UI::ViewPort>
294 L<Reaction::InterfaceModel::Action>
298 See L<Reaction::Class> for authors.
302 See L<Reaction::Class> for the license.