From 459d55c5d38421de70ad643be3397924f041a689 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Tue, 14 Jan 2020 14:53:20 +0530 Subject: [PATCH] Remove setlang URL parameter on dialog close Add two new options for ext.uls.dialog - afterOpen and afterClose. These are callback function which will be triggered after the dialog opens or closes. Using the afterClose callback in ext.uls.setlang to remove the setlang parameter from the URL if the dialog is closed without pressing any button. This might happen if the user closes the dialog by pressing Esc, or by clicking on the overlay. Also see: Ie3215d12d9c77f15597495e21610707b272eeee9 In addition, renamed all occurrence of setlang to setLang. Bug: T63115 Change-Id: Icaf086f947b1d91bf7ad5b36f126da0be1fc7747 --- resources/js/ext.uls.dialog.js | 9 +++++++-- resources/js/ext.uls.setlang.js | 15 +++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/resources/js/ext.uls.dialog.js b/resources/js/ext.uls.dialog.js index 013a99c2..8b0a29d6 100644 --- a/resources/js/ext.uls.dialog.js +++ b/resources/js/ext.uls.dialog.js @@ -24,7 +24,8 @@ 'use strict'; var ULSDialog = function ( options ) { - var $dialog = options.container, + var noop = function () { }, + $dialog = options.container, hasOverlay = options.hasOverlay, $overlay, // Source: https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L4 @@ -40,7 +41,9 @@ 'embed', '[contenteditable]', '[tabindex]:not([tabindex^="-"])' - ]; + ], + afterClose = options.afterClose || noop, + afterOpen = options.afterOpen || noop; function getFocusableNodes() { return $dialog.find( FOCUSABLE_NODES.join( ', ' ) ); @@ -156,12 +159,14 @@ addEvents(); showOverlay(); focusFirstNodeOrOverlay(); + afterOpen(); } function close() { $dialog.hide(); removeEvents(); hideOverlay(); + afterClose(); } function elem() { diff --git a/resources/js/ext.uls.setlang.js b/resources/js/ext.uls.setlang.js index 82f54873..3cb729f9 100644 --- a/resources/js/ext.uls.setlang.js +++ b/resources/js/ext.uls.setlang.js @@ -73,9 +73,12 @@ return uri.toString(); } - function removeSetlangFromHistory() { - var urlWithoutSetlang = removeParam( 'setlang' ); - history.replaceState( null, 'no-setlang-url', urlWithoutSetlang ); + function removeSetLangFromHistory() { + var urlWithoutSetLang = removeParam( 'setlang' ); + if ( urlWithoutSetLang === mw.Uri().toString() ) { + return; + } + history.replaceState( null, 'no-setlang-url', urlWithoutSetLang ); } function updateLanguage( langCode ) { @@ -120,7 +123,6 @@ } ); $cancelBtn.on( 'click', function () { - removeSetlangFromHistory(); ulsDialog.close(); } ); } @@ -132,7 +134,7 @@ $ulsDialog, ulsSetLangDialog; if ( currentLangCode === setLangCode ) { - removeSetlangFromHistory(); + removeSetLangFromHistory(); return; } @@ -140,7 +142,8 @@ $ulsDialog = createSetLangDialog( setLangName, setLangCode ); ulsSetLangDialog = new mw.uls.Dialog( { container: $ulsDialog, - hasOverlay: true + hasOverlay: true, + afterClose: removeSetLangFromHistory } ); addSetLangDialogEvents( ulsSetLangDialog );