--- trunk/lib/Frey/Web.pm 2008/07/16 14:14:18 142 +++ trunk/lib/Frey/Web.pm 2008/11/19 17:57:48 460 @@ -1,70 +1,234 @@ package Frey::Web; use Moose::Role; -use MooseX::AttributeHelpers; + +use Frey::Types; use Continuity::Widget::DomNode; use Data::Dump qw/dump/; use Carp qw/confess/; +use File::Slurp; + +use Frey::Bookmarklet; +use Frey::ClassBrowser; -has 'javascript' => ( - metaclass => 'Collection::Array', +has 'head' => ( is => 'rw', isa => 'ArrayRef[Str]', - default => sub { [] }, - provides => { - 'push' => 'add_javascript', - }, + default => sub { [ 'static/frey.css' ] }, ); -has 'css' => ( - metaclass => 'Collection::Array', +has 'status' => ( is => 'rw', - isa => 'ArrayRef[Str]', - default => sub { [ 'static/app.css' ] }, - provides => { - 'push' => 'add_css', + isa => 'ArrayRef[HashRef[Str]]', + lazy => 1, + default => sub { [ + { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, + { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, + ] }, +); + +has 'request_url' => ( + is => 'rw', + isa => 'Uri', coerce => 1, + default => '/', +); + +has 'title' => ( + is => 'rw', + isa => 'Str', + lazy => 1, + default => sub { + my ($self) = @_; + ref($self); }, ); +has 'content_type' => ( + is => 'rw', + isa => 'Str', + default => 'text/html', +); + +=head2 inline_smaller_than + +Inline JavaScript and CSS smaller than this size into page reducing +round-trips to server. + +=cut + +has 'inline_smaller_than' => ( + is => 'rw', + isa => 'Int', + default => 10240, +); + sub dom2html { # warn "## dom2html ",dump( @_ ); return Continuity::Widget::DomNode->create( @_ )->to_string; } -sub _unroll_markup { -# warn "## _unroll_markup ",dump( @_ ); - my ( $markup, $array ) = @_; +sub _inline_path { + my ( $self, $path ) = @_; + -s $path < $self->inline_smaller_than; +} + +sub _head_html { + my $self = shift; my $out = ''; - foreach my $path ( @$array ) { + foreach my $path ( @{ $self->head } ) { $path =~ s!^/!!; - confess "can't find $path" unless -e $path; - $out .= sprintf( $markup, $path ); + if ( $path =~ m/\.js$/ ) { + $out .= $self->_inline_path( $path ) ? + qq|| : + qq||; + } elsif ( $path =~ m/\.css$/ ) { + $out .= $self->_inline_path( $path ) ? + qq|| : + qq||; + } elsif ( $path =~ m{<.+>}s ) { + $out .= $path; + } else { + confess "don't know how to render $path"; + } + $out .= "\n"; } return $out; } +=head2 add_head + + $o->add_head( 'path/to/external.js' ); + + my $size = $o->add_head( 'path/to/external.css' ); + + $o->add_head( '' ); + +=cut + +sub add_head { + my ( $self, $path ) = @_; + return if ! defined $path || $path eq ''; + $path =~ s!^/!!; + + if ( $path =~ m{<.*>}s ) { + push @{ $self->head }, $path; + } elsif ( -e $path ) { + if ( $path =~ m/\.(?:js|css)$/ ) { + push @{ $self->head }, $path; + } else { + confess "can't add_head( $path ) it's not js or css"; + } + return -s $path; + } else { + confess "can't find $path: $!"; + } + +} + our $reload_counter = 0; + +=head2 page + + $self->page( + title => 'page title', + head => '', + body => 'Page Body', + ); + +=cut + sub page { my $self = shift; my $a = {@_}; $reload_counter++; - my $html = qq|| - . _unroll_markup( qq||, $self->javascript ) - . _unroll_markup( qq||, $self->css ) - . '' . ( $a->{title} || ref($self) ) . '' - . ( $a->{head} || '' ) - . '' - . ( $a->{body} || '' ) - . qq|
Frey $Frey::VERSION reload
| - . '' - ; + my $status_line = ''; + foreach my $part ( @{ $self->status } ) { + if ( ref($part) ne 'HASH' ) { + warn "part not hash ",dump( $part ) ; + #$self->status( $part ); + next; + } + foreach my $name ( keys %$part ) { + my $content = $part->{$name}; + if ( ref($content) ) { + $content = '' . dump($content) . ''; + my $l = length($content); + $content = qq|$l bytes| if $l > 1024; + } else { + $content = qq|$content|; + } + warn "### part [$name] = ", length( $content ), " bytes" if $self->debug; + $status_line .= qq|$name $content\n|; + } + } + + my $url = $self->request_url; + $url =~ s{\?reload=\d+}{}; + + my $body = $a->{body}; + $body ||= $self->as_markup if $self->can('as_markup'); + if ( $self->content_type !~ m{html} ) { + warn "# return only $self body ", $self->content_type; + return $body + } elsif ( ! defined $body ) { + warn "# no body"; + $body = ''; + } + + my $html = join("\n", + qq||, + $self->_head_html, + '' . ( $self->title || $a->{title} || ref($self) ) . '', + '', + ( $a->{head} || '' ), + qq| + + $body +
+ Frey $Frey::VERSION + $url + $status_line +
+ + |, + ); warn "## >>> page ",length($html), " bytes\n" if $self->debug; return $html; } +sub error { + my $self = shift; + my $error = join(" ", @_); + + my @backtrace; + foreach ( 0 .. 5 ) { + my @caller = caller($_) or last; + my @description = ( qw/ + package filename line + subroutine hasargs + wantarray evaltext is_require + hints bitmask hinthash + /); + push @backtrace, join(' ', + map { + $description[$_] . ': ' . dump $caller[$_] + } ( 0 .. $#caller ) + ); + } + if ( @backtrace ) { + warn "# append backtrace: ", dump( @backtrace ); + $error .= "\n\t" . join( "\n\t", @backtrace ); + } + + warn "ERROR: $error\n"; + $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at $1 line $2}gsm; + $error =~ s{(via package ")([\w:]+)(")}{$1$2$3}gsm; + return qq|
$error
|; +} + 1;