Merge branch 'master' of github.com:wikimedia/jquery.uls

This commit is contained in:
Siebrand Mazeland
2013-01-13 13:54:46 +00:00
10 changed files with 130 additions and 46 deletions

View File

@@ -127,7 +127,7 @@ languages:
fj: [Latn, [PA], Na Vosa Vakaviti]
fo: [Latn, [EU], føroyskt]
fr: [Latn, [EU, AM, WW], français]
frc: [Latn, [EU], français cadien]
frc: [Latn, [AM], français cadien]
frp: [Latn, [EU], arpetan]
frr: [Latn, [EU], Nordfriisk]
fur: [Latn, [EU], furlan]
@@ -312,7 +312,8 @@ languages:
nl-informal: [Latn, [EU, AM], Nederlands (informeel)]
nl: [Latn, [EU, AM], Nederlands]
nn: [Latn, [EU], norsk (nynorsk)]
"no": [Latn, [EU]]
# There's also nb for Bokmål and nn for Nynorsk
"no": [Latn, [EU], norsk]
nov: [Latn, [WW], Novial]
nqo: [Nkoo, [AF], ߒߞߏ]
nrm: [Latn, [EU], Nouormand]
@@ -551,6 +552,8 @@ rtlscripts:
regiongroups:
# Worldwide, international
WW: 1
# Special languages. To be shown near Worldwide when relevant.
SP: 1
# America
AM: 2
# Europe

View File

@@ -72,6 +72,7 @@ $json = json_encode( $parsedLangdb );
$js = <<<JAVASCRIPT
// Please do not edit. This file is generated from data/langdb.yaml by ulsdata2json.php
( function ( $ ) {
'use strict';
$.uls = $.uls || {};
$.uls.data = $json;
} ( jQuery ) );

View File

@@ -7,6 +7,7 @@
},
"uls-select-language": "Select Language",
"uls-region-WW": "Worldwide",
"uls-region-SP": "Special",
"uls-region-AM": "America",
"uls-region-AF": "Africa",
"uls-region-EU": "Europe",
@@ -15,7 +16,7 @@
"uls-region-PA": "Pacific",
"uls-no-results-found": "No results found",
"uls-common-languages": "Common languages",
"uls-no-results-suggestion-title": "You may be interested in:",
"uls-no-results-suggestion-title": "You may be interested in:",
"uls-search-help": "You can search by language name, script name, ISO code of language or you can browse by region:",
"uls-search-placeholder": "Language search"
}

View File

@@ -7,6 +7,7 @@
},
"uls-select-language": "Language Selector Title",
"uls-region-WW": "Label for worldwide languages. They are languages spoken in multiple countries. Eg: English, French, Spanish etc. This label is used in the map region of ULS and as the title of section showing worldwide languages. Translation should not be descriptive.",
"uls-region-SP": "Label for the special languages section. This section may include special custom languages that can be added in run time. It's shown after the Worldwide section.",
"uls-region-AM": "Label for America",
"uls-region-AF": "Label for Africa",
"uls-region-EU": "Label for Europe",
@@ -18,4 +19,4 @@
"uls-no-results-suggestion-title": "Title for language suggestion in 'no results found' screen",
"uls-search-help": "Help text for searching. After this text, there will be a set of links for regions",
"uls-search-placeholder": "Placeholder text in search box"
}
}

View File

