From a5ae7cb55649776b2a21e1a54cac2a37a6843738 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Sun, 19 Jan 2020 17:00:40 +0100 Subject: [PATCH] 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 --- resources/js/ext.uls.dialog.js | 15 +++++++++------ resources/js/ext.uls.setlang.js | 25 +++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/resources/js/ext.uls.dialog.js b/resources/js/ext.uls.dialog.js index 8b0a29d6..737f6522 100644 --- a/resources/js/ext.uls.dialog.js +++ b/resources/js/ext.uls.dialog.js @@ -24,8 +24,7 @@ 'use strict'; var ULSDialog = function ( options ) { - var noop = function () { }, - $dialog = options.container, + var $dialog = options.container, hasOverlay = options.hasOverlay, $overlay, // Source: https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L4 @@ -42,8 +41,8 @@ '[contenteditable]', '[tabindex]:not([tabindex^="-"])' ], - afterClose = options.afterClose || noop, - afterOpen = options.afterOpen || noop; + afterOpen = options.afterOpen, + afterClose = options.afterClose; function getFocusableNodes() { return $dialog.find( FOCUSABLE_NODES.join( ', ' ) ); @@ -159,14 +158,18 @@ addEvents(); showOverlay(); focusFirstNodeOrOverlay(); - afterOpen(); + if ( afterOpen ) { + afterOpen(); + } } function close() { $dialog.hide(); removeEvents(); hideOverlay(); - afterClose(); + if ( afterClose ) { + afterClose(); + } } function elem() { diff --git a/resources/js/ext.uls.setlang.js b/resources/js/ext.uls.setlang.js index 3cb729f9..84f0251a 100644 --- a/resources/js/ext.uls.setlang.js +++ b/resources/js/ext.uls.setlang.js @@ -58,27 +58,24 @@ } function toggleLoading( $btnSubmit, isLoading ) { - if ( isLoading ) { - $btnSubmit.text( mw.msg( 'ext-uls-setlang-loading' ) ); - } else { - $btnSubmit.text( mw.msg( 'ext-uls-setlang-accept' ) ); - } - - $btnSubmit.prop( 'disabled', isLoading ); + $btnSubmit + .text( mw.msg( isLoading ? 'ext-uls-setlang-loading' : 'ext-uls-setlang-accept' ) ) + .prop( 'disabled', isLoading ); } - function removeParam( key ) { + /** + * @return {string} + */ + function currentUrlWithoutSetLang() { var uri = new mw.Uri(); - delete uri.query[ key ]; + delete uri.query.setlang; return uri.toString(); } function removeSetLangFromHistory() { - var urlWithoutSetLang = removeParam( 'setlang' ); - if ( urlWithoutSetLang === mw.Uri().toString() ) { - return; + if ( 'setlang' in mw.Uri().query ) { + history.replaceState( null, 'no-setlang-url', currentUrlWithoutSetLang() ); } - history.replaceState( null, 'no-setlang-url', urlWithoutSetLang ); } function updateLanguage( langCode ) { @@ -88,7 +85,7 @@ languagecode: langCode, formatversion: 2 } ).done( function () { - location.replace( removeParam( 'setlang' ) ); + location.replace( currentUrlWithoutSetLang() ); } ).fail( function ( code, result ) { var apiErrorInfo = mw.msg( 'ext-uls-setlang-unknown-error' ); if ( result.error && result.error.info ) {