1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
|
with 'Frey::Backtrace'; |
5 |
|
|
6 |
|
use Frey::Types; |
7 |
|
|
8 |
use Continuity::Widget::DomNode; |
use Continuity::Widget::DomNode; |
9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
|
use Carp qw/confess/; |
11 |
|
use File::Slurp; |
12 |
|
|
13 |
|
use Frey::Bookmarklet; |
14 |
|
use Frey::ClassBrowser; |
15 |
|
|
16 |
|
has 'head' => ( |
17 |
|
is => 'rw', |
18 |
|
isa => 'ArrayRef[Str]', |
19 |
|
default => sub { [ 'static/frey.css' ] }, |
20 |
|
); |
21 |
|
|
22 |
|
has 'status' => ( |
23 |
|
is => 'rw', |
24 |
|
isa => 'ArrayRef[HashRef[Str]]', |
25 |
|
lazy => 1, |
26 |
|
default => sub { [ |
27 |
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
28 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
29 |
|
] }, |
30 |
|
); |
31 |
|
|
32 |
|
has 'request_url' => ( |
33 |
|
is => 'rw', |
34 |
|
isa => 'Uri', coerce => 1, |
35 |
|
default => '/', |
36 |
|
); |
37 |
|
|
38 |
|
has 'title' => ( |
39 |
|
is => 'rw', |
40 |
|
isa => 'Str', |
41 |
|
lazy => 1, |
42 |
|
default => sub { |
43 |
|
my ($self) = @_; |
44 |
|
ref($self); |
45 |
|
}, |
46 |
|
); |
47 |
|
|
48 |
|
has 'content_type' => ( |
49 |
|
is => 'rw', |
50 |
|
isa => 'Str', |
51 |
|
default => 'text/html', |
52 |
|
documentation => 'Content-type header', |
53 |
|
); |
54 |
|
|
55 |
|
has 'dump_max_bytes' => ( |
56 |
|
is => 'rw', |
57 |
|
isa => 'Int', |
58 |
|
default => 4096, |
59 |
|
documentation => 'Maximum dump size sent to browser before truncation', |
60 |
|
); |
61 |
|
|
62 |
|
=head2 inline_smaller_than |
63 |
|
|
64 |
|
Inline JavaScript and CSS smaller than this size into page reducing |
65 |
|
round-trips to server. |
66 |
|
|
67 |
|
=cut |
68 |
|
|
69 |
|
has 'inline_smaller_than' => ( |
70 |
|
is => 'rw', |
71 |
|
isa => 'Int', |
72 |
|
default => 10240, |
73 |
|
); |
74 |
|
|
75 |
sub dom2html { |
sub dom2html { |
76 |
warn "## dom2html ",dump( @_ ); |
# warn "## dom2html ",dump( @_ ); |
77 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
78 |
} |
} |
79 |
|
|
80 |
our @javascript = ( qw' |
sub _inline_path { |
81 |
../lib/Joose.js |
my ( $self, $path ) = @_; |
82 |
'); |
-s $path < $self->inline_smaller_than; |
83 |
|
} |
84 |
|
|
85 |
sub head_javascript { |
sub _head_html { |
86 |
my $self = shift; |
my $self = shift; |
87 |
|
my $out = ''; |
88 |
|
foreach my $path ( @{ $self->head } ) { |
89 |
|
$path =~ s!^/!!; |
90 |
|
if ( $path =~ m/\.js$/ ) { |
91 |
|
$out .= $self->_inline_path( $path ) ? |
92 |
|
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
93 |
|
qq|<script type="text/javascript" src="/$path"></script>|; |
94 |
|
} elsif ( $path =~ m/\.css$/ ) { |
95 |
|
$out .= $self->_inline_path( $path ) ? |
96 |
|
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
97 |
|
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
98 |
|
} elsif ( $path =~ m{<.+>}s ) { |
99 |
|
$out .= $path; |
100 |
|
} else { |
101 |
|
confess "don't know how to render $path"; |
102 |
|
} |
103 |
|
$out .= "\n"; |
104 |
|
} |
105 |
|
return $out; |
106 |
|
} |
107 |
|
|
108 |
|
=head2 add_head |
109 |
|
|
110 |
|
$o->add_head( 'path/to/external.js' ); |
111 |
|
|
112 |
|
my $size = $o->add_head( 'path/to/external.css' ); |
113 |
|
|
114 |
|
$o->add_head( '<!-- html content -->' ); |
115 |
|
|
116 |
|
=cut |
117 |
|
|
118 |
|
sub add_head { |
119 |
|
my ( $self, $path ) = @_; |
120 |
|
return if ! defined $path || $path eq ''; |
121 |
|
$path =~ s!^/!!; |
122 |
|
|
123 |
|
if ( $path =~ m{<.*>}s ) { |
124 |
|
push @{ $self->head }, $path; |
125 |
|
} elsif ( -e $path ) { |
126 |
|
if ( $path =~ m/\.(?:js|css)$/ ) { |
127 |
|
push @{ $self->head }, $path; |
128 |
|
} else { |
129 |
|
confess "can't add_head( $path ) it's not js or css"; |
130 |
|
} |
131 |
|
return -s $path; |
132 |
|
} else { |
133 |
|
confess "can't find $path: $!"; |
134 |
|
} |
135 |
|
|
136 |
|
} |
137 |
|
|
138 |
|
our $reload_counter = 0; |
139 |
|
|
140 |
|
|
141 |
|
=head2 page |
142 |
|
|
143 |
my $js = Continuity::Widget::DomNode->create( |
$self->page( |
144 |
map { |
title => 'page title', |
145 |
( script => { type => 'text/javascript', src => $_ } ) |
head => '<!-- optional head markup -->', |
146 |
} @javascript |
body => '<b>Page Body</b>', |
147 |
)->to_string; |
); |
148 |
|
|
149 |
|
=cut |
150 |
|
|
151 |
|
sub page { |
152 |
|
my $self = shift; |
153 |
|
my $a = {@_}; |
154 |
|
|
155 |
|
$reload_counter++; |
156 |
|
|
157 |
|
my $status_line = ''; |
158 |
|
foreach my $part ( @{ $self->status } ) { |
159 |
|
if ( ref($part) ne 'HASH' ) { |
160 |
|
warn "part not hash ",dump( $part ) ; |
161 |
|
#$self->status( $part ); |
162 |
|
next; |
163 |
|
} |
164 |
|
foreach my $name ( keys %$part ) { |
165 |
|
my $content = $part->{$name}; |
166 |
|
if ( ref($content) ) { |
167 |
|
$content = '<code>' . dump($content) . '</code>'; |
168 |
|
my $l = length($content); |
169 |
|
$content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes; |
170 |
|
} else { |
171 |
|
$content = qq|<span>$content</span>|; |
172 |
|
} |
173 |
|
warn "### part [$name] = ", length( $content ), " bytes" if $self->debug; |
174 |
|
$status_line .= qq|<span class="frey-popup">$name $content</span>\n|; |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
my $url = $self->request_url; |
179 |
|
$url =~ s{\?reload=\d+}{}; |
180 |
|
|
181 |
|
my $body = $a->{body}; |
182 |
|
$body ||= $self->as_markup if $self->can('as_markup'); |
183 |
|
if ( $self->content_type !~ m{html} ) { |
184 |
|
warn "# return only $self body ", $self->content_type; |
185 |
|
return $body |
186 |
|
} elsif ( ! defined $body ) { |
187 |
|
warn "# no body"; |
188 |
|
$body = '<!-- no body -->'; |
189 |
|
} |
190 |
|
|
191 |
|
$status_line |
192 |
|
.= qq|<span class="frey-popup">warn<code>| |
193 |
|
. $self->editor_links( join("", $self->warnings ) ) |
194 |
|
. qq|</code></span>| |
195 |
|
if $self->warnings; |
196 |
|
|
197 |
|
my $right = |
198 |
|
qq| |
199 |
|
<span class="right"> |
200 |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
201 |
|
</span> |
202 |
|
|; |
203 |
|
|
204 |
|
my $html = join("\n", |
205 |
|
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
206 |
|
$self->_head_html, |
207 |
|
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
208 |
|
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
209 |
|
( $a->{head} || '' ), |
210 |
|
qq| |
211 |
|
</head><body> |
212 |
|
$body |
213 |
|
<div class="frey-status-line"> |
214 |
|
<a href="/">Frey</a> $Frey::VERSION |
215 |
|
$status_line |
216 |
|
$right |
217 |
|
</div> |
218 |
|
</body></html> |
219 |
|
|, |
220 |
|
); |
221 |
|
|
222 |
|
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
223 |
|
|
224 |
|
return $html; |
225 |
|
} |
226 |
|
|
227 |
|
sub editor_links { |
228 |
|
my ( $self, $error ) = @_; |
229 |
|
|
230 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
231 |
|
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
232 |
|
|
233 |
|
$error =~ s{(via package ")([\w:]+)(")} |
234 |
|
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
235 |
|
|
236 |
|
return $error; |
237 |
|
} |
238 |
|
|
239 |
|
sub error { |
240 |
|
my $self = shift; |
241 |
|
my $error = join(" ", @_); |
242 |
|
|
243 |
warn "# >>> js\n$js\n" if $self->debug; |
my @backtrace = $self->backtrace; |
244 |
|
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
245 |
|
|
246 |
return $js; |
warn "ERROR: $error\n"; |
247 |
|
return |
248 |
|
qq|<pre class="frey-error">| |
249 |
|
. $self->editor_links( $error ) . |
250 |
|
qq|</pre>| |
251 |
|
; |
252 |
} |
} |
253 |
|
|
254 |
1; |
1; |