--- trunk/lib/Frey/Web.pm 2008/07/13 12:22:14 106 +++ trunk/lib/Frey/Web.pm 2008/07/17 17:04:21 154 @@ -1,30 +1,98 @@ package Frey::Web; use Moose::Role; +use MooseX::AttributeHelpers; use Continuity::Widget::DomNode; use Data::Dump qw/dump/; +use Carp qw/confess/; + +has 'js' => ( + metaclass => 'Collection::Array', + is => 'rw', + isa => 'ArrayRef[Str]', + default => sub { [] }, + provides => { + 'push' => 'add_js', + }, +); + +has 'css' => ( + metaclass => 'Collection::Array', + is => 'rw', + isa => 'ArrayRef[Str]', + default => sub { [ 'static/app.css' ] }, + provides => { + 'push' => 'add_css', + }, +); sub dom2html { # warn "## dom2html ",dump( @_ ); return Continuity::Widget::DomNode->create( @_ )->to_string; } -our @javascript = ( qw' -../lib/Joose.js -'); +sub _unroll_markup { +# warn "## _unroll_markup ",dump( @_ ); + my ( $markup, $array ) = @_; + my $out = ''; + foreach my $path ( @$array ) { + $path =~ s!^/!!; + confess "can't find $path" unless -e $path; + $out .= sprintf( $markup, $path ); + } + return $out; +} + +=head2 add_head + + $o->add_head( 'path/to/external.js' ); + + my $size = $o->add_head( 'path/to/external.css' ); + +=cut -sub head_javascript { +sub add_head { + my ( $self, $path ) = @_; + return if ! defined $path || $path eq ''; + $path =~ s!^/!!; + + if ( -e $path ) { + if ( $path =~ m/\.js$/ ) { + $self->add_js( $path ); + } elsif ( $path =~ m/\.css$/ ) { + $self->add_css( $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; + +sub page { my $self = shift; + my $a = {@_}; + + $reload_counter++; - my $js = Continuity::Widget::DomNode->create( - map { - ( script => { type => 'text/javascript', src => $_ } ) - } @javascript - )->to_string; + my $html = qq|| + . _unroll_markup( qq||, $self->js ) + . _unroll_markup( qq||, $self->css ) + . '' . ( $a->{title} || ref($self) ) . '' + . ( $a->{head} || '' ) + . '' + . ( $a->{body} || '' ) + . qq|
Frey $Frey::VERSION reload
| + . '' + ; - warn "# >>> js\n$js\n" if $self->debug; + warn "## >>> page ",length($html), " bytes\n" if $self->debug; - return $js; + return $html; } 1;