fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-12-05 03:26 pm

[dw-free] Need a way to include a subset of script / CSS files

[commit: http://hg.dwscoalition.org/dw-free/rev/15c9ff5272b1]

http://bugs.dwscoalition.org/show_bug.cgi?id=4065

Pass in parameters to LJ::res_includes so you can optionally choose to not
include js variables and libraries. Add a fragment argument to
DW::Template->render_template which handles inclusion of resources for page
fragments.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/DW/Controller/Entry.pm
  • cgi-bin/DW/Template.pm
  • cgi-bin/LJ/Web.pm
  • views/entry/options.tt
--------------------------------------------------------------------------------
diff -r 5f87391fecc9 -r 15c9ff5272b1 cgi-bin/DW/Controller/Entry.pm
--- a/cgi-bin/DW/Controller/Entry.pm	Mon Dec 05 22:31:48 2011 +0800
+++ b/cgi-bin/DW/Controller/Entry.pm	Mon Dec 05 23:26:21 2011 +0800
@@ -1067,10 +1067,11 @@
 
     my $vars = _options( $rv->{remote} );
     $vars->{use_js} = 1;
+
     my $r = DW::Request->get;
     $r->status( @{$vars->{error_list} || []} ? HTTP_BAD_REQUEST : HTTP_OK );
 
-    return DW::Template->render_template( 'entry/options.tt', $vars, { no_sitescheme => 1 } );
+    return DW::Template->render_template( 'entry/options.tt', $vars, { fragment => 1 } );
 }
 
 sub _load_visible_panels {
diff -r 5f87391fecc9 -r 15c9ff5272b1 cgi-bin/DW/Template.pm
--- a/cgi-bin/DW/Template.pm	Mon Dec 05 22:31:48 2011 +0800
+++ b/cgi-bin/DW/Template.pm	Mon Dec 05 23:26:21 2011 +0800
@@ -316,6 +316,12 @@
         $r->print( $out );
 
         return $r->OK;
+    } elsif ( $extra->{fragment} ) {
+        LJ::set_active_resource_group( "fragment" );
+        $out .= LJ::res_includes( nojs => 1, nolib => 1 );
+        $r->print( $out );
+
+        return $r->OK;
     } elsif ( $scheme->engine eq 'tt' ) {
         $r->content_type("text/html; charset=utf-8");
         $r->print( $class->render_scheme( $scheme, $out, $extra ) );
diff -r 5f87391fecc9 -r 15c9ff5272b1 cgi-bin/LJ/Web.pm
--- a/cgi-bin/LJ/Web.pm	Mon Dec 05 22:31:48 2011 +0800
+++ b/cgi-bin/LJ/Web.pm	Mon Dec 05 23:26:21 2011 +0800
@@ -2484,6 +2484,12 @@
 }
 
 sub res_includes {
+    my ( %opts ) = @_;
+
+    my $include_js = ! $opts{nojs};
+    my $include_links = ! $opts{nolinks};
+    my $include_libs = ! $opts{nolib};
+
     # TODO: automatic dependencies from external map and/or content of files,
     # currently it's limited to dependencies on the order you call LJ::need_res();
     my $ret = "";
@@ -2505,158 +2511,166 @@
         $wstatprefix = $LJ::WSTATPREFIX;
     }
 
