From 7f898c439eb139537545eda6eb3ccc580ac8460b Mon Sep 17 00:00:00 2001 From: Yorhel Date: Wed, 12 Nov 2008 12:41:37 +0100 Subject: Created VNDB::Func for a few small and often used functions, split CommonHTML in LayoutHTML, and abstracted the browser generation I was planning to commit often, but I still end up doing everything in one commit >.> --- lib/VNDB/Func.pm | 26 +++++ lib/VNDB/Handler/Users.pm | 84 ++++------------ lib/VNDB/Util/CommonHTML.pm | 227 ++++++++++++++++++-------------------------- lib/VNDB/Util/LayoutHTML.pm | 146 ++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 202 deletions(-) create mode 100644 lib/VNDB/Func.pm create mode 100644 lib/VNDB/Util/LayoutHTML.pm (limited to 'lib/VNDB') diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm new file mode 100644 index 00000000..9e948e0d --- /dev/null +++ b/lib/VNDB/Func.pm @@ -0,0 +1,26 @@ + +package VNDB::Func; + +use strict; +use warnings; +use Exporter 'import'; +use POSIX 'strftime'; +our @EXPORT = qw| shorten date |; + + +# I would've done this as a #define if this was C... +sub shorten { + my($str, $len) = @_; + return length($str) > $len ? substr($str, 0, $len-3).'...' : $str; +} + + +# argument: unix timestamp +# return value: yyyy-mm-dd +# (maybe an idea to use cgit-style ages for recent timestamps) +sub date { + return strftime '%Y-%m-%d', gmtime shift; +} + + +1; diff --git a/lib/VNDB/Handler/Users.pm b/lib/VNDB/Handler/Users.pm index 9a8929df..1dc014d3 100644 --- a/lib/VNDB/Handler/Users.pm +++ b/lib/VNDB/Handler/Users.pm @@ -5,7 +5,7 @@ use strict; use warnings; use YAWF ':html'; use Digest::MD5 'md5_hex'; -use POSIX 'strftime'; +use VNDB::Func; YAWF::register( @@ -339,54 +339,25 @@ sub list { page => $f->{p}, ); - if($f->{p} > 1 || $np) { - ul class => 'maintabs notfirst'; - if($f->{p} > 1) { - li class => 'left'; - a href => "/u/list/$char?o=$f->{o}&s=$f->{s}&p=".($f->{p}-1), '<- previous'; - end; - } - if($np) { - li; - a href => "/u/list/$char?o=$f->{o}&s=$f->{s}&p=".($f->{p}+1), 'next ->'; - end; - } - end; - } - - div class => 'mainbox browse'; - table; - thead; - Tr; - td class => 'tc1'; - txt 'Username '; - lit $f->{s} eq 'username' && $f->{o} eq 'a' ? "\x{25B4}" : qq|\x{25B4}|; - lit $f->{s} eq 'username' && $f->{o} eq 'd' ? "\x{25BE}" : qq|\x{25BE}|; - end; - td class => 'tc2'; - txt 'Registered '; - lit $f->{s} eq 'registered' && $f->{o} eq 'a' ? "\x{25B4}" : qq|\x{25B4}|; - lit $f->{s} eq 'registered' && $f->{o} eq 'd' ? "\x{25BE}" : qq|\x{25BE}|; - end; - td class => 'tc3'; - txt 'Votes '; - lit $f->{s} eq 'votes' && $f->{o} eq 'a' ? "\x{25B4}" : qq|\x{25B4}|; - lit $f->{s} eq 'votes' && $f->{o} eq 'd' ? "\x{25BE}" : qq|\x{25BE}|; - end; - td class => 'tc4'; - txt 'Edits '; - lit $f->{s} eq 'changes' && $f->{o} eq 'a' ? "\x{25B4}" : qq|\x{25B4}|; - lit $f->{s} eq 'changes' && $f->{o} eq 'd' ? "\x{25BE}" : qq|\x{25BE}|; - end; - end; - end; - for(0..$#$list) { - my $l = $list->[$_]; - Tr $_ % 2 ? (class => 'odd') : (); + $self->htmlBrowse( + items => $list, + options => $f, + nextpage => $np, + pageurl => "/u/list/$char?o=$f->{o}&s=$f->{s}", + sorturl => "/u/list/$char", + header => [ + [ 'Username', 'username' ], + [ 'Registered', 'registered' ], + [ 'Votes', 'votes' ], + [ 'Edits', 'changes' ], + ], + row => sub { + my($s, $n, $l) = @_; + Tr $n % 2 ? (class => 'odd') : (); td class => 'tc1'; a href => '/u'.$l->{id}, $l->{username}; end; - td class => 'tc2', strftime '%Y-%m-%d', gmtime $l->{registered}; + td class => 'tc2', date $l->{registered}; td class => 'tc3'; lit !$l->{show_list} ? '-' : !$l->{c_votes} ? 0 : qq|$l->{c_votes}|; @@ -395,25 +366,8 @@ sub list { lit !$l->{c_changes} ? 0 : qq|$l->{c_changes}|; end; end; - } - end; - end; - - if($f->{p} > 1 || $np) { - ul class => 'maintabs bottom'; - if($f->{p} > 1) { - li class => 'left'; - a href => "/u/list/$char?o=$f->{o}&s=$f->{s}&p=".($f->{p}-1), '<- previous'; - end; - } - if($np) { - li; - a href => "/u/list/$char?o=$f->{o}&s=$f->{s}&p=".($f->{p}+1), 'next ->'; - end; - } - end; - } - + }, + ); $self->htmlFooter; } diff --git a/lib/VNDB/Util/CommonHTML.pm b/lib/VNDB/Util/CommonHTML.pm index 1f0c3edb..bb385916 100644 --- a/lib/VNDB/Util/CommonHTML.pm +++ b/lib/VNDB/Util/CommonHTML.pm @@ -6,143 +6,7 @@ use warnings; use YAWF ':html'; use Exporter 'import'; -our @EXPORT = qw| - htmlHeader htmlFooter htmlMainTabs htmlDenied -|; - - -sub htmlHeader { # %options->{ title } - my($self, %o) = @_; - - # heading - html; - head; - title $o{title}; - Link rel => 'shortcut icon', href => '/favicon.ico', type => 'image/x-icon'; - Link rel => 'stylesheet', href => $self->{url_static}.'/f/style.css', type => 'text/css', media => 'all'; - script type => 'text/javascript', src => $self->{url_static}.'/f/script.js'; - # most browsers don't like a self-closing