Start refactoring aspect-filters.js
This commit is contained in:
parent
f13304073b
commit
9969dbd109
3 changed files with 114 additions and 104 deletions
|
|
@ -3,26 +3,35 @@
|
|||
* the COPYRIGHT file.
|
||||
*/
|
||||
|
||||
$(document).ready(function(){
|
||||
var selectedGUIDS = [],
|
||||
requests = 0;
|
||||
var AspectFilters = {
|
||||
selectedGUIDS: [],
|
||||
requests: 0,
|
||||
initialize: function(){
|
||||
AspectFilters.initializeSelectedGUIDS();
|
||||
AspectFilters.interceptAspectLinks();
|
||||
AspectFilters.interceptAspectNavLinks();
|
||||
|
||||
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');
|
||||
selectedGUIDS.push(guid);
|
||||
AspectFilters.selectedGUIDS.push(guid);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
interceptAspectLinks: function(){
|
||||
$("a.hard_aspect_link").live("click", function(e){
|
||||
var link = $(this);
|
||||
if( !link.hasClass('aspect_selector') ){
|
||||
e.preventDefault();
|
||||
requests++;
|
||||
AspectFilters.requests++;
|
||||
|
||||
var guid = $(this).attr('data-guid');
|
||||
|
||||
|
|
@ -40,16 +49,17 @@ $(document).ready(function(){
|
|||
$("#aspect_stream_container").fadeTo(200, 0.4);
|
||||
$("#aspect_contact_pictures").fadeTo(200, 0.4);
|
||||
|
||||
performAjax( $(this).attr('href'));
|
||||
AspectFilters.performAjax( $(this).attr('href'));
|
||||
}
|
||||
|
||||
$('html, body').animate({scrollTop:0}, 'fast');
|
||||
});
|
||||
|
||||
},
|
||||
interceptAspectNavLinks: function(){
|
||||
$("#aspect_nav a.aspect_selector").click(function(e){
|
||||
e.preventDefault();
|
||||
|
||||
requests++;
|
||||
AspectFilters.requests++;
|
||||
|
||||
// loading animation
|
||||
$("#aspect_stream_container").fadeTo(100, 0.4);
|
||||
|
|
@ -63,38 +73,37 @@ $(document).ready(function(){
|
|||
|
||||
if( listElement.hasClass('selected') ){
|
||||
// remove filter
|
||||
var idx = selectedGUIDS.indexOf( guid );
|
||||
var idx = AspectFilters.selectedGUIDS.indexOf( guid );
|
||||
if( idx != -1 ){
|
||||
selectedGUIDS.splice(idx,1);
|
||||
AspectFilters.selectedGUIDS.splice(idx,1);
|
||||
}
|
||||
listElement.removeClass('selected');
|
||||
|
||||
if(selectedGUIDS.length == 0){
|
||||
if(AspectFilters.selectedGUIDS.length == 0){
|
||||
homeListElement.addClass('selected');
|
||||
}
|
||||
|
||||
} else {
|
||||
// append filter
|
||||
if(selectedGUIDS.indexOf( guid == 1)){
|
||||
selectedGUIDS.push( guid );
|
||||
if(AspectFilters.selectedGUIDS.indexOf( guid == 1)){
|
||||
AspectFilters.selectedGUIDS.push( guid );
|
||||
}
|
||||
listElement.addClass('selected');
|
||||
|
||||
homeListElement.removeClass('selected');
|
||||
}
|
||||
|
||||
performAjax(generateURL());
|
||||
AspectFilters.performAjax(AspectFilters.generateURL());
|
||||
});
|
||||
|
||||
|
||||
function 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,17 +114,16 @@ $(document).ready(function(){
|
|||
baseURL = baseURL.slice(0,baseURL.length-1);
|
||||
}
|
||||
return baseURL;
|
||||
}
|
||||
|
||||
function performAspectUpdate(){
|
||||
},
|
||||
performAspectUpdate: function(){
|
||||
// update the open aspects in the user
|
||||
updateURL = "/user";
|
||||
updateURL += '?';
|
||||
if(selectedGUIDS.length == 0){
|
||||
if(AspectFilters.selectedGUIDS.length == 0){
|
||||
updateURL += 'user[a_ids][]=home';
|
||||
} else {
|
||||
for(i=0; i < selectedGUIDS.length; i++){
|
||||
updateURL += 'user[a_ids][]='+ selectedGUIDS[i] +'&';
|
||||
for(i=0; i < AspectFilters.selectedGUIDS.length; i++){
|
||||
updateURL += 'user[a_ids][]='+ AspectFilters.selectedGUIDS[i] +'&';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -123,18 +131,11 @@ $(document).ready(function(){
|
|||
url : updateURL,
|
||||
type: "PUT",
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
if($("a.home_selector").parent().hasClass("selected")){
|
||||
performAspectUpdate();
|
||||
}
|
||||
|
||||
function performAjax(newURL) {
|
||||
},
|
||||
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();
|
||||
});
|
||||
|
|
|
|||
9
spec/javascripts/aspect-filters-spec.js
Normal file
9
spec/javascripts/aspect-filters-spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue