Streamline a few pieces of JavaScript code

The main goal here is to reduce complexity. For example, the method to
remove a parameter from the current URL is never used with another
parameter. The complexity is not needed.

This is inspired by the changes I have seen in Icaf086f.

Change-Id: If22c25e84f50ac380320cd581690835ddb70f01d
This commit is contained in:
Thiemo Kreuz
2020-01-19 17:00:40 +01:00
committed by jenkins-bot
parent 459d55c5d3
commit a5ae7cb556
2 changed files with 20 additions and 20 deletions

View File

@@ -24,8 +24,7 @@
'use strict'; 'use strict';
var ULSDialog = function ( options ) { var ULSDialog = function ( options ) {
var noop = function () { }, var $dialog = options.container,
$dialog = options.container,
hasOverlay = options.hasOverlay, hasOverlay = options.hasOverlay,
$overlay, $overlay,
// Source: https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L4 // Source: https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L4
@@ -42,8 +41,8 @@
'[contenteditable]', '[contenteditable]',
'[tabindex]:not([tabindex^="-"])' '[tabindex]:not([tabindex^="-"])'
], ],
afterClose = options.afterClose || noop, afterOpen = options.afterOpen,
afterOpen = options.afterOpen || noop; afterClose = options.afterClose;
function getFocusableNodes() { function getFocusableNodes() {
return $dialog.find( FOCUSABLE_NODES.join( ', ' ) ); return $dialog.find( FOCUSABLE_NODES.join( ', ' ) );
@@ -159,14 +158,18 @@
addEvents(); addEvents();
showOverlay(); showOverlay();
focusFirstNodeOrOverlay(); focusFirstNodeOrOverlay();
afterOpen(); if ( afterOpen ) {
afterOpen();
}
} }
function close() { function close() {
$dialog.hide(); $dialog.hide();
removeEvents(); removeEvents();
hideOverlay(); hideOverlay();
afterClose(); if ( afterClose ) {
afterClose();
}
} }
function elem() { function elem() {

View File

@@ -58,27 +58,24 @@
} }
function toggleLoading( $btnSubmit, isLoading ) { function toggleLoading( $btnSubmit, isLoading ) {
if ( isLoading ) { $btnSubmit
$btnSubmit.text( mw.msg( 'ext-uls-setlang-loading' ) ); .text( mw.msg( isLoading ? 'ext-uls-setlang-loading' : 'ext-uls-setlang-accept' ) )
} else { .prop( 'disabled', isLoading );
$btnSubmit.text( mw.msg( 'ext-uls-setlang-accept' ) );
}
$btnSubmit.prop( 'disabled', isLoading );
} }
function removeParam( key ) { /**
* @return {string}
*/
function currentUrlWithoutSetLang() {
var uri = new mw.Uri(); var uri = new mw.Uri();
delete uri.query[ key ]; delete uri.query.setlang;
return uri.toString(); return uri.toString();
} }
function removeSetLangFromHistory() { function removeSetLangFromHistory() {
var urlWithoutSetLang = removeParam( 'setlang' ); if ( 'setlang' in mw.Uri().query ) {
if ( urlWithoutSetLang === mw.Uri().toString() ) { history.replaceState( null, 'no-setlang-url', currentUrlWithoutSetLang() );
return;
} }
history.replaceState( null, 'no-setlang-url', urlWithoutSetLang );
} }
function updateLanguage( langCode ) { function updateLanguage( langCode ) {
@@ -88,7 +85,7 @@
languagecode: langCode, languagecode: langCode,
formatversion: 2 formatversion: 2
} ).done( function () { } ).done( function () {
location.replace( removeParam( 'setlang' ) ); location.replace( currentUrlWithoutSetLang() );
} ).fail( function ( code, result ) { } ).fail( function ( code, result ) {
var apiErrorInfo = mw.msg( 'ext-uls-setlang-unknown-error' ); var apiErrorInfo = mw.msg( 'ext-uls-setlang-unknown-error' );
if ( result.error && result.error.info ) { if ( result.error && result.error.info ) {