3 |
|
|
4 |
with 'Frey::Session'; |
with 'Frey::Session'; |
5 |
|
|
|
use Frey::Types; |
|
|
|
|
6 |
#use Continuity::Widget::DomNode; |
#use Continuity::Widget::DomNode; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
use Carp qw/confess cluck carp/; |
use Carp qw/confess cluck carp/; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
|
use Text::Tabs; # expand, unexpand |
11 |
|
|
12 |
|
use lib 'lib'; |
13 |
|
|
14 |
|
use Frey::Types; |
15 |
|
|
16 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
17 |
use Frey::Class::Browser; |
use Frey::Class::Browser; |
19 |
|
|
20 |
use Frey::SVK; |
use Frey::SVK; |
21 |
|
|
|
use Text::Tabs; # expand, unexpand |
|
|
|
|
22 |
our @head; |
our @head; |
23 |
sub head { @head } |
sub head { @head } |
24 |
|
|
116 |
} |
} |
117 |
} |
} |
118 |
|
|
119 |
sub _inline_path { |
sub _inline_path_size { |
120 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
121 |
-s $path < $self->inline_smaller_than; |
-s $path < $self->inline_smaller_than && -s $path; |
122 |
} |
} |
123 |
|
|
124 |
sub _head_html { |
sub _head_html { |
126 |
my $out = ''; |
my $out = ''; |
127 |
foreach my $path ( @head ) { |
foreach my $path ( @head ) { |
128 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
129 |
|
my $size = $self->_inline_path_size( $path ); |
130 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
131 |
$out .= $self->_inline_path( $path ) ? |
$out .= $size ? |
132 |
qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| : |
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
133 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
134 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
135 |
$out .= $self->_inline_path( $path ) ? |
$out .= $size ? |
136 |
qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| : |
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
137 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
138 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
139 |
$out .= $path; |
$out .= $path; |
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
|
sub _add_css_js { |
179 |
|
my ( $self, $what, $content ) = @_; |
180 |
|
|
181 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
182 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
183 |
|
my $head; |
184 |
|
|
185 |
|
my ( $package, $path, $line ) = caller(1); |
186 |
|
|
187 |
|
if ( $content =~ m{\.(js|css)} ) { |
188 |
|
$content = "/$content" if -e $content; |
189 |
|
if ( $what eq 'js' ) { |
190 |
|
$head = qq| |
191 |
|
<$tag type="$type" src="$content"> |
192 |
|
/* via $package at $path line $line */ |
193 |
|
</$tag> |
194 |
|
|; |
195 |
|
} else { |
196 |
|
$head = qq| |
197 |
|
<link rel="stylesheet" type="$type" href="$content"> |
198 |
|
<!-- via $package at $path line $line --> |
199 |
|
|; |
200 |
|
} |
201 |
|
} else { |
202 |
|
$head = qq| |
203 |
|
<$tag type="$type"> |
204 |
|
/* via $package at $path line $line */ |
205 |
|
$content |
206 |
|
</$tag> |
207 |
|
|; |
208 |
|
}; |
209 |
|
$self->add_head( $head ); |
210 |
|
} |
211 |
|
|
212 |
sub add_css { |
sub add_css { |
213 |
my ($self,$css) = @_; |
my ($self,$css) = @_; |
214 |
my ( $package, $path, $line ) = caller; |
$self->_add_css_js( 'css', $css ); |
|
$self->add_head( qq| |
|
|
<style type="text/css"> |
|
|
/* via $package at $path line $line */ |
|
|
$css |
|
|
</style> |
|
|
| ); |
|
215 |
} |
} |
216 |
|
|
217 |
sub add_js { |
sub add_js { |
218 |
my ($self,$css) = @_; |
my ($self,$js) = @_; |
219 |
my ( $package, $path, $line ) = caller; |
$self->_add_css_js( 'js', $js ); |
|
$self->add_head( qq| |
|
|
<script type="text/javascript"> |
|
|
/* via $package at $path line $line */ |
|
|
$css |
|
|
</script> |
|
|
| ); |
|
220 |
} |
} |
221 |
|
|
222 |
our $reload_counter = 0; |
our $reload_counter = 0; |
260 |
if ( ! $body ) { |
if ( ! $body ) { |
261 |
my $run = $a->{run} || 'as_markup'; |
my $run = $a->{run} || 'as_markup'; |
262 |
warn "# no body, invoke $self->$run on ", ref($self); |
warn "# no body, invoke $self->$run on ", ref($self); |
263 |
eval { |
$body = $self->$run; |
|
$body = $self->$run; |
|
|
}; |
|
|
$body = $self->error( $@, '' ) if $@; |
|
264 |
} |
} |
265 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} ) { |
266 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
376 |
return $error; |
return $error; |
377 |
} |
} |
378 |
|
|
379 |
|
sub html_self { |
380 |
|
my $self = shift; |
381 |
|
my $html = $self; |
382 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
383 |
|
return $html; |
384 |
|
} |
385 |
|
|
386 |
=head2 error |
=head2 error |
387 |
|
|
388 |
This method will return error to browser and backtrace unless |
This method will return error to browser and backtrace unless |
395 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
396 |
|
|
397 |
my $fatal = ''; |
my $fatal = ''; |
398 |
|
my $backtrace = ''; |
399 |
|
|
400 |
if ( $error !~ m{\n$} ) { |
if ( $error !~ m{\n$} ) { |
401 |
if ( my @backtrace = $self->backtrace ) { |
if ( my @backtrace = $self->backtrace ) { |
402 |
$error .= "\n\t" . join( "\n\t", @backtrace ); |
$backtrace = |
403 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
404 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
405 |
|
; |
406 |
$fatal = qq| frey-fatal|; |
$fatal = qq| frey-fatal|; |
407 |
} |
} |
408 |
} |
} |
409 |
|
|
410 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
411 |
return |
$self->add_icon('error'); |
412 |
qq|<pre class="frey-error$fatal">| |
$error = $self->html_links( $error ); |
413 |
. $self->html_links( $error ) . |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
qq|</pre>| |
|
|
; |
|
414 |
} |
} |
415 |
|
|
416 |
=head1 Status line |
=head1 Status line |
486 |
|
|
487 |
sub icon_path { |
sub icon_path { |
488 |
my ($self,$class,$variant) = @_; |
my ($self,$class,$variant) = @_; |
489 |
my $icon = $class; |
|
490 |
$icon ||= $self->title; |
sub icon_exists { |
491 |
$icon =~ s{::}{/}g; |
my $class = shift; |
492 |
$icon .= "/$variant" if $variant; |
$class =~ s{::}{/}g; |
493 |
my $path = 'static/icons/' . $icon . '.png'; |
$class .= "/$variant" if $variant; |
494 |
if ( -e $path ) { |
my $icon_path = 'static/icons/' . $class . '.png'; |
495 |
warn "# $class from $self icon_path $path" if $self->debug; |
return $icon_path if -e $icon_path; |
496 |
return $path; |
return; |
497 |
} else { |
} |
498 |
$self->TODO( "add $path icon for $class" ); |
|
499 |
|
my $path = icon_exists( $class ); |
500 |
|
if ( ! $path ) { |
501 |
|
my $super_class = $class; |
502 |
|
while ( $super_class =~ s{::[^:]+$}{} && ! $path ) { |
503 |
|
$path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
if ( ! $path ) { |
508 |
|
$self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) ); |
509 |
return undef; |
return undef; |
510 |
} |
} |
511 |
|
|
512 |
|
warn "# $class from $self icon_path $path" if $self->debug; |
513 |
|
return $path; |
514 |
} |
} |
515 |
|
|
516 |
sub add_icon { |
sub add_icon { |
517 |
my ($self,$variant) = @_; |
my ($self,$variant) = @_; |
518 |
|
|
519 |
my $class = ref($self); |
my $class = $self->class if $self->can('class'); |
520 |
$class = $self->class if $self->can('class'); |
#$class ||= $self->title; |
521 |
|
$class ||= ref($self); |
522 |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
523 |
|
|
524 |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
689 |
my ($self) = @_; |
my ($self) = @_; |
690 |
|
|
691 |
my @backtrace; |
my @backtrace; |
692 |
foreach ( 0 .. 5 ) { |
foreach ( 1 .. 5 ) { # 0 = backtrace |
693 |
my ( |
my ( |
694 |
$package,$path,$line |
$package,$path,$line |
695 |
# subroutine hasargs |
# subroutine hasargs |