Commit | Line | Data |
7adfd53f |
1 | package Reaction::UI::ViewPort::Field::DateTime; |
2 | |
3 | use Reaction::Class; |
4 | use Reaction::Types::DateTime; |
5 | use Time::ParseDate (); |
6 | |
7 | class DateTime is 'Reaction::UI::ViewPort::Field', which { |
8 | |
9 | has '+value' => (isa => 'DateTime'); |
6ab43711 |
10 | |
11 | #has '+layout' => (default => 'dt_textfield'); |
12 | |
7adfd53f |
13 | has value_string => ( |
14 | isa => 'Str', is => 'rw', lazy_build => 1, |
15 | trigger_adopt('value_string') |
16 | ); |
6ab43711 |
17 | |
7adfd53f |
18 | has value_string_default_format => ( |
19 | isa => 'Str', is => 'rw', required => 1, default => sub { "%F %H:%M:%S" } |
20 | ); |
6ab43711 |
21 | |
89939ff9 |
22 | implements _build_value_string => as { |
7adfd53f |
23 | my $self = shift; |
24 | |
25 | # XXX |
26 | #<mst> aha, I know why the fucker's lazy |
27 | #<mst> it's because if value's calculated |
28 | #<mst> it needs to be possible to clear it |
29 | #<mst> eval { $self->value } ... is probably the best solution atm |
30 | my $value = eval { $self->value }; |
31 | return '' unless $self->has_value; |
6ab43711 |
32 | my $format = $self->value_string_default_format; |
7adfd53f |
33 | return $value->strftime($format) if $value; |
34 | return ''; |
35 | }; |
6ab43711 |
36 | |
7adfd53f |
37 | implements adopt_value_string => as { |
38 | my ($self) = @_; |
39 | my $value = $self->value_string; |
40 | my ($epoch) = Time::ParseDate::parsedate($value, UK => 1); |
41 | if (defined $epoch) { |
42 | my $dt = 'DateTime'->from_epoch( epoch => $epoch ); |
43 | $self->value($dt); |
44 | } else { |
45 | $self->message("Could not parse date or time"); |
46 | $self->clear_value; |
47 | $self->needs_sync(1); |
48 | } |
49 | }; |
6ab43711 |
50 | |
7adfd53f |
51 | override accept_events => sub { |
52 | ('value_string', super()); |
53 | }; |
54 | |
55 | }; |
56 | |
6ab43711 |
57 | 1; |
7adfd53f |
58 | |
59 | =head1 NAME |
60 | |
61 | Reaction::UI::ViewPort::Field::DateTime |
62 | |
63 | =head1 DESCRIPTION |
64 | |
65 | =head1 METHODS |
66 | |
67 | =head2 value_string |
68 | |
69 | Accessor for the string representation of the DateTime object. |
70 | |
71 | =head2 value_string_default_format |
72 | |
73 | By default it is set to "%F %H:%M:%S". |
74 | |
75 | =head1 SEE ALSO |
76 | |
77 | =head2 L<DateTime> |
78 | |
79 | =head2 L<Reaction::UI::ViewPort::Field> |
80 | |
81 | =head1 AUTHORS |
82 | |
83 | See L<Reaction::Class> for authors. |
84 | |
85 | =head1 LICENSE |
86 | |
87 | See L<Reaction::Class> for the license. |
88 | |
89 | =cut |