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,
|
2020-10-27 12:18:50 +00:00
|
|
|
logger = require( 'ext.discussionTools.init' ).logger;
|
2019-12-10 21:46:22 +00:00
|
|
|
|
2020-06-09 18:42:05 +00:00
|
|
|
/**
|
|
|
|
* @external CommentController
|
2020-08-19 20:03:41 +00:00
|
|
|
* @external CommentItem
|
2020-06-09 18:42:05 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
* @param {string} pageName Page name the reply is being saved to
|
|
|
|
* @param {number} oldId Revision ID of page at time of editing
|
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
|
|
|
*/
|
2020-08-19 20:03:41 +00:00
|
|
|
function ReplyWidget( commentController, comment, pageName, oldId, config ) {
|
2020-03-04 16:19:19 +00:00
|
|
|
var returnTo, contextNode, inputConfig,
|
2020-08-19 20:03:41 +00:00
|
|
|
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;
|
|
|
|
this.pageName = pageName;
|
|
|
|
this.oldId = oldId;
|
2020-04-27 15:50:02 +00:00
|
|
|
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
|
|
|
|
2020-02-25 17:57:23 +00:00
|
|
|
inputConfig = $.extend(
|
2020-07-20 14:13:59 +00:00
|
|
|
{
|
|
|
|
placeholder: mw.msg( 'discussiontools-replywidget-placeholder-reply', comment.author ),
|
|
|
|
authors: comment.getHeading().getAuthorsBelow()
|
|
|
|
},
|
2020-02-25 17:57:23 +00:00
|
|
|
config.input
|
|
|
|
);
|
|
|
|
this.replyBodyWidget = this.createReplyBodyWidget( inputConfig );
|
2019-11-05 14:07:50 +00:00
|
|
|
this.replyButton = new OO.ui.ButtonWidget( {
|
|
|
|
flags: [ 'primary', 'progressive' ],
|
2019-12-06 18:43:46 +00:00
|
|
|
label: mw.msg( 'discussiontools-replywidget-reply' )
|
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' ),
|
|
|
|
framed: false
|
2019-11-05 14:07:50 +00:00
|
|
|
} );
|
|
|
|
|
2020-07-13 14:36:41 +00:00
|
|
|
this.modeTabSelect = new OO.ui.TabSelectWidget( {
|
|
|
|
classes: [ 'dt-ui-replyWidget-modeTabs' ],
|
|
|
|
items: [
|
|
|
|
new OO.ui.TabOptionWidget( {
|
|
|
|
label: mw.msg( 'discussiontools-replywidget-mode-visual' ),
|
|
|
|
data: 'visual'
|
|
|
|
} ),
|
|
|
|
new OO.ui.TabOptionWidget( {
|
|
|
|
label: mw.msg( 'discussiontools-replywidget-mode-source' ),
|
|
|
|
data: 'source'
|
|
|
|
} )
|
|
|
|
],
|
|
|
|
framed: false
|
|
|
|
} );
|
|
|
|
// Initialize to avoid flicker when switching mode
|
|
|
|
this.modeTabSelect.selectItemByData( this.getMode() );
|
|
|
|
|
2020-07-31 20:24:51 +00:00
|
|
|
this.$headerWrapper = $( '<div>' ).addClass( 'dt-ui-replyWidget-headerWrapper' );
|
|
|
|
this.$headerWrapper.append(
|
|
|
|
// (visual mode toolbar magically appears here)
|
|
|
|
this.modeTabSelect.$element
|
|
|
|
);
|
|
|
|
|
2020-05-20 21:48:06 +00:00
|
|
|
this.$preview = $( '<div>' )
|
|
|
|
.addClass( 'dt-ui-replyWidget-preview' )
|
|
|
|
.attr( 'data-label', mw.msg( 'discussiontools-replywidget-preview' ) )
|
|
|
|
// Set preview direction to content direction
|
|
|
|
.attr( 'dir', this.contentDir );
|
2020-02-03 20:34:13 +00:00
|
|
|
this.$actionsWrapper = $( '<div>' ).addClass( 'dt-ui-replyWidget-actionsWrapper' );
|
|
|
|
this.$actions = $( '<div>' ).addClass( 'dt-ui-replyWidget-actions' ).append(
|
|
|
|
this.cancelButton.$element,
|
|
|
|
this.replyButton.$element
|
|
|
|
);
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
this.editSummaryInput = new OO.ui.TextInputWidget( {
|
|
|
|
classes: [ 'dt-ui-replyWidget-editSummary' ]
|
|
|
|
} );
|
|
|
|
mw.widgets.visibleCodePointLimit( this.editSummaryInput, mw.config.get( 'wgCommentCodePointLimit' ) );
|
|
|
|
|
|
|
|
this.editSummaryField = new OO.ui.FieldLayout(
|
|
|
|
this.editSummaryInput,
|
|
|
|
{
|
|
|
|
align: 'top',
|
|
|
|
classes: [ 'dt-ui-replyWidget-editSummaryField' ],
|
|
|
|
label: mw.msg( 'discussiontools-replywidget-summary' )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.advancedToggle = new OO.ui.ButtonWidget( {
|
|
|
|
label: mw.msg( 'discussiontools-replywidget-advanced' ),
|
|
|
|
indicator: 'down',
|
|
|
|
framed: false,
|
|
|
|
flags: [ 'progressive' ],
|
|
|
|
classes: [ 'dt-ui-replyWidget-advancedToggle' ]
|
|
|
|
} );
|
|
|
|
this.advanced = new OO.ui.MessageWidget( {
|
|
|
|
type: 'message',
|
|
|
|
$content: this.editSummaryField.$element,
|
|
|
|
classes: [ 'dt-ui-replyWidget-advanced' ]
|
|
|
|
} ).toggle( false ).setIcon( '' );
|
|
|
|
|
2020-03-12 19:58:36 +00:00
|
|
|
this.$footer = $( '<div>' ).addClass( 'dt-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
|
|
|
) );
|
|
|
|
}
|
2020-04-07 14:33:32 +00:00
|
|
|
this.$footer.append(
|
2020-10-28 17:06:11 +00:00
|
|
|
$( '<p>' ).addClass( 'plainlinks' ).append(
|
2020-04-07 14:33:32 +00:00
|
|
|
mw.message( 'discussiontools-replywidget-terms-click', mw.msg( 'discussiontools-replywidget-reply' ) ).parseDom()
|
|
|
|
),
|
|
|
|
$( '<p>' ).append(
|
|
|
|
$( '<a>' )
|
|
|
|
.attr( {
|
|
|
|
href: mw.msg( 'discussiontools-replywidget-feedback-link' ),
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener'
|
|
|
|
} )
|
|
|
|
.text( mw.msg( 'discussiontools-replywidget-feedback' ) )
|
|
|
|
)
|
|
|
|
);
|
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 ) );
|
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
|
|
|
|
this.$element.addClass( 'dt-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' ) );
|
2019-11-19 19:33:14 +00:00
|
|
|
|
|
|
|
if ( mw.user.isAnon() ) {
|
|
|
|
returnTo = {
|
|
|
|
returntoquery: encodeURIComponent( window.location.search ),
|
|
|
|
returnto: mw.config.get( 'wgPageName' )
|
|
|
|
};
|
2020-02-03 20:34:13 +00:00
|
|
|
this.anonWarning = new OO.ui.MessageWidget( {
|
|
|
|
classes: [ 'dt-ui-replyWidget-anonWarning' ],
|
|
|
|
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 );
|
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-06-04 17:19:46 +00:00
|
|
|
var name;
|
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 ) {
|
|
|
|
widget.$checkboxes = $( '<div>' ).addClass( 'dt-ui-replyWidget-checkboxes' );
|
|
|
|
checkboxes.checkboxFields.forEach( function ( field ) {
|
|
|
|
widget.$checkboxes.append( field.$element );
|
|
|
|
} );
|
2020-08-25 12:31:54 +00:00
|
|
|
widget.advanced.$element.prepend( widget.$checkboxes );
|
2020-06-04 17:19:46 +00:00
|
|
|
|
|
|
|
// bind logging:
|
|
|
|
for ( name in checkboxes.checkboxesByName ) {
|
|
|
|
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-09-21 19:52:47 +00:00
|
|
|
this.toggleAdvanced( false );
|
|
|
|
|
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' );
|
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 () {
|
2020-10-15 15:04:36 +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
|
|
|
};
|
|
|
|
|
|
|
|
ReplyWidget.prototype.toggleAdvanced = function ( showAdvanced ) {
|
2020-10-07 17:39:43 +00:00
|
|
|
var summary, defaultReplyTrail, endCommentIndex;
|
2020-08-25 12:31:54 +00:00
|
|
|
this.showAdvanced = showAdvanced === undefined ? !this.showAdvanced : showAdvanced;
|
|
|
|
this.advanced.toggle( !!this.showAdvanced );
|
|
|
|
this.advancedToggle.setIndicator( this.showAdvanced ? 'up' : 'down' );
|
|
|
|
if ( this.showAdvanced ) {
|
2020-10-07 17:39:43 +00:00
|
|
|
summary = this.editSummaryInput.getValue();
|
|
|
|
// Same as summary.endsWith( defaultReplyTrail )
|
|
|
|
defaultReplyTrail = '*/ ' + mw.msg( 'discussiontools-defaultsummary-reply' );
|
|
|
|
endCommentIndex = summary.indexOf( defaultReplyTrail );
|
|
|
|
if ( endCommentIndex + defaultReplyTrail.length === summary.length ) {
|
|
|
|
// Select the default 'Reply' summary if still present
|
|
|
|
this.editSummaryInput.selectRange( endCommentIndex + 3, summary.length );
|
2020-08-25 12:31:54 +00:00
|
|
|
} else {
|
|
|
|
this.editSummaryInput.moveCursorToEnd();
|
|
|
|
}
|
|
|
|
this.editSummaryInput.focus();
|
|
|
|
} else {
|
|
|
|
this.focus();
|
|
|
|
}
|
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 ) {
|
|
|
|
var promise,
|
2020-05-07 19:16:14 +00:00
|
|
|
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.setPending( true );
|
|
|
|
this.modeTabSelect.setDisabled( 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.
|
2020-10-29 02:59:32 +00:00
|
|
|
promise.then( function () {
|
|
|
|
// Switch succeeded
|
|
|
|
mw.track( 'dt.schemaVisualEditorFeatureUse', {
|
|
|
|
feature: 'editor-switch',
|
|
|
|
// TODO: Log as `source-nwe-desktop` when enable2017Wikitext is set
|
|
|
|
action: ( mode === 'visual' ? 'visual' : 'source' ) + '-desktop'
|
|
|
|
} );
|
|
|
|
}, function () {
|
2020-04-27 16:23:27 +00:00
|
|
|
// Switch failed, restore previous tab selection
|
2020-05-07 19:16:14 +00:00
|
|
|
widget.modeTabSelect.selectItemByData( mode === 'source' ? 'visual' : 'source' );
|
2020-04-27 16:23:27 +00:00
|
|
|
} ).always( function () {
|
|
|
|
widget.setPending( false );
|
|
|
|
widget.modeTabSelect.setDisabled( 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 ) {
|
2020-11-05 16:07:56 +00:00
|
|
|
var title, summary;
|
2020-08-25 12:31:54 +00:00
|
|
|
|
2020-09-25 17:08:08 +00:00
|
|
|
data = data || {};
|
|
|
|
|
2019-12-10 15:15:40 +00:00
|
|
|
this.bindBeforeUnloadHandler();
|
2020-05-07 19:16:14 +00:00
|
|
|
if ( this.modeTabSelect ) {
|
|
|
|
this.modeTabSelect.selectItemByData( this.getMode() );
|
|
|
|
this.saveEditMode( this.getMode() );
|
|
|
|
}
|
2020-04-29 16:43:11 +00:00
|
|
|
|
2020-09-25 17:08:08 +00:00
|
|
|
summary = this.storage.get( this.storagePrefix + '/summary' ) || data.editSummary;
|
2020-08-25 12:31:54 +00:00
|
|
|
|
|
|
|
if ( !summary ) {
|
2020-11-05 16:07:56 +00:00
|
|
|
title = this.comment.getHeading().getLinkableTitle();
|
|
|
|
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-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
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
if ( !this.isEmpty() ) {
|
2020-07-09 14:07:06 +00:00
|
|
|
promise = OO.ui.getWindowManager().openWindow( '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
|
|
|
}
|
|
|
|
promise.then( function () {
|
2020-06-11 16:36:00 +00:00
|
|
|
widget.teardown( true );
|
2019-12-10 15:40:52 +00:00
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
return this;
|
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-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 ) {
|
2020-07-02 15:36:57 +00:00
|
|
|
var parsePromise, widget, indent;
|
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-07-02 15:36:57 +00:00
|
|
|
widget = this;
|
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 ];
|
|
|
|
indent = ':';
|
2020-07-02 15:36:57 +00:00
|
|
|
wikitext = wikitext || this.getValue();
|
|
|
|
|
2020-05-21 21:11:01 +00:00
|
|
|
if ( this.previewWikitext === wikitext ) {
|
|
|
|
return $.Deferred().resolve().promise();
|
2019-11-05 14:07:50 +00:00
|
|
|
}
|
2020-05-21 21:11:01 +00:00
|
|
|
this.previewWikitext = wikitext;
|
2019-11-20 20:27:08 +00:00
|
|
|
|
|
|
|
if ( this.previewRequest ) {
|
|
|
|
this.previewRequest.abort();
|
|
|
|
this.previewRequest = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !wikitext.trim() ) {
|
2020-01-02 14:59:50 +00:00
|
|
|
parsePromise = $.Deferred().resolve( null ).promise();
|
2019-11-20 20:27:08 +00:00
|
|
|
} else {
|
2020-07-15 21:43:56 +00:00
|
|
|
wikitext = modifier.sanitizeWikitextLinebreaks( wikitext );
|
|
|
|
if ( !modifier.isWikitextSigned( wikitext ) ) {
|
|
|
|
// Add signature.
|
|
|
|
// Drop opacity of signature in preview to make message body preview clearer.
|
|
|
|
wikitext = wikitext + '<span style="opacity: 0.6;">' + mw.msg( 'discussiontools-signature-prefix' ) + '~~~~</span>';
|
|
|
|
}
|
2019-11-20 20:27:08 +00:00
|
|
|
wikitext = indent + wikitext.replace( /\n/g, '\n' + indent );
|
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-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 );
|
|
|
|
}
|
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 () {
|
2020-04-27 15:50:02 +00:00
|
|
|
var widget = this,
|
2020-08-19 20:03:41 +00:00
|
|
|
pageName = this.pageName,
|
|
|
|
comment = this.comment;
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
2020-08-19 20:03:41 +00:00
|
|
|
logger( { action: 'saveAttempt' } );
|
|
|
|
widget.commentController.save( comment, pageName ).fail( function ( code, data ) {
|
2020-02-19 01:25:38 +00:00
|
|
|
var typeMap = {
|
|
|
|
// Compare to ve.init.mw.ArticleTargetEvents.js in VisualEditor.
|
|
|
|
editconflict: 'editConflict',
|
|
|
|
wasdeleted: 'editPageDeleted',
|
|
|
|
abusefilter: 'extensionAbuseFilter',
|
|
|
|
'abusefilter-disallowed': 'extensionAbuseFilter',
|
|
|
|
captcha: 'extensionCaptcha',
|
|
|
|
spamprotectiontext: 'extensionSpamBlacklist',
|
|
|
|
titleblacklist: 'extensionTitleBlacklist',
|
|
|
|
'titleblacklist-forbidden-edit': 'extensionTitleBlacklist',
|
|
|
|
badtoken: 'userBadToken',
|
|
|
|
newuser: 'userNewUser',
|
|
|
|
spamblacklist: 'extensionSpamBlacklist',
|
|
|
|
empty: 'responseEmpty',
|
|
|
|
unknown: 'responseUnknown',
|
|
|
|
pagedeleted: 'editPageDeleted'
|
|
|
|
};
|
|
|
|
|
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',
|
|
|
|
label: widget.captchaInput.$element
|
|
|
|
} );
|
|
|
|
widget.captchaMessage.$element.insertAfter( widget.$preview );
|
|
|
|
|
|
|
|
widget.captchaInput.focus();
|
|
|
|
widget.captchaInput.scrollElementIntoView();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
widget.errorMessage = new OO.ui.MessageWidget( {
|
|
|
|
type: 'error',
|
2020-10-28 16:57:23 +00:00
|
|
|
label: code instanceof Error ? code.toString() : controller.getApi().getErrorMessage( data )
|
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
|
|
|
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( {
|
|
|
|
action: 'saveFailure',
|
|
|
|
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
|
|
|
|
2019-12-10 15:40:52 +00:00
|
|
|
/* Window registration */
|
|
|
|
|
2020-07-09 14:07:06 +00:00
|
|
|
function AbandonCommentDialog() {
|
|
|
|
// Parent constructor
|
|
|
|
AbandonCommentDialog.super.apply( this, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( AbandonCommentDialog, mw.widgets.AbandonEditDialog );
|
|
|
|
|
|
|
|
AbandonCommentDialog.static.name = 'abandoncomment';
|
|
|
|
AbandonCommentDialog.static.message = OO.ui.deferMsg( 'discussiontools-replywidget-abandon' );
|
|
|
|
AbandonCommentDialog.static.actions = OO.copy( AbandonCommentDialog.static.actions );
|
|
|
|
AbandonCommentDialog.static.actions[ 0 ].label =
|
|
|
|
OO.ui.deferMsg( 'discussiontools-replywidget-abandon-discard' );
|
|
|
|
|
|
|
|
AbandonCommentDialog.static.actions[ 1 ].label =
|
|
|
|
OO.ui.deferMsg( 'discussiontools-replywidget-abandon-keep' );
|
|
|
|
|
|
|
|
OO.ui.getWindowManager().addWindows( [ new AbandonCommentDialog() ] );
|
2019-12-10 15:40:52 +00:00
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
module.exports = ReplyWidget;
|