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 |
|
use File::Slurp; |
8 |
|
|
9 |
has 'javascript' => ( |
has 'head' => ( |
|
metaclass => 'Collection::Array', |
|
10 |
is => 'rw', |
is => 'rw', |
11 |
isa => 'ArrayRef[Str]', |
isa => 'ArrayRef[Str]', |
12 |
default => sub { [] }, |
default => sub { [ 'static/frey.css' ] }, |
|
provides => { |
|
|
'push' => 'add_javascript', |
|
|
}, |
|
13 |
); |
); |
14 |
|
|
15 |
has 'css' => ( |
has 'inline_smaller_than' => ( |
|
metaclass => 'Collection::Array', |
|
16 |
is => 'rw', |
is => 'rw', |
17 |
isa => 'ArrayRef[Str]', |
isa => 'Int', |
18 |
default => sub { [ 'static/app.css' ] }, |
default => 10240, |
|
provides => { |
|
|
'push' => 'add_css', |
|
|
}, |
|
19 |
); |
); |
20 |
|
|
21 |
sub dom2html { |
sub dom2html { |
23 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
24 |
} |
} |
25 |
|
|
26 |
sub _unroll_markup { |
sub _inline_path { |
27 |
# warn "## _unroll_markup ",dump( @_ ); |
my ( $self, $path ) = @_; |
28 |
my ( $markup, $array ) = @_; |
-s $path < $self->inline_smaller_than; |
29 |
|
} |
30 |
|
|
31 |
|
sub _head_html { |
32 |
|
my $self = shift; |
33 |
my $out = ''; |
my $out = ''; |
34 |
foreach my $path ( @$array ) { |
foreach my $path ( @{ $self->head } ) { |
35 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
36 |
confess "can't find $path" unless -e $path; |
if ( $path =~ m/\.js$/ ) { |
37 |
$out .= sprintf( $markup, $path ); |
$out .= $self->_inline_path( $path ) ? |
38 |
|
qq|<script type="text/javascript">| . read_file($path) . qq|</script>| : |
39 |
|
qq|<script type="text/javascript" src="/$path"></script>|; |
40 |
|
} elsif ( $path =~ m/\.css$/ ) { |
41 |
|
$out .= $self->_inline_path( $path ) ? |
42 |
|
qq|<style type="text/css">| . read_file( $path ) . qq|</style>| : |
43 |
|
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
44 |
|
} else { |
45 |
|
confess "don't know how to render $path"; |
46 |
|
} |
47 |
} |
} |
48 |
return $out; |
return $out; |
49 |
} |
} |
50 |
|
|
51 |
|
=head2 add_head |
52 |
|
|
53 |
|
$o->add_head( 'path/to/external.js' ); |
54 |
|
|
55 |
|
my $size = $o->add_head( 'path/to/external.css' ); |
56 |
|
|
57 |
|
=cut |
58 |
|
|
59 |
|
sub add_head { |
60 |
|
my ( $self, $path ) = @_; |
61 |
|
return if ! defined $path || $path eq ''; |
62 |
|
$path =~ s!^/!!; |
63 |
|
|
64 |
|
if ( -e $path ) { |
65 |
|
if ( $path =~ m/\.(?:js|css)$/ ) { |
66 |
|
push @{ $self->head }, $path; |
67 |
|
} else { |
68 |
|
confess "can't add_head( $path ) it's not js or css"; |
69 |
|
} |
70 |
|
} else { |
71 |
|
confess "can't find $path: $!"; |
72 |
|
} |
73 |
|
|
74 |
|
return -s $path; |
75 |
|
} |
76 |
|
|
77 |
our $reload_counter = 0; |
our $reload_counter = 0; |
78 |
|
|
79 |
sub page { |
sub page { |
83 |
$reload_counter++; |
$reload_counter++; |
84 |
|
|
85 |
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>| |
86 |
. _unroll_markup( qq|<script type="text/javascript" src="/%s"></script>|, $self->javascript ) |
. $self->_head_html |
|
. _unroll_markup( qq|<link type="text/css" rel="stylesheet" href="/%s" media="screen">|, $self->css ) |
|
87 |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
88 |
. ( $a->{head} || '' ) |
. ( $a->{head} || '' ) |
89 |
. '</head><body>' |
. '</head><body>' |
90 |
. ( $a->{body} || '<!-- no body -->' ) |
. ( $a->{body} || '<!-- no body -->' ) |
91 |
. 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>| |
92 |
. '</body></html>' |
. '</body></html>' |
93 |
; |
; |
94 |
|
|