2020-02-25 02:10:27 +00:00
|
|
|
var controller = require( 'ext.discussionTools.init' ).controller,
|
2020-06-26 22:24:14 +00:00
|
|
|
modifier = require( 'ext.discussionTools.init' ).modifier,
|
2020-03-08 14:32:38 +00:00
|
|
|
utils = require( 'ext.discussionTools.init' ).utils,
|
2021-03-08 13:23:00 +00:00
|
|
|
logger = require( 'ext.discussionTools.init' ).logger,
|
2021-02-25 22:29:22 +00:00
|
|
|
ModeTabSelectWidget = require( './ModeTabSelectWidget.js' ),
|
|
|
|
ModeTabOptionWidget = require( './ModeTabOptionWidget.js' ),
|
2021-03-08 13:25:59 +00:00
|
|
|
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
|
|
|
|
enable2017Wikitext = featuresEnabled.sourcemodetoolbar;
|
2019-12-10 21:46:22 +00:00
|
|
|
|
2021-01-18 18:18:32 +00:00
|
|
|
require( './AbandonCommentDialog.js' );
|
|
|
|
require( './AbandonTopicDialog.js' );
|
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
/**
|
|
|
|
* DiscussionTools ReplyWidget class
|
|
|
|
*
|
2019-12-10 21:46:22 +00:00
|
|
|
* @class mw.dt.ReplyWidget
|
2019-11-05 14:07:50 +00:00
|
|
|
* @extends OO.ui.Widget
|
|
|
|
* @constructor
|
2020-04-27 15:50:02 +00:00
|
|
|
* @param {CommentController} commentController Comment controller
|
2020-08-19 20:03:41 +00:00
|
|
|
* @param {CommentItem} comment Comment item
|
2021-06-09 23:22:28 +00:00
|
|
|
* @param {CommentDetails} commentDetails
|
2019-11-05 14:07:50 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2019-11-20 20:27:08 +00:00
|
|
|
* @param {Object} [config.input] Configuration options for the comment input widget
|
2019-11-05 14:07:50 +00:00
|
|
|
*/
|
2021-06-09 23:22:28 +00:00
|
|
|
function ReplyWidget( commentController, comment, commentDetails, config ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var widget = this;
|
2019-11-20 20:27:08 +00:00
|
|
|
|
|
|
|
config = config || {};
|
2019-11-19 19:33:14 +00:00
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
// Parent constructor
|
2019-12-10 21:46:22 +00:00
|
|
|
ReplyWidget.super.call( this, config );
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2020-03-19 00:03:09 +00:00
|
|
|
this.pending = false;
|
2020-04-27 15:50:02 +00:00
|
|
|
this.commentController = commentController;
|
2020-08-19 20:03:41 +00:00
|
|
|
this.comment = comment;
|
2021-06-09 23:22:28 +00:00
|
|
|
this.commentDetails = commentDetails;
|
2020-08-29 12:00:51 +00:00
|
|
|
this.isNewTopic = !!comment.isNewTopic;
|
2021-06-09 23:22:28 +00:00
|
|
|
this.pageName = commentDetails.pageName;
|
|
|
|
this.oldId = commentDetails.oldId;
|
2021-04-08 13:46:09 +00:00
|
|
|
var contextNode = utils.closestElement( comment.range.endContainer, [ 'dl', 'ul', 'ol' ] );
|
2019-11-20 20:27:08 +00:00
|
|
|
this.context = contextNode ? contextNode.nodeName.toLowerCase() : 'dl';
|
2020-04-10 12:57:51 +00:00
|
|
|
// TODO: Should storagePrefix include pageName?
|
2020-04-27 15:50:02 +00:00
|
|
|
this.storagePrefix = 'reply/' + comment.id;
|
2020-04-29 16:43:11 +00:00
|
|
|
this.storage = mw.storage.session;
|
2020-05-20 21:48:06 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
this.contentDir = $( '#mw-content-text' ).css( 'direction' );
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var inputConfig = $.extend(
|
2020-07-20 14:13:59 +00:00
|
|
|
{
|
2020-08-29 12:00:51 +00:00
|
|
|
placeholder: this.isNewTopic ?
|
|
|
|
mw.msg( 'discussiontools-replywidget-placeholder-newtopic' ) :
|
|
|
|
mw.msg( 'discussiontools-replywidget-placeholder-reply', comment.author ),
|
2020-07-20 14:13:59 +00:00
|
|
|
authors: comment.getHeading().getAuthorsBelow()
|
|
|
|
},
|
2020-02-25 17:57:23 +00:00
|
|
|
config.input
|
|
|
|
);
|
|
|
|
this.replyBodyWidget = this.createReplyBodyWidget( inputConfig );
|
2020-08-29 12:00:51 +00:00
|
|
|
this.replyButtonLabel = this.isNewTopic ?
|
|
|
|
mw.msg( 'discussiontools-replywidget-newtopic' ) :
|
|
|
|
mw.msg( 'discussiontools-replywidget-reply' );
|
2019-11-05 14:07:50 +00:00
|
|
|
this.replyButton = new OO.ui.ButtonWidget( {
|
|
|
|
flags: [ 'primary', 'progressive' ],
|
2020-08-29 12:00:51 +00:00
|
|
|
label: this.replyButtonLabel,
|
|
|
|
title: this.replyButtonLabel + ' ' +
|
2020-11-22 22:04:25 +00:00
|
|
|
// TODO: Use VE keyboard shortcut generating code
|
|
|
|
( $.client.profile().platform === 'mac' ?
|
|
|
|
'⌘⏎' :
|
|
|
|
mw.msg( 'visualeditor-key-ctrl' ) + '+' + mw.msg( 'visualeditor-key-enter' )
|
|
|
|
)
|
2019-11-05 14:07:50 +00:00
|
|
|
} );
|
|
|
|
this.cancelButton = new OO.ui.ButtonWidget( {
|
|
|
|
flags: [ 'destructive' ],
|
2020-02-03 20:34:13 +00:00
|
|
|
label: mw.msg( 'discussiontools-replywidget-cancel' ),
|
2020-11-22 22:04:25 +00:00
|
|
|
framed: false,
|
|
|
|
title: mw.msg( 'discussiontools-replywidget-cancel' ) + ' ' +
|
|
|
|
// TODO: Use VE keyboard shortcut generating code
|
|
|
|
( $.client.profile().platform === 'mac' ?
|
|
|
|
'⎋' :
|
|
|
|
mw.msg( 'visualeditor-key-escape' )
|
|
|
|
)
|
2019-11-05 14:07:50 +00:00
|
|
|
} );
|
|
|
|
|
2021-02-25 22:29:22 +00:00
|
|
|
this.modeTabSelect = new ModeTabSelectWidget( {
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-modeTabs' ],
|
2020-07-13 14:36:41 +00:00
|
|
|
items: [
|
2021-02-25 22:29:22 +00:00
|
|
|
new ModeTabOptionWidget( {
|
2020-07-13 14:36:41 +00:00
|
|
|
label: mw.msg( 'discussiontools-replywidget-mode-visual' ),
|
|
|
|
data: 'visual'
|
|
|
|
} ),
|
2021-02-25 22:29:22 +00:00
|
|
|
new ModeTabOptionWidget( {
|
2020-07-13 14:36:41 +00:00
|
|
|
label: mw.msg( 'discussiontools-replywidget-mode-source' ),
|
|
|
|
data: 'source'
|
|
|
|
} )
|
|
|
|
],
|
|
|
|
framed: false
|
|
|
|
} );
|
2021-03-17 17:54:55 +00:00
|
|
|
this.modeTabSelect.$element.attr( 'aria-label', mw.msg( 'visualeditor-mweditmode-tooltip' ) );
|
2021-02-25 22:29:22 +00:00
|
|
|
// Make the option for the current mode disabled, to make it un-interactable
|
|
|
|
// (we override the styles to make it look as if it was selected)
|
|
|
|
this.modeTabSelect.findItemFromData( this.getMode() ).setDisabled( true );
|
2020-07-13 14:36:41 +00:00
|
|
|
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$headerWrapper = $( '<div>' ).addClass( 'ext-discussiontools-ui-replyWidget-headerWrapper' );
|
2020-07-31 20:24:51 +00:00
|
|
|
this.$headerWrapper.append(
|
2021-02-28 22:06:12 +00:00
|
|
|
// Visual mode toolbar attached here by CommentTarget#attachToolbar
|
2020-07-31 20:24:51 +00:00
|
|
|
this.modeTabSelect.$element
|
|
|
|
);
|
|
|
|
|
2020-05-20 21:48:06 +00:00
|
|
|
this.$preview = $( '<div>' )
|
2021-03-13 14:39:39 +00:00
|
|
|
.addClass( 'ext-discussiontools-ui-replyWidget-preview' )
|
2020-05-20 21:48:06 +00:00
|
|
|
.attr( 'data-label', mw.msg( 'discussiontools-replywidget-preview' ) )
|
|
|
|
// Set preview direction to content direction
|
|
|
|
.attr( 'dir', this.contentDir );
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$actionsWrapper = $( '<div>' ).addClass( 'ext-discussiontools-ui-replyWidget-actionsWrapper' );
|
|
|
|
this.$actions = $( '<div>' ).addClass( 'ext-discussiontools-ui-replyWidget-actions' ).append(
|
2020-02-03 20:34:13 +00:00
|
|
|
this.cancelButton.$element,
|
|
|
|
this.replyButton.$element
|
|
|
|
);
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
this.editSummaryInput = new OO.ui.TextInputWidget( {
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-editSummary' ]
|
2020-08-25 12:31:54 +00:00
|
|
|
} );
|
|
|
|
mw.widgets.visibleCodePointLimit( this.editSummaryInput, mw.config.get( 'wgCommentCodePointLimit' ) );
|
|
|
|
|
|
|
|
this.editSummaryField = new OO.ui.FieldLayout(
|
|
|
|
this.editSummaryInput,
|
|
|
|
{
|
|
|
|
align: 'top',
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-editSummaryField' ],
|
2020-08-25 12:31:54 +00:00
|
|
|
label: mw.msg( 'discussiontools-replywidget-summary' )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.advancedToggle = new OO.ui.ButtonWidget( {
|
|
|
|
label: mw.msg( 'discussiontools-replywidget-advanced' ),
|
|
|
|
indicator: 'down',
|
|
|
|
framed: false,
|
|
|
|
flags: [ 'progressive' ],
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-advancedToggle' ]
|
2020-08-25 12:31:54 +00:00
|
|
|
} );
|
|
|
|
this.advanced = new OO.ui.MessageWidget( {
|
|
|
|
type: 'message',
|
|
|
|
$content: this.editSummaryField.$element,
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-advanced' ]
|
2020-08-25 12:31:54 +00:00
|
|
|
} ).toggle( false ).setIcon( '' );
|
|
|
|
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$footer = $( '<div>' ).addClass( 'ext-discussiontools-ui-replyWidget-footer' );
|
2020-08-19 20:03:41 +00:00
|
|
|
if ( this.pageName !== mw.config.get( 'wgRelevantPageName' ) ) {
|
2020-03-12 19:58:36 +00:00
|
|
|
this.$footer.append( $( '<p>' ).append(
|
2020-08-19 20:03:41 +00:00
|
|
|
mw.message( 'discussiontools-replywidget-transcluded', this.pageName ).parseDom()
|
2020-03-12 19:58:36 +00:00
|
|
|
) );
|
|
|
|
}
|
2021-09-21 10:15:44 +00:00
|
|
|
var $footerLinks = $( '<ul>' ).addClass( 'ext-discussiontools-ui-replyWidget-footer-links' );
|
|
|
|
if ( !mw.user.isAnon() ) {
|
|
|
|
$footerLinks.append(
|
|
|
|
$( '<li>' ).append(
|
|
|
|
$( '<a>' )
|
|
|
|
.attr( {
|
|
|
|
href: mw.util.getUrl( 'Special:Preferences#mw-prefsection-editing-discussion' ),
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener'
|
|
|
|
} )
|
|
|
|
.text( mw.msg( 'discussiontools-replywidget-preferences' ) )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$footerLinks.append(
|
|
|
|
$( '<li>' ).append(
|
2020-04-07 14:33:32 +00:00
|
|
|
$( '<a>' )
|
|
|
|
.attr( {
|
2021-05-24 13:36:43 +00:00
|
|
|
href: this.isNewTopic ?
|
|
|
|
mw.msg( 'discussiontools-replywidget-feedback-link-newtopic' ) :
|
|
|
|
mw.msg( 'discussiontools-replywidget-feedback-link' ),
|
2020-04-07 14:33:32 +00:00
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener'
|
|
|
|
} )
|
|
|
|
.text( mw.msg( 'discussiontools-replywidget-feedback' ) )
|
|
|
|
)
|
|
|
|
);
|
2021-09-21 10:15:44 +00:00
|
|
|
this.$footer.append(
|
|
|
|
$( '<p>' ).addClass( 'plainlinks' ).append(
|
|
|
|
mw.message( 'discussiontools-replywidget-terms-click', this.replyButtonLabel ).parseDom()
|
|
|
|
),
|
|
|
|
$footerLinks
|
|
|
|
);
|
2020-03-12 19:58:36 +00:00
|
|
|
this.$actionsWrapper.append( this.$footer, this.$actions );
|
2019-11-20 20:27:08 +00:00
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
// Events
|
|
|
|
this.replyButton.connect( this, { click: 'onReplyClick' } );
|
2020-01-13 19:29:07 +00:00
|
|
|
this.cancelButton.connect( this, { click: 'tryTeardown' } );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.$element.on( 'keydown', this.onKeyDown.bind( this, true ) );
|
2019-12-10 15:15:40 +00:00
|
|
|
this.beforeUnloadHandler = this.onBeforeUnload.bind( this );
|
2020-02-26 05:20:56 +00:00
|
|
|
this.unloadHandler = this.onUnload.bind( this );
|
2020-07-13 14:36:41 +00:00
|
|
|
this.modeTabSelect.connect( this, {
|
|
|
|
choose: 'onModeTabSelectChoose'
|
|
|
|
} );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.advancedToggle.connect( this, { click: 'onAdvancedToggleClick' } );
|
|
|
|
this.editSummaryInput.connect( this, { change: 'onEditSummaryChange' } );
|
|
|
|
this.editSummaryInput.$input.on( 'keydown', this.onKeyDown.bind( this, false ) );
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
|
|
|
this.commentController.sectionTitle.$input.on( 'keydown', this.onKeyDown.bind( this, false ) );
|
|
|
|
}
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2019-11-20 20:27:08 +00:00
|
|
|
this.onInputChangeThrottled = OO.ui.throttle( this.onInputChange.bind( this ), 1000 );
|
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
// Initialization
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$element.addClass( 'ext-discussiontools-ui-replyWidget' ).append(
|
2020-07-31 20:24:51 +00:00
|
|
|
this.$headerWrapper,
|
2019-11-05 14:07:50 +00:00
|
|
|
this.replyBodyWidget.$element,
|
2020-02-03 20:34:13 +00:00
|
|
|
this.$preview,
|
2020-08-25 12:31:54 +00:00
|
|
|
this.advancedToggle.$element,
|
|
|
|
this.advanced.$element,
|
2020-02-03 20:34:13 +00:00
|
|
|
this.$actionsWrapper
|
2019-11-05 14:07:50 +00:00
|
|
|
);
|
2020-05-20 21:48:06 +00:00
|
|
|
// Set direction to interface direction
|
|
|
|
this.$element.attr( 'dir', $( document.body ).css( 'direction' ) );
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$element.addClass( 'ext-discussiontools-ui-replyWidget-newTopic' );
|
2020-08-29 12:00:51 +00:00
|
|
|
}
|
2019-11-19 19:33:14 +00:00
|
|
|
|
|
|
|
if ( mw.user.isAnon() ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var returnTo = {
|
2019-11-19 19:33:14 +00:00
|
|
|
returntoquery: encodeURIComponent( window.location.search ),
|
|
|
|
returnto: mw.config.get( 'wgPageName' )
|
|
|
|
};
|
2020-02-03 20:34:13 +00:00
|
|
|
this.anonWarning = new OO.ui.MessageWidget( {
|
2021-06-22 17:01:52 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-anonWarning' ],
|
2020-02-03 20:34:13 +00:00
|
|
|
type: 'warning',
|
|
|
|
label: mw.message( 'discussiontools-replywidget-anon-warning' )
|
|
|
|
.params( [
|
|
|
|
mw.util.getUrl( 'Special:Userlogin', returnTo ),
|
|
|
|
mw.util.getUrl( 'Special:Userlogin/signup', returnTo )
|
|
|
|
] )
|
|
|
|
.parseDom()
|
|
|
|
} );
|
|
|
|
this.anonWarning.$element.append( this.$actions );
|
2021-06-22 17:01:52 +00:00
|
|
|
this.anonWarning.$label.addClass( 'plainlinks' );
|
2020-03-12 19:58:36 +00:00
|
|
|
this.$element.append( this.anonWarning.$element, this.$footer );
|
2020-02-03 20:34:13 +00:00
|
|
|
this.$actionsWrapper.detach();
|
2019-11-19 19:33:14 +00:00
|
|
|
}
|
2019-11-20 20:27:08 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
this.checkboxesPromise = controller.getCheckboxesPromise( this.pageName, this.oldId );
|
2020-03-04 16:19:19 +00:00
|
|
|
this.checkboxesPromise.then( function ( checkboxes ) {
|
2020-08-26 22:46:34 +00:00
|
|
|
function trackCheckbox( n ) {
|
2020-06-04 17:19:46 +00:00
|
|
|
mw.track( 'dt.schemaVisualEditorFeatureUse', {
|
|
|
|
feature: 'dtReply',
|
2020-08-26 22:46:34 +00:00
|
|
|
action: 'checkbox-' + n
|
2020-06-04 17:19:46 +00:00
|
|
|
} );
|
|
|
|
}
|
2020-03-04 16:19:19 +00:00
|
|
|
if ( checkboxes.checkboxFields ) {
|
2021-03-13 14:39:39 +00:00
|
|
|
widget.$checkboxes = $( '<div>' ).addClass( 'ext-discussiontools-ui-replyWidget-checkboxes' );
|
2020-03-04 16:19:19 +00:00
|
|
|
checkboxes.checkboxFields.forEach( function ( field ) {
|
|
|
|
widget.$checkboxes.append( field.$element );
|
|
|
|
} );
|
2021-03-22 16:28:13 +00:00
|
|
|
widget.editSummaryField.$body.append( widget.$checkboxes );
|
2020-06-04 17:19:46 +00:00
|
|
|
|
|
|
|
// bind logging:
|
2021-04-08 13:46:09 +00:00
|
|
|
for ( var name in checkboxes.checkboxesByName ) {
|
2020-06-04 17:19:46 +00:00
|
|
|
checkboxes.checkboxesByName[ name ].$element.off( '.dtReply' ).on( 'click.dtReply', trackCheckbox.bind( this, name ) );
|
|
|
|
}
|
2020-03-04 16:19:19 +00:00
|
|
|
}
|
|
|
|
} );
|
2019-12-10 21:46:22 +00:00
|
|
|
}
|
2019-11-05 14:07:50 +00:00
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
OO.inheritClass( ReplyWidget, OO.ui.Widget );
|
2019-11-05 14:07:50 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
ReplyWidget.prototype.createReplyBodyWidget = null;
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
/**
|
|
|
|
* Focus the widget
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
2019-11-05 14:07:50 +00:00
|
|
|
ReplyWidget.prototype.focus = null;
|
|
|
|
|
|
|
|
ReplyWidget.prototype.getValue = null;
|
|
|
|
|
|
|
|
ReplyWidget.prototype.isEmpty = null;
|
|
|
|
|
2020-04-02 15:43:49 +00:00
|
|
|
ReplyWidget.prototype.getMode = null;
|
2020-03-06 15:18:30 +00:00
|
|
|
|
2020-09-21 19:52:47 +00:00
|
|
|
/**
|
|
|
|
* Restore the widget to its original state
|
|
|
|
*
|
|
|
|
* Clear any widget values, reset UI states, and clear
|
|
|
|
* any auto-save values.
|
|
|
|
*/
|
2019-12-11 04:40:17 +00:00
|
|
|
ReplyWidget.prototype.clear = function () {
|
|
|
|
if ( this.errorMessage ) {
|
|
|
|
this.errorMessage.$element.remove();
|
|
|
|
}
|
2020-08-19 20:03:41 +00:00
|
|
|
this.$preview.empty();
|
2020-08-29 12:00:51 +00:00
|
|
|
this.previewWikitext = null;
|
|
|
|
this.previewTitle = null;
|
2020-09-21 19:52:47 +00:00
|
|
|
this.toggleAdvanced( false );
|
|
|
|
|
2021-08-06 00:31:50 +00:00
|
|
|
this.clearStorage();
|
|
|
|
|
|
|
|
this.emit( 'clear' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any storage that the widget is using
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.clearStorage = function () {
|
2020-08-19 20:03:41 +00:00
|
|
|
this.storage.remove( this.storagePrefix + '/mode' );
|
|
|
|
this.storage.remove( this.storagePrefix + '/saveable' );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.storage.remove( this.storagePrefix + '/summary' );
|
|
|
|
this.storage.remove( this.storagePrefix + '/showAdvanced' );
|
2021-08-06 00:31:50 +00:00
|
|
|
|
|
|
|
this.emit( 'clearStorage' );
|
2019-12-11 04:40:17 +00:00
|
|
|
};
|
|
|
|
|
2019-11-05 14:07:50 +00:00
|
|
|
ReplyWidget.prototype.setPending = function ( pending ) {
|
2020-03-19 00:03:09 +00:00
|
|
|
this.pending = pending;
|
2019-11-05 14:07:50 +00:00
|
|
|
if ( pending ) {
|
|
|
|
this.replyButton.setDisabled( true );
|
|
|
|
this.cancelButton.setDisabled( true );
|
2020-08-21 19:43:05 +00:00
|
|
|
this.replyBodyWidget.setReadOnly( true );
|
|
|
|
this.replyBodyWidget.pushPending();
|
2019-11-05 14:07:50 +00:00
|
|
|
} else {
|
|
|
|
this.replyButton.setDisabled( false );
|
|
|
|
this.cancelButton.setDisabled( false );
|
2020-08-21 19:43:05 +00:00
|
|
|
this.replyBodyWidget.setReadOnly( false );
|
|
|
|
this.replyBodyWidget.popPending();
|
2020-04-29 16:43:11 +00:00
|
|
|
this.updateButtons();
|
2019-11-05 14:07:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-07 19:16:14 +00:00
|
|
|
ReplyWidget.prototype.saveEditMode = function ( mode ) {
|
2020-10-23 11:02:18 +00:00
|
|
|
controller.getApi().saveOption( 'discussiontools-editmode', mode ).then( function () {
|
2020-05-07 19:16:14 +00:00
|
|
|
mw.user.options.set( 'discussiontools-editmode', mode );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-08-25 12:31:54 +00:00
|
|
|
ReplyWidget.prototype.onAdvancedToggleClick = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var showAdvanced = !this.showAdvanced;
|
2020-09-14 18:46:59 +00:00
|
|
|
mw.track( 'dt.schemaVisualEditorFeatureUse', {
|
|
|
|
feature: 'dtReply',
|
2020-10-15 15:04:36 +00:00
|
|
|
action: 'advanced-' + ( showAdvanced ? 'show' : 'hide' )
|
2020-09-14 18:46:59 +00:00
|
|
|
} );
|
2020-10-28 16:57:23 +00:00
|
|
|
controller.getApi().saveOption( 'discussiontools-showadvanced', +showAdvanced ).then( function () {
|
2020-10-15 15:04:36 +00:00
|
|
|
mw.user.options.set( 'discussiontools-showadvanced', +showAdvanced );
|
|
|
|
} );
|
|
|
|
this.toggleAdvanced( showAdvanced );
|
2020-08-25 12:31:54 +00:00
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( showAdvanced ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var summary = this.editSummaryInput.getValue();
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
// If the current summary has not been edited yet, select the text following the autocomment to
|
|
|
|
// make it easier to change. Otherwise, move cursor to end.
|
2021-04-08 13:46:09 +00:00
|
|
|
var selectFromIndex = summary.length;
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var titleText = this.commentController.sectionTitle.getValue();
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( summary === this.commentController.generateSummary( titleText ) ) {
|
|
|
|
selectFromIndex = titleText.length + '/* '.length + ' */ '.length;
|
|
|
|
}
|
2020-08-25 12:31:54 +00:00
|
|
|
} else {
|
2020-08-29 12:00:51 +00:00
|
|
|
// Same as summary.endsWith( defaultReplyTrail )
|
2021-04-08 13:46:09 +00:00
|
|
|
var defaultReplyTrail = '*/ ' + mw.msg( 'discussiontools-defaultsummary-reply' );
|
|
|
|
var endCommentIndex = summary.indexOf( defaultReplyTrail );
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( endCommentIndex + defaultReplyTrail.length === summary.length ) {
|
|
|
|
selectFromIndex = endCommentIndex + 3;
|
|
|
|
}
|
2020-08-25 12:31:54 +00:00
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
this.editSummaryInput.selectRange( selectFromIndex, summary.length );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.editSummaryInput.focus();
|
|
|
|
} else {
|
|
|
|
this.focus();
|
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ReplyWidget.prototype.toggleAdvanced = function ( showAdvanced ) {
|
|
|
|
this.showAdvanced = showAdvanced === undefined ? !this.showAdvanced : showAdvanced;
|
|
|
|
this.advanced.toggle( !!this.showAdvanced );
|
|
|
|
this.advancedToggle.setIndicator( this.showAdvanced ? 'up' : 'down' );
|
|
|
|
|
2020-09-21 19:38:40 +00:00
|
|
|
this.storeEditSummary();
|
2020-08-25 12:31:54 +00:00
|
|
|
this.storage.set( this.storagePrefix + '/showAdvanced', this.showAdvanced ? '1' : '' );
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyWidget.prototype.onEditSummaryChange = function () {
|
2020-09-21 19:38:40 +00:00
|
|
|
this.storeEditSummary();
|
2020-08-25 12:31:54 +00:00
|
|
|
};
|
|
|
|
|
2020-09-21 19:38:40 +00:00
|
|
|
ReplyWidget.prototype.storeEditSummary = function () {
|
|
|
|
this.storage.set( this.storagePrefix + '/summary', this.getEditSummary() );
|
|
|
|
};
|
|
|
|
|
2020-08-25 12:31:54 +00:00
|
|
|
ReplyWidget.prototype.getEditSummary = function () {
|
2020-09-17 19:06:51 +00:00
|
|
|
return this.editSummaryInput.getValue();
|
2020-08-25 12:31:54 +00:00
|
|
|
};
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
ReplyWidget.prototype.onModeTabSelectChoose = function ( option ) {
|
2021-03-24 14:07:47 +00:00
|
|
|
var mode = option.getData(),
|
2020-04-27 16:23:27 +00:00
|
|
|
widget = this;
|
2020-05-07 19:16:14 +00:00
|
|
|
|
2020-05-21 17:26:08 +00:00
|
|
|
if ( mode === this.getMode() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
this.modeTabSelect.setDisabled( true );
|
2021-03-24 14:07:47 +00:00
|
|
|
this.switch( mode ).then(
|
|
|
|
null,
|
|
|
|
function () {
|
2021-03-29 19:19:52 +00:00
|
|
|
// Switch failed, clear the tab selection
|
|
|
|
widget.modeTabSelect.selectItem( null );
|
2021-03-24 14:07:47 +00:00
|
|
|
}
|
|
|
|
).always( function () {
|
|
|
|
widget.modeTabSelect.setDisabled( false );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyWidget.prototype.switch = function ( mode ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var widget = this;
|
2021-03-24 14:07:47 +00:00
|
|
|
|
|
|
|
if ( mode === this.getMode() ) {
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var promise;
|
2021-03-24 14:07:47 +00:00
|
|
|
this.setPending( true );
|
2020-05-07 19:16:14 +00:00
|
|
|
switch ( mode ) {
|
2020-04-27 16:23:27 +00:00
|
|
|
case 'source':
|
|
|
|
promise = this.commentController.switchToWikitext();
|
|
|
|
break;
|
|
|
|
case 'visual':
|
|
|
|
promise = this.commentController.switchToVisual();
|
|
|
|
break;
|
|
|
|
}
|
2020-05-07 19:16:14 +00:00
|
|
|
// TODO: We rely on #setup to call #saveEditMode, so when we have 2017WTE
|
|
|
|
// we will need to save the new preference here as switching will not
|
|
|
|
// reload the editor.
|
2021-03-24 14:07:47 +00:00
|
|
|
return promise.then( function () {
|
2020-10-29 02:59:32 +00:00
|
|
|
// Switch succeeded
|
|
|
|
mw.track( 'dt.schemaVisualEditorFeatureUse', {
|
|
|
|
feature: 'editor-switch',
|
2021-03-08 13:23:00 +00:00
|
|
|
action: (
|
|
|
|
mode === 'visual' ?
|
|
|
|
'visual' :
|
|
|
|
( enable2017Wikitext ? 'source-nwe' : 'source' )
|
|
|
|
) + '-desktop'
|
2020-10-29 02:59:32 +00:00
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
} ).always( function () {
|
|
|
|
widget.setPending( false );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the widget
|
|
|
|
*
|
2020-09-25 17:08:08 +00:00
|
|
|
* @param {Object} [data] Initial data
|
|
|
|
* @param {Mixed} [data.value] Initial value
|
|
|
|
* @param {string} [data.showAdvanced] Whether the "Advanced" menu is initially visible
|
|
|
|
* @param {string} [data.editSummary] Initial edit summary
|
2020-04-27 16:23:27 +00:00
|
|
|
* @chainable
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
2020-09-25 17:08:08 +00:00
|
|
|
ReplyWidget.prototype.setup = function ( data ) {
|
|
|
|
data = data || {};
|
|
|
|
|
2019-12-10 15:15:40 +00:00
|
|
|
this.bindBeforeUnloadHandler();
|
2020-05-07 19:16:14 +00:00
|
|
|
if ( this.modeTabSelect ) {
|
2021-02-25 22:29:22 +00:00
|
|
|
// Make the option for the current mode disabled, to make it un-interactable
|
|
|
|
// (we override the styles to make it look as if it was selected)
|
|
|
|
this.modeTabSelect.findItemFromData( this.getMode() ).setDisabled( true );
|
2020-05-07 19:16:14 +00:00
|
|
|
this.saveEditMode( this.getMode() );
|
|
|
|
}
|
2020-04-29 16:43:11 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var summary = this.storage.get( this.storagePrefix + '/summary' ) || data.editSummary;
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
if ( !summary ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
|
|
|
// Edit summary is filled in when the user inputs the topic title,
|
|
|
|
// in NewTopicController#onSectionTitleChange
|
|
|
|
summary = '';
|
|
|
|
} else {
|
2021-04-08 13:46:09 +00:00
|
|
|
var title = this.comment.getHeading().getLinkableTitle();
|
2020-08-29 12:00:51 +00:00
|
|
|
summary = ( title ? '/* ' + title + ' */ ' : '' ) +
|
|
|
|
mw.msg( 'discussiontools-defaultsummary-reply' );
|
|
|
|
}
|
2020-08-25 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:04:36 +00:00
|
|
|
this.toggleAdvanced(
|
|
|
|
!!this.storage.get( this.storagePrefix + '/showAdvanced' ) ||
|
|
|
|
!!+mw.user.options.get( 'discussiontools-showadvanced' ) ||
|
|
|
|
!!data.showAdvanced
|
|
|
|
);
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
this.editSummaryInput.setValue( summary );
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
|
|
|
this.commentController.sectionTitle.connect( this, { change: 'onInputChangeThrottled' } );
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyWidget.prototype.afterSetup = function () {
|
2020-04-27 16:23:27 +00:00
|
|
|
// Init preview and button state
|
|
|
|
this.onInputChange();
|
2020-04-29 16:43:11 +00:00
|
|
|
// Autosave
|
|
|
|
this.storage.set( this.storagePrefix + '/mode', this.getMode() );
|
2019-12-10 15:15:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
/**
|
|
|
|
* Try to teardown the widget, prompting the user if unsaved changes will be lost.
|
|
|
|
*
|
|
|
|
* @chainable
|
2021-01-25 22:41:35 +00:00
|
|
|
* @return {jQuery.Promise} Resolves if widget was torn down, rejects if it wasn't
|
2020-04-27 16:23:27 +00:00
|
|
|
*/
|
2020-01-13 19:29:07 +00:00
|
|
|
ReplyWidget.prototype.tryTeardown = function () {
|
2019-12-10 15:40:52 +00:00
|
|
|
var promise,
|
|
|
|
widget = this;
|
2020-01-13 19:29:07 +00:00
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( !this.isEmpty() || ( this.isNewTopic && this.commentController.sectionTitle.getValue() ) ) {
|
2021-01-18 18:18:32 +00:00
|
|
|
promise = OO.ui.getWindowManager().openWindow( this.isNewTopic ? 'abandontopic' : 'abandoncomment' )
|
2019-12-10 15:40:52 +00:00
|
|
|
.closed.then( function ( data ) {
|
|
|
|
if ( !( data && data.action === 'discard' ) ) {
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( {
|
|
|
|
action: 'abort',
|
|
|
|
mechanism: 'cancel',
|
|
|
|
type: 'abandon'
|
|
|
|
} );
|
2019-12-10 15:40:52 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
promise = $.Deferred().resolve().promise();
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( {
|
|
|
|
action: 'abort',
|
|
|
|
mechanism: 'cancel',
|
|
|
|
type: 'nochange'
|
|
|
|
} );
|
2019-12-10 15:40:52 +00:00
|
|
|
}
|
2021-01-25 22:41:35 +00:00
|
|
|
promise = promise.then( function () {
|
2020-06-11 16:36:00 +00:00
|
|
|
widget.teardown( true );
|
2019-12-10 15:40:52 +00:00
|
|
|
} );
|
2021-01-25 22:41:35 +00:00
|
|
|
return promise;
|
2019-12-10 15:15:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
/**
|
|
|
|
* Teardown the widget
|
|
|
|
*
|
2020-06-11 16:36:00 +00:00
|
|
|
* @param {boolean} [abandoned] Widget was torn down after a reply was abandoned
|
2020-04-27 16:23:27 +00:00
|
|
|
* @chainable
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
2020-06-11 16:36:00 +00:00
|
|
|
ReplyWidget.prototype.teardown = function ( abandoned ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.isNewTopic ) {
|
|
|
|
this.commentController.sectionTitle.disconnect( this );
|
|
|
|
}
|
2021-02-15 20:59:32 +00:00
|
|
|
// Make sure that the selector is blurred before it gets removed from the document, otherwise
|
|
|
|
// event handlers for arrow keys are not removed, and it keeps trying to switch modes (T274423)
|
|
|
|
this.modeTabSelect.blur();
|
2020-01-13 19:29:07 +00:00
|
|
|
this.unbindBeforeUnloadHandler();
|
|
|
|
this.clear();
|
2020-06-11 16:36:00 +00:00
|
|
|
this.emit( 'teardown', abandoned );
|
2020-04-27 16:23:27 +00:00
|
|
|
return this;
|
2020-01-13 19:29:07 +00:00
|
|
|
};
|
|
|
|
|
2020-08-25 12:31:54 +00:00
|
|
|
ReplyWidget.prototype.onKeyDown = function ( isMultiline, e ) {
|
2019-11-05 14:07:50 +00:00
|
|
|
if ( e.which === OO.ui.Keys.ESCAPE ) {
|
2020-01-13 19:29:07 +00:00
|
|
|
this.tryTeardown();
|
2019-11-05 14:07:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
// VE surfaces already handle CTRL+Enter, but this will catch
|
|
|
|
// the plain surface, and the edit summary input.
|
|
|
|
if ( e.which === OO.ui.Keys.ENTER && ( !isMultiline || e.ctrlKey || e.metaKey ) ) {
|
|
|
|
this.onReplyClick();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-05 14:07:50 +00:00
|
|
|
};
|
|
|
|
|
2019-11-20 20:27:08 +00:00
|
|
|
ReplyWidget.prototype.onInputChange = function () {
|
2020-05-21 21:11:01 +00:00
|
|
|
this.updateButtons();
|
|
|
|
this.storage.set( this.storagePrefix + '/saveable', this.isEmpty() ? '' : '1' );
|
2020-07-02 15:36:57 +00:00
|
|
|
this.preparePreview();
|
2020-05-21 21:11:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the interface with the preview of the given wikitext.
|
|
|
|
*
|
2020-07-02 15:36:57 +00:00
|
|
|
* @param {string} [wikitext] Wikitext to preview, defaults to current value
|
2020-05-21 21:11:01 +00:00
|
|
|
* @return {jQuery.Promise} Promise resolved when we're done
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.preparePreview = function ( wikitext ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var widget = this;
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2020-04-02 15:43:49 +00:00
|
|
|
if ( this.getMode() !== 'source' ) {
|
2020-05-21 21:11:01 +00:00
|
|
|
return $.Deferred().resolve().promise();
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:15:38 +00:00
|
|
|
// For now, indentation is always ':'. If we need context-aware
|
|
|
|
// indentation we would use the following:
|
|
|
|
// indent = {
|
|
|
|
// dl: ':',
|
|
|
|
// ul: '*',
|
|
|
|
// ol: '#'
|
|
|
|
// }[ this.context ];
|
2021-04-08 13:46:09 +00:00
|
|
|
var indent = ':';
|
2020-09-21 20:13:35 +00:00
|
|
|
wikitext = wikitext !== undefined ? wikitext : this.getValue();
|
2021-03-24 16:48:10 +00:00
|
|
|
wikitext = utils.htmlTrim( wikitext );
|
2021-04-08 13:46:09 +00:00
|
|
|
var title = this.isNewTopic && this.commentController.sectionTitle.getValue();
|
2020-07-02 15:36:57 +00:00
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( this.previewWikitext === wikitext && this.previewTitle === title ) {
|
2020-05-21 21:11:01 +00:00
|
|
|
return $.Deferred().resolve().promise();
|
2019-11-05 14:07:50 +00:00
|
|
|
}
|
2020-05-21 21:11:01 +00:00
|
|
|
this.previewWikitext = wikitext;
|
2020-08-29 12:00:51 +00:00
|
|
|
this.previewTitle = title;
|
2019-11-20 20:27:08 +00:00
|
|
|
|
|
|
|
if ( this.previewRequest ) {
|
|
|
|
this.previewRequest.abort();
|
|
|
|
this.previewRequest = null;
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var parsePromise;
|
2021-03-24 16:48:10 +00:00
|
|
|
if ( !wikitext ) {
|
2020-01-02 14:59:50 +00:00
|
|
|
parsePromise = $.Deferred().resolve( null ).promise();
|
2019-11-20 20:27:08 +00:00
|
|
|
} else {
|
2021-02-17 23:42:57 +00:00
|
|
|
wikitext = this.commentController.doIndentReplacements( wikitext, indent );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2020-07-15 21:43:56 +00:00
|
|
|
if ( !modifier.isWikitextSigned( wikitext ) ) {
|
|
|
|
// Add signature.
|
2021-04-08 13:46:09 +00:00
|
|
|
var signature = mw.msg( 'discussiontools-signature-prefix' ) + '~~~~';
|
2020-07-15 21:43:56 +00:00
|
|
|
// Drop opacity of signature in preview to make message body preview clearer.
|
2021-03-06 18:16:21 +00:00
|
|
|
// Extract any leading spaces outside the <span> markup to ensure accurate previews.
|
|
|
|
signature = signature.replace( /^( *)(.+)$/, function ( _, leadingSpaces, sig ) {
|
|
|
|
return leadingSpaces + '<span style="opacity: 0.6;">' + sig + '</span>';
|
|
|
|
} );
|
|
|
|
wikitext += signature;
|
2020-07-15 21:43:56 +00:00
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( title ) {
|
|
|
|
wikitext = '== ' + title + ' ==\n' + wikitext;
|
|
|
|
}
|
2020-10-23 11:02:18 +00:00
|
|
|
this.previewRequest = parsePromise = controller.getApi().post( {
|
2020-01-02 14:59:50 +00:00
|
|
|
action: 'parse',
|
|
|
|
text: wikitext,
|
2019-12-27 18:30:24 +00:00
|
|
|
pst: true,
|
2020-08-29 12:00:51 +00:00
|
|
|
preview: true,
|
|
|
|
disableeditsection: true,
|
2020-03-09 13:04:43 +00:00
|
|
|
prop: [ 'text', 'modules', 'jsconfigvars' ],
|
2020-08-19 20:03:41 +00:00
|
|
|
title: this.pageName
|
2019-12-27 18:30:24 +00:00
|
|
|
} );
|
2019-11-20 20:27:08 +00:00
|
|
|
}
|
|
|
|
// TODO: Add list context
|
|
|
|
|
2020-05-21 21:11:01 +00:00
|
|
|
return parsePromise.then( function ( response ) {
|
2020-01-02 14:59:50 +00:00
|
|
|
widget.$preview.html( response ? response.parse.text : '' );
|
2019-11-20 20:27:08 +00:00
|
|
|
|
2020-01-02 14:59:50 +00:00
|
|
|
if ( response ) {
|
2020-03-09 13:04:43 +00:00
|
|
|
mw.config.set( response.parse.jsconfigvars );
|
2020-01-02 14:59:50 +00:00
|
|
|
mw.loader.load( response.parse.modulestyles );
|
|
|
|
mw.loader.load( response.parse.modules );
|
|
|
|
}
|
2021-04-08 19:38:45 +00:00
|
|
|
|
|
|
|
mw.hook( 'wikipage.content' ).fire( widget.$preview );
|
2019-11-20 20:27:08 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
ReplyWidget.prototype.updateButtons = function () {
|
|
|
|
this.replyButton.setDisabled( this.isEmpty() );
|
|
|
|
};
|
|
|
|
|
2020-02-19 01:25:38 +00:00
|
|
|
ReplyWidget.prototype.onFirstTransaction = function () {
|
|
|
|
logger( { action: 'firstChange' } );
|
|
|
|
};
|
|
|
|
|
2019-12-10 15:15:40 +00:00
|
|
|
/**
|
|
|
|
* Bind the beforeunload handler, if needed and if not already bound.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.bindBeforeUnloadHandler = function () {
|
|
|
|
$( window ).on( 'beforeunload', this.beforeUnloadHandler );
|
2020-02-26 05:20:56 +00:00
|
|
|
$( window ).on( 'unload', this.unloadHandler );
|
2019-12-10 15:15:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbind the beforeunload handler if it is bound.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.unbindBeforeUnloadHandler = function () {
|
|
|
|
$( window ).off( 'beforeunload', this.beforeUnloadHandler );
|
2020-02-26 05:20:56 +00:00
|
|
|
$( window ).off( 'unload', this.unloadHandler );
|
2019-12-10 15:15:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to beforeunload event.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {jQuery.Event} e Event
|
|
|
|
* @return {string|undefined}
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.onBeforeUnload = function ( e ) {
|
|
|
|
if ( !this.isEmpty() ) {
|
|
|
|
e.preventDefault();
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-26 05:20:56 +00:00
|
|
|
/**
|
|
|
|
* Respond to unload event.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {jQuery.Event} e Event
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.onUnload = function () {
|
|
|
|
logger( {
|
|
|
|
action: 'abort',
|
|
|
|
type: this.isEmpty() ? 'nochange' : 'abandon',
|
|
|
|
mechanism: 'navigate'
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
ReplyWidget.prototype.onReplyClick = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var widget = this;
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2020-03-19 00:03:09 +00:00
|
|
|
if ( this.pending || this.isEmpty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-11 04:40:17 +00:00
|
|
|
if ( this.errorMessage ) {
|
|
|
|
this.errorMessage.$element.remove();
|
|
|
|
}
|
|
|
|
|
2021-09-13 23:35:43 +00:00
|
|
|
this.saveInitiated = mw.now();
|
2019-11-05 14:07:50 +00:00
|
|
|
this.setPending( true );
|
2019-12-06 17:53:32 +00:00
|
|
|
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( { action: 'saveIntent' } );
|
|
|
|
|
2020-03-12 19:58:36 +00:00
|
|
|
// TODO: When editing a transcluded page, VE API returning the page HTML is a waste, since we won't use it
|
2021-04-08 13:46:09 +00:00
|
|
|
var pageName = this.pageName,
|
|
|
|
comment = this.comment;
|
2020-08-19 20:03:41 +00:00
|
|
|
logger( { action: 'saveAttempt' } );
|
|
|
|
widget.commentController.save( comment, pageName ).fail( function ( code, data ) {
|
2021-01-15 18:53:51 +00:00
|
|
|
// Compare to ve.init.mw.ArticleTargetEvents.js in VisualEditor.
|
2020-02-19 01:25:38 +00:00
|
|
|
var typeMap = {
|
2021-01-15 18:53:51 +00:00
|
|
|
badtoken: 'userBadToken',
|
|
|
|
assertanonfailed: 'userNewUser',
|
|
|
|
assertuserfailed: 'userNewUser',
|
|
|
|
assertnameduserfailed: 'userNewUser',
|
2020-02-19 01:25:38 +00:00
|
|
|
'abusefilter-disallowed': 'extensionAbuseFilter',
|
2021-01-15 18:53:51 +00:00
|
|
|
'abusefilter-warning': 'extensionAbuseFilter',
|
2020-02-19 01:25:38 +00:00
|
|
|
captcha: 'extensionCaptcha',
|
|
|
|
spamblacklist: 'extensionSpamBlacklist',
|
2021-01-15 18:53:51 +00:00
|
|
|
'titleblacklist-forbidden': 'extensionTitleBlacklist',
|
|
|
|
pagedeleted: 'editPageDeleted',
|
|
|
|
editconflict: 'editConflict'
|
2020-02-19 01:25:38 +00:00
|
|
|
};
|
|
|
|
|
2020-03-23 22:50:03 +00:00
|
|
|
if ( widget.captchaMessage ) {
|
|
|
|
widget.captchaMessage.$element.detach();
|
|
|
|
}
|
|
|
|
widget.captchaInput = undefined;
|
2020-02-19 01:25:38 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
if ( OO.getProp( data, 'discussiontoolsedit', 'edit', 'captcha' ) ) {
|
2020-02-19 01:25:38 +00:00
|
|
|
code = 'captcha';
|
2020-03-23 22:50:03 +00:00
|
|
|
|
|
|
|
widget.captchaInput = new mw.libs.confirmEdit.CaptchaInputWidget(
|
2020-08-19 20:03:41 +00:00
|
|
|
OO.getProp( data, 'discussiontoolsedit', 'edit', 'captcha' )
|
2020-03-23 22:50:03 +00:00
|
|
|
);
|
|
|
|
// Save when pressing 'Enter' in captcha field as it is single line.
|
|
|
|
widget.captchaInput.on( 'enter', function () {
|
|
|
|
widget.onReplyClick();
|
|
|
|
} );
|
|
|
|
|
|
|
|
widget.captchaMessage = new OO.ui.MessageWidget( {
|
|
|
|
type: 'notice',
|
2020-12-11 16:27:07 +00:00
|
|
|
label: widget.captchaInput.$element,
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-captcha' ]
|
2020-03-23 22:50:03 +00:00
|
|
|
} );
|
|
|
|
widget.captchaMessage.$element.insertAfter( widget.$preview );
|
|
|
|
|
|
|
|
widget.captchaInput.focus();
|
|
|
|
widget.captchaInput.scrollElementIntoView();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
widget.errorMessage = new OO.ui.MessageWidget( {
|
|
|
|
type: 'error',
|
2020-12-11 16:27:07 +00:00
|
|
|
label: code instanceof Error ? code.toString() : controller.getApi().getErrorMessage( data ),
|
2021-03-13 14:39:39 +00:00
|
|
|
classes: [ 'ext-discussiontools-ui-replyWidget-error' ]
|
2020-03-23 22:50:03 +00:00
|
|
|
} );
|
|
|
|
widget.errorMessage.$element.insertBefore( widget.replyBodyWidget.$element );
|
2020-02-19 01:25:38 +00:00
|
|
|
}
|
2020-03-23 22:50:03 +00:00
|
|
|
|
2021-09-09 20:57:42 +00:00
|
|
|
// Log more precise error codes, mw.Api just gives us 'http' in all of these cases
|
|
|
|
if ( data.textStatus === 'timeout' || data.textStatus === 'abort' || data.textStatus === 'parsererror' ) {
|
|
|
|
code = data.textStatus;
|
2021-09-28 11:14:29 +00:00
|
|
|
} else if ( data.xhr ) {
|
|
|
|
code = 'http-' + ( data.xhr.status || 0 );
|
2021-09-09 20:57:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( {
|
|
|
|
action: 'saveFailure',
|
2021-09-13 23:35:43 +00:00
|
|
|
timing: mw.now() - widget.saveInitiated,
|
2020-02-19 01:25:38 +00:00
|
|
|
message: code,
|
|
|
|
type: typeMap[ code ] || 'responseUnknown'
|
|
|
|
} );
|
2019-12-06 17:53:32 +00:00
|
|
|
} ).always( function () {
|
2019-11-05 14:07:50 +00:00
|
|
|
widget.setPending( false );
|
2019-11-05 14:07:50 +00:00
|
|
|
} );
|
|
|
|
};
|
2019-12-10 21:46:22 +00:00
|
|
|
|
|
|
|
module.exports = ReplyWidget;
|