[Jifty-commit] r1324 - in jifty/trunk: . lib lib/Jifty/Script share/web/static/js share/web/templates/__jifty/js share/web/templates/_elements

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Jun 20 13:06:43 EDT 2006


Author: trs
Date: Tue Jun 20 13:06:41 2006
New Revision: 1324

Added:
   jifty/trunk/share/web/static/js/setup_jsan.js
   jifty/trunk/share/web/templates/__jifty/js/
   jifty/trunk/share/web/templates/__jifty/js/dhandler
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty.pm
   jifty/trunk/lib/Jifty/Script/Server.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/share/web/templates/_elements/header

Log:
 r13255 at zot:  tom | 2006-06-20 13:03:43 -0400
 * Concatenate all JS into a single, cacheable file much like the CSS
 * Add a startup hook into the application's base class
 * Remove an extraneous Jifty->new() from Jifty::Script::Server


Modified: jifty/trunk/lib/Jifty.pm
==============================================================================
--- jifty/trunk/lib/Jifty.pm	(original)
+++ jifty/trunk/lib/Jifty.pm	Tue Jun 20 13:06:41 2006
@@ -70,7 +70,10 @@
 
 This class method instantiates a new C<Jifty> object. This object
 deals with configuration files, logging and database handles for the
-system.  Most of the time, the server will call this for you to set up
+system.  Before this method returns, it calls the application's C<start>
+method (i.e. C<MyApp->start>) to handle any application-specific startup.
+
+Most of the time, the server will call this for you to set up
 your C<Jifty> object.  If you are writing command-line programs that
 want to use your libraries (as opposed to web services) you will need
 to call this yourself.
@@ -131,12 +134,16 @@
     __PACKAGE__->handler(Jifty::Handler->new());
     __PACKAGE__->api(Jifty::API->new());
 
+    # Let's get the database rocking and rolling
+    __PACKAGE__->setup_database_connection(%args);
 
-   # Let's get the database rocking and rolling
-   __PACKAGE__->setup_database_connection(%args);
-
-
-
+    # Call the application's start method to let it do anything
+    # application specific for startup
+    my $app = Jifty->config->framework('ApplicationClass');
+    
+    $app->start()
+        if $app->can('start');
+    
 }
 
 =head2 config

Modified: jifty/trunk/lib/Jifty/Script/Server.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Script/Server.pm	(original)
+++ jifty/trunk/lib/Jifty/Script/Server.pm	Tue Jun 20 13:06:41 2006
@@ -81,8 +81,6 @@
     print $fh $$;
     close $fh;
 
-    Jifty->new();
-
     Jifty->handle->dbh->{Profile} = '6/DBI::ProfileDumper'
         if $self->{dbiprof};
 

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Tue Jun 20 13:06:41 2006
@@ -18,14 +18,43 @@
 use Digest::MD5 qw(md5_hex);
 use base qw/Class::Accessor::Fast Class::Data::Inheritable Jifty::Object/;
 
-use vars qw/$SERIAL/;
+use vars qw/$SERIAL @JS_INCLUDES/;
 
 __PACKAGE__->mk_accessors(
     qw(next_page request response session temporary_current_user _current_user)
 );
 
 __PACKAGE__->mk_classdata($_)
-    for qw(cached_css cached_css_digest cached_css_mtime);
+    for qw(cached_css        cached_css_digest
+           cached_javascript cached_javascript_digest javascript_libs);
+
+__PACKAGE__->javascript_libs([qw(
+    jsan/JSAN.js
+    setup_jsan.js
+    jsan/Upgrade/Array/push.js
+    jsan/DOM/Events.js
+    json.js
+    prototype.js
+    cssquery/cssQuery.js
+    cssquery/cssQuery-level2.js
+    cssquery/cssQuery-level3.js
+    cssquery/cssQuery-standard.js
+    behaviour.js
+    scriptaculous/builder.js
+    scriptaculous/effects.js
+    scriptaculous/controls.js
+    jifty.js
+    jifty_utils.js
+    dom-drag.js
+    halo.js
+    combobox.js
+    key_bindings.js
+    context_menu.js
+    bps_util.js
+    rico.js
+    app.js
+    app_behaviour.js
+)]);
 
 =head1 METHODS
 
@@ -875,6 +904,169 @@
     return $self->{page_navigation};
 }
 
+=head3 include_css
+
+Returns a C<< <link> >> tag for the compressed CSS
+
+=cut
+
+sub include_css {
+    my $self = shift;
+    
+    if ( Jifty->config->framework('DevelMode') ) {
+        $self->out(
+            '<link rel="stylesheet" type="text/css" '
+            . 'href="/static/css/main.css" />'
+        );
+    }
+    else {
+        $self->generate_css;
+    
+        $self->out(
+            '<link rel="stylesheet" type="text/css" href="/__jifty/css/'
+            . __PACKAGE__->cached_css_digest . '.css" />'
+        );
+    }
+    
+    return '';
+}
+
+=head3 generate_css
+
+Checks if the compressed CSS is generated, and if it isn't, generates
+and caches it.
+
+=cut
+
+sub generate_css {
+    my $self = shift;
+    
+    if (not defined __PACKAGE__->cached_css_digest
+            or Jifty->config->framework('DevelMode'))
+    {
+        Jifty->log->debug("Generating CSS...");
+        
+        my $app   = File::Spec->catdir(
+                        Jifty->config->framework('Web')->{'StaticRoot'},
+                        'css'
+                    );
+
+        my $jifty = File::Spec->catdir(
+                        Jifty->config->framework('Web')->{'DefaultStaticRoot'},
+                        'css'
+                    );
+
+        my $file = Jifty::Util->absolute_path(
+                        File::Spec->catpath( '', $app, 'main.css' )
+                   );
+
+        if ( not -e $file ) {
+            $file = Jifty::Util->absolute_path(
+                         File::Spec->catpath( '', $jifty, 'main.css' )
+                    );
+        }
+
+        CSS::Squish->roots( $jifty );
+        
+        my $css = CSS::Squish->concatenate( $file );
+
+        __PACKAGE__->cached_css( $css );
+        __PACKAGE__->cached_css_digest( md5_hex( $css ) );
+    }
+}
+
+=head3 include_javascript
+
+Returns a C<< <script> >> tag for the compressed Javascript.
+
+=cut
+
+sub include_javascript {
+    my $self  = shift;
+    
+    if ( Jifty->config->framework('DevelMode') ) {
+        for my $file ( @{ __PACKAGE__->javascript_libs } ) {
+            $self->out(
+                qq[<script type="text/javascript" src="/static/js/$file"></script>\n]
+            );
+        }
+    }
+    else {
+        $self->generate_javascript;
+    
+        $self->out(
+            qq[<script type="text/javascript" src="/__jifty/js/]
+            . __PACKAGE__->cached_javascript_digest . qq[.js"></script>]
+        );
+    }
+    
+    return '';
+}
+
+=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 __PACKAGE__->cached_javascript_digest
+            or Jifty->config->framework('DevelMode'))
+    {
+        Jifty->log->debug("Generating JS...");
+        
+        my @roots = (
+            File::Spec->catdir(
+                Jifty->config->framework('Web')->{'StaticRoot'},
+                'js'
+            ), 
+
+            File::Spec->catdir(
+                Jifty->config->framework('Web')->{'DefaultStaticRoot'},
+                'js'
+            ),
+        );
+        
+        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 ) );
+    }
+}
+
 =head2 STATE VARIABLES
 
 =head3 get_variable NAME
@@ -992,70 +1184,4 @@
     return join( "-", map { $_->name } @{ $self->{'region_stack'} || [] }, @_ );
 }
 
-=head3 include_css
-
-Returns a C<< <link> >> tag for the compressed CSS
-
-=cut
-
-sub include_css {
-    my $self = shift;
-    
-    if ( Jifty->config->framework('DevelMode') ) {
-        $self->out(
-            '<link rel="stylesheet" type="text/css" '
-            . 'href="/static/css/main.css" />'
-        );
-    }
-    else {
-        $self->generate_css;
-    
-        $self->out(
-            '<link rel="stylesheet" type="text/css" href="/__jifty/css/'
-            . __PACKAGE__->cached_css_digest . '.css" />'
-        );
-    }
-    
-    return '';
-}
-
-=head3 generate_css
-
-Checks if the compressed CSS is generated, and if it isn't, generates
-and caches it.
-
-=cut
-
-sub generate_css {
-    my $self = shift;
-    
-    if (not defined __PACKAGE__->cached_css_digest
-            or Jifty->config->framework('DevelMode'))
-    {
-        Jifty->log->debug("Generating CSS...");
-        
-        my $css_dir = File::Spec->catdir(
-                           Jifty->config->framework('Web')->{'StaticRoot'},
-                           'css'
-                      );
-
-        CSS::Squish->roots(
-            File::Spec->catdir(
-                Jifty->config->framework('Web')->{'DefaultStaticRoot'},
-                'css'
-            )
-        );
-        
-        my $css = CSS::Squish->concatenate(
-                     Jifty::Util->absolute_path(
-                         File::Spec->catpath( '', $css_dir, 'main.css' )
-                     )
-                  );
-
-        __PACKAGE__->cached_css( $css );
-        __PACKAGE__->cached_css_digest( md5_hex( $css ) );
-        __PACKAGE__->cached_css_mtime( time );
-    }
-}
-
 1;

Added: jifty/trunk/share/web/static/js/setup_jsan.js
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/static/js/setup_jsan.js	Tue Jun 20 13:06:41 2006
@@ -0,0 +1,13 @@
+/* Setup JSAN for Jifty defaults */
+JSAN.includePath = [ "/static/js/jsan" ];
+JSAN.errorLevel  = "none";
+
+/*
+ * Stub out JSAN.use to avoid Ajax loading of JSAN libs if they've
+ * already been loaded by a <script> tag
+ */
+JSAN._use = JSAN.use;
+JSAN.use  = function() {
+    if ( !arguments[0] ) JSAN._use(arguments);
+};
+

Added: jifty/trunk/share/web/templates/__jifty/js/dhandler
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/templates/__jifty/js/dhandler	Tue Jun 20 13:06:41 2006
@@ -0,0 +1,25 @@
+<%init>
+if ( $m->dhandler_arg !~ /^[0-9a-f]{32}\.js$/ ) {
+    # This doesn't look like a real request for squished JS,
+    # so redirect to a more failsafe place
+    Jifty->web->redirect( "/static/js/" . $m->dhandler_arg );
+}
+
+Jifty->web->generate_javascript;
+
+use HTTP::Date ();
+
+if ( Jifty->handler->cgi->http('If-Modified-Since')
+        and $m->dhandler_arg eq Jifty->web->cached_javascript_digest . '.js' )
+{
+    Jifty->log->debug("Returning 304 for cached javascript");
+    $r->header_out( Status => 304 );
+    return;
+}
+
+$r->content_type("application/x-javascript");
+$r->header_out( 'Expires' => HTTP::Date::time2str(time + 31536000) );
+
+Jifty->web->out( Jifty->web->cached_javascript );
+return;
+</%init>

Modified: jifty/trunk/share/web/templates/_elements/header
==============================================================================
--- jifty/trunk/share/web/templates/_elements/header	(original)
+++ jifty/trunk/share/web/templates/_elements/header	Tue Jun 20 13:06:41 2006
@@ -7,7 +7,7 @@
   <title><% _($title) %></title>
   
   <% Jifty->web->include_css %>
-  <& javascript &> 
+  <% Jifty->web->include_javascript %> 
 </head>
 <%args>
 $title => ""


More information about the Jifty-commit mailing list