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/; |
7 |
|
use File::Slurp; |
8 |
|
|
9 |
|
has 'head' => ( |
10 |
|
is => 'rw', |
11 |
|
isa => 'ArrayRef[Str]', |
12 |
|
default => sub { [ 'static/frey.css' ] }, |
13 |
|
); |
14 |
|
|
15 |
|
has 'inline_smaller_than' => ( |
16 |
|
is => 'rw', |
17 |
|
isa => 'Int', |
18 |
|
default => 10240, |
19 |
|
); |
20 |
|
|
21 |
sub dom2html { |
sub dom2html { |
22 |
# warn "## dom2html ",dump( @_ ); |
# warn "## dom2html ",dump( @_ ); |
23 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
24 |
} |
} |
25 |
|
|
26 |
our @javascript = ( qw' |
sub _inline_path { |
27 |
../lib/Joose.js |
my ( $self, $path ) = @_; |
28 |
'); |
-s $path < $self->inline_smaller_than; |
29 |
|
} |
30 |
|
|
31 |
|
sub _head_html { |
32 |
|
my $self = shift; |
33 |
|
my $out = ''; |
34 |
|
foreach my $path ( @{ $self->head } ) { |
35 |
|
$path =~ s!^/!!; |
36 |
|
if ( $path =~ m/\.js$/ ) { |
37 |
|
$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; |
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; |
78 |
|
|
79 |
sub head_javascript { |
sub page { |
80 |
my $self = shift; |
my $self = shift; |
81 |
|
my $a = {@_}; |
82 |
|
|
83 |
|
$reload_counter++; |
84 |
|
|
85 |
my $js = Continuity::Widget::DomNode->create( |
my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>| |
86 |
map { |
. $self->_head_html |
87 |
( script => { type => 'text/javascript', src => $_ } ) |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
88 |
} @javascript |
. ( $a->{head} || '' ) |
89 |
)->to_string; |
. '</head><body>' |
90 |
|
. ( $a->{body} || '<!-- no body -->' ) |
91 |
|
. qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>| |
92 |
|
. '</body></html>' |
93 |
|
; |
94 |
|
|
95 |
warn "# >>> js\n$js\n" if $self->debug; |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
96 |
|
|
97 |
return $js; |
return $html; |
98 |
} |
} |
99 |
|
|
100 |
1; |
1; |