diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index 018720da1..2ea5876bc 100755 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -3097,9 +3097,8 @@ class ApiController(RedditController, OAuth2ResourceController): @validatedForm(link=nop('link'), - campaign=nop('campaign'), - show=VBoolean('show')) - def GET_fetch_promo(self, form, jquery, link, campaign, show): + campaign=nop('campaign')) + def GET_fetch_promo(self, form, jquery, link, campaign): promo_tuples = [promote.PromoTuple(link, 1., campaign)] builder = CampaignBuilder(promo_tuples, wrap=default_thing_wrapper(), @@ -3110,9 +3109,6 @@ class ApiController(RedditController, OAuth2ResourceController): interestbar=None).listing() jquery(".content").replace_things(listing) - if show: - jquery('.organic-listing .thing:visible').hide() - jquery('.organic-listing .id-%s' % link).show() @noresponse(VUser(), ui_elem = VOneOf('id', ('organic',))) diff --git a/r2/r2/public/static/css/reddit.css b/r2/r2/public/static/css/reddit.css index dd83c235c..88de53729 100755 --- a/r2/r2/public/static/css/reddit.css +++ b/r2/r2/public/static/css/reddit.css @@ -986,6 +986,9 @@ a.author { margin-right: 0.5em; } /* This is a really weird value, but it needs to be low to hide text without affecting layout in IE. */ text-indent: 50px; } +.organic-listing .nextprev .arrow.prev { + background-image: url(../prev_organic.png); /* SPRITE */ +} .organic-listing .nextprev .arrow.next { background-image: url(../next_organic.png); /* SPRITE */ } diff --git a/r2/r2/public/static/js/base.js b/r2/r2/public/static/js/base.js index 7cc407ba1..9c45a2875 100644 --- a/r2/r2/public/static/js/base.js +++ b/r2/r2/public/static/js/base.js @@ -19,6 +19,7 @@ $(function() { r.login.ui.init() r.analytics.init() r.ui.init() + r.spotlight.init() r.interestbar.init() r.apps.init() r.wiki.init() diff --git a/r2/r2/public/static/js/reddit.js b/r2/r2/public/static/js/reddit.js index 444874368..323fa56b4 100644 --- a/r2/r2/public/static/js/reddit.js +++ b/r2/r2/public/static/js/reddit.js @@ -522,19 +522,19 @@ function updateEventHandlers(thing) { .click(function() { var a = $(this).get(0); change_state(a, 'hide', - function() { r.spotlight.shuffle() }); + function() { r.spotlight.next() }); }); thing.find(".del-button a.yes") .click(function() { var a = $(this).get(0); change_state(a, 'del', - function() { r.spotlight.shuffle() }); + function() { r.spotlight.next() }); }); thing.find(".report-button a.yes") .click(function() { var a = $(this).get(0); change_state(a, 'report', - function() { r.spotlight.shuffle() }); + function() { r.spotlight.next() }); }); } }; @@ -1157,8 +1157,6 @@ $(function() { $(this).select(); }); - r.spotlight.shuffle(); - /* ajax ynbutton */ function toggleThis() { return toggle(this); } $("body") diff --git a/r2/r2/public/static/js/spotlight.js b/r2/r2/public/static/js/spotlight.js index 40cff3fa4..a89e88edf 100644 --- a/r2/r2/public/static/js/spotlight.js +++ b/r2/r2/public/static/js/spotlight.js @@ -1,15 +1,53 @@ r.spotlight = {} -r.spotlight.link_by_camp = {} -r.spotlight.weights = {} -r.spotlight.organics = [] -r.spotlight.interest_prob = 0 -r.spotlight.promotion_prob = 1 +r.spotlight.init = function() { + var listing = $('.organic-listing') + if (!listing.length) { + return + } -r.spotlight.init = function(links, interest_prob, promotion_prob) { - var link_by_camp = {}, - weights = {}, - organics = [] + $('.organic-listing .arrow.prev').on('click', $.proxy(this, 'prev')) + $('.organic-listing .arrow.next').on('click', $.proxy(this, 'next')) + + _.each(this.link_by_camp, function(fullname, campaign) { + if (!listing.find('.id-' + fullname).length) { + this.createStub(fullname, campaign) + } + }, this) + + var selectedThing, + lastClickFullname = r.analytics.breadcrumbs.lastClickFullname(), + lastClickThing = $(lastClickFullname ? '.id-' + lastClickFullname : null) + if (lastClickThing.length && listing.has(lastClickThing).length) { + r.debug('restoring spotlight selection to last click') + selectedThing = lastClickThing + } else { + selectedThing = this.chooseRandom() + } + + this.lineup = _.chain(listing.find('.thing')) + .reject(function(el) { return selectedThing.is(el) }) + .shuffle() + .unshift(selectedThing) + .map(function(el) { + var fullname = $(el).data('fullname') + if (fullname) { + // convert things with ids to queries to handle stub replacement + return '.id-' + fullname + } else { + return el + } + }) + .value() + + this.lineup.pos = 0 + r.spotlight._advance(0) +} + +r.spotlight.setup = function(links, interest_prob, promotion_prob) { + this.link_by_camp = {}, + this.weights = {}, + this.organics = [] for (var index in links) { var link = links[index][0], @@ -18,66 +56,80 @@ r.spotlight.init = function(links, interest_prob, promotion_prob) { weight = links[index][3] if (is_promo) { - link_by_camp[campaign] = link - weights[campaign] = weight + this.link_by_camp[campaign] = link + this.weights[campaign] = weight } else { - organics.push(link) + this.organics.push(link) } } - _.extend(r.spotlight.link_by_camp, link_by_camp) - _.extend(r.spotlight.weights, weights) - _.extend(r.spotlight.organics, organics) - r.spotlight.interest_prob = interest_prob - r.spotlight.promotion_prob = promotion_prob + this.interest_prob = interest_prob + this.promotion_prob = promotion_prob } -r.spotlight.shuffle = function() { - var listing = $('.organic-listing'), - visible = listing.find(".thing:visible") +r.spotlight.createStub = function(fullname, campaign) { + var stub = $('