@@ -236,7 +236,8 @@
quickList: uls.options.quickList,
clickhandler: $.proxy( uls.select, uls ),
lazyload: uls.options.lazyload,
source: uls.$languageFilter
source: uls.$languageFilter,
showRegions: uls.options.showRegions
} ).data( 'lcd' );
uls.$languageFilter.languagefilter( {
@@ -366,7 +367,8 @@
searchAPI: null, // Language search API
languages: $.uls.data.getAutonyms(), // Languages to be used for ULS, default is all languages
quickList: null, // Array of language codes or function that returns such
lazyload: true // Lazy load the language list when scrolled.
lazyload: true, // Lazy load the language list when scrolled.
showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA']
};
// Define a dummy i18n function, if jquery.i18n not integrated.

File diff suppressed because one or more lines are too long

View File

@@ -220,7 +220,6 @@
return $.uls.data.getLanguagesByScriptGroupInRegions( [ region ] );
};
/**
* Returns an associative array of all languages,
* grouped by script group.
@@ -237,23 +236,20 @@
*/
$.uls.data.getLanguagesByScriptGroup = function ( languages ) {
var languagesByScriptGroup = {},
scriptGroup,
language,
langScriptGroup;
language, codeToAdd, langScriptGroup;
for ( scriptGroup in $.uls.data.scriptgroups ) {
for ( language in languages ) {
langScriptGroup = $.uls.data.getScriptGroupOfLanguage( language );
for ( language in languages ) {
codeToAdd = $.uls.data.isRedirect( language ) || language;
if ( langScriptGroup !== scriptGroup ) {
continue;
}
langScriptGroup = $.uls.data.getScriptGroupOfLanguage( codeToAdd );
if ( !languagesByScriptGroup[scriptGroup] ) {
languagesByScriptGroup[scriptGroup] = [];
}
if ( !languagesByScriptGroup[langScriptGroup] ) {
languagesByScriptGroup[langScriptGroup] = [];
}
languagesByScriptGroup[scriptGroup].push( language );
// Prevent duplicate adding of redirects
if ( $.inArray( codeToAdd, languagesByScriptGroup[langScriptGroup] ) === -1 ) {
languagesByScriptGroup[langScriptGroup].push( codeToAdd );
}
}
@@ -421,4 +417,38 @@
$.uls.data.getLanguagesInTerritory = function ( territory ) {
return $.uls.data.territories[territory];
};
/**
* Adds a language in run time and sets its options as provided.
* If the target option is provided, the language is defined as a redirect.
* Other possible options are script, regions and autonym.
*
* @param code string New language code.
* @param options Object Language properties.
* @return list of language codes
*/
$.uls.data.addLanguage = function( code, options ) {
if ( options.target ) {
$.uls.data.languages[code] = [options.target];
} else {
$.uls.data.languages[code] = [options.script, options.regions, options.autonym];
}
};
/**
* Removes a language from the langdb in run time.
*
* @param code string Language code to delete.
* @return true if the language was removed, false otherwise.
*/
$.uls.data.deleteLanguage = function( code ) {
if ( $.uls.data.languages[code] ) {
delete $.uls.data.languages[code];
return true;
}
return false;
};
} ( jQuery ) );

View File

@@ -147,7 +147,10 @@
$rowDiv.append( $ul );
}
$divRegionCode.show();
// Don't show the region unless it was enabled
if ( $.inArray( regionCode, this.options.showRegions ) > -1 ) {
$divRegionCode.show();
}
return $ul;
},
@@ -158,6 +161,7 @@
regions = {
// These are fallback text when i18n library not present
WW: 'Worldwide',
SP: 'Special',
AM: 'America',
EU: 'Europe',
ME: 'Middle East',
@@ -268,7 +272,8 @@
// The region section need to be in sync with the map filter.
lcd.$element.scroll( function () {
var $ulsLanguageList = $( this ),
var inview, inviewRegion,
$ulsLanguageList = $( this ),
scrollTop = $ulsLanguageList.position().top,
scrollBottom = $ulsLanguageList.height();
@@ -278,7 +283,7 @@
}
}
// The region section need to be in sync with the map filter.
var inviewRegion = 'WW';
inviewRegion = 'WW';
lcd.$element.find( 'div.uls-lcd-region-section' ).each( function () {
var $lcdRegionSection = $( this ),
top = $lcdRegionSection.position().top,
@@ -292,7 +297,7 @@
}
} );
var inview = $.uls.data.regiongroups[inviewRegion];
inview = $.uls.data.regiongroups[inviewRegion];
$( '.regionselector' ).removeClass( 'active' );
$( '#uls-region-' + inview ).addClass( 'active' );
} );
@@ -317,6 +322,7 @@
$.fn.lcd.defaults = {
languages: null,
showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA'],
lazyload: true
};

View File

