aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/ActionForm.pm
blob: b28c25d50504dbb71944eaeeb4f43011425df0da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package Reaction::UI::ViewPort::ActionForm;

use Reaction::Class;

use aliased 'Reaction::UI::ViewPort::Field::Text';
use aliased 'Reaction::UI::ViewPort::Field::Number';
use aliased 'Reaction::UI::ViewPort::Field::Boolean';
use aliased 'Reaction::UI::ViewPort::Field::File';
use aliased 'Reaction::UI::ViewPort::Field::String';
use aliased 'Reaction::UI::ViewPort::Field::Password';
use aliased 'Reaction::UI::ViewPort::Field::DateTime';
use aliased 'Reaction::UI::ViewPort::Field::ChooseOne';
use aliased 'Reaction::UI::ViewPort::Field::ChooseMany';
use aliased 'Reaction::UI::ViewPort::Field::HiddenArray';
use aliased 'Reaction::UI::ViewPort::Field::TimeRange';

class ActionForm is 'Reaction::UI::ViewPort', which {
  has action => (
                 isa => 'Reaction::InterfaceModel::Action', is => 'ro', required => 1
                );

  has ordered_fields => (is => 'rw', isa => 'ArrayRef', lazy_build => 1);

  has _field_map => (
                     isa => 'HashRef', is => 'rw', init_arg => 'fields', lazy_build => 1,
                    );

  has changed => (
                  isa => 'Int', is => 'rw', reader => 'is_changed', default => sub { 0 }
                 );

  has next_action => (
                      isa => 'ArrayRef', is => 'rw', required => 0, predicate => 'has_next_action'
                     );

  has on_apply_callback => (
                            isa => 'CodeRef', is => 'rw', required => 0,
                            predicate => 'has_on_apply_callback'
                           );

  has ok_label => (
                   isa => 'Str', is => 'rw', required => 1, default => sub { 'ok' }
                  );

  has apply_label => (
                      isa  => 'Str', is => 'rw', required => 1, default => sub { 'apply' }
                     );

  has close_label => (isa => 'Str', is => 'rw', lazy_fail => 1);

  has close_label_close => (
                            isa => 'Str', is => 'rw', required => 1, default => sub { 'close' }
                           );

  has close_label_cancel => (
                             isa => 'Str', is => 'rw', required => 1, default => sub { 'cancel' }
                            );

  sub fields { shift->_field_map }

  implements BUILD => as {
    my ($self, $args) = @_;
    unless ($self->_has_field_map) {
      my @field_map;
      my $action = $self->action;
      foreach my $attr ($action->parameter_attributes) {
        push(@field_map, $self->build_fields_for($attr => $args));
      }
      $self->_field_map({ @field_map });
    }
    $self->close_label($self->close_label_close);
  };

  implements build_fields_for => as {
    my ($self, $attr, $args) = @_;
    my $attr_name = $attr->name;
    #TODO: DOCUMENT ME!!!!!!!!!!!!!!!!!
    my $builder = "build_fields_for_name_${attr_name}";
    my @fields;
    if ($self->can($builder)) {
      @fields = $self->$builder($attr, $args); # re-use coderef from can()
    } elsif ($attr->has_type_constraint) {
      my $constraint = $attr->type_constraint;
      my $base_name = $constraint->name;
      my $tried_isa = 0;
    CONSTRAINT: while (defined($constraint)) {
        my $name = $constraint->name;
        if (eval { $name->can('meta') } && !$tried_isa++) {
          foreach my $class ($name->meta->class_precedence_list) {
            my $mangled_name = $class;
            $mangled_name =~ s/:+/_/g;
            my $builder = "build_fields_for_type_${mangled_name}";
            if ($self->can($builder)) {
              @fields = $self->$builder($attr, $args);
              last CONSTRAINT;
            }
          }
        }
        if (defined($name)) {
          unless (defined($base_name)) {
            $base_name = "(anon subtype of ${name})";
          }
          my $mangled_name = $name;
          $mangled_name =~ s/:+/_/g;
          my $builder = "build_fields_for_type_${mangled_name}";
          if ($self->can($builder)) {
            @fields = $self->$builder($attr, $args);
            last CONSTRAINT;
          }
        }
        $constraint = $constraint->parent;
      }
      if (!defined($constraint)) {
        confess "Can't build field ${attr_name} of type ${base_name} without $builder method or build_fields_for_type_<type> method for type or any supertype";
      }
    } else {
      confess "Can't build field ${attr} without $builder method or type constraint";
    }
    return @fields;
  };

  implements build_field_map => as {
    confess "Lazy field map building not supported by default";
  };

  implements build_ordered_fields => as {
    my $self = shift;
    my $ordered = $self->sort_by_spec($self->column_order, [keys %{$self->_field_map}]);
    return [@{$self->_field_map}{@$ordered}];
  };

  implements can_apply => as {
    my ($self) = @_;
    foreach my $field ( @{ $self->ordered_fields } ) {
      return 0 if $field->needs_sync;
      # if e.g. a datetime field has an invalid value that can't be re-assembled
      # into a datetime object, the action may be in a consistent state but
      # not synchronized from the fields; in this case, we must not apply
    }
    return $self->action->can_apply;
  };

