Merge "Separate ULS previous interface language from previously selected languages"

This commit is contained in:
jenkins-bot
2016-05-25 06:13:13 +00:00
committed by Gerrit Code Review

View File

@@ -20,7 +20,23 @@
( function ( $, mw ) {
'use strict';
var previousLanguageAutonymStorageKey = 'uls-previous-language-autonym';
// Replace with mediawiki.storage when >= MW 1.26
var Store = function ( key ) {
this.key = key;
};
// Returns null if key does not exist according to documentation.
Store.prototype.get = function () {
try {
return localStorage.getItem( this.key );
} catch ( e ) {}
};
Store.prototype.set = function ( value ) {
try {
localStorage.setItem( this.key, value );
} catch ( e ) {}
};
/**
* Construct the display settings link
@@ -171,59 +187,17 @@
} );
}
/**
* Gets the name of the previously active language
*
* @param {string} code Language code of previously selected language.
* @return {jQuery.Promise}
*/
function getUndoAutonym( code ) {
var autonym,
deferred = $.Deferred();
try {
autonym = localStorage.getItem( previousLanguageAutonymStorageKey );
} catch ( e ) {}
if ( autonym ) {
mw.loader.using( 'jquery.tipsy', function () {
deferred.resolve( autonym );
} );
} else {
mw.loader.using( [ 'jquery.uls.data', 'jquery.tipsy' ], function () {
deferred.resolve( $.uls.data.getAutonym( code ) );
} );
}
return deferred.promise();
}
function userCanChangeLanguage() {
return mw.config.get( 'wgULSAnonCanChangeLanguage' ) || !mw.user.isAnon();
}
function userHasChangedLanguage() {
var previousLang = mw.uls.getPreviousLanguages()[ 0 ],
currentLang = mw.config.get( 'wgUserLanguage' );
// Changed language is saved in showUndoTooltip, which is never
// called if previousLang is not defined, which will never be
// defined unless we do it now.
if ( previousLang === undefined ) {
mw.uls.addPreviousLanguage( currentLang );
}
return previousLang && previousLang !== currentLang;
}
/**
* The tooltip to be shown when language changed using ULS.
* It also allows to undo the language selection.
*/
function showUndoTooltip() {
var previousLanguages, previousLang, $ulsTrigger,
function showUndoTooltip( previousLang, previousAutonym ) {
var $ulsTrigger,
ulsPosition = mw.config.get( 'wgULSPosition' ),
currentLang = mw.config.get( 'wgUserLanguage' ),
rtlPage = $( 'body' ).hasClass( 'rtl' ),
tipsyGravity = {
personal: 'n',
@@ -274,12 +248,6 @@
tipsyTimer = window.setTimeout( hideTipsy, timeout );
}
previousLanguages = mw.uls.getPreviousLanguages();
previousLang = previousLanguages[ 0 ];
mw.uls.addPreviousLanguage( currentLang );
getUndoAutonym( previousLang ).done( function ( autonym ) {
// Attach a tipsy tooltip to the trigger
$ulsTrigger.tipsy( {
gravity: tipsyGravity[ ulsPosition ],
@@ -290,7 +258,7 @@
title: function () {
var link;
link = $( '<a>' ).text( autonym )
link = $( '<a>' ).text( previousAutonym )
.attr( {
href: '#',
'class': 'uls-prevlang-link',
@@ -322,17 +290,6 @@
showTipsy( 3000 );
}
} );
} );
// Now that we set the previous languages,
// we can store the previous autonym.
// TODO: Refactor this, because it doesn't directly belong
// to the tooltip.
try {
localStorage.setItem(
previousLanguageAutonymStorageKey, mw.config.get( 'wgULSCurrentAutonym' )
);
} catch ( e ) {}
}
function initInterface() {
@@ -342,7 +299,6 @@
rtlPage = $( 'body' ).hasClass( 'rtl' ),
anonMode = ( mw.user.isAnon() &&
!mw.config.get( 'wgULSAnonCanChangeLanguage' ) ),
imeSelector = mw.config.get( 'wgULSImeSelectors' ).join( ', ' ),
ulsPosition = mw.config.get( 'wgULSPosition' );
if ( ulsPosition === 'interlanguage' ) {
@@ -494,11 +450,46 @@
return false;
} );
if ( userCanChangeLanguage() && userHasChangedLanguage() ) {
showUndoTooltip();
}
function initTooltip() {
var previousLanguageCodeStore, previousLanguageAutonymStore,
previousLanguage, currentLanguage, previousAutonym, currentAutonym;
if ( !userCanChangeLanguage() ) {
return;
}
previousLanguageCodeStore = new Store( 'uls-previous-language-code' );
previousLanguageAutonymStore = new Store( 'uls-previous-language-autonym' );
previousLanguage = previousLanguageCodeStore.get();
currentLanguage = mw.config.get( 'wgUserLanguage' );
previousAutonym = previousLanguageAutonymStore.get();
currentAutonym = mw.config.get( 'wgULSCurrentAutonym' );
// If storage is empty, i.e. first visit, then store the current language
// immediately so that we know when it changes.
if ( !previousLanguage || !previousAutonym ) {
previousLanguageCodeStore.set( currentLanguage );
previousLanguageAutonymStore.set( currentAutonym );
return;
}
if ( previousLanguage !== currentLanguage ) {
mw.loader.using( 'jquery.tipsy' ).done( function () {
showUndoTooltip( previousLanguage, previousAutonym );
} );
previousLanguageCodeStore.set( currentLanguage );
previousLanguageAutonymStore.set( currentAutonym );
// Store this language in a list of frequently used languages
mw.uls.addPreviousLanguage( currentLanguage );
}
}
function initIme() {
var imeSelector = mw.config.get( 'wgULSImeSelectors' ).join( ', ' );
$( 'body' ).on( 'focus.imeinit', imeSelector, function () {
var $input = $( this );
$( 'body' ).off( '.imeinit' );
@@ -511,5 +502,7 @@
$( document ).ready( function () {
initInterface();
initTooltip();
initIme();
} );
}( jQuery, mediaWiki ) );