/[Frey]/branches/zimbardo/lib/Frey/Web.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /branches/zimbardo/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (show annotations)
Thu Jul 17 19:11:01 2008 UTC (15 years, 8 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 1766 byte(s)
call add_head all over the place, and include frey.css
1 package Frey::Web;
2 use Moose::Role;
3
4 use Continuity::Widget::DomNode;
5 use Data::Dump qw/dump/;
6 use Carp qw/confess/;
7
8 has 'head' => (
9 is => 'rw',
10 isa => 'ArrayRef[Str]',
11 default => sub { [ 'static/frey.css' ] },
12 );
13
14 sub dom2html {
15 # warn "## dom2html ",dump( @_ );
16 return Continuity::Widget::DomNode->create( @_ )->to_string;
17 }
18
19 sub _head_html {
20 my $self = shift;
21 my $out = '';
22 foreach my $path ( @{ $self->head } ) {
23 $path =~ s!^/!!;
24 if ( $path =~ m/\.js$/ ) {
25 $out .= qq|<script type="text/javascript" src="/$path"></script>|;
26 } elsif ( $path =~ m/\.css$/ ) {
27 $out .= qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
28 } else {
29 confess "don't know how to render $path";
30 }
31 }
32 return $out;
33 }
34
35 =head2 add_head
36
37 $o->add_head( 'path/to/external.js' );
38
39 my $size = $o->add_head( 'path/to/external.css' );
40
41 =cut
42
43 sub add_head {
44 my ( $self, $path ) = @_;
45 return if ! defined $path || $path eq '';
46 $path =~ s!^/!!;
47
48 if ( -e $path ) {
49 if ( $path =~ m/\.(?:js|css)$/ ) {
50 push @{ $self->head }, $path;
51 } else {
52 confess "can't add_head( $path ) it's not js or css";
53 }
54 } else {
55 confess "can't find $path: $!";
56 }
57
58 return -s $path;
59 }
60
61 our $reload_counter = 0;
62
63 sub page {
64 my $self = shift;
65 my $a = {@_};
66
67 $reload_counter++;
68
69 my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|
70 . $self->_head_html
71 . '<title>' . ( $a->{title} || ref($self) ) . '</title>'
72 . ( $a->{head} || '' )
73 . '</head><body>'
74 . ( $a->{body} || '<!-- no body -->' )
75 . qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|
76 . '</body></html>'
77 ;
78
79 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
80
81 return $html;
82 }
83
84 1;

  ViewVC Help
Powered by ViewVC 1.1.26