|
(function() { |
|
|
|
if (typeof GW !== 'object') { |
|
alert('JS error, GW undefined!!') |
|
}; |
|
|
|
GW.SERPView = {} |
|
|
|
GW.SERPView.sort = function(e) { |
|
//var sorting = j(e).attr('href').split(',') |
|
var query = GW.URI.decodeComponents() |
|
query['sort'] = j(this).attr('href').split('#')[1] |
|
var target = window.location.href.split('?')[0] +'?'+ j.param( query ) +'#'+ window.location.href.split('#')[1] |
|
//console.log('target', target) |
|
window.location.href = target |
|
return false |
|
}; |
|
|
|
var filterCriteria = null |
|
, h1Template = " Found ? games! " |
|
|
|
function resetFilterCriteria(){ |
|
filterCriteria = { |
|
platform: [], |
|
genre: [], |
|
tag: [], |
|
} |
|
} |
|
resetFilterCriteria() |
|
|
|
/* |
|
* user just clicked a filter (toggle) |
|
*/ |
|
GW.SERPView.filterClick = function(e) { |
|
try{ |
|
var je = null |
|
, target = '' |
|
//var je = j(this) |
|
// , target = je.attr('name')//je.data('target') |
|
// , value = je.next().text() |
|
|
|
//debugger |
|
|
|
//if( je.hasClass('active') ){ |
|
// je.removeClass( 'active' ) |
|
// var i = filterCriteria[target].indexOf( value ) |
|
// filterCriteria[target].remove( i ) |
|
//} else { |
|
// je.addClass( 'active' ) |
|
// filterCriteria[target].push( value ) |
|
//} |
|
|
|
// do a complete recount of boxes, just to be sure! |
|
resetFilterCriteria() |
|
j('.filter:checked').each( function(i,el) { |
|
je = j(this) |
|
target = je.attr('name'); |
|
target = target.substr(0, target.length -1) |
|
if( target === 'mode' ) target = 'tag' |
|
filterCriteria[target].push( je.next().text() ) |
|
}) |
|
|
|
GW.SERPView.updateGameList() |
|
} catch(err) { |
|
console.log('filter error!', err, je) |
|
} |
|
|
|
GW.SERPView.updateURL() |
|
|
|
|
|
return true |
|
}; |
|
|
|
/* |
|
* All games that do not present any criteria should be hidden! |
|
*/ |
|
GW.SERPView.updateGameList = function( instant ){ |
|
var games = j('.gamel') |
|
, hide = [] |
|
, je = null |
|
, inter = null |
|
|
|
if( typeof instant === 'undefined' ) instant = false |
|
|
|
games.each(function(i,el){ |
|
je = j(el) |
|
// every element should not be hid, unless it fail to match some criteria |
|
hide[i] = false |
|
|
|
// platforms |
|
if( filterCriteria.platform.length > 0 ){ |
|
inter = _.intersection( je.find('.gplatforms strong').text().split(' '), filterCriteria.platform ) |
|
//console.log( 'gplatforms', inter.length, filterCriteria.platform.length ) |
|
if( inter.length != filterCriteria.platform.length ){ |
|
hide[i] = true |
|
return true // go next |
|
} |
|
} |
|
|
|
// genres |
|
if( filterCriteria.genre.length > 0 ){ |
|
inter = _.intersection( je.find('.ggenres').text().split(' '), filterCriteria.genre ) |
|
//console.log( 'ggenres', inter.length, filterCriteria.genre.length ) |
|
if( inter.length != filterCriteria.genre.length ){ |
|
hide[i] = true |
|
return true // go next |
|
} |
|
} |
|
|
|
// tags |
|
if( filterCriteria.tag.length > 0 ){ |
|
inter = _.intersection( je.find('.gtags').text().split(' '), filterCriteria.tag ) |
|
//console.log( 'gtags', inter.length, filterCriteria.tag.length ) |
|
if( inter.length != filterCriteria.tag.length ){ |
|
hide[i] = true |
|
return true // go next |
|
} |
|
} |
|
|
|
}) |
|
|
|
var num = 0//_.reduce(hide, function(memo, val){ return (!val ? memo+1 : memo ) }, 0); |
|
for (var i=0; i < hide.length; i++) { |
|
if(!hide[i]) num += 1 |
|
}; |
|
j('.content h1:first').text( h1Template.replace('?', num+'' ) ) |
|
|
|
games.each(function(i,el){ |
|
je = j(this); |
|
if( hide[i] ){ |
|
if(je.is(':visible')){ |
|
if( instant ) je.hide() |
|
else je.hide('slow')//slideUp() |
|
} |
|
} else { |
|
if(je.is(':hidden')){ |
|
if( instant ) je.show() |
|
else je.show('slow')//slideDown() |
|
} |
|
} |
|
}) |
|
} |
|
|
|
/* |
|
* After user toogle 1 filter URI should be updated |
|
*/ |
|
GW.SERPView.updateURL = function() { |
|
var url = window.location.href |
|
var anchor = [ |
|
_.map(filterCriteria.platform, function(s) {return s.replace(/ /g,'_')}).join('-'), |
|
_.map(filterCriteria.genre, function(s) {return s.replace(/ /g,'_')}).join('-'), |
|
_.map(filterCriteria.tag, function(s) {return s.replace(/ /g,'_')}).join('-'), |
|
] |
|
anchor = anchor.join('!') |
|
window.location.href = url.split('#')[0] +'#'+ encodeURIComponent(anchor) |
|
}; |
|
|
|
/* |
|
* does filtering once the page load |
|
*/ |
|
GW.SERPView.applyURLFilters = function() { |
|
var url = window.location.href |
|
|
|
// URL broken down params |
|
var original = {} |
|
_.each( _.flatten( _.compact(GW.URI.decodeComponents()) ), function(s){ |
|
original[s] = true |
|
}) |
|
//console.log(original) |
|
j('.filter').each(function(i,el){ |
|
//console.log(this.value) |
|
if( original[this.value] ) this.checked = true |
|
}) |
|
|
|
|
|
// filters applyed |
|
|
|
var anchor = url.split('#')[1] |
|
if( anchor ){ |
|
anchor = decodeURIComponent( anchor.replace(/\_/g,' ') ) |
|
anchor = anchor.split('!') |
|
} else { |
|
anchor = [] |
|
} |
|
if( anchor.length == 3 ){ |
|
|
|
filterCriteria = { |
|
platform: _.compact(anchor[0].split('-')), |
|
genre: _.compact(anchor[1].split('-')), |
|
tag: _.compact(anchor[2].split('-')), |
|
} |
|
|
|
console.log(filterCriteria) |
|
|
|
GW.SERPView.updateGameList( true ) |
|
|
|
|
|
var allCriteria = filterCriteria.platform.concat( filterCriteria.genre, filterCriteria.tag ) |
|
|
|
_.each( allCriteria, function(str) { |
|
j('#SERPfilters li:contains('+str+')').children('input').attr('checked', true) |
|
//j('.filter:contains("'+ str +'")').addClass('active') |
|
}) |
|
} |
|
}; |
|
|
|
|
|
GW.SERPView.onLoad = function (){ |
|
// bind the click on detailed reviews to the content |
|
j('.resultsFilters a').click( GW.SERPView.sort ) |
|
|
|
GW.SERPView.applyURLFilters() |
|
|
|
j('.filter').click( GW.SERPView.filterClick ) |
|
|
|
j('#dofilter').click( GW.GameView.filterReview ) |
|
|
|
} |
|
|
|
|
|
})() |