-    # find current journal
-    my $r = DW::Request->get;
-    my $journal_base = '';
-    my $journal = '';
-    if ($r) {
-        my $journalid = $r->note('journalid');
-
-        my $ju;
-        $ju = LJ::load_userid($journalid) if $journalid;
-
-        if ($ju) {
-            $journal_base = $ju->journal_base;
-            $journal = $ju->{user};
+    if ( $include_js ) {
+        # find current journal
+        my $r = DW::Request->get;
+        my $journal_base = '';
+        my $journal = '';
+        if ($r) {
+            my $journalid = $r->note('journalid');
+
+            my $ju;
+            $ju = LJ::load_userid($journalid) if $journalid;
+
+            if ($ju) {
+                $journal_base = $ju->journal_base;
+                $journal = $ju->{user};
+            }
         }
+
+        my $remote = LJ::get_remote();
+        my $hasremote = $remote ? 1 : 0;
+
+        # ctxpopup prop
+        my $ctxpopup_icons = 1;
+        my $ctxpopup_userhead = 1;
+        $ctxpopup_icons = 0 if $remote && ! $remote->opt_ctxpopup_icons;
+        $ctxpopup_userhead = 0 if $remote && ! $remote->opt_ctxpopup_userhead;
+
+        # poll for esn inbox updates?
+        my $inbox_update_poll = LJ::is_enabled('inbox_update_poll');
+
+        # are media embeds enabled?
+        my $embeds_enabled = LJ::is_enabled('embed_module');
+
+        # esn ajax enabled?
+        my $esn_async = LJ::is_enabled('esn_ajax');
+
+        my %site = (
+                    imgprefix => "$imgprefix",
+                    siteroot => "$siteroot",
+                    statprefix => "$statprefix",
+                    currentJournalBase => "$journal_base",
+                    currentJournal => "$journal",
+                    has_remote => $hasremote,
+                    ctx_popup => ( $ctxpopup_icons || $ctxpopup_userhead ),
+                    ctx_popup_icons => $ctxpopup_icons,
+                    ctx_popup_userhead => $ctxpopup_userhead,
+                    inbox_update_poll => $inbox_update_poll,
+                    media_embed_enabled => $embeds_enabled,
+                    esn_async => $esn_async,
+                    );
+
+        my $site_params = LJ::js_dumper(\%site);
+        my $site_param_keys = LJ::js_dumper([keys %site]);
+
+        # include standard JS info
+        $ret .= qq {
+            <script type="text/javascript">
+                var Site;
+                if (!Site)
+                    Site = {};
+
+                var site_p = $site_params;
+                var site_k = $site_param_keys;
+                for (var i = 0; site_k.length > i; i++) {
+                    Site[site_k[i]] = site_p[site_k[i]];
+                }
+           </script>
+        };
     }
 
-    my $remote = LJ::get_remote();
-    my $hasremote = $remote ? 1 : 0;
-
-    # ctxpopup prop
-    my $ctxpopup_icons = 1;
-    my $ctxpopup_userhead = 1;
-    $ctxpopup_icons = 0 if $remote && ! $remote->opt_ctxpopup_icons;
-    $ctxpopup_userhead = 0 if $remote && ! $remote->opt_ctxpopup_userhead;
-
-    # poll for esn inbox updates?
-    my $inbox_update_poll = LJ::is_enabled('inbox_update_poll');
-
-    # are media embeds enabled?
-    my $embeds_enabled = LJ::is_enabled('embed_module');
-
-    # esn ajax enabled?
-    my $esn_async = LJ::is_enabled('esn_ajax');
-
-    my %site = (
-                imgprefix => "$imgprefix",
-                siteroot => "$siteroot",
-                statprefix => "$statprefix",
-                currentJournalBase => "$journal_base",
-                currentJournal => "$journal",
-                has_remote => $hasremote,
-                ctx_popup => ( $ctxpopup_icons || $ctxpopup_userhead ),
-                ctx_popup_icons => $ctxpopup_icons,
-                ctx_popup_userhead => $ctxpopup_userhead,
-                inbox_update_poll => $inbox_update_poll,
-                media_embed_enabled => $embeds_enabled,
-                esn_async => $esn_async,
-                );
-
-    my $site_params = LJ::js_dumper(\%site);
-    my $site_param_keys = LJ::js_dumper([keys %site]);
-
-    # include standard JS info
-    $ret .= qq {
-        <script type="text/javascript">
-            var Site;
-            if (!Site)
-                Site = {};
-
-            var site_p = $site_params;
-            var site_k = $site_param_keys;
-            for (var i = 0; site_k.length > i; i++) {
-                Site[site_k[i]] = site_p[site_k[i]];
-            }
-       </script>
-    };
-
-    my $now = time();
-    my %list;   # type -> [];
-    my %oldest; # type -> $oldest
-    my %included = ();
-    my $add = sub {
-        my ($type, $what, $modtime) = @_;
-
-        # the same file may have been included twice
-        # if it was in two different groups and not JS
-        # so add another check here
-        next if $included{$what};
-        $included{$what} = 1;
-
-        # in the concat-res case, we don't directly append the URL w/
-        # the modtime, but rather do one global max modtime at the
-        # end, which is done later in the tags function.
-        $what .= "?v=$modtime" unless $do_concat;
-
-        push @{$list{$type} ||= []}, $what;
-        $oldest{$type} = $modtime if $modtime > ( $oldest{$type} || 0 );
-    };
-
-    foreach my $by_priority ( reverse @LJ::NEEDED_RES ) {
-        next unless $by_priority;
-
-        foreach my $resrow ( @$by_priority ) {
-            # determine if this resource is part of the resource group that is active;
-            # or 'default' if no group explicitly active
-            my ( $group, $key ) = @$resrow;
-            next if
-                $group ne 'all' &&
-                ( ( defined $LJ::ACTIVE_RES_GROUP && $group ne $LJ::ACTIVE_RES_GROUP ) ||
-                  ( ! defined $LJ::ACTIVE_RES_GROUP && $group ne 'default' ) );
-
-            my $path;
-            my $mtime = _file_modtime($key, $now);
-            if ($key =~ m!^stc/fck/! || $LJ::FORCE_WSTAT{$key}) {
-                $path = "w$key";  # wstc/ instead of stc/
-            } else {
-                $path = $key;
-            }
-
-            # if we want to also include a local version of this file, include that too
-            if (@LJ::USE_LOCAL_RES) {
-                if (grep { lc $_ eq lc $key } @LJ::USE_LOCAL_RES) {
-                    my $inc = $key;
-                    $inc =~ s/(\w+)\.(\w+)$/$1-local.$2/;
-                    LJ::need_res($inc);
+    if ( $include_links ) {
+        my $now = time();
+        my %list;   # type -> [];
+        my %oldest; # type -> $oldest
+        my %included = ();
+        my $add = sub {
+            my ($type, $what, $modtime) = @_;
+
+            # the same file may have been included twice
+            # if it was in two different groups and not JS
+            # so add another check here
+            next if $included{$what};
+            $included{$what} = 1;
+
+            # in the concat-res case, we don't directly append the URL w/
+            # the modtime, but rather do one global max modtime at the
+            # end, which is done later in the tags function.
+            $what .= "?v=$modtime" unless $do_concat;
+
+            push @{$list{$type} ||= []}, $what;
+            $oldest{$type} = $modtime if $modtime > ( $oldest{$type} || 0 );
+        };
+
+        # we may not want to pull in the libraries again, say if we're pulling in elements via an ajax load
+        delete $LJ::NEEDED_RES[$LJ::LIB_RES_PRIORITY] unless $include_libs;
+
+        foreach my $by_priority ( reverse @LJ::NEEDED_RES ) {
+            next unless $by_priority;
+
+            foreach my $resrow ( @$by_priority ) {
+                # determine if this resource is part of the resource group that is active;
+                # or 'default' if no group explicitly active
+                my ( $group, $key ) = @$resrow;
+                next if
+                    $group ne 'all' &&
+                    ( ( defined $LJ::ACTIVE_RES_GROUP && $group ne $LJ::ACTIVE_RES_GROUP ) ||
+                      ( ! defined $LJ::ACTIVE_RES_GROUP && $group ne 'default' ) );
+
+                my $path;
+                my $mtime = _file_modtime($key, $now);
+                if ($key =~ m!^stc/fck/! || $LJ::FORCE_WSTAT{$key}) {
+                    $path = "w$key";  # wstc/ instead of stc/
+                } else {
+                    $path = $key;
+                }
+
+                # if we want to also include a local version of this file, include that too
+                if (@LJ::USE_LOCAL_RES) {
+                    if (grep { lc $_ eq lc $key } @LJ::USE_LOCAL_RES) {
+                        my $inc = $key;
+                        $inc =~ s/(\w+)\.(\w+)$/$1-local.$2/;
+                        LJ::need_res($inc);
+                    }
+                }
+
+                if ($path =~ m!^js/(.+)!) {
+                    $add->('js', $1, $mtime);
+                } elsif ($path =~ /\.css$/ && $path =~ m!^(w?)stc/(.+)!) {
+                    $add->("${1}stccss", $2, $mtime);
+                } elsif ($path =~ /\.js$/ && $path =~ m!^(w?)stc/(.+)!) {
+                    $add->("${1}stcjs", $2, $mtime);
                 }
             }
-
-            if ($path =~ m!^js/(.+)!) {
-                $add->('js', $1, $mtime);
-            } elsif ($path =~ /\.css$/ && $path =~ m!^(w?)stc/(.+)!) {
-                $add->("${1}stccss", $2, $mtime);
-            } elsif ($path =~ /\.js$/ && $path =~ m!^(w?)stc/(.+)!) {
-                $add->("${1}stcjs", $2, $mtime);
+        }
+
+        my $tags = sub {
+            my ($type, $template) = @_;
+            my $list;
+            return unless $list = $list{$type};
+
+            if ($do_concat) {
+                my $csep = join(',', @$list);
+                $csep .= "?v=" . $oldest{$type};
+                $template =~ s/__+/??$csep/;
+                $ret .= $template;
+            } else {
+                foreach my $item (@$list) {
+                    my $inc = $template;
+                    $inc =~ s/__+/$item/;
+                    $ret .= $inc;
+                }
             }
-        }
+        };
+
+        $tags->("js",      "<script type=\"text/javascript\" src=\"$jsprefix/___\"></script>\n");
+        $tags->("stccss",  "<link rel=\"stylesheet\" type=\"text/css\" href=\"$statprefix/___\" />\n");
+        $tags->("wstccss", "<link rel=\"stylesheet\" type=\"text/css\" href=\"$wstatprefix/___\" />\n");
+        $tags->("stcjs",   "<script type=\"text/javascript\" src=\"$statprefix/___\"></script>\n");
+        $tags->("wstcjs",  "<script type=\"text/javascript\" src=\"$wstatprefix/___\"></script>\n");
     }
 
-    my $tags = sub {
-        my ($type, $template) = @_;
-        my $list;
-        return unless $list = $list{$type};
-
-        if ($do_concat) {
-            my $csep = join(',', @$list);
-            $csep .= "?v=" . $oldest{$type};
-            $template =~ s/__+/??$csep/;
-            $ret .= $template;
-        } else {
-            foreach my $item (@$list) {
-                my $inc = $template;
-                $inc =~ s/__+/$item/;
-                $ret .= $inc;
-            }
-        }
-    };
-
-    $tags->("js",      "<script type=\"text/javascript\" src=\"$jsprefix/___\"></script>\n");
-    $tags->("stccss",  "<link rel=\"stylesheet\" type=\"text/css\" href=\"$statprefix/___\" />\n");
-    $tags->("wstccss", "<link rel=\"stylesheet\" type=\"text/css\" href=\"$wstatprefix/___\" />\n");
-    $tags->("stcjs",   "<script type=\"text/javascript\" src=\"$statprefix/___\"></script>\n");
-    $tags->("wstcjs",  "<script type=\"text/javascript\" src=\"$wstatprefix/___\"></script>\n");
     return $ret;
 }
 
diff -r 5f87391fecc9 -r 15c9ff5272b1 views/entry/options.tt
--- a/views/entry/options.tt	Mon Dec 05 22:31:48 2011 +0800
+++ b/views/entry/options.tt	Mon Dec 05 23:26:21 2011 +0800
@@ -15,6 +15,7 @@
 [%- sections.windowtitle = ".title" | ml -%]
 
 [% dw.need_res( "stc/simple-form.css" ) %]
+[%- dw.need_res( { group => "fragment" }, "stc/simple-form.css", "stc/jquery.postoptions.css", "js/jquery.postoptions.js" ) -%]
 
 <form action="/entry/options" method="post" id="post-options" class="simple-form">
 
@@ -123,12 +124,4 @@
 </form>
 
 
-[%- IF use_js -%]
-<link rel="stylesheet" type="text/css" href="[%site.statroot%]/simple-form.css">
-<link rel="stylesheet" type="text/css" href="[%site.statroot%]/jquery.postoptions.css">
-
-
-<script type="text/javascript" src="[%site.jsroot%]/jquery.postoptions.js"></script>
-[%- ELSE -%]
-    <p><a href="[%site.root%]/entry/new">[% ".back" | ml %]</a></p>
-[%- END -%]
+[%- UNLESS use_js -%]<p><a href="[%site.root%]/entry/new">[% ".back" | ml %]</a></p>[%- END -%]
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
No Subject Icon Selected
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org