  implements do_apply => as {
    my $self = shift;
    return $self->action->do_apply;
  };

  implements ok => as {
    my $self = shift;
    if ($self->apply(@_)) {
      $self->close(@_);
    }
  };

  implements apply => as {
    my $self = shift;
    if ($self->can_apply && (my $result = $self->do_apply)) {
      $self->changed(0);
      $self->close_label($self->close_label_close);
      $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
      return 1;
    } else {
      $self->changed(1);
      $self->close_label($self->close_label_cancel);
      return 0;
    }
  };

  implements close => as {
    my $self = shift;
    my ($controller, $name, @args) = @{$self->next_action};
    $controller->pop_viewport;
    $controller->$name($self->action->ctx, @args);
  };

  sub can_close { 1 }

  override accept_events => sub {
    (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
  }; # can't do a close-type operation if there's nowhere to go afterwards

  override child_event_sinks => sub {
    my ($self) = @_;
    return ((grep { ref($_) =~ 'Hidden' } values %{$self->_field_map}),
            (grep { ref($_) !~ 'Hidden' } values %{$self->_field_map}),
            super());
  };

  after apply_child_events => sub {
    # interrupt here because fields will have been updated
    my ($self) = @_;
    $self->sync_action_from_fields;
  };

  implements sync_action_from_fields => as {
    my ($self) = @_;
    my $field_map = $self->_field_map;
    my @fields = values %{$field_map};
    foreach my $field (@fields) {
      $field->sync_to_action; # get the field to populate the $action if possible
    }
    $self->action->sync_all;
    foreach my $field (@fields) {
      $field->sync_from_action; # get errors from $action if applicable
    }
  };

  implements build_simple_field => as {
    my ($self, $class, $attr, $args) = @_;
    my $attr_name = $attr->name;
    my %extra;
    if (my $config = $args->{Field}{$attr_name}) {
      %extra = %$config;
    }
    my $field = $class->new(
                            action => $self->action,
                            attribute => $attr,
                            name => $attr->name,
                            location => join('-', $self->location, 'field', $attr->name),
                            ctx => $self->ctx,
                            %extra
                           );
    return ($attr_name => $field);
  };

  implements build_fields_for_type_Num => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Number, $attr, $args);
  };

  implements build_fields_for_type_Int => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Number, $attr, $args);
  };

  implements build_fields_for_type_Bool => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Boolean, $attr, $args);
  };

  implements build_fields_for_type_File => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(File, $attr, $args);
  };

  implements build_fields_for_type_Str => as {
    my ($self, $attr, $args) = @_;
    if ($attr->has_valid_values) { # There's probably a better way to do this
      return $self->build_simple_field(ChooseOne, $attr, $args);
    }
    return $self->build_simple_field(Text, $attr, $args);
  };

  implements build_fields_for_type_SimpleStr => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(String, $attr, $args);
  };

  implements build_fields_for_type_Password => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(Password, $attr, $args);
  };

  implements build_fields_for_type_DateTime => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(DateTime, $attr, $args);
  };

  implements build_fields_for_type_Enum => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(ChooseOne, $attr, $args);
  };

  #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
  implements build_fields_for_type_DBIx_Class_Row => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(ChooseOne, $attr, $args);
  };

  implements build_fields_for_type_ArrayRef => as {
    my ($self, $attr, $args) = @_;
    if ($attr->has_valid_values) {
      return $self->build_simple_field(ChooseMany, $attr, $args)
    } else {
      return $self->build_simple_field(HiddenArray, $attr, $args)
    }
  };

  implements build_fields_for_type_DateTime_Spanset => as {
    my ($self, $attr, $args) = @_;
    return $self->build_simple_field(TimeRange, $attr, $args);
  };

  no Moose;

  no strict 'refs';
  delete ${__PACKAGE__ . '::'}{inner};

};

  1;

=head1 NAME

Reaction::UI::ViewPort::ActionForm

=head1 SYNOPSIS

  use aliased 'Reaction::UI::ViewPort::ActionForm';

  $self->push_viewport(ActionForm,
    layout => 'register',
    action => $action,
    next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
    ctx => $c,
    column_order => [
      qw / contact_title company_name email address1 address2 address3
           city country post_code telephone mobile fax/ ],
  );

=head1 DESCRIPTION

This subclass of viewport is used for rendering a collection of
L<Reaction::UI::ViewPort::Field> objects for user editing.

=head1 ATTRIBUTES

=head2 action

L<Reaction::InterfaceModel::Action>

=head2 ok_label

Default: 'ok'

=head2 apply_label

Default: 'apply'

=head2 close_label_close

Default: 'close'

=head2 close_label_cancel

This label is only shown when C<changed> is true.

Default: 'cancel'

=head2 fields

=head2 can_apply

=head2 can_close

=head2 changed

Returns true if a field has been edited.

=head2 next_action

=head2 on_apply_callback

CodeRef.

=head1 METHODS

=head2 ok

Calls C<apply>, and then C<close> if successful.

=head2 close

Pop viewport and proceed to C<next_action>.

=head2 apply

Attempt to save changes and update C<changed> attribute if required.

=head1 SEE ALSO

L<Reaction::UI::ViewPort>

L<Reaction::InterfaceModel::Action>

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut