1 package Reaction::UI::ViewPort::Action;
5 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Text';
6 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Array';
7 use aliased 'Reaction::UI::ViewPort::Field::Mutable::String';
8 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Number';
9 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Integer';
10 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Boolean';
11 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Password';
12 use aliased 'Reaction::UI::ViewPort::Field::Mutable::DateTime';
13 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseOne';
14 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseMany';
16 use aliased 'Reaction::UI::ViewPort::Field::Mutable::File';
17 #use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange';
19 class Action is 'Reaction::UI::ViewPort::Object', which {
20 has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
21 #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
23 has next_action => (is => 'rw', isa => 'ArrayRef');
24 has on_apply_callback => (is => 'rw', isa => 'CodeRef');
26 has ok_label => (is => 'rw', isa => 'Str', lazy_build => 1);
27 has apply_label => (is => 'rw', isa => 'Str', lazy_build => 1);
28 has close_label => (is => 'rw', isa => 'Str', lazy_fail => 1);
29 has close_label_close => (is => 'rw', isa => 'Str', lazy_build => 1);
30 has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
32 has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
34 implements BUILD => as{
36 $self->close_label($self->close_label_close);
39 implements _build_ok_label => as{ 'ok' };
40 implements _build_apply_label => as{ 'apply' };
41 implements _build_close_label_close => as{ 'close' };
42 implements _build_close_label_cancel => as{ 'cancel' };
44 implements can_apply => as {
46 foreach my $field ( @{ $self->fields } ) {
47 return 0 if $field->needs_sync;
48 # if e.g. a datetime field has an invalid value that can't be re-assembled
49 # into a datetime object, the action may be in a consistent state but
50 # not synchronized from the fields; in this case, we must not apply
52 return $self->model->can_apply;
55 implements do_apply => as {
56 shift->model->do_apply;
61 $self->close(@_) if $self->apply(@_);
64 implements apply => as {
66 if ($self->can_apply && (my $result = $self->do_apply)) {
68 $self->close_label($self->close_label_close);
69 $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
73 $self->close_label($self->close_label_cancel);
78 implements close => as {
80 my ($controller, $name, @args) = @{$self->next_action};
81 $controller->pop_viewport;
82 $controller->$name($self->ctx, @args);
85 implements can_close => as { 1 };
87 override accept_events => sub {
88 (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
89 }; # can't do a close-type operation if there's nowhere to go afterwards
91 after apply_child_events => sub {
92 # interrupt here because fields will have been updated
94 $self->sync_action_from_fields;
97 implements sync_action_from_fields => as {
99 foreach my $field (@{$self->fields}) {
100 $field->sync_to_action; # get the field to populate the $action if possible
102 $self->model->sync_all;
103 foreach my $field (@{$self->fields}) {
104 $field->sync_from_action; # get errors from $action if applicable
109 implements _build_fields_for_type_Num => as {
110 my ($self, $attr, $args) = @_;
111 $self->_build_simple_field(attribute => $attr, class => Number, %$args);
114 implements _build_fields_for_type_Int => as {
115 my ($self, $attr, $args) = @_;
116 $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
119 implements _build_fields_for_type_Bool => as {
120 my ($self, $attr, $args) = @_;
121 $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
124 implements _build_fields_for_type_SimpleStr => as {
125 my ($self, $attr, $args) = @_;
126 $self->_build_simple_field(attribute => $attr, class => String, %$args);
129 implements _build_fields_for_type_File => as {
130 my ($self, $attr, $args) = @_;
131 $self->_build_simple_field(attribute => $attr, class => File, %$args);
134 implements _build_fields_for_type_Str => as {
135 my ($self, $attr, $args) = @_;
136 if ($attr->has_valid_values) { # There's probably a better way to do this
137 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
139 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
143 implements _build_fields_for_type_Password => as {
144 my ($self, $attr, $args) = @_;
145 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
148 implements _build_fields_for_type_DateTime => as {
149 my ($self, $attr, $args) = @_;
150 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
153 implements _build_fields_for_type_Enum => as {
154 my ($self, $attr, $args) = @_;
155 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
158 #this needs to be fixed. somehow. beats the shit our of me. really.
159 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
160 implements _build_fields_for_type_DBIx_Class_Row => as {
161 my ($self, $attr, $args) = @_;
162 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
165 implements _build_fields_for_type_ArrayRef => as {
166 my ($self, $attr, $args) = @_;
167 if ($attr->has_valid_values) {
168 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
170 $self->_build_simple_field
174 layout => 'interface_model/field/mutable/array/hidden',
179 #implements _build_fields_for_type_DateTime_Spanset => as {
180 # my ($self, $attr, $args) = @_;
181 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
190 Reaction::UI::ViewPort::Action
194 use aliased 'Reaction::UI::ViewPort::Action';
196 $self->push_viewport(Action,
197 layout => 'register',
199 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
202 qw / contact_title company_name email address1 address2 address3
203 city country post_code telephone mobile fax/ ],
208 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
209 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
215 L<Reaction::InterfaceModel::Action>
225 =head2 close_label_close
229 =head2 close_label_cancel
231 This label is only shown when C<changed> is true.
243 Returns true if a field has been edited.
247 =head2 on_apply_callback
255 Calls C<apply>, and then C<close> if successful.
259 Pop viewport and proceed to C<next_action>.
263 Attempt to save changes and update C<changed> attribute if required.
267 L<Reaction::UI::ViewPort::Object>
269 L<Reaction::UI::ViewPort>
271 L<Reaction::InterfaceModel::Action>
275 See L<Reaction::Class> for authors.
279 See L<Reaction::Class> for the license.