/[Frey]/branches/zimbardo/lib/Frey/Web.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /branches/zimbardo/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 154 - (hide annotations)
Thu Jul 17 17:04:21 2008 UTC (15 years, 8 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 2090 byte(s)
- rename javascript to shorter js (much nicer in code!)
- add_head now checks if file exists and add it approriately
- more tests
1 dpavlin 100 package Frey::Web;
2     use Moose::Role;
3 dpavlin 121 use MooseX::AttributeHelpers;
4 dpavlin 100
5     use Continuity::Widget::DomNode;
6     use Data::Dump qw/dump/;
7 dpavlin 121 use Carp qw/confess/;
8 dpavlin 100
9 dpavlin 154 has 'js' => (
10 dpavlin 121 metaclass => 'Collection::Array',
11     is => 'rw',
12     isa => 'ArrayRef[Str]',
13     default => sub { [] },
14     provides => {
15 dpavlin 154 'push' => 'add_js',
16 dpavlin 121 },
17     );
18    
19     has 'css' => (
20     metaclass => 'Collection::Array',
21     is => 'rw',
22     isa => 'ArrayRef[Str]',
23     default => sub { [ 'static/app.css' ] },
24     provides => {
25     'push' => 'add_css',
26     },
27     );
28    
29 dpavlin 100 sub dom2html {
30 dpavlin 106 # warn "## dom2html ",dump( @_ );
31 dpavlin 100 return Continuity::Widget::DomNode->create( @_ )->to_string;
32     }
33    
34 dpavlin 121 sub _unroll_markup {
35     # warn "## _unroll_markup ",dump( @_ );
36     my ( $markup, $array ) = @_;
37     my $out = '';
38     foreach my $path ( @$array ) {
39     $path =~ s!^/!!;
40     confess "can't find $path" unless -e $path;
41     $out .= sprintf( $markup, $path );
42     }
43     return $out;
44     }
45 dpavlin 100
46 dpavlin 154 =head2 add_head
47    
48     $o->add_head( 'path/to/external.js' );
49    
50     my $size = $o->add_head( 'path/to/external.css' );
51    
52     =cut
53    
54     sub add_head {
55     my ( $self, $path ) = @_;
56     return if ! defined $path || $path eq '';
57     $path =~ s!^/!!;
58    
59     if ( -e $path ) {
60     if ( $path =~ m/\.js$/ ) {
61     $self->add_js( $path );
62     } elsif ( $path =~ m/\.css$/ ) {
63     $self->add_css( $path );
64     } else {
65     confess "can't add_head( $path ) it's not js or css";
66     }
67     } else {
68     confess "can't find $path: $!";
69     }
70    
71     return -s $path;
72     }
73    
74 dpavlin 142 our $reload_counter = 0;
75    
76 dpavlin 121 sub page {
77 dpavlin 100 my $self = shift;
78 dpavlin 121 my $a = {@_};
79 dpavlin 100
80 dpavlin 142 $reload_counter++;
81    
82 dpavlin 121 my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|
83 dpavlin 154 . _unroll_markup( qq|<script type="text/javascript" src="/%s"></script>|, $self->js )
84 dpavlin 121 . _unroll_markup( qq|<link type="text/css" rel="stylesheet" href="/%s" media="screen">|, $self->css )
85     . '<title>' . ( $a->{title} || ref($self) ) . '</title>'
86     . ( $a->{head} || '' )
87     . '</head><body>'
88     . ( $a->{body} || '<!-- no body -->' )
89 dpavlin 142 . qq|<div class="status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|
90 dpavlin 121 . '</body></html>'
91     ;
92 dpavlin 100
93 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
94 dpavlin 100
95 dpavlin 121 return $html;
96 dpavlin 100 }
97    
98     1;

  ViewVC Help
Powered by ViewVC 1.1.26