--- trunk/lib/Frey/Web.pm 2008/07/16 14:14:18 142 +++ trunk/lib/Frey/Web.pm 2008/10/30 22:35:11 206 @@ -1,29 +1,28 @@ package Frey::Web; use Moose::Role; -use MooseX::AttributeHelpers; use Continuity::Widget::DomNode; use Data::Dump qw/dump/; use Carp qw/confess/; +use File::Slurp; -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', +=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 => 'ArrayRef[Str]', - default => sub { [ 'static/app.css' ] }, - provides => { - 'push' => 'add_css', - }, + isa => 'Int', + default => 10240, ); sub dom2html { @@ -31,20 +30,73 @@ 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||; + } 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' ); + +=cut + +sub add_head { + my ( $self, $path ) = @_; + return if ! defined $path || $path eq ''; + $path =~ s!^/!!; + + if ( -e $path ) { + if ( $path =~ m/\.(?:js|css)$/ ) { + push @{ $self->head }, $path; + } else { + confess "can't add_head( $path ) it's not js or css"; + } + } else { + confess "can't find $path: $!"; + } + + return -s $path; +} + our $reload_counter = 0; + +=head2 page + + $self->page( + title => 'page title', + head => '', + body => 'Page Body', + ); + +=cut + +use Frey::Bookmarklet; + sub page { my $self = shift; my $a = {@_}; @@ -52,13 +104,15 @@ $reload_counter++; my $html = qq|| - . _unroll_markup( qq||, $self->javascript ) - . _unroll_markup( qq||, $self->css ) + . $self->_head_html . '' . ( $a->{title} || ref($self) ) . '' . ( $a->{head} || '' ) . '' . ( $a->{body} || '' ) - . qq|
Frey $Frey::VERSION reload
| + . qq|
+ Frey $Frey::VERSION + reload + Bookmarklets| . Frey::Bookmarklet->markup . qq|| . '' ;