Support UI language change when global preferences are present

Notable changes:
* First query global preferences to detect if global language setting
  is in use. If there is no global language setting, or if GlobalPreferences
  extension is not installed, it will fall back to changing the language
  as usual. If global language setting is found, it will add an override
  instead.
* If a local override is added, the undo tooltip is different and links to
  the global preferences page. The task design shows mw.notify style popup
  located on a bottom right corner (LTR). I deviate from the design and
  re-use the old undo tooltip with a different message instead, for
  consistency. The message is chosen depending on whether local storage
  value `uls-gp` is set to '1' (set in mw.uls.changeLanguage).
* I removed one use of deprecated mediawiki.api.options module. One other
  use still remains.
* I changed tooltip text generation from html acrobatics to use
  mw.message.parseDom. Because of that I also had to move the click handler
  to avoid buildup of click handlers.
* In message documentation fixed acronym -> autonym.

Bug: T198206
Change-Id: Ie2ed792e222be919522bd1cdea98042515a0619d
This commit is contained in:
Niklas Laxström
2018-08-14 09:46:37 +02:00
committed by jenkins-bot
parent 2e5e57d92c
commit fa9cea4627
5 changed files with 70 additions and 42 deletions

View File

@@ -50,23 +50,49 @@
}
deferred.done( function () {
var api;
var api = new mw.Api();
if ( mw.user.isAnon() ) {
changeLanguageAnon();
return;
}
api = new mw.Api();
api.saveOption( 'language', language )
.done( function () {
location.reload();
} )
.fail( function () {
// Set options failed. Maybe the user has logged off.
// Continue like anonymous user and set cookie.
changeLanguageAnon();
// TODO We can avoid doing this query if we know global preferences are not enabled
api.get( {
action: 'query',
meta: 'globalpreferences',
gprprop: 'preferences'
} ).then( function ( res ) {
// Check whether global preferences are in use. If they are not, `res.query` is
// an empty object. `res` will also contain warnings about unknown parameters.
try {
return !!res.query.globalpreferences.preferences.language;
} catch ( e ) {
return false;
}
} ).then( function ( hasGlobalPreference ) {
var apiModule;
if ( hasGlobalPreference ) {
apiModule = 'globalpreferenceoverrides';
mw.storage.set( 'uls-gp', '1' );
} else {
apiModule = 'options';
mw.storage.remove( 'uls-gp' );
}
return api.postWithToken( 'csrf', {
action: apiModule,
optionname: 'language',
optionvalue: language
} );
} ).done( function () {
location.reload();
} ).fail( function () {
// Setting the option failed. Maybe the user has logged off.
// Continue like anonymous user and set cookie.
changeLanguageAnon();
} );
} );
mw.hook( 'mw.uls.interface.language.change' ).fire( language, deferred );

View File

@@ -157,24 +157,6 @@
// hide the tooltip when clicked on it
$( '.uls-tipsy' ).on( 'click', hideTipsy );
// Event handler for links in the tooltip.
// It looks like the tipsy is always created from scratch so that
// there wont be multiple event handlers bound to same click.
$( 'a.uls-prevlang-link' ).on( 'click.ulstipsy', function ( event ) {
var deferred = $.Deferred();
event.preventDefault();
deferred.done( function () {
mw.uls.changeLanguage( event.target.lang );
} );
mw.hook( 'mw.uls.language.revert' ).fire( deferred );
// Delay is zero if event logging is not enabled
window.setTimeout( function () {
deferred.resolve();
}, mw.config.get( 'wgULSEventLogging' ) * 500 );
} );
tipsyTimer = window.setTimeout( hideTipsy, timeout );
}
@@ -203,9 +185,12 @@
$floatableContainer: $ulsTrigger,
position: ulsPopupPosition,
$content: ( function () {
var link = $( '<a>' ).text( previousAutonym )
.attr( {
href: '#',
var messageKey, $link;
$link = $( '<a>' )
.text( previousAutonym )
.prop( {
href: '',
'class': 'uls-prevlang-link',
lang: previousLang,
// We could get dir from uls.data,
@@ -213,16 +198,30 @@
// and 'auto' is safe enough in this context.
// T130390: must use attr
dir: 'auto'
} )
.on( 'click', function ( event ) {
var deferred = $.Deferred();
event.preventDefault();
deferred.done( function () {
mw.uls.changeLanguage( event.target.lang );
} );
mw.hook( 'mw.uls.language.revert' ).fire( deferred );
// Delay is zero if event logging is not enabled
window.setTimeout( function () {
deferred.resolve();
}, mw.config.get( 'wgULSEventLogging' ) * 500 );
} );
// Get the html of the link by wrapping it in div first
link = $( '<div>' ).html( link ).html();
if ( mw.storage.get( 'uls-gp' ) === '1' ) {
messageKey = 'ext-uls-undo-language-tooltip-text-local';
} else {
messageKey = 'ext-uls-undo-language-tooltip-text';
}
return $( '<p>' )
.html(
mw.message( 'ext-uls-undo-language-tooltip-text', '$1' )
.escaped().replace( '$1', link )
);
return $( '<p>' ).append( mw.message( messageKey, $link ).parseDom() );
}() )
} );