/[Frey]/trunk/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

Diff of /trunk/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 106 by dpavlin, Sun Jul 13 12:22:14 2008 UTC revision 183 by dpavlin, Tue Sep 9 23:14:31 2008 UTC
# Line 3  use Moose::Role; Line 3  use Moose::Role;
3    
4  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
5  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
6    use Carp qw/confess/;
7    use File::Slurp;
8    
9    has 'head' => (
10            is => 'rw',
11            isa => 'ArrayRef[Str]',
12            default => sub { [ 'static/frey.css' ] },
13    );
14    
15    has 'inline_smaller_than' => (
16            is => 'rw',
17            isa => 'Int',
18            default => 10240,
19    );
20    
21  sub dom2html {  sub dom2html {
22  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
23          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
24  }  }
25    
26  our @javascript = ( qw'  sub _inline_path {
27  ../lib/Joose.js          my ( $self, $path ) = @_;
28  ');          -s $path < $self->inline_smaller_than;
29    }
30    
31    sub _head_html {
32            my $self = shift;
33            my $out = '';
34            foreach my $path ( @{ $self->head } ) {
35                    $path =~ s!^/!!;
36                    if ( $path =~ m/\.js$/ ) {
37                            $out .= $self->_inline_path( $path ) ?
38                                    qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
39                                    qq|<script type="text/javascript" src="/$path"></script>|;
40                    } elsif ( $path =~ m/\.css$/ ) {
41                            $out .= $self->_inline_path( $path ) ?
42                                    qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
43                                    qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
44                    } else {
45                            confess "don't know how to render $path";
46                    }
47                    $out .= "\n";
48            }
49            return $out;
50    }
51    
52    =head2 add_head
53    
54      $o->add_head( 'path/to/external.js' );
55    
56      my $size = $o->add_head( 'path/to/external.css' );
57    
58    =cut
59    
60    sub add_head {
61            my ( $self, $path ) = @_;
62            return if ! defined $path || $path eq '';
63            $path =~ s!^/!!;
64    
65            if ( -e $path ) {
66                    if ( $path =~ m/\.(?:js|css)$/ ) {
67                            push @{ $self->head }, $path;
68                    } else {
69                            confess "can't add_head( $path ) it's not js or css";
70                    }
71            } else {
72                    confess "can't find $path: $!";
73            }
74    
75            return -s $path;
76    }
77    
78    our $reload_counter = 0;
79    
80    
81  sub head_javascript {  =head2 page
82    
83      $self->page(
84            title => 'page title',
85            head  => '<!-- optional head markup -->',
86            body  => '<b>Page Body</b>',
87      );
88    
89    =cut
90    
91    sub page {
92          my $self = shift;          my $self = shift;
93            my $a = {@_};
94    
95            $reload_counter++;
96    
97          my $js = Continuity::Widget::DomNode->create(          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|
98                  map {          . $self->_head_html
99                          ( script => { type => 'text/javascript', src => $_ } )          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'
100                  } @javascript          . ( $a->{head} || '' )
101          )->to_string;          . '</head><body>'
102            . ( $a->{body} || '<!-- no body -->' )
103            . qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|
104            . '</body></html>'
105            ;
106    
107          warn "# >>> js\n$js\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
108    
109          return $js;          return $html;
110  }  }
111    
112  1;  1;

Legend:
Removed from v.106  
changed lines
  Added in v.183

  ViewVC Help
Powered by ViewVC 1.1.26