/** * Compact the interlanguage links in the sidebar * * Copyright (C) 2012-2014 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland, Niharika Kohli * and other contributors. See CREDITS for a list. * * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't * have to do anything special to choose one license or the other and you don't * have to notify anyone which license you are using. You are free to use * UniversalLanguageSelector in commercial projects as long as the copyright * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. * * @file * @ingroup Extensions * @licence GNU GPL-2.0+ * @licence MIT License */ ( function ( $, mw ) { 'use strict'; /** * Add a language to the interlanguage list * @param {string} name Name of language in Autonym font * @param {string} url Link of the article in the respective language wiki */ function addLanguage( name, url ) { var $linkNode, $listNode, $interlanguageList; $linkNode = $( '' ) .addClass( 'active' ) .attr( 'href', url ) .text( name ); $listNode = $( '
  • ' ) .append( $linkNode ); $interlanguageList = $( '#p-lang > div > ul' ); $interlanguageList.append( $listNode ); } /** * Find out the existing languages supported * by article and fetch their href * @return {Object} List of exiting language codes and their hrefs */ function getInterlanguageList() { var interlangList = {}, selectedElement; $( '#p-lang > div > ul > li > a' ).each( function() { selectedElement = $( this ); interlangList[ selectedElement.attr( 'lang' ) ] = selectedElement.attr( 'href' ); } ); return interlangList; } /** * Fetch list of language names(in Autonym) which are supported by the article * @return {Object} List of Language names in Autonym supported by article */ function getCurrentLanguages() { var acceptedLanglist = {}, interlangList = getInterlanguageList(), i; for ( i in interlangList ) { if( interlangList.hasOwnProperty( i ) ) { acceptedLanglist[ i ] = $.uls.data.getAutonym(i); } } return acceptedLanglist; } /** * Frequently spoken languages which are supported by the article for * the Common Languages section of the ULS * @return {Array} List of those language codes which are supported by article and appears * in the Common Languages section of ULS */ function getCommonLanguages() { // From ext.uls.init.js var frequentLang = mw.uls.getFrequentLanguageList(), $acceptedLang = $.map( getCurrentLanguages(), function ( element, index ) { return index; } ), commonLanguages = [], i; for ( i = 0; i < frequentLang.length; i++ ) { if ( $.inArray( frequentLang[i], $acceptedLang ) >= 0 ) { commonLanguages.push( frequentLang[i] ); } } return commonLanguages; } /** * Add a ULS trigger beneath the interlanguage links */ function addULSlink() { var $newLinknode, $interlanguageList, supportedLangs, posOfTrigger; $newLinknode = $( '
    ' ) .addClass( 'mw-interlanguage-selector mw-ui-button active' ) .html( '…' ) .append( $newLinknode ); $interlanguageList = $( '#p-lang > div > ul' ); $interlanguageList.append( $newLinknode ); posOfTrigger = $newLinknode.offset(); $( '.mw-interlanguage-selector' ).uls( { onReady: function() { this.$menu.addClass( 'interlanguage-uls-menu' ); }, onSelect: function( language ) { supportedLangs = getInterlanguageList(); window.location.href = supportedLangs[language]; }, compact: true, left: posOfTrigger.left + $newLinknode.width() + 50 + 'px', top: posOfTrigger.top - $newLinknode.height()/2 - 250 + 'px', languages: getCurrentLanguages(), quickList: getCommonLanguages() } ); } /** * Hide existing languages displayed on the page */ function hideLanguages() { var $languages = $( '.interlanguage-link' ); $languages.hide(); } /** * Returns all languages returned by the commonLanguages function * and randomly some more if the number falls short of numberOfLanguagesToShow parameter * @param {number} numberOfLanguagesToShow Number of languages to be shown in sidebar * @return {Array} Language codes of the final list to be displayed */ function displayLanguages( numberOfLanguagesToShow ) { var commonLang = getCommonLanguages(), currentLangs = getInterlanguageList(), i, count, finalList = []; // Check existing languages for ones in common, and add them to final list for ( i = 0; i < commonLang.length; i++ ) { finalList.push( commonLang[i] ); } count = commonLang.length; if ( count < numberOfLanguagesToShow ) { for ( i in currentLangs ) { if ( $.inArray( i, commonLang ) === -1 ) { finalList.push( i ); count++; if ( count === numberOfLanguagesToShow ) { break; } } } } // Sorting the language list in alphabetical order of ISO codes finalList = finalList.sort(); return finalList; } /* * Adds a label stating the number of more languages * beneath the ULS link * @param {Number} numberOfLanguagesSupported Number of languages supported by article * @param {Number} numberOfLanguagesToShow Number of languages to be shown in the sidebar */ function addLabel( numberOfLanguagesSupported, numberOfLanguagesToShow ) { var $interlanguageList = $( '#p-lang > div > ul' ), newLabel = $( '