/**
* 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 ( $, mw, undefined ) {
'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 chechbox with label
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ '
' // End font settings section
// Separator
+ ''
// Apply and Cancel buttons
+ '
'
+ '
'
+ ''
+ ''
+ '
'
+ '
'
+ '
';
function DisplaySettings( $parent ) {
this.name = $.i18n( 'ext-uls-display-settings-title-short' );
this.description = $.i18n( 'ext-uls-display-settings-desc' );
this.$template = $( template );
this.uiLanguage = this.getUILanguage();
this.contentLanguage = this.getContentLanguage();
this.$webfonts = null;
this.$parent = $parent;
}
DisplaySettings.prototype = {
Constructor: DisplaySettings,
/**
* Render the module into a given target
*/
render: function () {
this.$parent.$settingsPanel.empty();
this.$webfonts = $( 'body' ).data( 'webfonts' );
this.$parent.$settingsPanel.append( this.$template );
this.prepareLanguages();
this.prepareUIFonts();
this.prepareContentFonts();
this.prepareWebfontsCheckbox();
this.i18n();
this.listen();
},
prepareWebfontsCheckbox: function () {
var webFontsEnabled = this.isWebFontsEnabled();
if ( !webFontsEnabled ) {
this.$template.find(
'#uls-display-settings-font-selectors'
).addClass( 'hide' );
}
$( '#webfonts-enable-checkbox' ).prop( 'checked', webFontsEnabled );
},
isWebFontsEnabled: function () {
var enable = mw.webfonts.preferences.isEnabled();
// If the user didn't use the checkbox, the preference will be undefined.
// The default for now is to enable webfonts if the user didn't select anything.
if ( enable === undefined ) {
enable = true;
}
return enable;
},
/**
* Prepare the UI language selector
*/
prepareLanguages: function () {
var loginUri, $loginCta,
displaySettings = this,
SUGGESTED_LANGUAGES_NUMBER = 3,
anonsAllowed = mw.config.get( 'wgULSAnonCanChangeLanguage' ),
languagesForButtons, $languages, suggestedLanguages,
lang, i, language, $button;
// Don't let anonymous users change interface language
if ( !anonsAllowed && mw.user.isAnon() ) {
loginUri = new mw.Uri();
loginUri.query = {
title: 'Special:UserLogin'
};
$loginCta = $( '
' ).append(
$( '' )
.addClass( 'uls-display-settings-anon-label' )
.html( $.i18n( 'ext-uls-display-settings-anon-label' ) + ' ' ),
$( '' )
.text( $.i18n( 'ext-uls-display-settings-anon-same-as-content' ) )
),
$loginCta
);
new mw.Api().parse( $.i18n( 'ext-uls-display-settings-anon-log-in-cta' ) )
.done( function ( parsedCta ) {
$loginCta.html( parsedCta );
} );
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 ( $.inArray( suggestedLanguages[lang], languagesForButtons ) > -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.enableApplyButton();
displaySettings.uiLanguage = button.data( 'language' ) || displaySettings.uiLanguage;
$( 'div.uls-ui-languages button.button' ).removeClass( 'down' );
button.addClass( 'down' );
displaySettings.prepareUIFonts();
$.i18n().locale = displaySettings.uiLanguage;
displaySettings.i18n();
};
}
// Add the buttons for the most likely languages
for ( i = 0; i < SUGGESTED_LANGUAGES_NUMBER; i++ ) {
language = languagesForButtons[i];
$button = $( '