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