Simplify JS usage in templates and move jQuery into js.py.

When g.uncompressedJS is true, an unminified version of jQuery will now
be loaded.
This commit is contained in:
Max Goodman
2011-06-21 09:18:13 -07:00
parent 9f7659dcf7
commit 7d591a48f3
14 changed files with 40 additions and 42 deletions

View File

@@ -146,6 +146,8 @@ class Globals(object):
"""
global_conf.setdefault("debug", False)
# slop over all variables to start with
for k, v in global_conf.iteritems():
if not k.startswith("_") and not hasattr(self, k):
@@ -163,7 +165,7 @@ class Globals(object):
% (k, v, self.choice_props[k]))
v = self.choice_props[k][v]
setattr(self, k, v)
self.paths = paths
self.running_as_script = global_conf.get('running_as_script', False)

View File

@@ -160,8 +160,25 @@ class LocalizedModule(Module):
url = os.path.join(g.static_path, name + "." + get_lang()[0] + ext)
return script_tag.format(src=static(url))
class JQuery(Module):
def __init__(self, cdn_src=None):
self.cdn_src = cdn_src or "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery"
def build(self, closure):
pass
def use(self):
from r2.lib.template_helpers import static
if c.secure:
return script_tag.format(src=static("jquery.js"))
else:
ext = ".js" if g.uncompressedJS else ".min.js"
return script_tag.format(src=self.cdn_src+ext)
module = {}
module["jquery"] = JQuery()
module["reddit"] = LocalizedModule("reddit.js",
"jquery.json.js",
"jquery.reddit.js",
@@ -188,6 +205,9 @@ module["sponsored"] = Module("sponsored.js",
module["flot"] = Module("jquery.flot.js")
def use(*names):
return "\n".join(module[name].use() for name in names)
def build_reddit_js():
closure = ClosureCompiler("r2/lib/contrib/closure_compiler/compiler.jar")
for name in module:

View File

@@ -64,22 +64,6 @@ def static(path):
return os.path.join(c.site.static_path, path) + query
external_resources = {
"jquery.js": {
True: lambda: static("jquery.js"),
False: lambda: "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"
}
}
def external(name):
"""
Look up a named external URL from a static mapping. Helper for making
frequently used URLs consistent across templates.
"""
return external_resources[name][c.secure]()
def s3_https_if_secure(url):
return url if not c.secure else url.replace("http://", "https://s3.amazonaws.com/")

View File

@@ -20,7 +20,7 @@
## CondeNet, Inc. All Rights Reserved.
################################################################################
<%!
from r2.lib.template_helpers import static, external
from r2.lib.template_helpers import static
from r2.lib import js
%>
<%namespace file="utils.html" import="js_preamble"/>
@@ -42,8 +42,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
${self.robots()}
<link rel="stylesheet" href="${static('compact.css')}" type="text/css" media="screen" />
<script src="${external('jquery.js')}" type="text/javascript"></script>
${unsafe(js.module['mobile'].use())}
${unsafe(js.use('jquery', 'mobile'))}
<script type="text/javascript">
${js_preamble()}
</script>

View File

@@ -20,7 +20,7 @@
## CondeNet, Inc. All Rights Reserved.
################################################################################
<%!
from r2.lib.template_helpers import get_domain, static, external
from r2.lib.template_helpers import get_domain
from r2.lib.utils import query_string
%>
<%inherit file="reddit.html"/>
@@ -31,8 +31,8 @@
<%def name="javascript()">
<script src="${external('jquery.js')}" type="text/javascript"></script>
<script src="${static('jquery.reddit.js')}" type="text/javascript"></script>
<% from r2.lib import js %>
${unsafe(js.use('jquery', 'reddit'))}
<script type="text/javascript">
function showcover() { $("#cover").show(); }
</script>

View File

@@ -20,7 +20,7 @@
## CondeNet, Inc. All Rights Reserved.
################################################################################
<%
from r2.lib.template_helpers import add_sr, static, external
from r2.lib.template_helpers import add_sr, static
from r2.lib import js
from r2.lib.utils import query_string
from r2.lib.filters import _force_unicode
@@ -30,8 +30,7 @@
<html>
<head>
<title>${thing.title}</title>
<script src="${external('jquery.js')}" type="text/javascript"></script>
${unsafe(js.module['mobile'].use())}
${unsafe(js.use('jquery', 'mobile'))}
<script type="text/javascript">
${js_preamble()}
</script>

View File

@@ -4,10 +4,8 @@
<html>
<head>
<title>escappit</title>
<% from r2.lib.template_helpers import static, external %>
<script src="${external('jquery.js')}" type="text/javascript"></script>
<script src="${static('jquery.json.js')}" type="text/javascript"></script>
<script src="${static('jquery.reddit.js')}" type="text/javascript"></script>
<% from r2.lib import js %>
${unsafe(js.use('jquery', 'reddit'))}
</head>
<body>
<script type="text/javascript">

View File

@@ -25,7 +25,7 @@
%>
<%namespace file="utils.html" import="error_field"/>
${unsafe(js.module['sponsored'].use())}
${unsafe(js.use('sponsored'))}
<script type="text/javascript">
$(function() { update_bid("#bid-field"); });
</script>

View File

@@ -31,7 +31,7 @@
%>
<h1>Sponsored link calendar</h1>
${unsafe(js.module['flot'].use())}
${unsafe(js.use('flot'))}
%if not c.user_is_sponsor:
<div class="instructions">

View File

@@ -28,7 +28,7 @@
return locale.format('%d', x, True)
%>
${unsafe(js.module['flot'].use())}
${unsafe(js.use('flot'))}
<div class="promoted-traffic">

View File

@@ -33,7 +33,7 @@
import="error_field, checkbox, image_upload, reddit_selector" />
<%namespace name="utils" file="utils.html"/>
${unsafe(js.module['sponsored'].use())}
${unsafe(js.use('sponsored'))}
<script type="text/javascript">
$(function() { update_bid("*[name=bid]"); });
</script>

View File

@@ -111,12 +111,8 @@
</%def>
<%def name="javascript()">
<%
from r2.lib.template_helpers import static, external
from r2.lib import js
%>
<script src="${external('jquery.js')}" type="text/javascript"></script>
${unsafe(js.module['reddit'].use())}
<% from r2.lib import js %>
${unsafe(js.use('jquery', 'reddit'))}
</%def>
<%def name="javascript_run()">

View File

@@ -28,7 +28,7 @@
return locale.format('%d', x, True)
%>
${unsafe(js.module['flot'].use())}
${unsafe(js.use('flot'))}
<%def name="daily_summary()">
<%

View File

@@ -28,7 +28,7 @@
<%namespace file="utils.html"
import="error_field, checkbox, image_upload, reddit_selector" />
${unsafe(js.module['sponsored'].use())}
${unsafe(js.use('sponsored'))}
<div class="pretty-form campaign">
<%utils:line_field title="${_('create a roadblock')}" id="campaign-field">