Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Action; |
2 | |
3 | use Reaction::Class; |
4 | |
d9a3266f |
5 | use aliased 'Reaction::UI::ViewPort::Object'; |
6 | |
7 | BEGIN { *DEBUG_EVENTS = \&Reaction::UI::ViewPort::DEBUG_EVENTS; } |
8 | |
ddccc6a2 |
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'; |
19 | |
ddd1dc65 |
20 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::File'; |
bd1283bd |
21 | #use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange'; |
ddccc6a2 |
22 | |
d9a3266f |
23 | class Action is Object, which { |
c8fbb8ad |
24 | has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1); |
25 | #has '+model' => (isa => 'Reaction::InterfaceModel::Action'); |
ddccc6a2 |
26 | |
27 | has next_action => (is => 'rw', isa => 'ArrayRef'); |
28 | has on_apply_callback => (is => 'rw', isa => 'CodeRef'); |
29 | |
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); |
35 | |
36 | has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0}); |
37 | |
38 | implements BUILD => as{ |
39 | my $self = shift; |
40 | $self->close_label($self->close_label_close); |
41 | }; |
42 | |
43 | implements _build_ok_label => as{ 'ok' }; |
36d54b14 |
44 | implements _build_apply_label => as{ 'apply' }; |
ddccc6a2 |
45 | implements _build_close_label_close => as{ 'close' }; |
46 | implements _build_close_label_cancel => as{ 'cancel' }; |
47 | |
48 | implements can_apply => as { |
49 | my ($self) = @_; |
36d54b14 |
50 | foreach my $field ( @{ $self->fields } ) { |
d9a3266f |
51 | if ($field->needs_sync) { |
52 | if (DEBUG_EVENTS) { |
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" |
56 | ); |
57 | } |
5344cd2b |
58 | return 0; |
d9a3266f |
59 | } |
ddccc6a2 |
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 |
63 | } |
d9a3266f |
64 | if (DEBUG_EVENTS) { |
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}" |
69 | ); |
70 | return $ret; |
71 | } |
ddccc6a2 |
72 | return $self->model->can_apply; |
73 | }; |
74 | |
75 | implements do_apply => as { |
76 | shift->model->do_apply; |
77 | }; |
78 | |
79 | implements ok => as { |
80 | my $self = shift; |
81 | $self->close(@_) if $self->apply(@_); |
82 | }; |
83 | |
84 | implements apply => as { |
85 | my $self = shift; |
86 | if ($self->can_apply && (my $result = $self->do_apply)) { |
87 | $self->changed(0); |
88 | $self->close_label($self->close_label_close); |
89 | $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback; |
90 | return 1; |
91 | } else { |
92 | $self->changed(1); |
93 | $self->close_label($self->close_label_cancel); |
94 | return 0; |
95 | } |
96 | }; |
97 | |
98 | implements close => as { |
99 | my $self = shift; |
100 | my ($controller, $name, @args) = @{$self->next_action}; |
101 | $controller->pop_viewport; |
102 | $controller->$name($self->ctx, @args); |
103 | }; |
104 | |
105 | implements can_close => as { 1 }; |
106 | |
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 |
110 | |
111 | after apply_child_events => sub { |
112 | # interrupt here because fields will have been updated |
113 | my ($self) = @_; |
114 | $self->sync_action_from_fields; |
115 | }; |
116 | |
117 | implements sync_action_from_fields => as { |
118 | my ($self) = @_; |
36d54b14 |
119 | foreach my $field (@{$self->fields}) { |
ddccc6a2 |
120 | $field->sync_to_action; # get the field to populate the $action if possible |
121 | } |
36d54b14 |
122 | $self->model->sync_all; |
123 | foreach my $field (@{$self->fields}) { |
ddccc6a2 |
124 | $field->sync_from_action; # get errors from $action if applicable |
125 | } |
126 | }; |
127 | |
128 | |
129 | implements _build_fields_for_type_Num => as { |
130 | my ($self, $attr, $args) = @_; |
131 | $self->_build_simple_field(attribute => $attr, class => Number, %$args); |
132 | }; |
133 | |
134 | implements _build_fields_for_type_Int => as { |
135 | my ($self, $attr, $args) = @_; |
136 | $self->_build_simple_field(attribute => $attr, class => Integer, %$args); |
137 | }; |
138 | |
139 | implements _build_fields_for_type_Bool => as { |
140 | my ($self, $attr, $args) = @_; |
141 | $self->_build_simple_field(attribute => $attr, class => Boolean, %$args); |
142 | }; |
143 | |
d9a3266f |
144 | implements _build_fields_for_type_Reaction_Types_Core_SimpleStr => as { |
ddccc6a2 |
145 | my ($self, $attr, $args) = @_; |
146 | $self->_build_simple_field(attribute => $attr, class => String, %$args); |
147 | }; |
148 | |
5ae92ec0 |
149 | implements _build_fields_for_type_Reaction_Types_File_File => as { |
ddd1dc65 |
150 | my ($self, $attr, $args) = @_; |
151 | $self->_build_simple_field(attribute => $attr, class => File, %$args); |
152 | }; |
ddccc6a2 |
153 | |
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); |
cf1dfeb5 |
158 | } else { |
159 | $self->_build_simple_field(attribute => $attr, class => Text, %$args); |
ddccc6a2 |
160 | } |
ddccc6a2 |
161 | }; |
162 | |
d9a3266f |
163 | implements _build_fields_for_type_Reaction_Types_Core_Password => as { |
ddccc6a2 |
164 | my ($self, $attr, $args) = @_; |
165 | $self->_build_simple_field(attribute => $attr, class => Password, %$args); |
166 | }; |
167 | |
abc28589 |
168 | implements _build_fields_for_type_Reaction_Types_DateTime_DateTime => as { |
ddccc6a2 |
169 | my ($self, $attr, $args) = @_; |
170 | $self->_build_simple_field(attribute => $attr, class => DateTime, %$args); |
171 | }; |
172 | |
173 | implements _build_fields_for_type_Enum => as { |
174 | my ($self, $attr, $args) = @_; |
175 | $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args); |
176 | }; |
177 | |
178 | #this needs to be fixed. somehow. beats the shit our of me. really. |
179 | #implements build_fields_for_type_Reaction_InterfaceModel_Object => as { |
5976ddc4 |
180 | implements _build_fields_for_type_DBIx_Class_Row => as { |
ddccc6a2 |
181 | my ($self, $attr, $args) = @_; |
182 | $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args); |
183 | }; |
184 | |
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); |
189 | } else { |
190 | $self->_build_simple_field |
191 | ( |
192 | attribute => $attr, |
193 | class => Array, |
13427367 |
194 | layout => 'field/mutable/hidden_array', |
ddccc6a2 |
195 | %$args); |
196 | } |
197 | }; |
198 | |
199 | #implements _build_fields_for_type_DateTime_Spanset => as { |
200 | # my ($self, $attr, $args) = @_; |
201 | # $self->_build_simple_field(attribute => $attr, class => TimeRange, %$args); |
202 | #}; |
203 | |
204 | }; |
205 | |
206 | 1; |
207 | |
208 | =head1 NAME |
209 | |
bd1283bd |
210 | Reaction::UI::ViewPort::Action |
ddccc6a2 |
211 | |
212 | =head1 SYNOPSIS |
213 | |
2f670e13 |
214 | use aliased 'Reaction::UI::ViewPort::Action'; |
ddccc6a2 |
215 | |
216 | $self->push_viewport(Action, |
217 | layout => 'register', |
218 | model => $action, |
219 | next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ], |
220 | ctx => $c, |
221 | field_order => [ |
222 | qw / contact_title company_name email address1 address2 address3 |
223 | city country post_code telephone mobile fax/ ], |
224 | ); |
225 | |
226 | =head1 DESCRIPTION |
227 | |
2dba7201 |
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. |
ddccc6a2 |
230 | |
231 | =head1 ATTRIBUTES |
232 | |
233 | =head2 model |
234 | |
235 | L<Reaction::InterfaceModel::Action> |
236 | |
237 | =head2 ok_label |
238 | |
239 | Default: 'ok' |
240 | |
241 | =head2 apply_label |
242 | |
243 | Default: 'apply' |
244 | |
245 | =head2 close_label_close |
246 | |
247 | Default: 'close' |
248 | |
249 | =head2 close_label_cancel |
250 | |
251 | This label is only shown when C<changed> is true. |
252 | |
253 | Default: 'cancel' |
254 | |
255 | =head2 fields |
256 | |
257 | =head2 can_apply |
258 | |
259 | =head2 can_close |
260 | |
261 | =head2 changed |
262 | |
263 | Returns true if a field has been edited. |
264 | |
265 | =head2 next_action |
266 | |
267 | =head2 on_apply_callback |
268 | |
269 | CodeRef. |
270 | |
271 | =head1 METHODS |
272 | |
273 | =head2 ok |
274 | |
275 | Calls C<apply>, and then C<close> if successful. |
276 | |
277 | =head2 close |
278 | |
279 | Pop viewport and proceed to C<next_action>. |
280 | |
281 | =head2 apply |
282 | |
283 | Attempt to save changes and update C<changed> attribute if required. |
284 | |
285 | =head1 SEE ALSO |
286 | |
2dba7201 |
287 | L<Reaction::UI::ViewPort::Object> |
288 | |
ddccc6a2 |
289 | L<Reaction::UI::ViewPort> |
290 | |
291 | L<Reaction::InterfaceModel::Action> |
292 | |
293 | =head1 AUTHORS |
294 | |
295 | See L<Reaction::Class> for authors. |
296 | |
297 | =head1 LICENSE |
298 | |
299 | See L<Reaction::Class> for the license. |
300 | |
301 | =cut |