Adding utility functions for the ULS langdb
* More functions for making useful queries to the langdb. * Tests. Change-Id: I29a3a25ef1b15d5925df2aa3d06f9221f8e5231e
This commit is contained in:
@@ -48,6 +48,8 @@
|
|||||||
// Register all event listeners to the ULS here.
|
// Register all event listeners to the ULS here.
|
||||||
that.$element.on( 'click', $.proxy( that.click, that ) );
|
that.$element.on( 'click', $.proxy( that.click, that ) );
|
||||||
$( ".icon-close" ).on( 'click', $.proxy( that.click, that ) );
|
$( ".icon-close" ).on( 'click', $.proxy( that.click, that ) );
|
||||||
|
|
||||||
|
// The search input box
|
||||||
$( "#languagefilter" ).languagefilter( {
|
$( "#languagefilter" ).languagefilter( {
|
||||||
$target: $( 'ul.uls-language-filter-result' ),
|
$target: $( 'ul.uls-language-filter-result' ),
|
||||||
clickhandler: function( langCode ) {
|
clickhandler: function( langCode ) {
|
||||||
@@ -55,6 +57,7 @@
|
|||||||
},
|
},
|
||||||
languages: that.languages
|
languages: that.languages
|
||||||
} );
|
} );
|
||||||
|
|
||||||
// Create region selectors, one per region
|
// Create region selectors, one per region
|
||||||
$( '.uls-region' ).regionselector( {
|
$( '.uls-region' ).regionselector( {
|
||||||
$target: $( 'ul.uls-language-filter-result' ),
|
$target: $( 'ul.uls-language-filter-result' ),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* @author Amir E. Aharoni
|
* @author Amir E. Aharoni
|
||||||
* Utilities for querying the language db.
|
* Utilities for querying language data.
|
||||||
*/
|
*/
|
||||||
(function ( $ ) {
|
(function ( $ ) {
|
||||||
"use strict";
|
"use strict";
|
||||||
@@ -9,6 +9,103 @@
|
|||||||
var scriptIndex = 0,
|
var scriptIndex = 0,
|
||||||
regionsIndex = 1;
|
regionsIndex = 1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns all languages written in script.
|
||||||
|
* @param script string
|
||||||
|
* @return array of strings (languages codes)
|
||||||
|
*/
|
||||||
|
$.uls.data.languagesInScript = function( script ) {
|
||||||
|
return $.uls.data.languagesInScripts( [ script ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns all languages written in the given scripts.
|
||||||
|
* @param scripts array of strings
|
||||||
|
* @return array of strings (languages codes)
|
||||||
|
*/
|
||||||
|
$.uls.data.languagesInScripts = function( scripts ) {
|
||||||
|
var languagesInScripts = [];
|
||||||
|
|
||||||
|
for ( var language in $.uls.data.languages ) {
|
||||||
|
for ( var i = 0; i < scripts.length; i++ ) {
|
||||||
|
if ( scripts[i] === $.uls.data.languages[language][scriptIndex] ) {
|
||||||
|
languagesInScripts.push( language );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return languagesInScripts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns all languages in a given region.
|
||||||
|
* @param region string
|
||||||
|
* @return array of strings (languages codes)
|
||||||
|
*/
|
||||||
|
$.uls.data.languagesInRegion = function( region ) {
|
||||||
|
return $.uls.data.languagesInRegions( [ region ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns all languages in given regions.
|
||||||
|
* @param region array of strings.
|
||||||
|
* @return array of strings (languages codes)
|
||||||
|
*/
|
||||||
|
$.uls.data.languagesInRegions = function( regions ) {
|
||||||
|
var languagesInRegions = [];
|
||||||
|
|
||||||
|
for ( var language in $.uls.data.languages ) {
|
||||||
|
for ( var i = 0; i < regions.length; i++ ) {
|
||||||
|
if ( $.inArray( regions[i], $.uls.data.languages[language][regionsIndex] ) != -1 ) {
|
||||||
|
languagesInRegions.push( language );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return languagesInRegions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns an associative array of languages in a region,
|
||||||
|
* grouped by script.
|
||||||
|
* @param string region code
|
||||||
|
* @return associative array
|
||||||
|
*/
|
||||||
|
$.uls.data.languagesByScriptInRegion = function( region ) {
|
||||||
|
var languagesByScriptInRegion = {};
|
||||||
|
|
||||||
|
for ( var language in $.uls.data.languages ) {
|
||||||
|
if ( $.inArray( region, $.uls.data.languages[language][regionsIndex] ) != -1 ) {
|
||||||
|
var script = $.uls.data.languages[language][scriptIndex];
|
||||||
|
if ( languagesByScriptInRegion[script] === undefined ) {
|
||||||
|
languagesByScriptInRegion[script] = [];
|
||||||
|
}
|
||||||
|
languagesByScriptInRegion[script].push( language );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return languagesByScriptInRegion;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns all regions in a region group.
|
||||||
|
* @param number groupNum
|
||||||
|
* @return array of strings
|
||||||
|
*/
|
||||||
|
$.uls.data.regionsInGroup = function( groupNum ) {
|
||||||
|
var regionsInGroup = [];
|
||||||
|
|
||||||
|
for ( var region in $.uls.data.regiongroups ) {
|
||||||
|
if ( $.uls.data.regiongroups[region] === groupNum ) {
|
||||||
|
regionsInGroup.push( region );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return regionsInGroup;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns the script group of a script or 'Other' if it doesn't
|
* Returns the script group of a script or 'Other' if it doesn't
|
||||||
* belong to any group.
|
* belong to any group.
|
||||||
|
|||||||
@@ -31,10 +31,34 @@ test( "-- Initial check", function() {
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
test( "-- $.uls.data testing", function() {
|
test( "-- $.uls.data testing", function() {
|
||||||
expect( 1 );
|
expect( 8 );
|
||||||
|
|
||||||
// Unless we actually want some scripts to be in the 'Other' group.
|
// Unless we actually want some scripts to be in the 'Other' group.
|
||||||
strictEqual( orphanScript(), '', 'No orphan scripts found.' );
|
strictEqual( orphanScript(), '', 'No orphan scripts found.' );
|
||||||
|
|
||||||
|
deepEqual( $.uls.data.languagesInRegion( 'AU' ), ["en", "en-gb", "hif", "hif-latn", "mi", "na"], "languages of region AU are selected correctly" );
|
||||||
|
deepEqual( $.uls.data.languagesInRegions( ['NA', 'WW'] ),
|
||||||
|
[
|
||||||
|
"avk", "cho", "chr", "chy", "cr", "en", "en-ca", "eo", "es",
|
||||||
|
"haw", "ht", "ia", "ie", "ik", "ike-cans", "ike-latn", "io",
|
||||||
|
"iu", "jam", "jbo", "kl", "lfn", "mus", "nah", "nov", "nv",
|
||||||
|
"pdc", "pdt", "rue", "sei", "simple", "srn", "tokipona",
|
||||||
|
"uk", "vo", "yi"
|
||||||
|
],
|
||||||
|
"languages of regions NA and WW are selected correctly"
|
||||||
|
);
|
||||||
|
|
||||||
|
deepEqual( $.uls.data.languagesInScript( 'Knda' ), ["kn", "tcy"], "languages in script Knda are selected correctly" );
|
||||||
|
deepEqual( $.uls.data.languagesInScripts( ['Geor', 'Armn'] ),
|
||||||
|
["hy", "ka", "xmf"],
|
||||||
|
"languages in scripts Geor and Armn are selected correctly"
|
||||||
|
);
|
||||||
|
|
||||||
|
deepEqual( $.uls.data.regionsInGroup( 1 ), ["NA", "LA", "SA"], "regions in group 1 are selected correctly" );
|
||||||
|
deepEqual( $.uls.data.regionsInGroup( 4 ), ["WW"], "regions in group 4 are selected correctly" );
|
||||||
|
|
||||||
|
languagesByScriptInNA = $.uls.data.languagesByScriptInRegion( 'NA' );
|
||||||
|
deepEqual( languagesByScriptInNA['Cans'], ["cr", "ike-cans", "iu"], "correct languages in Cans in NA selected" );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
}());
|
}() );
|
||||||
|
|||||||
Reference in New Issue
Block a user