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.
|
* the COPYRIGHT file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$(document).ready(function(){
|
var AspectFilters = {
|
||||||
var selectedGUIDS = [],
|
selectedGUIDS: [],
|
||||||
requests = 0;
|
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(){
|
$("#aspect_nav li").each(function(){
|
||||||
var button = $(this),
|
var button = $(this),
|
||||||
guid = button.attr('data-guid');
|
guid = button.attr('data-guid');
|
||||||
|
|
||||||
if(guid && location.href.search("a_ids..="+guid+"(&|$)") != -1){
|
if(guid && location.href.search("a_ids..="+guid+"(&|$)") != -1){
|
||||||
button.addClass('selected');
|
button.addClass('selected');
|
||||||
selectedGUIDS.push(guid);
|
AspectFilters.selectedGUIDS.push(guid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
interceptAspectLinks: function(){
|
||||||
$("a.hard_aspect_link").live("click", function(e){
|
$("a.hard_aspect_link").live("click", function(e){
|
||||||
var link = $(this);
|
var link = $(this);
|
||||||
if( !link.hasClass('aspect_selector') ){
|
if( !link.hasClass('aspect_selector') ){
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
requests++;
|
AspectFilters.requests++;
|
||||||
|
|
||||||
var guid = $(this).attr('data-guid');
|
var guid = $(this).attr('data-guid');
|
||||||
|
|
||||||
|
|
@ -40,16 +49,17 @@ $(document).ready(function(){
|
||||||
$("#aspect_stream_container").fadeTo(200, 0.4);
|
$("#aspect_stream_container").fadeTo(200, 0.4);
|
||||||
$("#aspect_contact_pictures").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');
|
$('html, body').animate({scrollTop:0}, 'fast');
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
interceptAspectNavLinks: function(){
|
||||||
$("#aspect_nav a.aspect_selector").click(function(e){
|
$("#aspect_nav a.aspect_selector").click(function(e){
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
requests++;
|
AspectFilters.requests++;
|
||||||
|
|
||||||
// loading animation
|
// loading animation
|
||||||
$("#aspect_stream_container").fadeTo(100, 0.4);
|
$("#aspect_stream_container").fadeTo(100, 0.4);
|
||||||
|
|
@ -63,38 +73,37 @@ $(document).ready(function(){
|
||||||
|
|
||||||
if( listElement.hasClass('selected') ){
|
if( listElement.hasClass('selected') ){
|
||||||
// remove filter
|
// remove filter
|
||||||
var idx = selectedGUIDS.indexOf( guid );
|
var idx = AspectFilters.selectedGUIDS.indexOf( guid );
|
||||||
if( idx != -1 ){
|
if( idx != -1 ){
|
||||||
selectedGUIDS.splice(idx,1);
|
AspectFilters.selectedGUIDS.splice(idx,1);
|
||||||
}
|
}
|
||||||
listElement.removeClass('selected');
|
listElement.removeClass('selected');
|
||||||
|
|
||||||
if(selectedGUIDS.length == 0){
|
if(AspectFilters.selectedGUIDS.length == 0){
|
||||||
homeListElement.addClass('selected');
|
homeListElement.addClass('selected');
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// append filter
|
// append filter
|
||||||
if(selectedGUIDS.indexOf( guid == 1)){
|
if(AspectFilters.selectedGUIDS.indexOf( guid == 1)){
|
||||||
selectedGUIDS.push( guid );
|
AspectFilters.selectedGUIDS.push( guid );
|
||||||
}
|
}
|
||||||
listElement.addClass('selected');
|
listElement.addClass('selected');
|
||||||
|
|
||||||
homeListElement.removeClass('selected');
|
homeListElement.removeClass('selected');
|
||||||
}
|
}
|
||||||
|
|
||||||
performAjax(generateURL());
|
AspectFilters.performAjax(AspectFilters.generateURL());
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
generateURL: function(){
|
||||||
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,17 +114,16 @@ $(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(selectedGUIDS.length == 0){
|
if(AspectFilters.selectedGUIDS.length == 0){
|
||||||
updateURL += 'user[a_ids][]=home';
|
updateURL += 'user[a_ids][]=home';
|
||||||
} else {
|
} else {
|
||||||
for(i=0; i < selectedGUIDS.length; i++){
|
for(i=0; i < AspectFilters.selectedGUIDS.length; i++){
|
||||||
updateURL += 'user[a_ids][]='+ selectedGUIDS[i] +'&';
|
updateURL += 'user[a_ids][]='+ AspectFilters.selectedGUIDS[i] +'&';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,18 +131,11 @@ $(document).ready(function(){
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
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/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.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue