Permalink
Browse files

Use a sidebar placeholder and load content with javascript.

When a reddit page is loaded the side bar contains placeholders for
recent comments and posts.

If javascript is off then a link may be followed and if javascript is
on then the content of the side box will be replaced with the response
from an ajax request.

This prevents google indexing rapidly changing sidebar content.
  • Loading branch information...
1 parent f4749e0 commit 4a3ae0a7f92cb5d53764cc4fa3c0175c4b982576 Darius Powell committed Apr 14, 2009
View
12 r2/r2/controllers/api.py
@@ -36,7 +36,8 @@
from r2.lib.utils import query_string, to36, timefromnow
from r2.lib.wrapped import Wrapped
from r2.lib.pages import FriendList, ContributorList, ModList, \
- BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage
+ BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage, \
+ RecentArticles, RecentComments
from r2.lib.menus import CommentSortMenu
from r2.lib.translation import Translator
@@ -898,7 +899,14 @@ def POST_delete_sr_header(self, res):
# reset the status boxes
res._update('img-status', innerHTML = _("Deleted"))
res._update('status', innerHTML = "")
-
+
+ def GET_side_posts(self, *a, **kw):
+ """Return HTML snippet of the recent posts for the side bar."""
+ return RecentArticles().render()
+
+ def GET_side_comments(self, *a, **kw):
+ """Return HTML snippet of the recent comments for the side bar."""
+ return RecentComments().render()
def GET_upload_sr_img(self, *a, **kw):
"""
View
26 r2/r2/lib/pages/pages.py
@@ -133,8 +133,8 @@ def rightbox(self):
if self.extension_handling:
ps.append(FeedLinkBar())
- ps.append(RecentComments())
- ps.append(RecentArticles())
+ ps.append(SideBoxPlaceholder('side-comments', _('Recent Comments'), '/comments'))
+ ps.append(SideBoxPlaceholder('side-posts', _('Recent Posts'), '/recentposts'))
for feed_url in g.feedbox_urls:
ps.append(FeedBox(feed_url))
@@ -248,6 +248,17 @@ class LoginFormWide(Wrapped):
"""generates a login form suitable for the 300px rightbox."""
pass
+class SideBoxPlaceholder(Wrapped):
+ """A minimal side box with a heading and an anchor.
+
+ If javascript is off the anchor may be followed and if it is on
+ then javascript will replace the content of the div with the HTML
+ result of an ajax request.
+ """
+
+ def __init__(self, node_id, link_text, link_path):
+ Wrapped.__init__(self, node_id=node_id, link_text=link_text, link_path=link_path)
+
class RecentItems(Wrapped):
def __init__(self, *args, **kwargs):
self.things = self.init_builder()
@@ -270,6 +281,17 @@ def wrap_thing(thing):
return w
+ def render(self, *a, **kw):
+ """Overrides default Wrapped.render to do space compression as well.
+
+ In addition, unlike Wrapped.render, the result is in the form of a pylons
+ Response object with it's content set.
+ """
+ res = Wrapped.render(self, *a, **kw)
+ res = spaceCompress(res)
+ c.response.content = res
+ return c.response
+
class RecentComments(RecentItems):
def query(self):
return c.site.get_comments('new', 'all')
View
16 r2/r2/public/static/reddit_piece.js
@@ -80,6 +80,22 @@ function init() {
if ( reddit_thing_info.fetch && reddit_thing_info.fetch.length != 0 )
updateLinks(reddit_thing_info.fetch);
update_reddit_count();
+
+ /* initiate ajax requests to populate the side bar */
+ populate_side_bar('side-posts');
+ populate_side_bar('side-comments');
+}
+
+function populate_side_bar(id) {
+ var node = $(id);
+ if (node) {
+ var path = '/api/' + id.replace('-', '_');
+ new Ajax.Request(path, {
+ method: 'get',
+ onSuccess: function(response) {
+ node.innerHTML = response.responseText;
+ }});
+ }
}
function updateLinks(f) {
View
26 r2/r2/templates/recentarticles.html
@@ -1,15 +1,13 @@
<%namespace file="utils.html" import="plain_link"/>
-<div id="side-posts" class="sidebox">
- <h2>
- ${plain_link(_('Recent Posts'), '/recentposts')}:
- </h2>
- <%
- t = thing.things
- article_generator = t.item_iter(t.get_items())
- %>
- %for a in article_generator:
- <div class="reddit-link">
- ${a.render()}
- </div>
- %endfor:
-</div>
+<h2>
+ ${plain_link(_('Recent Posts'), '/recentposts')}:
+</h2>
+<%
+ t = thing.things
+ article_generator = t.item_iter(t.get_items())
+%>
+%for a in article_generator:
+ <div class="reddit-link">
+ ${a.render()}
+ </div>
+%endfor:
View
26 r2/r2/templates/recentcomments.html
@@ -1,15 +1,13 @@
<%namespace file="utils.html" import="plain_link"/>
-<div id="side-comments" class="sidebox">
- <h2>
- ${plain_link(_('Recent Comments'), '/comments')}:
- </h2>
- <%
- t = thing.things
- comment_generator = t.item_iter(t.get_items())
- %>
- %for a in comment_generator:
- <div class="inline-comment">
- ${a.render()}
- </div>
- %endfor:
-</div>
+<h2>
+ ${plain_link(_('Recent Comments'), '/comments')}:
+</h2>
+<%
+ t = thing.things
+ comment_generator = t.item_iter(t.get_items())
+%>
+%for a in comment_generator:
+ <div class="inline-comment">
+ ${a.render()}
+ </div>
+%endfor:
View
28 r2/r2/templates/sideboxplaceholder.html
@@ -0,0 +1,28 @@
+## The contents of this file are subject to the Common Public Attribution
+## License Version 1.0. (the "License"); you may not use this file except in
+## compliance with the License. You may obtain a copy of the License at
+## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
+## License Version 1.1, but Sections 14 and 15 have been added to cover use of
+## software over a computer network and provide for limited attribution for the
+## Original Developer. In addition, Exhibit A has been modified to be consistent
+## with Exhibit B.
+##
+## Software distributed under the License is distributed on an "AS IS" basis,
+## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
+## the specific language governing rights and limitations under the License.
+##
+## The Original Code is Reddit.
+##
+## The Original Developer is the Initial Developer. The Initial Developer of
+## the Original Code is CondeNet, Inc.
+##
+## All portions of the code written by CondeNet are Copyright (c) 2006-2008
+## CondeNet, Inc. All Rights Reserved.
+################################################################################
+
+<%namespace file="utils.html" import="plain_link"/>
+<div id="${thing.node_id}" class="sidebox">
+ <h2>
+ ${plain_link(thing.link_text, thing.link_path)}
+ </h2>
+</div>

0 comments on commit 4a3ae0a

Please sign in to comment.