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 class Action is Object, which {
24 has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
25 #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
27 has next_action => (is => 'rw', isa => 'ArrayRef');
28 has on_apply_callback => (is => 'rw', isa => 'CodeRef');
30 has ok_label => (is => 'rw', isa => 'Str', lazy_build => 1);
31 has apply_label => (is => 'rw', isa => 'Str', lazy_build => 1);
32 has close_label => (is => 'rw', isa => 'Str', lazy_fail => 1);
33 has close_label_close => (is => 'rw', isa => 'Str', lazy_build => 1);
34 has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
36 has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
38 implements BUILD => as{
40 $self->close_label($self->close_label_close);
43 implements _build_ok_label => as{ 'ok' };
44 implements _build_apply_label => as{ 'apply' };
45 implements _build_close_label_close => as{ 'close' };
46 implements _build_close_label_cancel => as{ 'cancel' };
48 implements can_apply => as {
50 foreach my $field ( @{ $self->fields } ) {
51 if ($field->needs_sync) {
53 $self->ctx->log->debug(
54 "Failing out of can_apply on ${\ref($self)} at ${\$self->location}"
55 ." because field for ${\$field->attribute->name} needs sync"
60 # if e.g. a datetime field has an invalid value that can't be re-assembled
61 # into a datetime object, the action may be in a consistent state but
62 # not synchronized from the fields; in this case, we must not apply
65 my $ret = $self->model->can_apply;
66 $self->ctx->log->debug(
67 "model can_apply returned ${ret}"
68 ." on ${\ref($self)} at ${\$self->location}"
72 return $self->model->can_apply;
75 implements do_apply => as {
76 shift->model->do_apply;
81 $self->close(@_) if $self->apply(@_);
84 implements apply => as {
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);
98 implements close => as {
100 my ($controller, $name, @args) = @{$self->next_action};
101 $controller->pop_viewport;
102 $controller->$name($self->ctx, @args);
105 implements can_close => as { 1 };
107 override accept_events => sub {
108 (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
109 }; # can't do a close-type operation if there's nowhere to go afterwards
111 after apply_child_events => sub {
112 # interrupt here because fields will have been updated
114 $self->sync_action_from_fields;
117 implements sync_action_from_fields => as {
119 foreach my $field (@{$self->fields}) {
120 $field->sync_to_action; # get the field to populate the $action if possible
122 $self->model->sync_all;
123 foreach my $field (@{$self->fields}) {
124 $field->sync_from_action; # get errors from $action if applicable
129 implements _build_fields_for_type_Num => as {
130 my ($self, $attr, $args) = @_;
131 $self->_build_simple_field(attribute => $attr, class => Number, %$args);
134 implements _build_fields_for_type_Int => as {
135 my ($self, $attr, $args) = @_;
136 $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
139 implements _build_fields_for_type_Bool => as {
140 my ($self, $attr, $args) = @_;
141 $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
144 implements _build_fields_for_type_Reaction_Types_Core_SimpleStr => as {
145 my ($self, $attr, $args) = @_;
146 $self->_build_simple_field(attribute => $attr, class => String, %$args);
149 implements _build_fields_for_type_Reaction_Types_File_File => as {
150 my ($self, $attr, $args) = @_;
151 $self->_build_simple_field(attribute => $attr, class => File, %$args);
154 implements _build_fields_for_type_Str => as {
155 my ($self, $attr, $args) = @_;
156 if ($attr->has_valid_values) { # There's probably a better way to do this
157 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
159 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
163 implements _build_fields_for_type_Reaction_Types_Core_Password => as {
164 my ($self, $attr, $args) = @_;
165 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
168 implements _build_fields_for_type_Reaction_Types_DateTime_DateTime => as {
169 my ($self, $attr, $args) = @_;
170 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
173 implements _build_fields_for_type_Enum => as {
174 my ($self, $attr, $args) = @_;
175 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
178 #this needs to be fixed. somehow. beats the shit our of me. really.
179 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
180 implements _build_fields_for_type_DBIx_Class_Row => as {
181 my ($self, $attr, $args) = @_;
182 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
185 implements _build_fields_for_type_ArrayRef => as {
186 my ($self, $attr, $args) = @_;
187 if ($attr->has_valid_values) {
188 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
190 $self->_build_simple_field
194 layout => 'field/mutable/hidden_array',
199 #implements _build_fields_for_type_DateTime_Spanset => as {
200 # my ($self, $attr, $args) = @_;
201 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
210 Reaction::UI::ViewPort::Action
214 use aliased 'Reaction::UI::ViewPort::Action';
216 $self->push_viewport(Action,
217 layout => 'register',
219 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
222 qw / contact_title company_name email address1 address2 address3
223 city country post_code telephone mobile fax/ ],
228 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
229 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
235 L<Reaction::InterfaceModel::Action>
245 =head2 close_label_close
249 =head2 close_label_cancel
251 This label is only shown when C<changed> is true.
263 Returns true if a field has been edited.
267 =head2 on_apply_callback
275 Calls C<apply>, and then C<close> if successful.
279 Pop viewport and proceed to C<next_action>.
283 Attempt to save changes and update C<changed> attribute if required.
287 L<Reaction::UI::ViewPort::Object>
289 L<Reaction::UI::ViewPort>
291 L<Reaction::InterfaceModel::Action>
295 See L<Reaction::Class> for authors.
299 See L<Reaction::Class> for the license.