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 'js' => ( |
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_js', |
|
|
}, |
|
13 |
); |
); |
14 |
|
|
15 |
has 'css' => ( |
=head2 inline_smaller_than |
16 |
metaclass => 'Collection::Array', |
|
17 |
|
Inline JavaScript and CSS smaller than this size into page reducing |
18 |
|
round-trips to server. |
19 |
|
|
20 |
|
=cut |
21 |
|
|
22 |
|
has 'inline_smaller_than' => ( |
23 |
is => 'rw', |
is => 'rw', |
24 |
isa => 'ArrayRef[Str]', |
isa => 'Int', |
25 |
default => sub { [ 'static/app.css' ] }, |
default => 10240, |
|
provides => { |
|
|
'push' => 'add_css', |
|
|
}, |
|
26 |
); |
); |
27 |
|
|
28 |
sub dom2html { |
sub dom2html { |
30 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
31 |
} |
} |
32 |
|
|
33 |
sub _unroll_markup { |
sub _inline_path { |
34 |
# warn "## _unroll_markup ",dump( @_ ); |
my ( $self, $path ) = @_; |
35 |
my ( $markup, $array ) = @_; |
-s $path < $self->inline_smaller_than; |
36 |
|
} |
37 |
|
|
38 |
|
sub _head_html { |
39 |
|
my $self = shift; |
40 |
my $out = ''; |
my $out = ''; |
41 |
foreach my $path ( @$array ) { |
foreach my $path ( @{ $self->head } ) { |
42 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
43 |
confess "can't find $path" unless -e $path; |
if ( $path =~ m/\.js$/ ) { |
44 |
$out .= sprintf( $markup, $path ); |
$out .= $self->_inline_path( $path ) ? |
45 |
|
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
46 |
|
qq|<script type="text/javascript" src="/$path"></script>|; |
47 |
|
} elsif ( $path =~ m/\.css$/ ) { |
48 |
|
$out .= $self->_inline_path( $path ) ? |
49 |
|
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
50 |
|
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
51 |
|
} else { |
52 |
|
confess "don't know how to render $path"; |
53 |
|
} |
54 |
|
$out .= "\n"; |
55 |
} |
} |
56 |
return $out; |
return $out; |
57 |
} |
} |
70 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
71 |
|
|
72 |
if ( -e $path ) { |
if ( -e $path ) { |
73 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
74 |
$self->add_js( $path ); |
push @{ $self->head }, $path; |
|
} elsif ( $path =~ m/\.css$/ ) { |
|
|
$self->add_css( $path ); |
|
75 |
} else { |
} else { |
76 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
77 |
} |
} |
84 |
|
|
85 |
our $reload_counter = 0; |
our $reload_counter = 0; |
86 |
|
|
87 |
|
|
88 |
|
=head2 page |
89 |
|
|
90 |
|
$self->page( |
91 |
|
title => 'page title', |
92 |
|
head => '<!-- optional head markup -->', |
93 |
|
body => '<b>Page Body</b>', |
94 |
|
); |
95 |
|
|
96 |
|
=cut |
97 |
|
|
98 |
|
use Frey::Bookmarklet; |
99 |
|
use Frey::ClassBrowser; |
100 |
|
|
101 |
sub page { |
sub page { |
102 |
my $self = shift; |
my $self = shift; |
103 |
my $a = {@_}; |
my $a = {@_}; |
105 |
$reload_counter++; |
$reload_counter++; |
106 |
|
|
107 |
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>| |
108 |
. _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 ) |
|
109 |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
110 |
|
. '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' |
111 |
. ( $a->{head} || '' ) |
. ( $a->{head} || '' ) |
112 |
. '</head><body>' |
. '</head><body>' |
113 |
. ( $a->{body} || '<!-- no body -->' ) |
. ( $a->{body} || '<!-- no body -->' ) |
114 |
. 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"> |
115 |
. '</body></html>' |
<a href="/">Frey</a> $Frey::VERSION |
116 |
; |
<a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a> |
117 |
|
<span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span> |
118 |
|
<span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span> |
119 |
|
<span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span> |
120 |
|
</div> |
121 |
|
</body></html> |
122 |
|
|; |
123 |
|
|
124 |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
125 |
|
|
126 |
return $html; |
return $html; |
127 |
} |
} |
128 |
|
|
129 |
|
sub error { |
130 |
|
my ( $self, $error ) = @_; |
131 |
|
warn $error; |
132 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)}{ <a href="/editor$1+$2" target="editor">$1</a> line $2}gsm; |
133 |
|
return qq|<pre class="frey-error">$error</pre>|; |
134 |
|
} |
135 |
|
|
136 |
1; |
1; |