Push selected language to previous languages list

Bug: 62692
Change-Id: I3b40ed1038cec0b34d3b0fbefcae4d4ae650ce36
This commit is contained in:
Santhosh Thottingal
2014-03-24 12:51:03 +05:30
parent cd273dba57
commit ce383b803e
2 changed files with 49 additions and 6 deletions

View File

@@ -93,6 +93,14 @@
return commonLanguages; return commonLanguages;
} }
/**
* Push the selected language into the previous languages list
* @param {string} Language code of language selected (clicked on)
*/
function insertPreviousLanguage( currentLang ) {
mw.uls.insertPreviousLanguage( currentLang );
}
/** /**
* Add a ULS trigger beneath the interlanguage links * Add a ULS trigger beneath the interlanguage links
*/ */
@@ -118,6 +126,8 @@
onSelect: function( language ) { onSelect: function( language ) {
supportedLangs = getInterlanguageList(); supportedLangs = getInterlanguageList();
// To set selected language as a previous language
insertPreviousLanguage( language );
window.location.href = supportedLangs[language]; window.location.href = supportedLangs[language];
}, },
@@ -145,20 +155,34 @@
*/ */
function displayLanguages( numberOfLanguagesToShow ) { function displayLanguages( numberOfLanguagesToShow ) {
var commonLang = getCommonLanguages(), var commonLang = getCommonLanguages(),
currentLangs = getInterlanguageList(), i, acceptedLangs = $.map( getCurrentLanguages(), function ( element, index ) {
return index;
} ), i,
prevLangs = mw.uls.getPreviousLanguages(),
count, count,
finalList = []; finalList = [];
// Check existing languages for ones in common, and add them to final list // Add languages in the common list and accepted by article
for ( i = 0; i < commonLang.length; i++ ) { for ( i = 0; i < commonLang.length; i++ ) {
finalList.push( commonLang[i] ); finalList.push( commonLang[i] );
} }
count = commonLang.length; // Add languages in previous choices to list if not already in it
for ( i = 0; i < prevLangs.length; i++ ) {
if (
$.inArray( prevLangs[i], finalList ) < 0 &&
$.inArray( prevLangs[i], acceptedLangs ) >= 0
) {
finalList.push( prevLangs[i] );
}
}
// Add random languages to make the language list long enough, if it isn't already
count = finalList.length;
if ( count < numberOfLanguagesToShow ) { if ( count < numberOfLanguagesToShow ) {
for ( i in currentLangs ) { for ( i = 0; i < acceptedLangs.length; i++ ) {
if ( $.inArray( i, commonLang ) === -1 ) { if ( $.inArray( acceptedLangs[i], finalList ) < 0 ) {
finalList.push( i ); finalList.push( acceptedLangs[i] );
count++; count++;
if ( count === numberOfLanguagesToShow ) { if ( count === numberOfLanguagesToShow ) {
break; break;

View File

@@ -120,6 +120,25 @@
return $.parseJSON( previousLanguages ).slice( -5 ); return $.parseJSON( previousLanguages ).slice( -5 );
}; };
/**
* Push the selected language into the previous languages list
* if it isn't there already
* @param {string} Language code of language to be pushed into list
*/
mw.uls.insertPreviousLanguage = function ( prevLangCode ) {
var previousLanguages = mw.uls.getPreviousLanguages() || [],
currentLangIndex;
// Checking if it already exists in array
currentLangIndex = $.inArray( prevLangCode, previousLanguages );
if ( currentLangIndex < 0 ) {
previousLanguages.push( prevLangCode );
} else {
previousLanguages.splice( currentLangIndex, 1 );
previousLanguages.push( prevLangCode );
}
mw.uls.setPreviousLanguages( previousLanguages );
};
/** /**
* Returns the browser's user interface language or the system language. * Returns the browser's user interface language or the system language.
* The caller should check the validity of the returned language code. * The caller should check the validity of the returned language code.