new renamed viewports
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
1 package Reaction::UI::ViewPort::Action;
2
3 use Reaction::Class;
4
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';
15
16 #use aliased 'Reaction::UI::ViewPort::InterfaceModel::Field::Mutable::File';
17 #use aliased 'Reaction::UI::ViewPort::InterfaceModel::Field::Mutable::TimeRange';
18
19 class ActionForm is 'Reaction::UI::ViewPort', which {
20   has '+model' => (isa => 'Reaction::InterfaceModel::Action');
21
22   has next_action       => (is => 'rw', isa => 'ArrayRef');
23   has on_apply_callback => (is => 'rw', isa => 'CodeRef');
24
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);
30
31   has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
32
33   implements BUILD => as{
34     my $self = shift;
35     $self->close_label($self->close_label_close);
36   };
37
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' };
42
43   implements can_apply => as {
44     my ($self) = @_;
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
50     }
51     return $self->model->can_apply;
52   };
53
54   implements do_apply => as {
55     shift->model->do_apply;
56   };
57
58   implements ok => as {
59     my $self = shift;
60     $self->close(@_) if $self->apply(@_);
61   };
62
63   implements apply => as {
64     my $self = shift;
65     if ($self->can_apply && (my $result = $self->do_apply)) {
66       $self->changed(0);
67       $self->close_label($self->close_label_close);
68       $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
69       return 1;
70     } else {
71       $self->changed(1);
72       $self->close_label($self->close_label_cancel);
73       return 0;
74     }
75   };
76
77   implements close => as {
78     my $self = shift;
79     my ($controller, $name, @args) = @{$self->next_action};
80     $controller->pop_viewport;
81     $controller->$name($self->ctx, @args);
82   };
83
84   implements can_close => as { 1 };
85
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
89
90   after apply_child_events => sub {
91     # interrupt here because fields will have been updated
92     my ($self) = @_;
93     $self->sync_action_from_fields;
94   };
95
96   implements sync_action_from_fields => as {
97     my ($self) = @_;
98     foreach my $field ($self->fields) {
99       $field->sync_to_action; # get the field to populate the $action if possible
100     }
101     $self->action->sync_all;
102     foreach my $field ($self->fields) {
103       $field->sync_from_action; # get errors from $action if applicable
104     }
105   };
106
107
108   implements _build_fields_for_type_Num => as {
109     my ($self, $attr, $args) = @_;
110     $self->_build_simple_field(attribute => $attr, class => Number, %$args);
111   };
112
113   implements _build_fields_for_type_Int => as {
114     my ($self, $attr, $args) = @_;
115     $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
116   };
117
118   implements _build_fields_for_type_Bool => as {
119     my ($self,  $attr, $args) = @_;
120     $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
121   };
122
123   implements _build_fields_for_type_SimpleStr => as {
124     my ($self, $attr, $args) = @_;
125     $self->_build_simple_field(attribute => $attr, class => String, %$args);
126   };
127
128   #implements _build_fields_for_type_File => as {
129   #  my ($self, $attr, $args) = @_;
130   #  $self->_build_simple_field(attribute => $attr, class => File, %$args);
131   #};
132
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);
137     }
138     $self->_build_simple_field(attribute => $attr, class => Text, %$args);
139   };
140
141   implements _build_fields_for_type_Password => as {
142     my ($self, $attr, $args) = @_;
143     $self->_build_simple_field(attribute => $attr, class => Password, %$args);
144   };
145
146   implements _build_fields_for_type_DateTime => as {
147     my ($self, $attr, $args) = @_;
148     $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
149   };
150
151   implements _build_fields_for_type_Enum => as {
152     my ($self, $attr, $args) = @_;
153       $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
154   };
155
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);
161   };
162
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);
167     } else {
168       $self->_build_simple_field
169         (
170          attribute => $attr,
171          class     => Array,
172          layout    => 'interface_model/field/mutable/array/hidden',
173          %$args);
174     }
175   };
176
177   #implements _build_fields_for_type_DateTime_Spanset => as {
178   #  my ($self, $attr, $args) = @_;
179   #    $self->_build_simple_field(attribute => $attr, class => TimeRange,  %$args);
180   #};
181
182 };
183
184   1;
185
186 =head1 NAME
187
188 Reaction::UI::ViewPort::InterfaceModel::Action
189
190 =head1 SYNOPSIS
191
192   use aliased 'Reaction::UI::ViewPort::ActionForm';
193
194   $self->push_viewport(Action,
195     layout => 'register',
196     model => $action,
197     next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
198     ctx => $c,
199     field_order => [
200       qw / contact_title company_name email address1 address2 address3
201            city country post_code telephone mobile fax/ ],
202   );
203
204 =head1 DESCRIPTION
205
206 This subclass of viewport is used for rendering a collection of
207 L<Reaction::UI::ViewPort::Field> objects for user editing.
208
209 =head1 ATTRIBUTES
210
211 =head2 model
212
213 L<Reaction::InterfaceModel::Action>
214
215 =head2 ok_label
216
217 Default: 'ok'
218
219 =head2 apply_label
220
221 Default: 'apply'
222
223 =head2 close_label_close
224
225 Default: 'close'
226
227 =head2 close_label_cancel
228
229 This label is only shown when C<changed> is true.
230
231 Default: 'cancel'
232
233 =head2 fields
234
235 =head2 can_apply
236
237 =head2 can_close
238
239 =head2 changed
240
241 Returns true if a field has been edited.
242
243 =head2 next_action
244
245 =head2 on_apply_callback
246
247 CodeRef.
248
249 =head1 METHODS
250
251 =head2 ok
252
253 Calls C<apply>, and then C<close> if successful.
254
255 =head2 close
256
257 Pop viewport and proceed to C<next_action>.
258
259 =head2 apply
260
261 Attempt to save changes and update C<changed> attribute if required.
262
263 =head1 SEE ALSO
264
265 L<Reaction::UI::ViewPort>
266
267 L<Reaction::InterfaceModel::Action>
268
269 =head1 AUTHORS
270
271 See L<Reaction::Class> for authors.
272
273 =head1 LICENSE
274
275 See L<Reaction::Class> for the license.
276
277 =cut