aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/Field.pm
blob: f0fa0f0d7ea3b2a8a81af84e624388ea86ad15d7 (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
package Reaction::UI::ViewPort::Field;

use Reaction::Class;

class Field is 'Reaction::UI::ViewPort', which {

  has name => (
    isa => 'Str', is => 'rw', required => 1
  );

  has action => (
    isa => 'Reaction::InterfaceModel::Action',
    is => 'ro', required => 0, predicate => 'has_action',
  );

  has attribute => (
    isa => 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute',
    is => 'ro', predicate => 'has_attribute',
  );

  has value => (
    is => 'rw', lazy_build => 1, trigger_adopt('value'),
    clearer => 'clear_value',
  );

  has needs_sync => (
    isa => 'Int', is => 'rw', default => 0
  );

  has label => (isa => 'Str', is => 'rw', lazy_build => 1);

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

  implements BUILD => as {
    my ($self) = @_;
    if (!$self->has_attribute != !$self->has_action) {
      confess "Should have both action and attribute or neither";
    }
  };

  implements _build_label => as {
    my ($self) = @_;
    my $label = join(' ', map { ucfirst } split('_', $self->name));
    # print STDERR "Field " . $self->name . " has label '$label'\n";
    return $label;
  };

  implements _build_value => as {
    my ($self) = @_;
    if ($self->has_attribute) {
      my $reader = $self->attribute->get_read_method;
      my $predicate = $self->attribute->predicate;
      if (!$predicate || $self->action->$predicate) {
        return $self->action->$reader;
      }
    }
    return '';
  };

  implements adopt_value => as {
    my ($self) = @_;
    $self->needs_sync(1) if $self->has_attribute;
  };

  implements sync_to_action => as {
    my ($self) = @_;
    return unless $self->needs_sync && $self->has_attribute && $self->has_value;
    my $attr = $self->attribute;
    if (my $tc = $attr->type_constraint) {
      my $value = $self->value;
      if ($tc->has_coercion) {
        $value = $tc->coercion->coerce($value);
      }
      my $error = $tc->validate($self->value);
      if (defined $error) {
        $self->message($error);
        return;
      }
    }
    my $writer = $attr->get_write_method;
    confess "No writer for attribute" unless defined($writer);
    $self->action->$writer($self->value);
    $self->needs_sync(0);
  };

  implements sync_from_action => as {
    my ($self) = @_;
    return unless !$self->needs_sync && $self->has_attribute;
    $self->message($self->action->error_for($self->attribute)||'');
  };

  override accept_events => sub { ('value', super()) };

};

1;

=head1 NAME

Reaction::UI::ViewPort::Field

=head1 DESCRIPTION

This viewport is the base class for all field types.

=head1 ATTRIBUTES

=head2 name

=head2 action

L<Reaction::InterfaceModel::Action>

=head2 attribute

L<Reaction::Meta::InterfaceModel::Action::ParameterAttribute>

=head2 value

=head2 needs_sync

=head2 label

User friendly label, by default is based on the name.

=head2 message

Optional string relating to the field.

=head1 SEE ALSO

=head2 L<Reaction::UI::ViewPort>

=head2 L<Reaction::UI::ViewPort::DisplayField>

=head2 L<Reaction::UI::ViewPort::Field::Boolean>

=head2 L<Reaction::UI::ViewPort::Field::ChooseMany>

=head2 L<Reaction::UI::ViewPort::Field::ChooseOne>

=head2 L<Reaction::UI::ViewPort::Field::DateTime>

=head2 L<Reaction::UI::ViewPort::Field::File>

=head2 L<Reaction::UI::ViewPort::Field::HiddenArray>

=head2 L<Reaction::UI::ViewPort::Field::Number>

=head2 L<Reaction::UI::ViewPort::Field::Password>

=head2 L<Reaction::UI::ViewPort::Field::String>

=head2 L<Reaction::UI::ViewPort::Field::Text>

=head2 L<Reaction::UI::ViewPort::Field::TimeRange>

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

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

=cut