Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add time widget (date widget without date)
- Loading branch information
1 parent
8fd58e9
commit 9891400
Showing
1 changed file
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
CodeDef time Widget 1 | ||
CodeDef time Description Time selector | ||
CodeDef time Routine <<EOR | ||
sub { | ||
my($opt) = @_; | ||
|
||
my $name = $opt->{name}; | ||
my $val = $opt->{value}; | ||
|
||
my $now; | ||
if($opt->{time} and $opt->{time_adjust} =~ /([-+]?)(\d+)/) { | ||
my $sign = $1 || '+'; | ||
my $adjust = $2; | ||
$adjust *= 3600; | ||
$now = time; | ||
$now += $sign eq '+' ? $adjust : -$adjust; | ||
} | ||
|
||
my $sel_extra; | ||
my $opt_extra; | ||
for(qw/ class style extra /) { | ||
my $stag = "select_$_"; | ||
my $otag = "option_$_"; | ||
my $selapp; | ||
my $optapp; | ||
|
||
if($_ eq 'extra') { | ||
$selapp = " $opt->{$stag}"; | ||
$optapp = " $opt->{$otag}"; | ||
} | ||
else { | ||
$selapp = qq{ $_="$opt->{$stag}"}; | ||
$optapp = qq{ $_="$opt->{$otag}"}; | ||
} | ||
$sel_extra .= $opt->{$stag} ? $selapp : ''; | ||
$opt_extra .= $opt->{$otag} ? $optapp : ''; | ||
} | ||
|
||
my @t = localtime($now || time); | ||
if (not $val) { | ||
$t[2]++ if $t[2] < 23; | ||
$val = POSIX::strftime("%H00", @t); | ||
} | ||
|
||
my $sel = 0; | ||
my $out = qq{<SELECT NAME="$name"$sel_extra>}; | ||
my $o; | ||
if ($opt->{blank}) { | ||
$out .= '<OPTION VALUE="0"$opt_extra>------</OPTION>'; | ||
} | ||
|
||
$val = Vend::Form::round_to_fifteen($val); | ||
|
||
if ($opt->{blank}) { | ||
$out .= '<OPTION VALUE="0"$opt_extra>--:--</OPTION>'; | ||
} | ||
|
||
my $ampm = defined $opt->{ampm} ? $opt->{ampm} : 1; | ||
my $mod = ''; | ||
undef $sel; | ||
my %special = qw/ 0 midnight 12 noon /; | ||
|
||
my @min; | ||
|
||
$opt->{minutes} ||= ''; | ||
|
||
if($opt->{minutes} =~ /half/i) { | ||
@min = (0,30); | ||
} | ||
elsif($opt->{minutes} =~ /hourly/i) { | ||
@min = (0); | ||
} | ||
elsif($opt->{minutes} =~ /ten/i) { | ||
@min = (0,10,20,30,40,50); | ||
} | ||
elsif($opt->{minutes} =~ /[\0,]/) { | ||
@min = grep /^\d+$/ && $_ <= 59, split /[\0,\s]+/, $opt->{minutes}; | ||
} | ||
else { | ||
@min = (0,15,30,45); | ||
} | ||
|
||
$opt->{start_hour} ||= 0; | ||
for(qw/start_hour end_hour/) { | ||
$opt->{$_} = int(abs($opt->{$_})); | ||
if($opt->{$_} > 23) { | ||
$opt->{$_} = 0; | ||
} | ||
} | ||
$opt->{start_hour} ||= 0; | ||
$opt->{end_hour} ||= 23; | ||
|
||
for my $hr ( $opt->{start_hour} .. $opt->{end_hour} ) { | ||
next if defined $opt->{start_hour} and $hr < $opt->{start_hour}; | ||
next if defined $opt->{end_hour} and $hr > $opt->{end_hour}; | ||
for my $min ( @min ) { | ||
my $disp_hour = $hr; | ||
if($opt->{ampm}) { | ||
if( $hr < 12) { | ||
$mod = 'am'; | ||
} | ||
else { | ||
$mod = 'pm'; | ||
$disp_hour = $hr - 12 unless $hr == 12; | ||
} | ||
$mod = errmsg($mod); | ||
$mod = " $mod"; | ||
} | ||
if($special{$hr} and $min == 0) { | ||
$disp_hour = errmsg($special{$hr}); | ||
} | ||
elsif($ampm) { | ||
$disp_hour = sprintf("%2d:%02d%s", $disp_hour, $min, $mod); | ||
} | ||
else { | ||
$disp_hour = sprintf("%02d:%02d", $hr, $min); | ||
} | ||
my $time = sprintf "%02d%02d", $hr, $min; | ||
$o = sprintf qq{<OPTION VALUE="%s"$opt_extra>%s}, $time, $disp_hour; | ||
($out .= $o, next) unless ! $sel and $val; | ||
#::logDebug("prospect=$time actual=$val"); | ||
$o =~ s/>/ SELECTED>/ && $sel++ | ||
if $val eq $time; | ||
$out .= $o; | ||
} | ||
} | ||
$out .= "</SELECT>"; | ||
return $out; | ||
} | ||
EOR |