Subject: | PATCH: CSS support/remove most CGI.pm usage |
Here's a patch that uses the calendar style from the CSS Cookbook. It removes most of the CGI.pm HTML function usage, but that was not my primary goal. I've included a default_styles() method to give a sample usage of customizing the CSS rules. If you are interested in applying the patch I'll be happy to fix the docs as well.
Drew
diff -u Simple.pm.orig Simple.pm
--- Simple.pm.orig Mon Oct 25 21:29:06 2004
+++ Simple.pm Tue Oct 26 01:09:45 2004
@@ -95,7 +95,10 @@
my $ref = shift;
if (defined $ref && ref $ref eq 'HASH') {
my $month = exists $ref->{month} ? $ref->{month} : $valid_day->month;
- my $year = exists $ref->{year} ? $ref->{year} : $valid_day->year;
+ my $year = exists $ref->{year} ? $ref->{year} : $valid_day->year;
+ $self->{this_day} = $valid_day->day;
+ $self->{this_month} = $valid_day->month;
+ $self->{this_year} = $valid_day->year;
$valid_day = $self->_date_obj($year, $month, 1);
$valid_day = defined $valid_day ? $valid_day : Date::Simple->new;
}
@@ -121,8 +124,14 @@
sub month { $_[0]->{month} } # month in numerical format
sub year { $_[0]->{year} } # year in YYYY form
-sub _spacer { return "" } # the filler for the first few entries
+sub _spacer { return ""; } # the filler for the first few entries
sub _the_month { @{ $_[0]->{the_month} } } # this is the list of hashrefs.
+sub empty_cell {
+ my $self = shift;
+ my $weekend = shift || 0;
+ my $wc = $weekend ? qq(class="hcs_weekend") : '';
+ return qq( <td $wc><div class="hcs_emptydate"> </div></td>);
+}
sub _cgi {
my $self = shift;
@@ -189,44 +198,61 @@
next unless $day_ref && $day_ref->{date} == $day;
$day_ref->{$_} = $info{$_} foreach keys %info;
last;
+ $count++;
}
}
-# Glerg. Make each cell in the calendar table a table of its own. And each row
-# of this table will contain a little snippet of information.
+sub _link {
+ my $self = shift;
+ my $ref = shift or return;
+ ref $ref eq 'ARRAY' or return;
+ my ($link, $tag) = @$ref;
+ return qq(<a href="$link" class="hcs_event">$tag</a>);
+ #$self->_cgi->a({ -href => $link }, $tag);
+}
sub _row_elem {
my $self = shift;
- my $ref = shift or return $self->_spacer;
+ my $ref = shift or return $self->empty_cell; #_spacer
return $ref if $ref eq $self->_spacer;
my $q = $self->_cgi;
- my $day = exists $ref->{day_link}
- ? $q->a({ -href => $ref->{day_link} }, $ref->{date}->day)
- : $ref->{date}->day;
- my $elem = $q->start_table . $q->Tr($q->td($day));
+
+ # general cell info
+ my $day = $ref->{date}->day;
+ my $month = $ref->{date}->month;
+ my $year = $ref->{date}->year;
+ my $day_link = $ref->{day_link} || '#';
+ my $day_of_week = $ref->{date}->day_of_week;
+
+ # setup the cell classes
+ my ($td_class, $td_id) = ('','');
+ if ($day_of_week==0 || $day_of_week == 6) {
+ $td_class = ' class="weekend"';
+ }
+ if ($day == $self->{this_day} && $month == $self->{this_month} &&
+ $year == $self->{this_year}) {
+ $td_id = qq( id="today");
+ }
+
+ my $day = qq(<a href="$day_link" class="hcs_date">$day</a>);
+ my $elem = qq( <td).$td_id.$td_class.qq(><div>$day</div>);
+
my %info = %{ $ref };
foreach my $key (keys %info) {
next if ($key eq 'date' or $key eq 'day_link');
my $method = "_$key";
$elem .= $self->can($method)
- ? $q->Tr($q->td($self->$method($info{$key})))
- : $q->Tr($q->td($info{$key}));
+ ? $self->$method($info{$key})
+ : $info{$key};
}
- $elem .= $q->end_table;
+ $elem .= "</td>";
return $elem;
}
-sub _link {
- my $self = shift;
- my $ref = shift or return;
- ref $ref eq 'ARRAY' or return;
- my ($link, $tag) = @$ref;
- return $self->_cgi->a({ -href => $link }, $tag);
-}
-
sub _table_row {
my $self = shift;
- my @week = @_; my @row;
+ my @week = @_;
+ my @row;
push @row, $self->_row_elem($_) foreach @week;
return @row;
}
@@ -239,7 +265,7 @@
calendar I have hanging up in my kitchen, (It is a cat calendar, if
you are interested, as my second son loves cats. As do I!)
-This could be used to have a mechanic's garage Pirelli-style pr0n
+This could be used to have a mechanics garage Pirelli-style pr0n
calendar, but that would be your call. Mine would be something including
a Triumph Daytona 955i. Mmmm, nice.
@@ -278,18 +304,47 @@
my $self = shift;
my @seq = $self->_the_month;
my $q = $self->_cgi;
- my $mnth = $q->h3($months{$self->month} . " " . $self->year);
- my $cal = $q->start_table({-border => 1})
- . $q->th([sort { $days{$a} <=> $days{$b} } keys %days]);
+ my $mnth = $months{$self->month} . " " . $self->year;
+ my $cal = qq(<table class="hcs_table">\n);
+ $cal .= qq(<tr><td id="hcs_month" colspan="7">$mnth</td></tr>\n);
+ $cal .= qq(<tr id="hcs_days">\n);
+ $cal .= join("\n", map { " <th>$_</th>" }
+ sort { $days{$a} <=> $days{$b} }
+ keys %days);
+ $cal .= "\n</tr>\n";
+
while (@seq) {
my @week_row = $self->_table_row(splice @seq, 0, 7);
- $cal .= $q->Tr($q->td([@week_row]));
+ $cal .= "<tr>\n".join("\n ", @week_row)."</tr>\n";
}
$cal .= $q->end_table;
- $cal = $q->start_table . $q->Tr($q->td({ align => 'center' }, $mnth))
- . $q->Tr($q->td($cal)) . $q->end_table;
$cal = $self->_add_pic($cal) if $self->picture;
return $cal;
+}
+
+
+sub default_styles {
+ return <<STYLE;
+<style type="text/css">
+table.hcs_table { width: 95%; margin-left: 5%; text-decoration: none; border: 1px solid black; }
+table.hcs_table td { width: 14%; background-color: #ccc; border: 1px solid white;
+ vertical-align: top; font-size: .8em; }
+#today { border: 2px solid #792720; }
+#hcs_month { font-size: 2em; text-align: center; padding: none; margin: none;
+ font-family: monospace; color: #792720; }
+#hcs_days { background-color: #792720; color: white; width: 75px; font-size: .8em; }
+.hcs_date { border-right: 1px solid black; border-bottom: 1px solid black;
+ font-family: monospace; text-decoration: none; float: left;
+ width: 1.5em; height: 1.5em; background-color: white;
+ text-align: center; font-size: 1em; }
+.hcs_emptydate { border-right: 1px solid #666; border-bottom: 1px solid #666;
+ font-family: monospace; text-decoration: none; float: left;
+ width: 1.5em; height: 1.5em; background-color: #ccc;
+ text-align: center; }
+.hcs_event { clear: both; padding-left: .5em; padding-bottom: .5em;
+ display: block; }
+</style>
+STYLE
}
=head2 calendar_year