Created
September 21, 2012 19:05
-
-
Save ruzzbot/3763269 to your computer and use it in GitHub Desktop.
stash-ish : a temporary place to put code I'm unsure about adding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
// 2012-09-21 : RWM | |
(function(){ | |
var countrysortinator = function(){ | |
// Sort object | |
var sortinator = { | |
parse_countries : function($container){ | |
var countries = []; | |
_.each( $('input', $container), function(ckbox){ | |
ckbox = $(ckbox); | |
countries.push({ | |
input : ckbox, | |
label : ckbox.next('label') | |
}); | |
}); | |
return countries; | |
}, | |
group_countries : function(item, i, list){ | |
var result, | |
comparator = item.label.text(); | |
// fuzzy matching for utf-8 characters | |
switch( comparator.charCodeAt(0) ){ | |
case 196: result = 'A'; break; // Umulat - Ä | |
case 214: result = 'O'; break; // Umlaut - Ö | |
default : result = comparator.charAt(0); break; | |
} | |
// 'other' category should always be last | |
// [enlish, spanish, german, french] | |
var others = ['other','otro','andere','autre']; | |
if( _.include(others, comparator.toLowerCase()) ){ | |
result = "%"; | |
} | |
return result; | |
}, | |
buildResults : function(groups){ | |
var $result = $('<div/>'), | |
keys = _.chain(groups).keys().value().sort(); | |
// append to results based in order of sorted keys | |
_.each(keys, function(key){ | |
_.each(groups[key], function(country){ | |
$result | |
.append(country.input) | |
.append(country.label); | |
}); | |
}); | |
return $result.html(); | |
} | |
}; | |
// --------------------------------------------------------- GO, GO, GO! | |
var $container = $('#qDiv1', $('#panel1')) | |
.find('.questionContent') | |
.children('fieldset'); | |
// Array of country objects parsed from the DOM | |
// Exit the process if no countries are available | |
var countries = sortinator.parse_countries( $wrap ); | |
if( countries.length < 1 ) return false; | |
// Group by label text's first character | |
var groups = _.groupBy( countries, sortinator.group_countries ); | |
// Build results and append to DOM | |
$container.empty().html( sortinator.buildResults(groups) ); | |
return true; | |
//init countrysortinator when DOM ready | |
$(function(){ countrysortinator() }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment