From 326155af54525d23bd817dcea5a9e6ebb6e45ed7 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 30 Mar 2011 17:39:08 -0700 Subject: [PATCH] popstate now in aspect-filters. back button now actually works on aspect/index. --- app/views/aspects/index.js.erb | 22 ++++++++++++++++++++ features/saved_state.feature | 2 ++ public/javascripts/aspect-filters.js | 31 ++++++++++++++++++---------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/app/views/aspects/index.js.erb b/app/views/aspects/index.js.erb index 8e1bf9dd6..a0fd82732 100644 --- a/app/views/aspects/index.js.erb +++ b/app/views/aspects/index.js.erb @@ -2,6 +2,28 @@ $('#aspect_stream_container').html("<%= escape_javascript(render('aspects/aspect $('#aspect_listings').html("<%= escape_javascript(render('aspects/aspect_listings', :aspects => @aspects)) %>"); $('a[rel*=facebox]').facebox(); + +var aspectIds = <%= @aspect_ids.to_json %>, + selectors = $("li", "#aspect_nav"); + +if( selectors.length-2 != aspectIds.length ) { + $.each( selectors, function(){ + var element = $(this); + + if( $.inArray( parseInt(element.attr('data-guid')), aspectIds) != -1 ){ + element.addClass('selected'); + } else { + element.removeClass('selected'); + } + }); +} else { + $.each( selectors, function(){ + $(this).removeClass('selected'); + }); + + selectors.first().addClass('selected'); +} + $(document).ready(function() { $('#main_stream').infinitescroll({ navSelector : "#main_stream + div.pagination", diff --git a/features/saved_state.feature b/features/saved_state.feature index d327e5707..ffc1e2b2c 100644 --- a/features/saved_state.feature +++ b/features/saved_state.feature @@ -12,7 +12,9 @@ Feature: saved state And I go to the aspects page When I follow "Open 1" + And I wait for the ajax to finish And I follow "Open 2" + And I wait for the ajax to finish Then aspect "Open 1" should be selected And aspect "Open 2" should be selected But aspect "Closed 1" should not be selected diff --git a/public/javascripts/aspect-filters.js b/public/javascripts/aspect-filters.js index 89b74a9cb..2c1ff1388 100644 --- a/public/javascripts/aspect-filters.js +++ b/public/javascripts/aspect-filters.js @@ -17,6 +17,11 @@ $(document).ready(function(){ } }); + // popstate + $(window).bind("popstate", function(){ + $.getScript(location.href); + return false; + }); $("a.hard_aspect_link").live("click", function(e){ var link = $(this); @@ -88,19 +93,23 @@ $(document).ready(function(){ function generateURL(){ var baseURL = location.href.split("?")[0]; - // generate new url - baseURL = baseURL.replace('#',''); - baseURL += '?'; - for(i=0; i < selectedGUIDS.length; i++){ - baseURL += 'a_ids[]='+ selectedGUIDS[i] +'&'; - } + if(selectedGUIDS.length != $('li', '#aspect_nav').length-2) { + // generate new url + baseURL = baseURL.replace('#',''); + baseURL += '?'; + for(i=0; i < selectedGUIDS.length; i++){ + baseURL += 'a_ids[]='+ selectedGUIDS[i] +'&'; + } - if(!$("#publisher").hasClass("closed")) { - // open publisher - baseURL += "op=true"; + if(!$("#publisher").hasClass("closed")) { + // open publisher + baseURL += "op=true"; + } else { + // slice last '&' + baseURL = baseURL.slice(0,baseURL.length-1); + } } else { - // slice last '&' - baseURL = baseURL.slice(0,baseURL.length-1); + selectedGUIDS = []; } return baseURL; }