/*!
* ULS-based display settings panel
*
* Copyright (C) 2012 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland 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 General Public Licence 2.0 or later
* @licence MIT License
*/
( function () {
'use strict';
var template = '
' +
// Tab switcher buttons
'
' +
'
' +
'
' +
'' +
'' +
'
' +
'
' +
'
' +
// Begin display language sub-panel
'
' +
// "Display language", title above the buttons row
'
' +
'
' +
'' +
'
' +
'
' +
// UI languages buttons row
'
' +
'
' +
'' +
'
' +
'
' +
// End display language section
'
' +
// Begin font settings section, hidden by default
'
' +
// "Font settings" title
'
' +
'
' +
'' +
'
' +
'
' +
'
' +
// Menus font selection dropdown with label
'
' +
'
' +
'' +
'
' +
'' +
'
' +
// Content font selection dropdown with label
'
' +
'
' +
'' +
'
' +
'' +
'
' +
// End font selectors
'
' +
// Webfonts enabling checkbox with label
'
' +
'
' +
'
' +
'' +
'' +
'
' +
'
' +
'
' +
// End font settings section
'
';
function DisplaySettings( $parent ) {
this.nameI18n = 'ext-uls-display-settings-title-short';
this.descriptionI18n = 'ext-uls-display-settings-desc';
this.$template = $( template );
this.uiLanguage = this.getUILanguage();
this.contentLanguage = this.getContentLanguage();
this.$webfonts = null;
this.$parent = $parent;
this.savedRegistry = $.extend( true, {}, mw.webfonts.preferences );
this.dirty = false;
}
DisplaySettings.prototype = {
constructor: DisplaySettings,
/**
* Loads the webfonts module sets the `webfonts` property when its safe to do so
*
* @return {jQuery.Promise}
*/
setupWebFonts: function () {
var d = $.Deferred();
mw.loader.using( [ 'ext.uls.webfonts.repository' ] ).then( function () {
if ( this.isWebFontsEnabled ) {
mw.webfonts.setup();
}
// Allow the webfonts library to finish loading (hack)
setTimeout( function () {
this.$webfonts = $( document.body ).data( 'webfonts' );
d.resolve();
}.bind( this ), 1 );
}.bind( this ) );
return d.promise();
},
/**
* Render the module into a given target
*/
render: function () {
this.setupWebFonts().then( function () {
this.renderAfterDependenciesLoaded();
}.bind( this ) );
},
/**
* Render the module into a given target after all
*/
renderAfterDependenciesLoaded: function () {
this.$parent.$settingsPanel.empty();
this.$parent.$settingsPanel.append( this.$template );
this.prepareLanguages();
this.prepareUIFonts();
this.prepareContentFonts();
this.prepareWebfontsCheckbox();
// Usually this is already loaded, but when changing language it
// might not be.
this.preview( this.uiLanguage );
this.listen();
},
prepareWebfontsCheckbox: function () {
var webFontsEnabled = this.isWebFontsEnabled();
if ( !webFontsEnabled ) {
$( '#uls-display-settings-font-selectors' ).addClass( 'hide' );
}
$( '#webfonts-enable-checkbox' ).prop( 'checked', webFontsEnabled );
},
isWebFontsEnabled: function () {
return mw.webfonts.preferences.isEnabled();
},
/**
* Prepare the UI language selector
*/
prepareLanguages: function () {
var $loginCta,
displaySettings = this,
SUGGESTED_LANGUAGES_NUMBER = 3,
anonsAllowed = mw.config.get( 'wgULSAnonCanChangeLanguage' ),
languagesForButtons, $languages, suggestedLanguages,
lang, i, language, $button, autonym;
// Don't let anonymous users change interface language
if ( !anonsAllowed && mw.user.isAnon() ) {
$loginCta = $( '
' ).append(
$( '' )
.addClass( 'uls-display-settings-anon-label' )
// .html() is needed for correct parsing of the nbsp
.html( $.i18n( 'ext-uls-display-settings-anon-label' ) + ' ' ),
$( '' )
.text( $.i18n( 'ext-uls-display-settings-anon-same-as-content', autonym ) )
),
$loginCta
);
new mw.Api().parse( $.i18n( 'ext-uls-display-settings-anon-log-in-cta' ) )
.done( function ( parsedCta ) {
// The parsed CTA is HTML
$loginCta.html( parsedCta );
$loginCta.find( 'a' ).on( 'click', function () {
// If EventLogging is installed and enabled for ULS, give it a
// chance to log this event. There is no promise provided and in
// most browsers this will use the Beacon API in the background.
// In older browsers, this event will likely get lost.
mw.hook( 'mw.uls.login.click' );
} );
} );
return;
}
$languages = this.$template.find( 'div.uls-ui-languages' );
suggestedLanguages = this.frequentLanguageList()
// Common world languages, for the case that there are
// too few suggested languages
.concat( [ 'en', 'zh', 'fr' ] );
// Content language is always on the first button
languagesForButtons = [ this.contentLanguage ];
// This is needed when drawing the panel for the second time
// after selecting a different language
$languages.find( 'button' ).remove();
// UI language must always be present
if ( this.uiLanguage !== this.contentLanguage ) {
languagesForButtons.push( this.uiLanguage );
}
for ( lang in suggestedLanguages ) {
// Skip already found languages
if ( languagesForButtons.indexOf( suggestedLanguages[ lang ] ) > -1 ) {
continue;
}
languagesForButtons.push( suggestedLanguages[ lang ] );
// No need to add more languages than buttons
if ( languagesForButtons.length >= SUGGESTED_LANGUAGES_NUMBER ) {
break;
}
}
function buttonHandler( button ) {
return function () {
displaySettings.markDirty();
displaySettings.uiLanguage = button.data( 'language' ) || displaySettings.uiLanguage;
$( 'div.uls-ui-languages button.mw-ui-button' ).removeClass( 'mw-ui-pressed' );
button.addClass( 'mw-ui-pressed' );
displaySettings.prepareUIFonts();
displaySettings.preview( displaySettings.uiLanguage );
};
}
// Add the buttons for the most likely languages
for ( i = 0; i < SUGGESTED_LANGUAGES_NUMBER; i++ ) {
language = languagesForButtons[ i ];
$button = $( '