[Jifty-commit] r3427 - in jifty/trunk/lib/Jifty: Plugin
Plugin/CompressedCSSandJS
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Jun 10 19:15:51 EDT 2007
Author: clkao
Date: Sun Jun 10 19:15:50 2007
New Revision: 3427
Modified:
jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
jifty/trunk/lib/Jifty/Web.pm
Log:
Move the javascript concatenating logic from Jifty::Web to the plugin.
Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm Sun Jun 10 19:15:50 2007
@@ -3,6 +3,7 @@
package Jifty::Plugin::CompressedCSSandJS;
use base qw/Jifty::Plugin Class::Accessor/;
+use Digest::MD5 qw(md5_hex);
=head1 NAME
@@ -14,7 +15,7 @@
=cut
-__PACKAGE__->mk_accessors(qw(css js));
+__PACKAGE__->mk_accessors(qw(css js cached_javascript cached_javascript_digest cached_javascript_time ));
=head2 init
@@ -24,9 +25,15 @@
sub init {
my $self = shift;
- my %opt = @_;
- $self->css($opt{css});
- $self->js ($opt{js});
+ my %opt = @_;
+ $self->css( $opt{css} );
+ $self->js( $opt{js} );
+
+ Jifty::Web->add_trigger(
+ name => 'include_javascript',
+ callback => => sub { $self->_include_javascript(@_) },
+ abortable => 1
+ ) if $self->js_enabled;
}
=head2 js_enabled
@@ -51,4 +58,79 @@
defined $self->css ? $self->css : 1;
}
+sub _include_javascript {
+ my $self = shift;
+
+ $self->_generate_javascript;
+ Jifty->web->out( qq[<script type="text/javascript" src="/__jifty/js/]
+ . $self->cached_javascript_digest
+ . qq[.js"></script>] );
+ return 0;
+}
+
+=head3 _generate_javascript
+
+Checks if the compressed JS is generated, and if it isn't, generates
+and caches it.
+
+=cut
+
+sub _generate_javascript {
+ my $self = shift;
+
+ if ( not defined $self->cached_javascript_digest
+ or Jifty->config->framework('DevelMode') ) {
+ Jifty->log->debug("Generating JS...");
+
+ my @roots = (
+ Jifty::Util->absolute_path(
+ File::Spec->catdir(
+ Jifty->config->framework('Web')->{'StaticRoot'}, 'js'
+ )
+ ),
+
+ Jifty::Util->absolute_path(
+ File::Spec->catdir(
+ Jifty->config->framework('Web')->{'DefaultStaticRoot'},
+ 'js'
+ )
+ ),
+ );
+
+ my $js = "";
+
+ for my $file ( @{ Jifty::Web->javascript_libs } ) {
+ my $include;
+
+ for my $root (@roots) {
+ my @spec = File::Spec->splitpath( $root, 1 );
+ my $path = File::Spec->catpath( @spec[ 0, 1 ], $file );
+
+ if ( -e $path ) {
+ $include = $path;
+ last;
+ }
+ }
+
+ if ( defined $include ) {
+ my $fh;
+
+ if ( open $fh, '<', $include ) {
+ $js .= "/* Including '$file' */\n\n";
+ $js .= $_ while <$fh>;
+ $js .= "\n/* End of '$file' */\n\n";
+ } else {
+ $js .= "\n/* Unable to open '$file': $! */\n";
+ }
+ } else {
+ $js .= "\n/* Unable to find '$file' */\n";
+ }
+ }
+ $self->cached_javascript($js);
+ $self->cached_javascript_digest( md5_hex($js) );
+ $self->cached_javascript_time(time);
+ }
+}
+
+
1;
Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm Sun Jun 10 19:15:50 2007
@@ -13,7 +13,8 @@
which serve out compiled and compressed CSS and Javascript rules.
=cut
-
+use Compress::Zlib qw();
+use HTTP::Date ();
use Jifty::Dispatcher -base;
@@ -26,12 +27,13 @@
Jifty->web->redirect( "/static/js/" . $arg );
}
- Jifty->web->generate_javascript;
+ my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS')
+ or Jifty->web->redirect( "/static/js/" . $arg );
- use HTTP::Date ();
+ $ccjs->_generate_javascript;
if ( Jifty->handler->cgi->http('If-Modified-Since')
- and $arg eq Jifty->web->cached_javascript_digest . '.js' )
+ and $arg eq $ccjs->cached_javascript_digest . '.js' )
{
Jifty->log->debug("Returning 304 for cached javascript");
Jifty->handler->apache->header_out( Status => 304 );
@@ -45,18 +47,16 @@
# XXX TODO: If we start caching the squished JS in a file somewhere, we
# can have the static handler serve it, which would take care of gzipping
# for us.
- use Compress::Zlib qw();
-
if ( Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
Jifty->log->debug("Sending gzipped squished JS");
Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
Jifty->handler->apache->send_http_header();
binmode STDOUT;
- print Compress::Zlib::memGzip( Jifty->web->cached_javascript );
+ print Compress::Zlib::memGzip( $ccjs->cached_javascript );
} else {
Jifty->log->debug("Sending squished JS");
Jifty->handler->apache->send_http_header();
- print Jifty->web->cached_javascript;
+ print $ccjs->cached_javascript;
}
abort;
};
@@ -73,8 +73,6 @@
Jifty->web->generate_css;
- use HTTP::Date ();
-
if ( Jifty->handler->cgi->http('If-Modified-Since')
and $arg eq Jifty->web->cached_css_digest . '.css' )
{
@@ -89,8 +87,6 @@
# XXX TODO: If we start caching the squished CSS in a file somewhere, we
# can have the static handler serve it, which would take care of gzipping
# for us.
- use Compress::Zlib qw();
-
if ( Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
Jifty->log->debug("Sending gzipped squished CSS");
Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
@@ -104,4 +100,5 @@
}
abort;
};
+
1;
Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm (original)
+++ jifty/trunk/lib/Jifty/Web.pm Sun Jun 10 19:15:50 2007
@@ -27,7 +27,7 @@
__PACKAGE__->mk_classdata($_)
for qw(cached_css cached_css_digest cached_css_time
- cached_javascript cached_javascript_digest cached_javascript_time javascript_libs);
+ javascript_libs);
__PACKAGE__->javascript_libs([qw(
jsan/JSAN.js
@@ -71,6 +71,8 @@
css_browser_selector.js
)]);
+use Jifty::DBI::Class::Trigger;
+
=head1 METHODS
=head3 new
@@ -1174,92 +1176,16 @@
sub include_javascript {
my $self = shift;
- my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS');
- if ( $ccjs && $ccjs->js_enabled ) {
- $self->generate_javascript;
- $self->out(
- qq[<script type="text/javascript" src="/__jifty/js/]
- . __PACKAGE__->cached_javascript_digest . qq[.js"></script>]
- );
- }
- else {
- for my $file ( @{ __PACKAGE__->javascript_libs } ) {
- $self->out(
- qq[<script type="text/javascript" src="/static/js/$file"></script>\n]
- );
- }
- }
-
- return '';
-}
-
-=head3 generate_javascript
-Checks if the compressed JS is generated, and if it isn't, generates
-and caches it.
-
-=cut
+ $self->call_trigger('include_javascript', @_) or return;
-sub generate_javascript {
- my $self = shift;
-
- if (not defined __PACKAGE__->cached_javascript_digest
- or Jifty->config->framework('DevelMode'))
- {
- Jifty->log->debug("Generating JS...");
-
- my @roots = (
- Jifty::Util->absolute_path(
- File::Spec->catdir(
- Jifty->config->framework('Web')->{'StaticRoot'},
- 'js'
- )
- ),
-
- Jifty::Util->absolute_path(
- File::Spec->catdir(
- Jifty->config->framework('Web')->{'DefaultStaticRoot'},
- 'js'
- )
- ),
+ for my $file ( @{ __PACKAGE__->javascript_libs } ) {
+ $self->out(
+ qq[<script type="text/javascript" src="/static/js/$file"></script>\n]
);
-
- my $js = "";
-
- for my $file ( @{ __PACKAGE__->javascript_libs } ) {
- my $include;
-
- for my $root (@roots) {
- my @spec = File::Spec->splitpath( $root, 1 );
- my $path = File::Spec->catpath( @spec[0,1], $file );
-
- if ( -e $path ) {
- $include = $path;
- last;
- }
- }
-
- if ( defined $include ) {
- my $fh;
-
- if ( open $fh, '<', $include ) {
- $js .= "/* Including '$file' */\n\n";
- $js .= $_ while <$fh>;
- $js .= "\n/* End of '$file' */\n\n";
- }
- else {
- $js .= "\n/* Unable to open '$file': $! */\n";
- }
- }
- else {
- $js .= "\n/* Unable to find '$file' */\n";
- }
- }
-
- __PACKAGE__->cached_javascript( $js );
- __PACKAGE__->cached_javascript_digest( md5_hex( $js ) );
- __PACKAGE__->cached_javascript_time( time );
}
+
+ return '';
}
=head3 add_javascript FILE1, FILE2, ...
More information about the Jifty-commit
mailing list