Krinkle has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/352538 )
Change subject: windows: Update OO.ui.alert/confirm/prompt to use Window events
......................................................................
windows: Update OO.ui.alert/confirm/prompt to use Window events
The WindowManager#openWindow() promise is unreliable in jQuery 3
due to the way it resolves a promise with another promise without
meaning for one process to wait for the other. (See T163510.)
Change I9dbc9852434caba15 introduced new events that can be used
instead. This commit fixes them methods to use these events.
Ideally, the tests would've been introduced separately to verify
that pass the same way before and after. However, because the
jQuery 3 upgrade already landed in master, this is not possible
because in the current demos, these methods are already broken.
Bug: T163510
Change-Id: I6605ca52b8207c7707a790b5a5bf7a383387df4c
---
M package.json
M src/windows.js
M tests/index.php
A tests/windows.test.js
4 files changed, 102 insertions(+), 25 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/38/352538/1
diff --git a/package.json b/package.json
index fa09864..49cfb59 100644
--- a/package.json
+++ b/package.json
@@ -53,7 +53,7 @@
"karma-coverage": "1.1.0",
"karma-firefox-launcher": "1.0.0",
"karma-qunit": "1.1.0",
- "karma-remap-istanbul": "0.6.0",
+ "karma-remap-istanbul": "0.6.0",
"q": "1.4.1",
"qunitjs": "1.22.0",
"stylelint": "7.8.0",
diff --git a/src/windows.js b/src/windows.js
index cdc60e6..bd766d7 100644
--- a/src/windows.js
+++ b/src/windows.js
@@ -37,15 +37,19 @@
* @return {jQuery.Promise} Promise resolved when the user closes the dialog
*/
OO.ui.alert = function ( text, options ) {
- return OO.ui.getWindowManager().openWindow( 'message', $.extend( {
- message: text,
- actions: [ OO.ui.MessageDialog.static.actions[ 0 ] ]
- }, options ) ).then( function ( opened ) {
- return opened.then( function ( closing ) {
- return closing.then( function () {
- return $.Deferred().resolve();
+ return OO.ui.getWindowManager().getWindow( 'message' ).then( function (
win ) {
+ var closed = $.Deferred( function ( deferred ) {
+ win.once( 'closed', function () {
+ deferred.resolve();
} );
} );
+
+ win.open( $.extend( {
+ message: text,
+ actions: [ OO.ui.MessageDialog.static.actions[ 0 ] ]
+ }, options ) );
+
+ return closed;
} );
};
@@ -73,14 +77,18 @@
* `false`.
*/
OO.ui.confirm = function ( text, options ) {
- return OO.ui.getWindowManager().openWindow( 'message', $.extend( {
- message: text
- }, options ) ).then( function ( opened ) {
- return opened.then( function ( closing ) {
- return closing.then( function ( data ) {
- return $.Deferred().resolve( !!( data &&
data.action === 'accept' ) );
+ return OO.ui.getWindowManager().getWindow( 'message' ).then( function (
win ) {
+ var closed = $.Deferred( function ( deferred ) {
+ win.once( 'closed', function ( data ) {
+ deferred.resolve( !!( data && data.action ===
'accept' ) );
} );
} );
+
+ win.open( $.extend( {
+ message: text
+ }, options ) );
+
+ return closed;
} );
};
@@ -116,20 +124,30 @@
label: text
} );
- // TODO: This is a little hacky, and could be done by extending
MessageDialog instead.
+ return manager.getWindow( 'message' ).then( function ( win ) {
+ var closed;
- return manager.openWindow( 'message', $.extend( {
- message: textField.$element
- }, options ) ).then( function ( opened ) {
- // After ready
- textInput.on( 'enter', function () {
- manager.getCurrentWindow().close( { action: 'accept' }
);
+ win.once( 'opened', function () {
+ // After ready
+ // TODO: This is a little hacky, and should be done by
extending MessageDialog instead.
+ textInput.on( 'enter', function () {
+ manager.getCurrentWindow().close( { action:
'accept' } );
+ } );
+ textInput.focus();
} );
- textInput.focus();
- return opened.then( function ( closing ) {
- return closing.then( function ( data ) {
- return $.Deferred().resolve( data &&
data.action === 'accept' ? textInput.getValue() : null );
+
+ closed = $.Deferred( function ( deferred ) {
+ win.once( 'closed', function ( data ) {
+ deferred.resolve( data && data.action ===
'accept' ?
+ textInput.getValue() : null
+ );
} );
} );
+
+ win.open( $.extend( {
+ message: textField.$element
+ }, options ) );
+
+ return closed;
} );
};
diff --git a/tests/index.php b/tests/index.php
index 6b4417d..2150526 100644
--- a/tests/index.php
+++ b/tests/index.php
@@ -37,6 +37,7 @@
<script src="./core.test.js"></script>
<script src="./Element.test.js"></script>
<script src="./Process.test.js"></script>
+ <script src="./windows.test.js"></script>
<script src="./mixins/FlaggedElement.test.js"></script>
<script src="./widgets/TagMultiselectWidget.test.js"></script>
<script src="./widgets/MenuTagMultiselectWidget.test.js"></script>
diff --git a/tests/windows.test.js b/tests/windows.test.js
new file mode 100644
index 0000000..871fa6a
--- /dev/null
+++ b/tests/windows.test.js
@@ -0,0 +1,58 @@
+QUnit.module( 'windows' );
+
+QUnit.test( 'alert()', function ( assert ) {
+ OO.ui.getWindowManager().once( 'opening', function ( win ) {
+ win.once( 'opened', function () {
+ win.executeAction( 'accept' );
+ } );
+ } );
+ return OO.ui.alert( 'Text' ).then( function ( data ) {
+ assert.strictEqual( data, undefined );
+ } );
+} );
+
+QUnit.test( 'confirm() - accept', function ( assert ) {
+ OO.ui.getWindowManager().once( 'opening', function ( win ) {
+ win.once( 'opened', function () {
+ win.executeAction( 'accept' );
+ } );
+ } );
+ return OO.ui.confirm( 'Text' ).then( function ( result ) {
+ assert.strictEqual( result, true, 'Accepted' );
+ } );
+} );
+
+QUnit.test( 'confirm() - reject', function ( assert ) {
+ OO.ui.getWindowManager().once( 'opening', function ( win ) {
+ win.once( 'opened', function () {
+ win.executeAction( 'reject' );
+ } );
+ } );
+ return OO.ui.confirm( 'Text' ).then( function ( result ) {
+ assert.strictEqual( result, false, 'Rejected' );
+ } );
+} );
+
+QUnit.test( 'prompt() - accept', function ( assert ) {
+ OO.ui.getWindowManager().once( 'opening', function ( win ) {
+ win.once( 'opened', function () {
+ win.$body.find( 'input[type="text"]' ).val( 'Hello' );
+ win.executeAction( 'accept' );
+ } );
+ } );
+ return OO.ui.prompt( 'Text' ).then( function ( result ) {
+ assert.strictEqual( result, 'Hello', 'Accepted - text value' );
+ } );
+} );
+
+QUnit.test( 'prompt() - reject', function ( assert ) {
+ OO.ui.getWindowManager().once( 'opening', function ( win ) {
+ win.once( 'opened', function () {
+ win.$body.find( 'input[type="text"]' ).val( 'Hello' );
+ win.executeAction( 'reject' );
+ } );
+ } );
+ return OO.ui.prompt( 'Text' ).then( function ( result ) {
+ assert.strictEqual( result, null, 'Rejected' );
+ } );
+} );
--
To view, visit https://gerrit.wikimedia.org/r/352538
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6605ca52b8207c7707a790b5a5bf7a383387df4c
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits