Refactor interface integration code

Reduce document.ready code by moving functions outside and grouping
tooltip related code to a function.

Also remove window and document from wrapper anon function since
browser: true is defined in .jshintrc

Bug: 48156
Change-Id: I7228285cdcb33b915e96e7e2c59e4dbc90f5fc96
This commit is contained in:
Santhosh Thottingal
2013-05-24 10:29:01 +05:30
committed by Gerrit Code Review
parent 702197a8a7
commit 8655504371
2 changed files with 186 additions and 159 deletions

View File

@@ -760,6 +760,7 @@
*/ */
selectLanguage: function ( languageCode ) { selectLanguage: function ( languageCode ) {
var ime, var ime,
imePref = $.ime.preferences.getIM( languageCode ),
language = $.ime.languages[languageCode]; language = $.ime.languages[languageCode];
if ( !language ) { if ( !language ) {
@@ -772,7 +773,8 @@
// Nothing to do. It is same as the current language, // Nothing to do. It is same as the current language,
// but check whether the input method changed. // but check whether the input method changed.
if ( ime.inputmethod && if ( ime.inputmethod &&
ime.inputmethod.id === $.ime.preferences.getIM( languageCode ) ime.inputmethod.id === imePref ||
imePref === 'system'
) { ) {
return false; return false;
} }

View File

@@ -1,5 +1,5 @@
/** /**
* ULS Interface language selector * ULS interface integration logic
* *
* Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, * Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
@@ -17,56 +17,12 @@
* @licence MIT License * @licence MIT License
*/ */
( function ( $, mw, window, document, undefined ) { ( function ( $, mw ) {
'use strict'; 'use strict';
$( document ).ready( function () { /*
var $ulsTrigger, * Construct the display settings link
$ulsSettingsTrigger, */
$pLang,
ulsOptions,
previousLanguages, previousLang,
anonMode,
rtlPage = $( 'body' ).hasClass( 'rtl' ),
ulsPosition = mw.config.get( 'wgULSPosition' ),
tipsyGravity = {
personal: 'n',
interlanguage: rtlPage ? 'e' : 'w'
},
currentLang = mw.config.get( 'wgUserLanguage' );
anonMode = ( mw.user.isAnon() &&
!mw.config.get( 'wgULSAnonCanChangeLanguage' ) );
if ( ulsPosition === 'interlanguage' ) {
// The interlanguage links section
$pLang = $( '#p-lang' );
// Add an element near the interlanguage links header
$ulsSettingsTrigger = $( '<span>' )
.addClass( 'uls-settings-trigger' )
.attr( 'title', $.i18n( 'ext-uls-language-settings-title' ) );
$pLang.prepend( $ulsSettingsTrigger );
// Remove the dummy link that was added to make sure that the section appears
$pLang.find( '.uls-p-lang-dummy' ).remove();
if ( !$pLang.find( 'div ul' ).children().length ) {
// Replace the title of the interlanguage links
// area if there are no interlanguage links
$pLang.find( 'h3' )
.text( mw.msg( 'uls-plang-title-languages' ) );
// Remove the empty box that appears in the monobook skin
if ( mw.config.get( 'skin' ) === 'monobook' ) {
$pLang.find( 'div.pBody' ).remove();
}
}
}
$ulsTrigger = $( '.uls-trigger' );
previousLanguages = mw.uls.getPreviousLanguages() || [];
previousLang = previousLanguages.slice( -1 )[0];
function displaySettings() { function displaySettings() {
var $displaySettingsTitle, displaySettingsText, $displaySettings; var $displaySettingsTitle, displaySettingsText, $displaySettings;
@@ -82,6 +38,9 @@
return $displaySettings; return $displaySettings;
} }
/*
* Construct the input settings link
*/
function inputSettings() { function inputSettings() {
var $inputSettingsTitle, inputSettingsText, $inputSettings; var $inputSettingsTitle, inputSettingsText, $inputSettings;
@@ -97,8 +56,15 @@
return $inputSettings; return $inputSettings;
} }
/*
* Add display settings link to the settings bar in ULS
* @param {Object} uls The ULS object
*/
function addDisplaySettings( uls ) { function addDisplaySettings( uls ) {
var $displaySettings = displaySettings(), var $displaySettings = displaySettings(),
ulsPosition = mw.config.get( 'wgULSPosition' ),
anonMode = ( mw.user.isAnon() &&
!mw.config.get( 'wgULSAnonCanChangeLanguage' ) ),
displaySettingsOptions = { displaySettingsOptions = {
defaultModule: 'display' defaultModule: 'display'
}; };
@@ -121,6 +87,10 @@
} ); } );
} }
/*
* Add input settings link to the settings bar in ULS
* @param {Object} uls The ULS object
*/
function addInputSettings( uls ) { function addInputSettings( uls ) {
var $inputSettings, position; var $inputSettings, position;
@@ -142,46 +112,23 @@
} ); } );
} }
// ULS options that are common to all modes of showing /*
ulsOptions = { * The tooltip to be shown when language changed using ULS
onReady: function () { * It also allows to undo the language selection.
if ( $.fn.languagesettings ) { */
addDisplaySettings( this ); function showULSTooltip() {
addInputSettings( this ); var $ulsTrigger = $( '.uls-trigger' ),
} ulsPosition = mw.config.get( 'wgULSPosition' ),
currentLang = mw.config.get( 'wgUserLanguage' ),
previousLang,
rtlPage = $( 'body' ).hasClass( 'rtl' ),
tipsyGravity = {
personal: 'n',
interlanguage: rtlPage ? 'e' : 'w'
}, },
onSelect: function ( language ) { previousLanguages = mw.uls.getPreviousLanguages() || [];
mw.uls.changeLanguage( language );
},
languages: mw.config.get( 'wgULSLanguages' ),
searchAPI: mw.util.wikiScript( 'api' ) + '?action=languagesearch',
quickList: function () {
return mw.uls.getFrequentLanguageList();
}
};
if ( ulsPosition === 'interlanguage' ) { previousLang = previousLanguages.slice( -1 )[0];
$ulsSettingsTrigger.attr( 'title', $.i18n( 'ext-uls-select-language-settings-icon-tooltip' ) );
$ulsSettingsTrigger.languagesettings( {
onVisible: function () {
var ulsTriggerOffset = $ulsSettingsTrigger.offset();
this.left = rtlPage ? ulsTriggerOffset.left - 30
:ulsTriggerOffset.left + 30;
this.top = ulsTriggerOffset.top - 50;
this.position();
}
} );
$( '.uls-menu' ).each( function () {
$( this ).prepend(
$( '<span>' ).addClass( 'caret-before' ),
$( '<span>' ).addClass( 'caret-after' )
);
} );
} else if ( anonMode ) {
$ulsTrigger.languagesettings();
} else {
$ulsTrigger.uls( ulsOptions );
}
if ( previousLang === currentLang ) { if ( previousLang === currentLang ) {
// Do not show tooltip nor update language list // Do not show tooltip nor update language list
@@ -265,5 +212,83 @@
$ulsTrigger.on( 'click', function () { $ulsTrigger.on( 'click', function () {
hideTipsy(); hideTipsy();
} ); } );
}
$( document ).ready( function () {
var $ulsTrigger = $( '.uls-trigger' ),
$ulsSettingsTrigger,
$pLang,
ulsOptions,
rtlPage = $( 'body' ).hasClass( 'rtl' ),
anonMode = ( mw.user.isAnon() &&
!mw.config.get( 'wgULSAnonCanChangeLanguage' ) ),
ulsPosition = mw.config.get( 'wgULSPosition' );
if ( ulsPosition === 'interlanguage' ) {
// The interlanguage links section
$pLang = $( '#p-lang' );
// Add an element near the interlanguage links header
$ulsSettingsTrigger = $( '<span>' )
.addClass( 'uls-settings-trigger' )
.attr( 'title', $.i18n( 'ext-uls-language-settings-title' ) );
$pLang.prepend( $ulsSettingsTrigger );
// Remove the dummy link that was added to make sure that the section appears
$pLang.find( '.uls-p-lang-dummy' ).remove();
if ( !$pLang.find( 'div ul' ).children().length ) {
// Replace the title of the interlanguage links
// area if there are no interlanguage links
$pLang.find( 'h3' )
.text( mw.msg( 'uls-plang-title-languages' ) );
// Remove the empty box that appears in the monobook skin
if ( mw.config.get( 'skin' ) === 'monobook' ) {
$pLang.find( 'div.pBody' ).remove();
}
}
}
// ULS options that are common to all modes of showing
ulsOptions = {
onReady: function () {
if ( $.fn.languagesettings ) {
addDisplaySettings( this );
addInputSettings( this );
}
},
onSelect: function ( language ) {
mw.uls.changeLanguage( language );
},
languages: mw.config.get( 'wgULSLanguages' ),
searchAPI: mw.util.wikiScript( 'api' ) + '?action=languagesearch',
quickList: function () {
return mw.uls.getFrequentLanguageList();
}
};
if ( ulsPosition === 'interlanguage' ) {
$ulsSettingsTrigger.attr( 'title', $.i18n( 'ext-uls-select-language-settings-icon-tooltip' ) );
$ulsSettingsTrigger.languagesettings( {
onVisible: function () {
var ulsTriggerOffset = $ulsSettingsTrigger.offset();
this.left = rtlPage ? ulsTriggerOffset.left - 30
:ulsTriggerOffset.left + 30;
this.top = ulsTriggerOffset.top - 50;
this.position();
}
} ); } );
}( jQuery, mediaWiki, window, document ) ); $( '.uls-menu' ).each( function () {
$( this ).prepend(
$( '<span>' ).addClass( 'caret-before' ),
$( '<span>' ).addClass( 'caret-after' )
);
} );
} else if ( anonMode ) {
$ulsTrigger.languagesettings();
} else {
$ulsTrigger.uls( ulsOptions );
}
showULSTooltip();
} );
}( jQuery, mediaWiki ) );