1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
|
use MooseX::AttributeHelpers; |
|
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/; |
use Carp qw/confess/; |
7 |
|
|
8 |
has 'js' => ( |
has 'head' => ( |
|
metaclass => 'Collection::Array', |
|
9 |
is => 'rw', |
is => 'rw', |
10 |
isa => 'ArrayRef[Str]', |
isa => 'ArrayRef[Str]', |
11 |
default => sub { [] }, |
default => sub { [ 'static/frey.css' ] }, |
|
provides => { |
|
|
'push' => 'add_js', |
|
|
}, |
|
|
); |
|
|
|
|
|
has 'css' => ( |
|
|
metaclass => 'Collection::Array', |
|
|
is => 'rw', |
|
|
isa => 'ArrayRef[Str]', |
|
|
default => sub { [ 'static/app.css' ] }, |
|
|
provides => { |
|
|
'push' => 'add_css', |
|
|
}, |
|
12 |
); |
); |
13 |
|
|
14 |
sub dom2html { |
sub dom2html { |
16 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
17 |
} |
} |
18 |
|
|
19 |
sub _unroll_markup { |
sub _head_html { |
20 |
# warn "## _unroll_markup ",dump( @_ ); |
my $self = shift; |
|
my ( $markup, $array ) = @_; |
|
21 |
my $out = ''; |
my $out = ''; |
22 |
foreach my $path ( @$array ) { |
foreach my $path ( @{ $self->head } ) { |
23 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
24 |
confess "can't find $path" unless -e $path; |
if ( $path =~ m/\.js$/ ) { |
25 |
$out .= sprintf( $markup, $path ); |
$out .= qq|<script type="text/javascript" src="/$path"></script>|; |
26 |
|
} elsif ( $path =~ m/\.css$/ ) { |
27 |
|
$out .= qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
28 |
|
} else { |
29 |
|
confess "don't know how to render $path"; |
30 |
|
} |
31 |
} |
} |
32 |
return $out; |
return $out; |
33 |
} |
} |
46 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
47 |
|
|
48 |
if ( -e $path ) { |
if ( -e $path ) { |
49 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
50 |
$self->add_js( $path ); |
push @{ $self->head }, $path; |
|
} elsif ( $path =~ m/\.css$/ ) { |
|
|
$self->add_css( $path ); |
|
51 |
} else { |
} else { |
52 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
53 |
} |
} |
67 |
$reload_counter++; |
$reload_counter++; |
68 |
|
|
69 |
my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>| |
my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>| |
70 |
. _unroll_markup( qq|<script type="text/javascript" src="/%s"></script>|, $self->js ) |
. $self->_head_html |
|
. _unroll_markup( qq|<link type="text/css" rel="stylesheet" href="/%s" media="screen">|, $self->css ) |
|
71 |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
72 |
. ( $a->{head} || '' ) |
. ( $a->{head} || '' ) |
73 |
. '</head><body>' |
. '</head><body>' |
74 |
. ( $a->{body} || '<!-- no body -->' ) |
. ( $a->{body} || '<!-- no body -->' ) |
75 |
. qq|<div class="status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>| |
. qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>| |
76 |
. '</body></html>' |
. '</body></html>' |
77 |
; |
; |
78 |
|
|