I'm working with isotope.js to display a lot of boxes containing text in different languages. (fiddle) The items can be sorted by language - after clicking a link, items with the selected language will be sorted on top of the list, using getSortData and this code:
var $container = $('#container');
$container.isotope({
itemSelector: '.tipp',
getSortData : {
fr : function( $elem ) {
var isFr = $elem.hasClass('fr');
return (!isFr?' ':'');
},
en : function( $elem ) {
var isEn = $elem.hasClass('en');
return (!isEn?' ':'');
},
de : function( $elem ) {
var isDe = $elem.hasClass('de');
return (!isDe?' ':'');
}
// + 20 more languages!!!
}
});
This works fine, but the list of languages will be a lot longer (24 languages).
I'm asking if it's possible to get the sort data from an array, or to write a function to get all used (language)classes as sortData.
I found an article with a tutorial about such a function but this code did not work for me:
function createSort(){
var sorts = {};
var $source = jQuery('source'); //from where I will read data
$source.each(function() {
var $element = jQuery(this),
findBy = $element.children('a').data('sort-type'), //element, numerical data, data attribute or a property
key = $element.children('a').data('option-value'); //the name of the element, numerical data, data attribute or a property
//if the sort is based on a css class (an element)
if(findBy === "class"){
sortData[key] = function($elem){return $elem.find('.'+ key).text();};
}
//if the sort is based on a data attribute (in my scenario is a numerical attribute)
if(findBy === "attr"){
sortData[key] = function($elem){return parseInt($elem.attr('data-category-' + key), 10);};
}
});
return sortData;
}
//then I call the function and pass the result to getSortData
var sort_data = createSort(),
$container.isotope({
//more code.
getSortData:sort_data
});
Later I don't want to work with a link navigation, I'll use BBQ Hash history to sort the items from an external link (i.e. www.someurl.com/index.htm#sortBy=fr) So there should not be any nav or link list, I want to get the classes directly from the items or at least specify an array of language classes on top of my isotope code ['fr', 'de', 'en']
Basically this is a question of optimization, because it seems inefficent writing down the same snippet 24 times, right?
Again linking to my at least colorful jsfiddle.
Thanks a lot for any help!