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 => (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);
142 implements _build_fields_for_type_Password => as {
143 my ($self, $attr, $args) = @_;
144 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
147 implements _build_fields_for_type_DateTime => as {
148 my ($self, $attr, $args) = @_;
149 $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
152 implements _build_fields_for_type_Enum => as {
153 my ($self, $attr, $args) = @_;
154 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
157 #this needs to be fixed. somehow. beats the shit our of me. really.
158 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
159 implements _build_fields_for_type_DBIx_Class_Row => as {
160 my ($self, $attr, $args) = @_;
161 $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
164 implements _build_fields_for_type_ArrayRef => as {
165 my ($self, $attr, $args) = @_;
166 if ($attr->has_valid_values) {
167 $self->_build_simple_field(attribute => $attr, class => ChooseMany, %$args);
169 $self->_build_simple_field
173 layout => 'interface_model/field/mutable/array/hidden',
178 #implements _build_fields_for_type_DateTime_Spanset => as {
179 # my ($self, $attr, $args) = @_;
180 # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args);
189 Reaction::UI::ViewPort::InterfaceModel::Action
193 use aliased 'Reaction::UI::ViewPort::Action';
195 $self->push_viewport(Action,
196 layout => 'register',
198 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
201 qw / contact_title company_name email address1 address2 address3
202 city country post_code telephone mobile fax/ ],
207 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
208 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
214 L<Reaction::InterfaceModel::Action>
224 =head2 close_label_close
228 =head2 close_label_cancel
230 This label is only shown when C<changed> is true.
242 Returns true if a field has been edited.
246 =head2 on_apply_callback
254 Calls C<apply>, and then C<close> if successful.
258 Pop viewport and proceed to C<next_action>.
262 Attempt to save changes and update C<changed> attribute if required.
266 L<Reaction::UI::ViewPort::Object>
268 L<Reaction::UI::ViewPort>
270 L<Reaction::InterfaceModel::Action>
274 See L<Reaction::Class> for authors.
278 See L<Reaction::Class> for the license.