@@ -80,10 +80,17 @@
// Get the languages grouped by script group
var languagesByScriptGroup = $.uls.data.getLanguagesByScriptGroup( this.options.languages );
for ( var scriptGroup in languagesByScriptGroup ) {
// Make sure that we go by the original order
// of script groups
for ( var scriptGroup in $.uls.data.scriptgroups ) {
// Get the languages for the script group
var languages = languagesByScriptGroup[scriptGroup];
// It's possible that some script groups are missing
if ( !languages ) {
continue;
}
// Sort it based on autonym
languages.sort( $.uls.data.sortByAutonym );
@@ -110,21 +117,22 @@
},
next: function () {
if ( !this.$element.hasClass( 'active') ) {
var regionSelector = this;
if ( !this.$element.hasClass( 'active' ) ) {
return true;
}
var regionSelector = this;
// Do not respond to all scroll end events, but only after a short interval
delay( function () {
var regiongroup = regionSelector.$element.data( 'regiongroup' );
var nextRegiongroup = regiongroup + 1;
var nextRegionGroupNumber = regionSelector.$element.data( 'regiongroup' ) + 1,
$nextRegion = $( '#uls-region-' + nextRegionGroupNumber ),
nextRegionSelector = $nextRegion.length && $nextRegion.data( 'regionselector' );
var $nextRegion = $( '#uls-region-' + nextRegiongroup );
var next = $nextRegion.length && $nextRegion.data( 'regionselector' );
if ( next ) {
next.show();
// If cache is defined, then it is already rendered and there's no need
// to re-render it.
if ( nextRegionSelector && nextRegionSelector.cache === null ) {
nextRegionSelector.show();
}
}, 100 );
@@ -133,7 +141,7 @@
listen: function () {
this.$element.on( 'click', $.proxy( this.click, this ) );
this.options.$target.$element.bind( 'scrollend', $.proxy( this.next, this) );
this.options.$target.$element.bind( 'scrollend', $.proxy( this.next, this ) );
},
click: function ( e ) {

View File

@@ -89,7 +89,16 @@
assert.ok( $.fn.uls, "$.fn.uls is defined" );
} );
test( "-- $.uls.data testing", 41, function ( assert ) {
test( "-- $.uls.data testing", 46, function ( assert ) {
// Add a language in run time.
// This is done early to make sure that it doesn't break other functions.
$.uls.data.addLanguage( 'qqq', {
script: 'Latn',
regions: ['SP'],
autonym: 'Language documentation'
} );
assert.ok( $.uls.data.getAutonym( 'qqq' ), 'Language documentation', 'Language qqq was added with the correct autonym' );
assert.strictEqual( $.uls.data.isRedirect( 'sr-ec' ), 'sr-cyrl', "'sr-ec' is a redirect to 'sr-cyrl'" );
var autonyms = $.uls.data.getAutonyms();
@@ -144,7 +153,7 @@
assert.deepEqual( $.uls.data.getLanguagesInRegions( ["AM", "WW"] ),
[
"akz", "arn", "aro", "ase", "avk", "ay", "cho", "chr", "chy", "cr", "cr-latn",
"en-ca", "en", "eo", "es-419", "es-formal", "es", "esu", "fr", "gcf", "gn",
"en-ca", "en", "eo", "es-419", "es-formal", "es", "esu", "fr", "frc", "gcf", "gn",
"guc", "haw", "ht", "ia", "ie", "ik", "ike-cans", "ike-latn", "io", "iu", "jam",
"jbo", "kgp", "kl", "lad-latn", "lad-hebr", "lfn", "mfe", "mic", "mus", "nah", "nl-informal", "nl",
"nov", "nv", "pap", "pdc", "pdt", "ppl", "pt-br", "pt", "qu", "qug", "rap", "sei",
@@ -166,14 +175,14 @@
);
assert.deepEqual( $.uls.data.getRegionsInGroup( 3 ), [
"EU", "ME", "AF"
], "regions in group 3 are selected correctly" );
'EU', 'ME', 'AF'
], 'regions in group 3 are selected correctly' );
assert.deepEqual( $.uls.data.getRegionsInGroup( 2 ), [
"AM"
], "regions in group 2 are selected correctly" );
'AM'
], 'regions in group 2 are selected correctly' );
assert.deepEqual( $.uls.data.getRegionsInGroup( 1 ), [
"WW"
], "regions in group 1 are selected correctly" );
'WW', 'SP'
], 'regions in group 1 are selected correctly' );
var languagesByScriptInAM = $.uls.data.getLanguagesByScriptInRegion( "AM" );
assert.deepEqual( languagesByScriptInAM['Cans'], [
@@ -189,6 +198,24 @@
assert.strictEqual( $.uls.data.getAutonym( 'pa' ), 'ਪੰਜਾਬੀ', 'Correct autonym of the Punjabi language was selected using code pa.' );
assert.strictEqual( $.uls.data.getAutonym( 'pa-guru' ), 'ਪੰਜਾਬੀ', 'Correct autonym of the Punjabi language was selected using code pa-guru.' );
var languagesToGroup = {
'en': 'English',
'fiu-vro': 'Võro', // Alias before target
'ru': 'русский',
'sr': 'српски', // Alias before target
'sr-cyrl': 'српски', // Target before alias
'sr-latn': 'srpski', // Target before alias
'sr-el': 'srpski', // Alias after target
'vro': 'Võro' // Target after alias
},
groupedLanguages = {
Latin: [ 'en', 'vro', 'sr-latn' ],
Cyrillic: [ 'ru', 'sr-cyrl' ]
};
assert.deepEqual( $.uls.data.getLanguagesByScriptGroup( languagesToGroup ), groupedLanguages,
'A custom list of languages is grouped correctly using getLanguagesByScriptGroup.' );
var languagesByScriptGroupInEMEA = $.uls.data.getLanguagesByScriptGroupInRegions( $.uls.data.getRegionsInGroup( 3 ) );
assert.deepEqual( languagesByScriptGroupInEMEA['WestCaucasian'], [
'hy', 'ka', 'xmf'
@@ -204,8 +231,8 @@
], 'All languages in the Greek script found' );
assert.deepEqual( $.uls.data.getAllRegions(), [
"WW", "AM", "EU", "ME", "AF", "AS", "PA"
], "All regions found" );
'WW', 'SP', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA'
], 'All regions found' );
// autonyms: gn: avañe'ẽ, de: deutsch, hu: magyar, fi: suomi
assert.deepEqual( ['de', 'fi', 'gn', 'hu'].sort( $.uls.data.sortByAutonym ), [
@@ -219,6 +246,10 @@
assert.ok( $.inArray( "sah", $.uls.data.getLanguagesInTerritory( "RU" ) )
> -1, "Sakha language is spoken in Russia" );
assert.ok( $.uls.data.deleteLanguage( 'qqq' ), 'Deleting language qqq, which was added earlier, returns true.' );
assert.strictEqual( $.uls.data.languages['qqq'], undefined, 'Data about qqq is undefined after being deleted.' );
assert.ok( !$.uls.data.deleteLanguage( 'qqr' ), 'Deleting language qqr, which was never added, returns false.' );
} );
}( jQuery ) );