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::InterfaceModel::Field::Mutable::File';
17 #use aliased 'Reaction::UI::ViewPort::InterfaceModel::Field::Mutable::TimeRange';
19 class Action is 'Reaction::UI::ViewPort::Object', which {
20 has '+model' => (isa => 'Reaction::InterfaceModel::Action');
22 has next_action => (is => 'rw', isa => 'ArrayRef');
23 has on_apply_callback => (is => 'rw', isa => 'CodeRef');
25 has ok_label => (is => 'rw', isa => 'Str', lazy_build => 1);
26 has apply_label => (is => 'rw', isa => 'Str', lazy_build => 1);
27 has close_label => (is => 'rw', isa => 'Str', lazy_fail => 1);
28 has close_label_close => (is => 'rw', isa => 'Str', lazy_build => 1);
29 has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
31 has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
33 implements BUILD => as{
35 $self->close_label($self->close_label_close);
38 implements _build_ok_label => as{ 'ok' };
39 implements _build_apply_label_ => as{ 'apply' };
40 implements _build_close_label_close => as{ 'close' };
41 implements _build_close_label_cancel => as{ 'cancel' };
43 implements can_apply => as {
45 foreach my $field ( @{ $self->ordered_fields } ) {
46 return 0 if $field->needs_sync;
47 # if e.g. a datetime field has an invalid value that can't be re-assembled
48 # into a datetime object, the action may be in a consistent state but
49 # not synchronized from the fields; in this case, we must not apply
51 return $self->model->can_apply;
54 implements do_apply => as {
55 shift->model->do_apply;
60 $self->close(@_) if $self->apply(@_);
63 implements apply => as {
65 if ($self->can_apply && (my $result = $self->do_apply)) {
67 $self->close_label($self->close_label_close);
68 $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
72 $self->close_label($self->close_label_cancel);
77 implements close => as {
79 my ($controller, $name, @args) = @{$self->next_action};
80 $controller->pop_viewport;
81 $controller->$name($self->ctx, @args);
84 implements can_close => as { 1 };
86 override accept_events => sub {
87 (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
88 }; # can't do a close-type operation if there's nowhere to go afterwards
90 after apply_child_events => sub {
91 # interrupt here because fields will have been updated
93 $self->sync_action_from_fields;
96 implements sync_action_from_fields => as {
98 foreach my $field ($self->fields) {
99 $field->sync_to_action; # get the field to populate the $action if possible
101 $self->action->sync_all;
102 foreach my $field ($self->fields) {
103 $field->sync_from_action; # get errors from $action if applicable
108 implements _build_fields_for_type_Num => as {
109 my ($self, $attr, $args) = @_;
110 $self->_build_simple_field(attribute => $attr, class => Number, %$args);
113 implements _build_fields_for_type_Int => as {
114 my ($self, $attr, $args) = @_;
115 $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
118 implements _build_fields_for_type_Bool => as {
119 my ($self, $attr, $args) = @_;
120 $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
123 implements _build_fields_for_type_SimpleStr => as {
124 my ($self, $attr, $args) = @_;
125 $self->_build_simple_field(attribute => $attr, class => String, %$args);
128 #implements _build_fields_for_type_File => as {
129 # my ($self, $attr, $args) = @_;
130 # $self->_build_simple_field(attribute => $attr, class => File, %$args);
133 implements _build_fields_for_type_Str => as {
134 my ($self, $attr, $args) = @_;
135 if ($attr->has_valid_values) { # There's probably a better way to do this
136 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
138 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
141 implements _build_fields_for_type_Password => as {
142 my ($self, $attr, $args) = @_;
143 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
146 implements _build_fields_for_type_DateTime => as {
147 my ($self, $attr, $args) = @_;
148 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
151 implements _build_fields_for_type_Enum => as {
152 my ($self, $attr, $args) = @_;
153 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
156 #this needs to be fixed. somehow. beats the shit our of me. really.
157 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
158 implements _build_fields_for_type_DBIx_Class_Row => as {
159 my ($self, $attr, $args) = @_;
160 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
163 implements _build_fields_for_type_ArrayRef => as {
164 my ($self, $attr, $args) = @_;
165 if ($attr->has_valid_values) {
166 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
168 $self->_build_simple_field
172 layout => 'interface_model/field/mutable/array/hidden',
177 #implements _build_fields_for_type_DateTime_Spanset => as {
178 # my ($self, $attr, $args) = @_;
179 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
188 Reaction::UI::ViewPort::InterfaceModel::Action
192 use aliased 'Reaction::UI::ViewPort::Action';
194 $self->push_viewport(Action,
195 layout => 'register',
197 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
200 qw / contact_title company_name email address1 address2 address3
201 city country post_code telephone mobile fax/ ],
206 This subclass of viewport is used for rendering a collection of
207 L<Reaction::UI::ViewPort::Field> objects for user editing.
213 L<Reaction::InterfaceModel::Action>
223 =head2 close_label_close
227 =head2 close_label_cancel
229 This label is only shown when C<changed> is true.
241 Returns true if a field has been edited.
245 =head2 on_apply_callback
253 Calls C<apply>, and then C<close> if successful.
257 Pop viewport and proceed to C<next_action>.
261 Attempt to save changes and update C<changed> attribute if required.
265 L<Reaction::UI::ViewPort>
267 L<Reaction::InterfaceModel::Action>
271 See L<Reaction::Class> for authors.
275 See L<Reaction::Class> for the license.