2020-02-25 02:10:27 +00:00
|
|
|
var controller = require( 'ext.discussionTools.init' ).controller,
|
2020-03-08 14:32:38 +00:00
|
|
|
utils = require( 'ext.discussionTools.init' ).utils,
|
2020-02-25 02:10:27 +00:00
|
|
|
logger = require( 'ext.discussionTools.init' ).logger;
|
2019-12-10 21:46:22 +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-03-12 19:58:36 +00:00
|
|
|
* @param {Object} parsoidData Result from controller#getParsoidCommentData
|
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
|
2020-04-29 18:45:20 +00:00
|
|
|
* @param {boolean} [config.switchable] Widget can switch modes (visual/source)
|
2019-11-05 14:07:50 +00:00
|
|
|
*/
|
2020-04-27 15:50:02 +00:00
|
|
|
function ReplyWidget( commentController, parsoidData, config ) {
|
2020-03-04 16:19:19 +00:00
|
|
|
var returnTo, contextNode, inputConfig,
|
2020-04-27 15:50:02 +00:00
|
|
|
widget = this,
|
|
|
|
pageData = parsoidData.pageData,
|
|
|
|
comment = parsoidData.comment;
|
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;
|
|
|
|
this.parsoidData = parsoidData;
|
|
|
|
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-04-27 15:50:02 +00:00
|
|
|
{ placeholder: mw.msg( 'discussiontools-replywidget-placeholder-reply', comment.author ) },
|
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-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-03-12 19:58:36 +00:00
|
|
|
this.$footer = $( '<div>' ).addClass( 'dt-ui-replyWidget-footer' );
|
2020-04-27 15:50:02 +00:00
|
|
|
if ( pageData.pageName !== mw.config.get( 'wgRelevantPageName' ) ) {
|
2020-03-12 19:58:36 +00:00
|
|
|
this.$footer.append( $( '<p>' ).append(
|
2020-04-27 15:50:02 +00:00
|
|
|
mw.message( 'discussiontools-replywidget-transcluded', pageData.pageName ).parseDom()
|
2020-03-12 19:58:36 +00:00
|
|
|
) );
|
|
|
|
}
|
2020-04-07 14:33:32 +00:00
|
|
|
this.$footer.append(
|
|
|
|
$( '<p>' ).append(
|
|
|
|
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' } );
|
2019-11-05 14:07:50 +00:00
|
|
|
this.$element.on( 'keydown', this.onKeyDown.bind( this ) );
|
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 );
|
2019-11-05 14:07:50 +00:00
|
|
|
|
2019-11-20 20:27:08 +00:00
|
|
|
this.api = new mw.Api();
|
|
|
|
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(
|
2019-11-05 14:07:50 +00:00
|
|
|
this.replyBodyWidget.$element,
|
2020-02-03 20:34:13 +00:00
|
|
|
this.$preview,
|
|
|
|
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
|
|
|
|
2020-04-29 18:45:20 +00:00
|
|
|
if ( config.switchable ) {
|
2020-05-07 19:16:14 +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
|
|
|
|
} );
|
|
|
|
|
2020-05-22 12:20:06 +00:00
|
|
|
// Initialize to avoid flicker when switching mode
|
|
|
|
this.modeTabSelect.selectItemByData( this.getMode() );
|
|
|
|
|
2020-05-07 19:16:14 +00:00
|
|
|
this.modeTabSelect.connect( this, {
|
|
|
|
choose: 'onModeTabSelectChoose'
|
|
|
|
} );
|
|
|
|
|
2020-04-29 18:45:20 +00:00
|
|
|
this.$element.prepend( this.modeTabSelect.$element );
|
|
|
|
}
|
|
|
|
|
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-04-27 15:50:02 +00:00
|
|
|
this.checkboxesPromise = controller.getCheckboxesPromise( this.parsoidData.pageData );
|
2020-03-04 16:19:19 +00:00
|
|
|
this.checkboxesPromise.then( function ( checkboxes ) {
|
|
|
|
if ( checkboxes.checkboxFields ) {
|
|
|
|
widget.$checkboxes = $( '<div>' ).addClass( 'dt-ui-replyWidget-checkboxes' );
|
|
|
|
checkboxes.checkboxFields.forEach( function ( field ) {
|
|
|
|
widget.$checkboxes.append( field.$element );
|
|
|
|
} );
|
|
|
|
widget.$actions.prepend( widget.$checkboxes );
|
|
|
|
}
|
|
|
|
} );
|
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
|
|
|
|
2019-12-11 04:40:17 +00:00
|
|
|
ReplyWidget.prototype.clear = function () {
|
|
|
|
if ( this.errorMessage ) {
|
|
|
|
this.errorMessage.$element.remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 );
|
|
|
|
} else {
|
|
|
|
this.replyButton.setDisabled( false );
|
|
|
|
this.cancelButton.setDisabled( false );
|
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 ) {
|
|
|
|
this.api.saveOption( 'discussiontools-editmode', mode ).then( function () {
|
|
|
|
mw.user.options.set( 'discussiontools-editmode', mode );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
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-04-27 16:23:27 +00:00
|
|
|
promise.then( null, function () {
|
|
|
|
// 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-04-29 16:43:11 +00:00
|
|
|
* @param {Mixed} initialValue Initial value
|
2020-04-27 16:23:27 +00:00
|
|
|
* @chainable
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
2019-12-10 15:15:40 +00:00
|
|
|
ReplyWidget.prototype.setup = function () {
|
|
|
|
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
|
|
|
|
|
|
|
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() ) {
|
2019-12-10 15:40:52 +00:00
|
|
|
// 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();
|
|
|
|
}
|
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-01-13 19:29:07 +00:00
|
|
|
widget.teardown();
|
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
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @return {ReplyWidget}
|
|
|
|
*/
|
2020-01-13 19:29:07 +00:00
|
|
|
ReplyWidget.prototype.teardown = function () {
|
|
|
|
this.unbindBeforeUnloadHandler();
|
|
|
|
this.clear();
|
2020-04-29 16:43:11 +00:00
|
|
|
this.storage.remove( this.storagePrefix + '/mode' );
|
|
|
|
this.storage.remove( this.storagePrefix + '/saveable' );
|
2020-01-13 19:29:07 +00:00
|
|
|
this.$preview.empty();
|
|
|
|
this.emit( 'teardown' );
|
2020-04-27 16:23:27 +00:00
|
|
|
return this;
|
2020-01-13 19:29:07 +00:00
|
|
|
};
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
ReplyWidget.prototype.onKeyDown = function ( 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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' );
|
|
|
|
this.preparePreview( this.getValue() );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the interface with the preview of the given wikitext.
|
|
|
|
*
|
|
|
|
* @param {string} wikitext
|
|
|
|
* @return {jQuery.Promise} Promise resolved when we're done
|
|
|
|
*/
|
|
|
|
ReplyWidget.prototype.preparePreview = function ( wikitext ) {
|
|
|
|
var parsePromise,
|
2019-11-20 20:27:08 +00:00
|
|
|
widget = this,
|
|
|
|
indent = {
|
|
|
|
dl: ':',
|
|
|
|
ul: '*',
|
|
|
|
ol: '#'
|
|
|
|
}[ this.context ];
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-09 19:26:15 +00:00
|
|
|
wikitext = controller.sanitizeWikitextLinebreaks(
|
|
|
|
controller.autoSignWikitext( wikitext )
|
|
|
|
);
|
2020-05-20 12:09:33 +00:00
|
|
|
// Drop opacity of signature in preview to make message body preview clearer.
|
2020-02-18 15:43:06 +00:00
|
|
|
wikitext = wikitext.slice( 0, -4 ) + '<span style="opacity: 0.6;">~~~~</span>';
|
2019-11-20 20:27:08 +00:00
|
|
|
wikitext = indent + wikitext.replace( /\n/g, '\n' + indent );
|
2020-01-02 14:59:50 +00:00
|
|
|
this.previewRequest = parsePromise = this.api.post( {
|
|
|
|
formatversion: 2,
|
|
|
|
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' ],
|
2019-12-27 18:30:24 +00:00
|
|
|
title: mw.config.get( 'wgPageName' )
|
|
|
|
} );
|
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,
|
|
|
|
pageData = this.parsoidData.pageData,
|
|
|
|
comment = this.parsoidData.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-02-15 04:03:18 +00:00
|
|
|
// We must get a new copy of the document every time, otherwise any unsaved replies will pile up
|
2020-04-27 15:50:02 +00:00
|
|
|
// TODO: Move most of this logic to the CommentController
|
|
|
|
controller.getParsoidCommentData(
|
|
|
|
pageData.pageName,
|
|
|
|
pageData.oldId,
|
|
|
|
comment.id
|
|
|
|
).then( function ( parsoidData ) {
|
2020-02-19 01:25:38 +00:00
|
|
|
logger( { action: 'saveAttempt' } );
|
2020-04-27 15:50:02 +00:00
|
|
|
return widget.commentController.save( parsoidData );
|
2019-11-13 13:57:57 +00:00
|
|
|
} ).then( function ( data ) {
|
2020-03-12 19:58:36 +00:00
|
|
|
var
|
|
|
|
pageUpdated = $.Deferred(),
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$container = $( '#mw-content-text' );
|
2019-11-13 13:57:57 +00:00
|
|
|
|
2020-01-15 16:21:59 +00:00
|
|
|
widget.teardown();
|
|
|
|
// TODO: Tell controller to teardown all other open widgets
|
|
|
|
|
2019-11-13 13:57:57 +00:00
|
|
|
// Update page state
|
2020-04-27 15:50:02 +00:00
|
|
|
if ( pageData.pageName === mw.config.get( 'wgRelevantPageName' ) ) {
|
2020-03-12 19:58:36 +00:00
|
|
|
// We can use the result from the VisualEditor API
|
|
|
|
$container.html( data.content );
|
|
|
|
mw.config.set( {
|
|
|
|
wgCurRevisionId: data.newrevid,
|
|
|
|
wgRevisionId: data.newrevid
|
|
|
|
} );
|
|
|
|
mw.config.set( data.jsconfigvars );
|
|
|
|
// Note: VE API merges 'modules' and 'modulestyles'
|
|
|
|
mw.loader.load( data.modules );
|
|
|
|
// TODO update categories, displaytitle, lastmodified
|
|
|
|
// (see ve.init.mw.DesktopArticleTarget.prototype.replacePageContent)
|
|
|
|
|
|
|
|
pageUpdated.resolve();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// We saved to another page, we must purge and then fetch the current page
|
|
|
|
widget.api.post( {
|
|
|
|
action: 'purge',
|
|
|
|
titles: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} ).then( function () {
|
|
|
|
return widget.api.get( {
|
|
|
|
formatversion: 2,
|
|
|
|
action: 'parse',
|
|
|
|
prop: [ 'text', 'modules', 'jsconfigvars' ],
|
|
|
|
page: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} );
|
|
|
|
} ).then( function ( parseResp ) {
|
|
|
|
$container.html( parseResp.parse.text );
|
|
|
|
mw.config.set( parseResp.parse.jsconfigvars );
|
|
|
|
mw.loader.load( parseResp.parse.modulestyles );
|
|
|
|
mw.loader.load( parseResp.parse.modules );
|
|
|
|
// TODO update categories, displaytitle, lastmodified
|
|
|
|
// We may not be able to use prop=displaytitle without making changes in the action=parse API,
|
|
|
|
// VE API has some confusing code that changes the HTML escaping on it before returning???
|
|
|
|
|
|
|
|
pageUpdated.resolve();
|
|
|
|
|
|
|
|
} ).catch( function () {
|
|
|
|
// We saved the reply, but couldn't purge or fetch the updated page. Seems difficult to
|
|
|
|
// explain this problem. Redirect to the page where the user can at least see their reply…
|
2020-04-27 15:50:02 +00:00
|
|
|
window.location = mw.util.getUrl( pageData.pageName );
|
2020-03-12 19:58:36 +00:00
|
|
|
} );
|
|
|
|
}
|
2020-02-19 01:25:38 +00:00
|
|
|
|
2020-03-12 19:58:36 +00:00
|
|
|
pageUpdated.then( function () {
|
|
|
|
// Re-initialize
|
|
|
|
controller.init( $container.find( '.mw-parser-output' ), {
|
2020-04-27 15:50:02 +00:00
|
|
|
repliedTo: comment.id
|
2020-03-12 19:58:36 +00:00
|
|
|
} );
|
|
|
|
mw.hook( 'wikipage.content' ).fire( $container );
|
|
|
|
|
|
|
|
logger( {
|
|
|
|
action: 'saveSuccess',
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
revision_id: data.newrevid
|
|
|
|
} );
|
2020-02-19 01:25:38 +00:00
|
|
|
} );
|
2020-03-12 19:58:36 +00:00
|
|
|
|
2019-12-11 04:40:17 +00:00
|
|
|
}, 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-03-23 22:50:03 +00:00
|
|
|
if ( OO.getProp( data, 'visualeditoredit', '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(
|
|
|
|
OO.getProp( data, 'visualeditoredit', 'edit', 'captcha' )
|
|
|
|
);
|
|
|
|
// 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-05-11 16:38:21 +00:00
|
|
|
label: code instanceof Error ? code.toString() : widget.api.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 */
|
|
|
|
|
|
|
|
OO.ui.getWindowManager().addWindows( [ new mw.widgets.AbandonEditDialog() ] );
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
module.exports = ReplyWidget;
|