From ce383b803efdd61c5e23e4a829aabf5e73175b68 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Mon, 24 Mar 2014 12:51:03 +0530 Subject: [PATCH] Push selected language to previous languages list Bug: 62692 Change-Id: I3b40ed1038cec0b34d3b0fbefcae4d4ae650ce36 --- resources/js/ext.uls.compactlinks.js | 36 +++++++++++++++++++++++----- resources/js/ext.uls.init.js | 19 +++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/resources/js/ext.uls.compactlinks.js b/resources/js/ext.uls.compactlinks.js index 6e3b3705..6d97c637 100644 --- a/resources/js/ext.uls.compactlinks.js +++ b/resources/js/ext.uls.compactlinks.js @@ -93,6 +93,14 @@ 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 */ @@ -118,6 +126,8 @@ onSelect: function( language ) { supportedLangs = getInterlanguageList(); + // To set selected language as a previous language + insertPreviousLanguage( language ); window.location.href = supportedLangs[language]; }, @@ -145,20 +155,34 @@ */ function displayLanguages( numberOfLanguagesToShow ) { var commonLang = getCommonLanguages(), - currentLangs = getInterlanguageList(), i, + acceptedLangs = $.map( getCurrentLanguages(), function ( element, index ) { + return index; + } ), i, + prevLangs = mw.uls.getPreviousLanguages(), count, 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++ ) { 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 ) { - for ( i in currentLangs ) { - if ( $.inArray( i, commonLang ) === -1 ) { - finalList.push( i ); + for ( i = 0; i < acceptedLangs.length; i++ ) { + if ( $.inArray( acceptedLangs[i], finalList ) < 0 ) { + finalList.push( acceptedLangs[i] ); count++; if ( count === numberOfLanguagesToShow ) { break; diff --git a/resources/js/ext.uls.init.js b/resources/js/ext.uls.init.js index 2dbe1d0a..b86a6c35 100644 --- a/resources/js/ext.uls.init.js +++ b/resources/js/ext.uls.init.js @@ -120,6 +120,25 @@ 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. * The caller should check the validity of the returned language code.