/** * 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 = '
' + '
' // Top "Display settings" title + '
' + '

' + '
' + '
' // "Language for display", title above the buttons row + '
' + '
' + '

' + '
' + '
' // UI languages buttons row + '
' + '
' + '
' // "Font settings" title + '
' + '
' + '

' + '
' + '
' // Webfonts enabling chechbox with label + '
' + '
' + '' + '
' + '
' // Menus font selection dropdown with label + '
' + '
' + '' + '
' + '' + '
' // Content font selection dropdown with label + '
' + '
' + '' + '
' + '' + '
' // Separator + '
' // Apply and Cancel buttons + '
' + '
' + '' + '' + '
' + '
' + '
'; // FIXME i18n and too much hardcoding. var DisplaySettings = function ( $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; this.webfontPreferences = mw.uls.preferences( 'webfonts' ); }; 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.listen(); }, prepareWebfontsCheckbox: function () { $( '#webfonts-enable-checkbox' ).prop( 'checked', this.isWebFontsEnabled() ); }, isWebFontsEnabled: function () { var enable = this.webfontPreferences.get( 'webfonts-enabled' ); // 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 displaySettings = this, $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], SUGGESTED_LANGUAGES_NUMBER = 3; // This is needed when drawing the panel for the second time // after selecting a different language $languages.empty(); // UI language must always be present if ( this.uiLanguage !== this.contentLanguage ) { languagesForButtons.push( this.uiLanguage ); } for ( var 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.uiLanguage = button.data( "language" ) || displaySettings.uiLanguage; $( "div.uls-ui-languages button.button" ).removeClass( "down" ); button.addClass( "down" ); displaySettings.prepareUIFonts(); }; } // Add the buttons for the most likely languages for ( var i = 0; i < SUGGESTED_LANGUAGES_NUMBER; i++ ) { var language = languagesForButtons[i], $button = $( '