Skip to content

Commit

Permalink
Add MUL as last language in Termbox if any MUL term exists
Browse files Browse the repository at this point in the history
This is the client side part only, the server side
termbox rendering will be changed in I9d214fdb21c.

This is going to result in more mismatches with the
server generated DOM: We want to have "mul" in the
last position always (unless the user has "mul" in
their babels), which is the same behavoir we expect
in the server rendering. But, for logged out users,
we might dynamically add more languages from ULS,
meaning we need to discard the previous "mul" entry
and re-create it (but if adding languages from ULS,
we we were already touching the DOM anyway).

Bug: T339103
Change-Id: I1778ea90ebbf89bb6083e3fe324219878f3c9033
  • Loading branch information
mariushoch committed Jun 25, 2023
1 parent 2823ef7 commit 5bafe18
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
*/
_moreLanguagesItems: {},

/**
* @type {string[]} List of languages shown per default.
*/
_defaultLanguages: [],

/**
* @see jQuery.ui.TemplatedWidget._create
*/
Expand All @@ -83,6 +88,13 @@
}

PARENT.prototype._create.call( this );
this._defaultLanguages = this.options.userLanguages;

var furtherLanguages = this._getMoreLanguages();
if ( 'mul' in furtherLanguages ) {
// We have a mul term, always show it per default (but in the last position)
this._defaultLanguages.push( 'mul' );
}

this._verifyExistingDom();
this._createListView();
Expand Down Expand Up @@ -123,19 +135,23 @@

// Scrape languages from static HTML:
var mismatchAt = null,
userLanguages = this.options.userLanguages;
languages = this._defaultLanguages;
$entitytermsforlanguageview.each( function ( i ) {
var match = $( this )
.attr( 'class' )
.match( /(?:^|\s)wikibase-entitytermsforlanguageview-(\S+)/ );
if ( match && match[ 1 ] !== userLanguages[ i ] ) {
if ( match && match[ 1 ] !== languages[ i ] ) {
if ( match[ 1 ] !== 'mul' ) {
// "mul" might be included in the existing term box, but we want it to be after
// everything else, thus discarding it is expected.
mw.log.warn( 'Existing entitytermsforlanguagelistview DOM does not match configured languages' );
}
mismatchAt = i;
return false;
}
} );

if ( mismatchAt !== null ) {
mw.log.warn( 'Existing entitytermsforlanguagelistview DOM does not match configured languages' );
$entitytermsforlanguageview.slice( mismatchAt ).remove();
}
},
Expand Down Expand Up @@ -183,7 +199,7 @@
};
}
} ),
value: this.options.userLanguages.map( function ( lang ) {
value: this._defaultLanguages.map( function ( lang ) {
return self._getValueForLanguage( lang );
} ),
listItemNodeName: 'TR'
Expand Down Expand Up @@ -222,7 +238,7 @@
*/
_hasMoreLanguages: function () {
var fingerprint = this.options.value,
minLength = this.options.userLanguages.length;
minLength = this._defaultLanguages.length;

if ( fingerprint.getLabels().length > minLength
|| fingerprint.getDescriptions().length > minLength
Expand Down Expand Up @@ -340,7 +356,7 @@
languages[ lang ] = lang;
} );

this.options.userLanguages.forEach( function ( lang ) {
this._defaultLanguages.forEach( function ( lang ) {
delete languages[ lang ];
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
undefined,
'Created widget.'
);
assert.deepEqual(
entitytermsforlanguagelistview._defaultLanguages,
[ 'de', 'en' ],
'Default languages without "mul".'
);

entitytermsforlanguagelistview.destroy();

Expand Down Expand Up @@ -158,4 +163,19 @@
assert.strictEqual( !entitytermsforlanguagelistview._hasMoreLanguages(), true );
} );

QUnit.test( 'mul handling', function ( assert ) {
var fingerprint = createFingerprint();
fingerprint.setLabel( 'mul', new datamodel.Term( 'mul', 'mul-label' ) );

var $entitytermsforlanguagelistview = createEntitytermsforlanguagelistview( { value: fingerprint } ),
entitytermsforlanguagelistview
= $entitytermsforlanguagelistview.data( 'entitytermsforlanguagelistview' );

assert.deepEqual(
entitytermsforlanguagelistview._defaultLanguages,
[ 'de', 'en', 'mul' ],
'Default languages if a "mul" term is present.'
);
} );

}() );

0 comments on commit 5bafe18

Please sign in to comment.