Prompt users when pressing 'cancel' with content in the widget

And actually discard the contents when they confirm.

For now this uses the generic editor message, but that can
be tweaked later.

Bug: T240271
Change-Id: I2dfa19b2cc7ac49d7efea37ac8c9429c75934a91
This commit is contained in:
Ed Sanders 2019-12-10 15:40:52 +00:00
parent 179b29a598
commit 49edbb82ab
2 changed files with 26 additions and 6 deletions

View file

@ -95,7 +95,8 @@
"oojs-ui-core",
"mediawiki.editfont.styles",
"mediawiki.user",
"mediawiki.jqueryMsg"
"mediawiki.jqueryMsg",
"mediawiki.widgets.AbandonEditDialog"
]
}
},

View file

@ -45,7 +45,7 @@ function ReplyWidget( comment, config ) {
// Events
this.replyButton.connect( this, { click: 'onReplyClick' } );
this.cancelButton.connect( this, { click: 'teardown' } );
this.cancelButton.connect( this, { click: [ 'teardown', true ] } );
this.$element.on( 'keydown', this.onKeyDown.bind( this ) );
this.beforeUnloadHandler = this.onBeforeUnload.bind( this );
@ -101,10 +101,25 @@ ReplyWidget.prototype.setup = function () {
this.bindBeforeUnloadHandler();
};
ReplyWidget.prototype.teardown = function () {
// TODO: OOUI prompt if !empty
this.unbindBeforeUnloadHandler();
this.emit( 'teardown' );
ReplyWidget.prototype.teardown = function ( confirm ) {
var promise,
widget = this;
if ( confirm && !this.isEmpty() ) {
// TODO: Override messages in dialog to be more ReplyWidget specific
promise = OO.ui.getWindowManager().openWindow( 'abandonedit' )
.closed.then( function ( data ) {
if ( !( data && data.action === 'discard' ) ) {
return $.Deferred().reject().promise();
}
} );
} else {
promise = $.Deferred().resolve().promise();
}
promise.then( function () {
widget.unbindBeforeUnloadHandler();
widget.clear();
widget.emit( 'teardown' );
} );
};
ReplyWidget.prototype.focus = function () {
@ -239,4 +254,8 @@ ReplyWidget.prototype.onReplyClick = function () {
} );
};
/* Window registration */
OO.ui.getWindowManager().addWindows( [ new mw.widgets.AbandonEditDialog() ] );
module.exports = ReplyWidget;