Ensure that when clicking the browser's 'back' button after clicking

an organic link that the organic link is always available on the
page
This commit is contained in:
ketralnis
2009-03-05 15:02:23 -08:00
parent 67de80eb9c
commit ac7ff91324
4 changed files with 104 additions and 24 deletions

View File

@@ -1227,15 +1227,21 @@ class ApiController(RedditController):
tr._is_enabled = True
@validatedForm(links = VByName('links', thing_cls = Link, multiple = True))
def POST_fetch_links(self, form, jquery, links):
@validatedForm(links = VByName('links', thing_cls = Link, multiple = True),
show = VByName('show', thing_cls = Link, multiple = False))
def POST_fetch_links(self, form, jquery, links, show):
b = IDBuilder([l._fullname for l in links],
wrap = ListingController.builder_wrapper)
l = OrganicListing(b)
l.num_margin = 0
l.mid_margin = 0
jquery.replace_things(l, stubs = True)
if show:
jquery('.organic-listing .link:visible').hide()
jquery('.organic-listing .id-%s' % show._fullname).show()
@noresponse(VUser(),
ui_elem = VOneOf('id', ('organic',)))
def POST_disable_ui(self, ui_elem):

View File

@@ -22,23 +22,27 @@
from r2.models import *
from r2.lib import promote
import string
import random
def populate(sr_name = 'reddit.com', sr_title = "reddit.com: what's new online",
num = 100):
create_accounts(num)
a = Author._query(limit = 1)
a = list(Account._query(limit = 1))[0]
try:
sr = Subreddit._new(name = sr_name, title = sr_title,
ip = '0.0.0.0', author_id = a._id)
sr._commit()
except SubredditExists:
pass
sr = Subreddit._new(name = sr_name, title = sr_title,
ip = '0.0.0.0', author_id = a._id)
sr._commit()
create_links(num)
def create_accounts(num):
chars = 'abcdefghijklmnopqrztuvwxyz'
for i in range(num):
name_ext = ''.join([ random.choice(chars)
name_ext = ''.join([ random.choice(string.letters)
for x
in range(int(random.uniform(1, 10))) ])
name = 'test_' + name_ext

View File

@@ -248,6 +248,16 @@ $.things = function() {
return $(sel);
};
$.fn.things = function() {
/*
* try to find all things that occur below a given selector, like:
* $('.organic-listing').things('t3_12345')
*/
var sel = $.map(arguments, function(x) { return ".thing.id-" + x; })
.join(", ");
return this.find(sel);
};
$.listing = function(name) {
/*
* Given an element name (a sitetable ID or a thing ID, with

View File

@@ -496,7 +496,8 @@ function updateEventHandlers(thing) {
/* set the click cookie. */
add_thing_to_cookie(this, "click");
/* remember this as the last thing clicked */
last_click(thing);
var wasorganic = $(this).parents('.organic-listing').length > 0;
last_click(thing, wasorganic);
});
if (listing.filter(".organic-listing").length) {
@@ -528,20 +529,80 @@ function updateEventHandlers(thing) {
}
};
function last_click(thing) {
var cookie = "last_thing";
if(thing) {
var data = {href: window.location.href,
what: $(thing).thing_id()};
$.cookie_write({name: cookie, data: data});
}
else {
var current = $.cookie_read(cookie).data;
if(current && current.href == window.location.href) {
$.cookie_write({name: cookie, data: ""});
return current.what;
function last_click(thing, organic) {
/* called with zero arguments, marks the last-clicked item on this
page (to which the user probably clicked the 'back' button in
their browser). Otherwise sets the last-clicked item to the
arguments passed */
var cookie = "last_thing";
if(thing) {
var data = {href: window.location.href,
what: $(thing).thing_id(),
organic: organic};
$.cookie_write({name: cookie, data: data});
} else {
var current = $.cookie_read(cookie).data;
if(current && current.href == window.location.href) {
/* if they got there organically, make sure that it's in the
organic box */
var olisting = $('.organic-listing');
if(current.organic && olisting.length == 1) {
if(olisting.find('.thing:visible').thing_id() == current.what) {
/* if it's available in the organic box, *and* it's the one
that's already shown, do nothing */
} else {
var thing = olisting.things(current.what);
if(thing.length > 0 && !thing.hasClass('stub')) {
/* if it's available in the organic box and not a stub,
switch index to it */
olisting.find('.thing:visible').hide();
thing.show();
} else {
/* we're going to have to put it into the organic box
somehow */
var thingelsewhere = $.things(current.what).filter(':not(.stub):first');
if(thingelsewhere.length > 0) {
/* if it's available on the page somewhere else, we can
clone it up into the organic box rather than go to
the server for it */
/* if there was a stub before, remove it */
thing.remove();
var othercopy = thingelsewhere.clone();
olisting.find('.thing:visible').before(othercopy).hide();
othercopy.show();
} else {
/* either it's available in the organic box, but the
data there is a stub, or it's not available at
all. either way, we need a server round-trip */
thing.remove();
/* and add a new stub */
olisting.find('.thing:visible')
.before('<div class="thing id-'+current.what+' stub" style="display: none"></div');
/* and ask the server to fill in that stub */
$.request('fetch_links',
{links: [current.what],
show: current.what,
listing: olisting.attr('id')});
}
}
}
}
/* mark it in the list */
$.things(current.what).addClass("last-clicked");
/* and wipe the cookie */
$.cookie_write({name: cookie, data: ""});
}
}
};
function login(elem) {
@@ -580,9 +641,8 @@ $(function() {
if(reddit.cur_site)
update_reddit_count(reddit.cur_site);
var last = last_click();
if(last)
$.things(last).addClass("last-clicked");
/* visually mark the last-clicked entry */
last_click();
});