diff --git a/public/javascripts/aspect-filters.js b/public/javascripts/aspect-filters.js index fd1e62d61..b41db702b 100644 --- a/public/javascripts/aspect-filters.js +++ b/public/javascripts/aspect-filters.js @@ -3,98 +3,107 @@ * the COPYRIGHT file. */ -$(document).ready(function(){ - var selectedGUIDS = [], - requests = 0; +var AspectFilters = { + selectedGUIDS: [], + requests: 0, + initialize: function(){ + AspectFilters.initializeSelectedGUIDS(); + AspectFilters.interceptAspectLinks(); + AspectFilters.interceptAspectNavLinks(); - $("#aspect_nav li").each(function(){ - var button = $(this), - guid = button.attr('data-guid'); - - if(guid && location.href.search("a_ids..="+guid+"(&|$)") != -1){ - button.addClass('selected'); - selectedGUIDS.push(guid); + if($("a.home_selector").parent().hasClass("selected")){ + AspectFilters.performAspectUpdate(); } - }); + }, + initializeSelectedGUIDS: function(){ + $("#aspect_nav li").each(function(){ + var button = $(this), + guid = button.attr('data-guid'); + if(guid && location.href.search("a_ids..="+guid+"(&|$)") != -1){ + button.addClass('selected'); + AspectFilters.selectedGUIDS.push(guid); + } + }); + }, + interceptAspectLinks: function(){ + $("a.hard_aspect_link").live("click", function(e){ + var link = $(this); + if( !link.hasClass('aspect_selector') ){ + e.preventDefault(); + AspectFilters.requests++; - $("a.hard_aspect_link").live("click", function(e){ - var link = $(this); - if( !link.hasClass('aspect_selector') ){ + var guid = $(this).attr('data-guid'); + + // select correct aspect in filter list & deselect others + $("#aspect_nav li").each(function(){ + var $this = $(this); + if( $this.attr('data-guid') == guid){ + $this.addClass('selected'); + } else { + $this.removeClass('selected'); + } + }); + + // loading animation + $("#aspect_stream_container").fadeTo(200, 0.4); + $("#aspect_contact_pictures").fadeTo(200, 0.4); + + AspectFilters.performAjax( $(this).attr('href')); + } + + $('html, body').animate({scrollTop:0}, 'fast'); + }); + }, + interceptAspectNavLinks: function(){ + $("#aspect_nav a.aspect_selector").click(function(e){ e.preventDefault(); - requests++; - var guid = $(this).attr('data-guid'); - - // select correct aspect in filter list & deselect others - $("#aspect_nav li").each(function(){ - var $this = $(this); - if( $this.attr('data-guid') == guid){ - $this.addClass('selected'); - } else { - $this.removeClass('selected'); - } - }); + AspectFilters.requests++; // loading animation - $("#aspect_stream_container").fadeTo(200, 0.4); - $("#aspect_contact_pictures").fadeTo(200, 0.4); + $("#aspect_stream_container").fadeTo(100, 0.4); + $("#aspect_contact_pictures").fadeTo(100, 0.4); - performAjax( $(this).attr('href')); - } + // filtering ////////////////////// + var $this = $(this), + listElement = $this.parent(), + guid = listElement.attr('data-guid'), + homeListElement = $("#aspect_nav a.home_selector").parent(); - $('html, body').animate({scrollTop:0}, 'fast'); - }); + if( listElement.hasClass('selected') ){ + // remove filter + var idx = AspectFilters.selectedGUIDS.indexOf( guid ); + if( idx != -1 ){ + AspectFilters.selectedGUIDS.splice(idx,1); + } + listElement.removeClass('selected'); - $("#aspect_nav a.aspect_selector").click(function(e){ - e.preventDefault(); + if(AspectFilters.selectedGUIDS.length == 0){ + homeListElement.addClass('selected'); + } - requests++; + } else { + // append filter + if(AspectFilters.selectedGUIDS.indexOf( guid == 1)){ + AspectFilters.selectedGUIDS.push( guid ); + } + listElement.addClass('selected'); - // loading animation - $("#aspect_stream_container").fadeTo(100, 0.4); - $("#aspect_contact_pictures").fadeTo(100, 0.4); - - // filtering ////////////////////// - var $this = $(this), - listElement = $this.parent(), - guid = listElement.attr('data-guid'), - homeListElement = $("#aspect_nav a.home_selector").parent(); - - if( listElement.hasClass('selected') ){ - // remove filter - var idx = selectedGUIDS.indexOf( guid ); - if( idx != -1 ){ - selectedGUIDS.splice(idx,1); - } - listElement.removeClass('selected'); - - if(selectedGUIDS.length == 0){ - homeListElement.addClass('selected'); + homeListElement.removeClass('selected'); } - } else { - // append filter - if(selectedGUIDS.indexOf( guid == 1)){ - selectedGUIDS.push( guid ); - } - listElement.addClass('selected'); - - homeListElement.removeClass('selected'); - } - - performAjax(generateURL()); - }); - - - function generateURL(){ + AspectFilters.performAjax(AspectFilters.generateURL()); + }); + }, + generateURL: function(){ 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] +'&'; + for(i=0; i < AspectFilters.selectedGUIDS.length; i++){ + baseURL += 'a_ids[]='+ AspectFilters.selectedGUIDS[i] +'&'; } if(!$("#publisher").hasClass("closed")) { @@ -105,36 +114,28 @@ $(document).ready(function(){ baseURL = baseURL.slice(0,baseURL.length-1); } return baseURL; - } - - function performAspectUpdate(){ - // update the open aspects in the user - updateURL = "/user"; - updateURL += '?'; - if(selectedGUIDS.length == 0){ - updateURL += 'user[a_ids][]=home'; - } else { - for(i=0; i < selectedGUIDS.length; i++){ - updateURL += 'user[a_ids][]='+ selectedGUIDS[i] +'&'; - } + }, + performAspectUpdate: function(){ + // update the open aspects in the user + updateURL = "/user"; + updateURL += '?'; + if(AspectFilters.selectedGUIDS.length == 0){ + updateURL += 'user[a_ids][]=home'; + } else { + for(i=0; i < AspectFilters.selectedGUIDS.length; i++){ + updateURL += 'user[a_ids][]='+ AspectFilters.selectedGUIDS[i] +'&'; } + } - $.ajax({ - url : updateURL, - type: "PUT", - }); - - } - - if($("a.home_selector").parent().hasClass("selected")){ - performAspectUpdate(); - } - - function performAjax(newURL) { + $.ajax({ + url : updateURL, + type: "PUT", + }); + }, + performAjax: function(newURL) { var post = $("#publisher textarea").val(), photos = {}; - //pass photos $('#photodropzone img').each(function(){ var img = $(this); @@ -143,8 +144,6 @@ $(document).ready(function(){ photos[guid] = url; }); - - // set url // some browsers (Firefox for example) don't support pushState if (typeof(history.pushState) == 'function') { @@ -155,7 +154,7 @@ $(document).ready(function(){ url : newURL, dataType : 'script', success : function(data){ - requests--; + AspectFilters.requests--; // fill in publisher // (not cached because this element changes) @@ -181,14 +180,15 @@ $(document).ready(function(){ Publisher.initialize(); // fade contents back in - if(requests == 0){ + if(AspectFilters.requests == 0){ $("#aspect_stream_container").fadeTo(100, 1); $("#aspect_contact_pictures").fadeTo(100, 1); - performAspectUpdate(); + AspectFilters.performAspectUpdate(); } } }); - } - +} +$(document).ready(function(){ + AspectFilters.initialize(); }); diff --git a/spec/javascripts/aspect-filters-spec.js b/spec/javascripts/aspect-filters-spec.js new file mode 100644 index 000000000..3c677b348 --- /dev/null +++ b/spec/javascripts/aspect-filters-spec.js @@ -0,0 +1,9 @@ + +describe('AspectFilters', function(){ + it('initializes selectedGUIDS', function(){ + expect(AspectFilters.selectedGUIDS).toEqual([]); + }); + it('initializes requests', function(){ + expect(AspectFilters.requests).toEqual(0); + }); +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 626edff19..c8855ff33 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -37,6 +37,7 @@ src_files: - public/javascripts/stream.js - public/javascripts/validation.js - public/javascripts/rails.js + - public/javascripts/aspect-filters.js # stylesheets # # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.