From 7290ed91f98732a50efdbdecffef030dbebcd0a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Sun, 22 Apr 2018 15:32:46 +0200 Subject: [PATCH] mw.special.contributions: Do not infuse the date input while it has user focus This is especially important on Firefox, where hiding the native date input while the native date picker is open will cause the date picker to remain visible (but non-functional), but not replacing the interface while the user is working with it is probably a good idea anyway. Bug: T183624 Change-Id: I4d5e0f6b98c7c1ecdff552ddf0950db1ec3ae023 --- .../src/mediawiki.special.contributions.js | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/resources/src/mediawiki.special.contributions.js b/resources/src/mediawiki.special.contributions.js index f65a2579b8..6558bda513 100644 --- a/resources/src/mediawiki.special.contributions.js +++ b/resources/src/mediawiki.special.contributions.js @@ -1,12 +1,38 @@ ( function ( mw, $ ) { + + // Return a promise that is resolved when the element is blurred (loses focus). + // If it already is blurred, resolved immediately. + function whenBlurred( $elem ) { + var deferred = $.Deferred(); + if ( $elem.is( ':focus' ) ) { + $elem.one( 'blur', deferred.resolve ); + } else { + deferred.resolve(); + } + return deferred.promise(); + } + $( function () { - var startInput = mw.widgets.DateInputWidget.static.infuse( 'mw-date-start' ), - endInput = mw.widgets.DateInputWidget.static.infuse( 'mw-date-end' ); + var startReady, endReady; + + // Do not infuse the date input while it has user focus. + // This is especially important on Firefox, where hiding the native date input while the native + // date picker is open will cause the date picker to remain visible (but non-functional), but + // not replacing the interface while the user is working with it is probably a good idea anyway. + startReady = whenBlurred( $( '#mw-date-start .oo-ui-inputWidget-input' ) ).then( function () { + return mw.widgets.DateInputWidget.static.infuse( 'mw-date-start' ); + } ); + endReady = whenBlurred( $( '#mw-date-end .oo-ui-inputWidget-input' ) ).then( function () { + return mw.widgets.DateInputWidget.static.infuse( 'mw-date-end' ); + } ); - startInput.on( 'deactivate', function ( userSelected ) { - if ( userSelected ) { - endInput.focus(); - } + $.when( startReady, endReady ).then( function ( startInput, endInput ) { + startInput.on( 'deactivate', function ( userSelected ) { + if ( userSelected ) { + endInput.focus(); + } + } ); } ); } ); + }( mediaWiki, jQuery ) ); -- 2.20.1