Do not precompute href or autonym in #getInterlanguageList

Only store reference to the element, where they can easily be read
when needed.

Also removed one unnecessary use of `self` as an alias.

Change-Id: I39fa897002037a6d6478a3fb1f40ac60833e392e
This commit is contained in:
Niklas Laxström
2018-10-10 15:31:48 +02:00
committed by jenkins-bot
parent 3f4d43a4cc
commit dd390d2ec0

View File

@@ -172,8 +172,7 @@
* Initialize the plugin * Initialize the plugin
*/ */
CompactInterlanguageList.prototype.init = function () { CompactInterlanguageList.prototype.init = function () {
var self = this, var max = this.options.max || DEFAULT_LIST_SIZE;
max = this.options.max || DEFAULT_LIST_SIZE;
this.interlanguageList = this.getInterlanguageList(); this.interlanguageList = this.getInterlanguageList();
this.listSize = Object.keys( this.interlanguageList ).length; this.listSize = Object.keys( this.interlanguageList ).length;
@@ -186,11 +185,11 @@
// If we're only a bit beyond max, limit to 7 instead of 9. // If we're only a bit beyond max, limit to 7 instead of 9.
// FIXME: This assumes the max is 9. // FIXME: This assumes the max is 9.
self.compactSize = ( self.listSize <= 12 ) ? 7 : max; this.compactSize = ( this.listSize <= 12 ) ? 7 : max;
self.compactList = self.getCompactList(); this.compactList = this.getCompactList();
self.hideOriginal(); this.hideOriginal();
self.render(); this.render();
self.listen(); this.listen();
}; };
/** /**
@@ -200,7 +199,7 @@
var language; var language;
for ( language in this.compactList ) { for ( language in this.compactList ) {
this.compactList[ language ].element.parentNode.style.display = ''; this.compactList[ language ].parentNode.style.display = '';
} }
this.addTrigger(); this.addTrigger();
@@ -218,8 +217,8 @@
self = this, self = this,
ulsLanguageList = {}; ulsLanguageList = {};
$.each( this.interlanguageList, function ( languageCode, language ) { $.each( this.interlanguageList, function ( languageCode, el ) {
ulsLanguageList[ languageCode ] = language.autonym; ulsLanguageList[ languageCode ] = el.textContent;
} ); } );
// Attach ULS to the trigger // Attach ULS to the trigger
@@ -274,20 +273,20 @@
$trigger.addClass( 'selector-open' ); $trigger.addClass( 'selector-open' );
}, },
languageDecorator: function ( $languageLink, language ) { languageDecorator: function ( $languageLink, language ) {
var data = self.interlanguageList[ language ]; var element = self.interlanguageList[ language ];
// Set href, text, and tooltip exactly same as what was in // Set href, text, and tooltip exactly same as what was in
// interlanguage link. The ULS autonym might be different in some // interlanguage link. The ULS autonym might be different in some
// cases like sr. In ULS it is "српски", while in interlanguage links // cases like sr. In ULS it is "српски", while in interlanguage links
// it is "српски / srpski" // it is "српски / srpski"
$languageLink $languageLink
.prop( { .prop( {
href: data.href, href: element.href,
title: data.element.title title: element.title
} ) } )
.text( data.autonym ); .text( element.textContent );
// This code is to support badges used in Wikimedia // This code is to support badges used in Wikimedia
$languageLink.parent().addClass( data.element.parentNode.className ); $languageLink.parent().addClass( element.parentNode.className );
}, },
onCancel: function () { onCancel: function () {
$trigger.removeClass( 'selector-open' ); $trigger.removeClass( 'selector-open' );
@@ -439,18 +438,14 @@
/** /**
* Get the list of languages links. * Get the list of languages links.
* *
* @return {Object} Map of language codes to data * @return {Object} Map of language codes to elements.
*/ */
CompactInterlanguageList.prototype.getInterlanguageList = function () { CompactInterlanguageList.prototype.getInterlanguageList = function () {
var interlanguageList = {}; var interlanguageList = {};
$.each( this.listElement.querySelectorAll( '.interlanguage-link-target' ), function ( i, el ) { $.each( this.listElement.querySelectorAll( '.interlanguage-link-target' ), function ( i, el ) {
var langCode = convertMediaWikiLanguageCodeToULS( el.lang ); var langCode = convertMediaWikiLanguageCodeToULS( el.lang );
interlanguageList[ langCode ] = { interlanguageList[ langCode ] = el;
href: el.href,
autonym: el.textContent,
element: el
};
} ); } );
return interlanguageList; return interlanguageList;