Start refactoring aspect-filters.js

This commit is contained in:
Raphael Sofaer 2011-04-13 15:55:10 -07:00
parent f13304073b
commit 9969dbd109
3 changed files with 114 additions and 104 deletions

View file

@ -3,98 +3,107 @@
* the COPYRIGHT file. * the COPYRIGHT file.
*/ */
$(document).ready(function(){ var AspectFilters = {
var selectedGUIDS = [], selectedGUIDS: [],
requests = 0; requests: 0,
initialize: function(){
AspectFilters.initializeSelectedGUIDS();
AspectFilters.interceptAspectLinks();
AspectFilters.interceptAspectNavLinks();
$("#aspect_nav li").each(function(){ if($("a.home_selector").parent().hasClass("selected")){
var button = $(this), AspectFilters.performAspectUpdate();
guid = button.attr('data-guid');
if(guid && location.href.search("a_ids..="+guid+"(&|$)") != -1){
button.addClass('selected');
selectedGUIDS.push(guid);
} }
}); },
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 guid = $(this).attr('data-guid');
var link = $(this);
if( !link.hasClass('aspect_selector') ){ // 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(); e.preventDefault();
requests++;
var guid = $(this).attr('data-guid'); AspectFilters.requests++;
// 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 // loading animation
$("#aspect_stream_container").fadeTo(200, 0.4); $("#aspect_stream_container").fadeTo(100, 0.4);
$("#aspect_contact_pictures").fadeTo(200, 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){ if(AspectFilters.selectedGUIDS.length == 0){
e.preventDefault(); homeListElement.addClass('selected');
}
requests++; } else {
// append filter
if(AspectFilters.selectedGUIDS.indexOf( guid == 1)){
AspectFilters.selectedGUIDS.push( guid );
}
listElement.addClass('selected');
// loading animation homeListElement.removeClass('selected');
$("#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');
} }
} else { AspectFilters.performAjax(AspectFilters.generateURL());
// append filter });
if(selectedGUIDS.indexOf( guid == 1)){ },
selectedGUIDS.push( guid ); generateURL: function(){
}
listElement.addClass('selected');
homeListElement.removeClass('selected');
}
performAjax(generateURL());
});
function generateURL(){
var baseURL = location.href.split("?")[0]; var baseURL = location.href.split("?")[0];
// generate new url // generate new url
baseURL = baseURL.replace('#',''); baseURL = baseURL.replace('#','');
baseURL += '?'; baseURL += '?';
for(i=0; i < selectedGUIDS.length; i++){ for(i=0; i < AspectFilters.selectedGUIDS.length; i++){
baseURL += 'a_ids[]='+ selectedGUIDS[i] +'&'; baseURL += 'a_ids[]='+ AspectFilters.selectedGUIDS[i] +'&';
} }
if(!$("#publisher").hasClass("closed")) { if(!$("#publisher").hasClass("closed")) {
@ -105,36 +114,28 @@ $(document).ready(function(){
baseURL = baseURL.slice(0,baseURL.length-1); baseURL = baseURL.slice(0,baseURL.length-1);
} }
return baseURL; return baseURL;
} },
performAspectUpdate: function(){
function performAspectUpdate(){ // update the open aspects in the user
// update the open aspects in the user updateURL = "/user";
updateURL = "/user"; updateURL += '?';
updateURL += '?'; if(AspectFilters.selectedGUIDS.length == 0){
if(selectedGUIDS.length == 0){ updateURL += 'user[a_ids][]=home';
updateURL += 'user[a_ids][]=home'; } else {
} else { for(i=0; i < AspectFilters.selectedGUIDS.length; i++){
for(i=0; i < selectedGUIDS.length; i++){ updateURL += 'user[a_ids][]='+ AspectFilters.selectedGUIDS[i] +'&';
updateURL += 'user[a_ids][]='+ selectedGUIDS[i] +'&';
}
} }
}
$.ajax({ $.ajax({
url : updateURL, url : updateURL,
type: "PUT", type: "PUT",
}); });
},
} performAjax: function(newURL) {
if($("a.home_selector").parent().hasClass("selected")){
performAspectUpdate();
}
function performAjax(newURL) {
var post = $("#publisher textarea").val(), var post = $("#publisher textarea").val(),
photos = {}; photos = {};
//pass photos //pass photos
$('#photodropzone img').each(function(){ $('#photodropzone img').each(function(){
var img = $(this); var img = $(this);
@ -143,8 +144,6 @@ $(document).ready(function(){
photos[guid] = url; photos[guid] = url;
}); });
// set url // set url
// some browsers (Firefox for example) don't support pushState // some browsers (Firefox for example) don't support pushState
if (typeof(history.pushState) == 'function') { if (typeof(history.pushState) == 'function') {
@ -155,7 +154,7 @@ $(document).ready(function(){
url : newURL, url : newURL,
dataType : 'script', dataType : 'script',
success : function(data){ success : function(data){
requests--; AspectFilters.requests--;
// fill in publisher // fill in publisher
// (not cached because this element changes) // (not cached because this element changes)
@ -181,14 +180,15 @@ $(document).ready(function(){
Publisher.initialize(); Publisher.initialize();
// fade contents back in // fade contents back in
if(requests == 0){ if(AspectFilters.requests == 0){
$("#aspect_stream_container").fadeTo(100, 1); $("#aspect_stream_container").fadeTo(100, 1);
$("#aspect_contact_pictures").fadeTo(100, 1); $("#aspect_contact_pictures").fadeTo(100, 1);
performAspectUpdate(); AspectFilters.performAspectUpdate();
} }
} }
}); });
} }
}
$(document).ready(function(){
AspectFilters.initialize();
}); });

View file

@ -0,0 +1,9 @@
describe('AspectFilters', function(){
it('initializes selectedGUIDS', function(){
expect(AspectFilters.selectedGUIDS).toEqual([]);
});
it('initializes requests', function(){
expect(AspectFilters.requests).toEqual(0);
});
});

View file

@ -37,6 +37,7 @@ src_files:
- public/javascripts/stream.js - public/javascripts/stream.js
- public/javascripts/validation.js - public/javascripts/validation.js
- public/javascripts/rails.js - public/javascripts/rails.js
- public/javascripts/aspect-filters.js
# stylesheets # stylesheets
# #
# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs. # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.