From 49edbb82ab8b1a9358f6835d755bdccf5be0b398 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Tue, 10 Dec 2019 15:40:52 +0000 Subject: [PATCH] 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 --- extension.json | 3 ++- modules/dt.ui.ReplyWidget.js | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/extension.json b/extension.json index 735b3112d..d28492158 100644 --- a/extension.json +++ b/extension.json @@ -95,7 +95,8 @@ "oojs-ui-core", "mediawiki.editfont.styles", "mediawiki.user", - "mediawiki.jqueryMsg" + "mediawiki.jqueryMsg", + "mediawiki.widgets.AbandonEditDialog" ] } }, diff --git a/modules/dt.ui.ReplyWidget.js b/modules/dt.ui.ReplyWidget.js index 57c6f74a5..10d539658 100644 --- a/modules/dt.ui.ReplyWidget.js +++ b/modules/dt.ui.ReplyWidget.js @@ -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;