/[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 417 by dpavlin, Tue Nov 18 16:39:13 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4    #with 'Frey::Escape';
5    
6    use Frey::Types;
7    
8  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10    use Carp qw/confess/;
11    use File::Slurp;
12    
13    use Frey::Bookmarklet;
14    use Frey::ClassBrowser;
15    
16    has 'head' => (
17            is => 'rw',
18            isa => 'ArrayRef[Str]',
19            default => sub { [ 'static/frey.css' ] },
20    );
21    
22    has 'status' => (
23            is => 'rw',
24            isa => 'ArrayRef[HashRef[Str]]',
25            lazy => 1,
26            default => sub { [
27                    { 'Bookmarklets' => Frey::Bookmarklet->new->markup },
28                    { 'ClassBrowser' => Frey::ClassBrowser->new->markup },
29            ] },
30    );
31    
32    has 'request_url' => (
33            is => 'rw',
34            isa => 'Uri', coerce => 1,
35            default => '/',
36    );
37    
38    =head2 inline_smaller_than
39    
40    Inline JavaScript and CSS smaller than this size into page reducing
41    round-trips to server.
42    
43    =cut
44    
45    has 'inline_smaller_than' => (
46            is => 'rw',
47            isa => 'Int',
48            default => 10240,
49    );
50    
51  sub dom2html {  sub dom2html {
52  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
53          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
54  }  }
55    
56  our @javascript = ( qw'  sub _inline_path {
57  ../lib/Joose.js          my ( $self, $path ) = @_;
58  ');          -s $path < $self->inline_smaller_than;
59    }
60    
61    sub _head_html {
62            my $self = shift;
63            my $out = '';
64            foreach my $path ( @{ $self->head } ) {
65                    $path =~ s!^/!!;
66                    if ( $path =~ m/\.js$/ ) {
67                            $out .= $self->_inline_path( $path ) ?
68                                    qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
69                                    qq|<script type="text/javascript" src="/$path"></script>|;
70                    } elsif ( $path =~ m/\.css$/ ) {
71                            $out .= $self->_inline_path( $path ) ?
72                                    qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
73                                    qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
74                    } else {
75                            confess "don't know how to render $path";
76                    }
77                    $out .= "\n";
78            }
79            return $out;
80    }
81    
82    =head2 add_head
83    
84      $o->add_head( 'path/to/external.js' );
85    
86      my $size = $o->add_head( 'path/to/external.css' );
87    
88    =cut
89    
90    sub add_head {
91            my ( $self, $path ) = @_;
92            return if ! defined $path || $path eq '';
93            $path =~ s!^/!!;
94    
95            if ( -e $path ) {
96                    if ( $path =~ m/\.(?:js|css)$/ ) {
97                            push @{ $self->head }, $path;
98                    } else {
99                            confess "can't add_head( $path ) it's not js or css";
100                    }
101            } else {
102                    confess "can't find $path: $!";
103            }
104    
105            return -s $path;
106    }
107    
108    our $reload_counter = 0;
109    
110    
111    =head2 page
112    
113  sub head_javascript {    $self->page(
114            title => 'page title',
115            head  => '<!-- optional head markup -->',
116            body  => '<b>Page Body</b>',
117      );
118    
119    =cut
120    
121    sub page {
122          my $self = shift;          my $self = shift;
123            my $a = {@_};
124    
125            $reload_counter++;
126    
127          my $js = Continuity::Widget::DomNode->create(          my $status_line = '';
128                  map {          foreach my $part ( @{ $self->status } ) {
129                          ( script => { type => 'text/javascript', src => $_ } )                  confess "part not hash ",dump( $part ) unless ref($part) eq 'HASH';
130                  } @javascript                  foreach my $name ( keys %$part ) {
131          )->to_string;                          my $content = $part->{$name};
132                            if ( ref($content) ) {
133                                    $content = '<code>' . dump($content) . '</code>';
134                                    my $l = length($content);
135                                    $content = qq|<span>$l bytes</span>| if $l > 1024;
136                            } else {
137                                    $content = qq|<span>$content</span>|;
138                            }
139                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
140                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
141                    }
142            }
143    
144            my $html = join("\n",
145                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
146                    $self->_head_html,
147                    '<title>' . ( $a->{title} || ref($self) ) . '</title>',
148                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
149                    ( $a->{head} || '' ),
150                    '</head><body>',
151                    ( $a->{body} || '<!-- no body -->' ),
152                    qq|
153                    <div class="frey-status-line">
154                            <a href="/">Frey</a> $Frey::VERSION
155                            <a href="?reload=$reload_counter"><code>| . $self->request_url . qq|</code></a>
156                            $status_line
157                    </div>
158                </body></html>
159                    |,
160            );
161    
162          warn "# >>> js\n$js\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
163    
164          return $js;          return $html;
165    }
166    
167    sub error {
168            my $self = shift;
169            my $error = join(" ", @_);
170            my ($package, $filename, $line) = caller;
171            $error .= " at $filename line $line" if $error !~ m{ at };
172            warn "WARN: $error\n";
173            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
174            $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
175            return qq|<pre class="frey-error">$error</pre>|;
176  }  }
177    
178  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26