2019-11-05 13:55:01 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-04-27 21:52:11 +00:00
|
|
|
/**
|
|
|
|
* @external CommentItem
|
|
|
|
*/
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
var
|
2020-04-30 13:20:41 +00:00
|
|
|
$pageContainer,
|
2020-08-29 12:00:51 +00:00
|
|
|
newTopicController,
|
2020-11-30 23:32:29 +00:00
|
|
|
$overlay,
|
2021-01-25 14:15:35 +00:00
|
|
|
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
|
2020-10-21 15:52:04 +00:00
|
|
|
Parser = require( './Parser.js' ),
|
2020-09-16 12:07:27 +00:00
|
|
|
ThreadItem = require( './ThreadItem.js' ),
|
2020-08-19 20:03:41 +00:00
|
|
|
logger = require( './logger.js' ),
|
2021-02-17 22:34:02 +00:00
|
|
|
utils = require( './utils.js' ),
|
2020-04-30 13:20:41 +00:00
|
|
|
pageDataCache = {};
|
2019-11-05 13:55:01 +00:00
|
|
|
|
2020-02-25 02:10:27 +00:00
|
|
|
mw.messages.set( require( './controller/contLangMessages.json' ) );
|
2020-03-10 13:03:45 +00:00
|
|
|
|
2020-10-23 11:02:18 +00:00
|
|
|
function getApi() {
|
2020-10-23 11:32:00 +00:00
|
|
|
return new mw.Api( {
|
|
|
|
parameters: {
|
|
|
|
formatversion: 2,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' )
|
|
|
|
}
|
|
|
|
} );
|
2020-10-23 11:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 13:57:57 +00:00
|
|
|
function highlight( comment ) {
|
|
|
|
var padding = 5,
|
2021-03-13 14:39:39 +00:00
|
|
|
$highlight = $( '<div>' ).addClass( 'ext-discussiontools-init-highlight' );
|
2019-11-13 13:57:57 +00:00
|
|
|
|
2020-11-30 23:32:29 +00:00
|
|
|
if ( !$overlay ) {
|
|
|
|
// $overlay must be position:relative/absolute
|
|
|
|
$overlay = $( '<div>' ).addClass( 'oo-ui-defaultOverlay' ).appendTo( 'body' );
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var overlayRect = $overlay[ 0 ].getBoundingClientRect();
|
|
|
|
var rect = RangeFix.getBoundingClientRect( comment.getNativeRange() );
|
2019-11-13 13:57:57 +00:00
|
|
|
$highlight.css( {
|
2020-11-30 23:32:29 +00:00
|
|
|
top: rect.top - overlayRect.top - padding,
|
|
|
|
left: rect.left - overlayRect.left - padding,
|
2019-11-13 13:57:57 +00:00
|
|
|
width: rect.width + ( padding * 2 ),
|
|
|
|
height: rect.height + ( padding * 2 )
|
|
|
|
} );
|
2020-11-30 23:32:29 +00:00
|
|
|
$overlay.prepend( $highlight );
|
2019-11-13 13:57:57 +00:00
|
|
|
|
2021-02-04 19:12:03 +00:00
|
|
|
// Show a highlight with the same timing as the post-edit message (mediawiki.action.view.postEdit):
|
|
|
|
// show for 3000ms, fade out for 250ms (animation duration is defined in CSS).
|
2020-06-11 16:36:59 +00:00
|
|
|
OO.ui.Element.static.scrollIntoView( $highlight[ 0 ], { padding: { top: 10, bottom: 10 } } ).then( function () {
|
2021-03-13 14:39:39 +00:00
|
|
|
// Toggle the 'ext-discussiontools-init-highlight-overlay' class only when needed, because using mix-blend-mode
|
2021-02-04 19:12:03 +00:00
|
|
|
// affects the text rendering of the whole page, disabling subpixel antialiasing on Windows
|
2021-03-13 14:39:39 +00:00
|
|
|
$overlay.addClass( 'ext-discussiontools-init-highlight-overlay' );
|
|
|
|
$highlight.addClass( 'ext-discussiontools-init-highlight-fadein' );
|
2019-11-13 13:57:57 +00:00
|
|
|
setTimeout( function () {
|
2021-03-13 14:39:39 +00:00
|
|
|
$highlight.addClass( 'ext-discussiontools-init-highlight-fadeout' );
|
2020-06-11 16:36:59 +00:00
|
|
|
setTimeout( function () {
|
2021-02-04 19:12:03 +00:00
|
|
|
$highlight.remove();
|
2021-03-13 14:39:39 +00:00
|
|
|
$overlay.removeClass( 'ext-discussiontools-init-highlight-overlay' );
|
2021-02-04 19:12:03 +00:00
|
|
|
}, 250 );
|
|
|
|
}, 3000 );
|
2020-06-11 16:36:59 +00:00
|
|
|
} );
|
2019-11-13 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:03:18 +00:00
|
|
|
/**
|
2020-08-19 20:03:41 +00:00
|
|
|
* Get various pieces of page metadata.
|
2020-02-15 04:03:18 +00:00
|
|
|
*
|
|
|
|
* This method caches responses. If you call it again with the same parameters, you'll get the exact
|
|
|
|
* same Promise object, and no API request will be made.
|
|
|
|
*
|
|
|
|
* @param {string} pageName Page title
|
|
|
|
* @param {number} oldId Revision ID
|
2021-03-24 18:41:39 +00:00
|
|
|
* @param {boolean} [isNewTopic=false]
|
2020-02-15 04:03:18 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
2021-03-24 18:41:39 +00:00
|
|
|
function getPageData( pageName, oldId, isNewTopic ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var api = getApi();
|
2020-10-23 11:02:18 +00:00
|
|
|
|
2020-02-15 04:03:18 +00:00
|
|
|
pageDataCache[ pageName ] = pageDataCache[ pageName ] || {};
|
|
|
|
if ( pageDataCache[ pageName ][ oldId ] ) {
|
|
|
|
return pageDataCache[ pageName ][ oldId ];
|
|
|
|
}
|
2020-06-02 23:00:59 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var lintPromise, transcludedFromPromise;
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( oldId && !isNewTopic ) {
|
2020-09-16 11:19:42 +00:00
|
|
|
lintPromise = api.get( {
|
|
|
|
action: 'query',
|
|
|
|
list: 'linterrors',
|
|
|
|
lntcategories: 'fostered',
|
|
|
|
lntlimit: 1,
|
|
|
|
lnttitle: pageName
|
|
|
|
} ).then( function ( response ) {
|
|
|
|
return OO.getProp( response, 'query', 'linterrors' ) || [];
|
|
|
|
} );
|
2020-06-02 23:00:59 +00:00
|
|
|
|
2020-09-16 11:19:42 +00:00
|
|
|
transcludedFromPromise = api.get( {
|
|
|
|
action: 'discussiontools',
|
|
|
|
paction: 'transcludedfrom',
|
|
|
|
page: pageName,
|
|
|
|
oldid: oldId
|
|
|
|
} ).then( function ( response ) {
|
2020-09-16 20:30:11 +00:00
|
|
|
return OO.getProp( response, 'discussiontools' ) || {};
|
2020-09-16 11:19:42 +00:00
|
|
|
} );
|
|
|
|
} else {
|
2020-09-16 20:30:11 +00:00
|
|
|
lintPromise = $.Deferred().resolve( [] ).promise();
|
|
|
|
transcludedFromPromise = $.Deferred().resolve( {} ).promise();
|
2020-09-16 11:19:42 +00:00
|
|
|
}
|
2020-08-19 20:03:41 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var veMetadataPromise = api.get( {
|
2020-08-19 20:03:41 +00:00
|
|
|
action: 'visualeditor',
|
|
|
|
paction: 'metadata',
|
|
|
|
page: pageName
|
|
|
|
} ).then( function ( response ) {
|
|
|
|
return OO.getProp( response, 'visualeditor' ) || [];
|
|
|
|
} );
|
|
|
|
|
|
|
|
pageDataCache[ pageName ][ oldId ] = $.when( lintPromise, transcludedFromPromise, veMetadataPromise )
|
|
|
|
.then( function ( linterrors, transcludedfrom, metadata ) {
|
|
|
|
return {
|
|
|
|
linterrors: linterrors,
|
|
|
|
transcludedfrom: transcludedfrom,
|
|
|
|
metadata: metadata
|
|
|
|
};
|
|
|
|
}, function () {
|
|
|
|
// Clear on failure
|
|
|
|
pageDataCache[ pageName ][ oldId ] = null;
|
2020-08-12 21:37:10 +00:00
|
|
|
// Let caller handle the error
|
|
|
|
return $.Deferred().rejectWith( this, arguments );
|
2020-08-19 20:03:41 +00:00
|
|
|
} );
|
2020-02-15 04:03:18 +00:00
|
|
|
return pageDataCache[ pageName ][ oldId ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-19 20:03:41 +00:00
|
|
|
* Check if a given comment on a page can be replied to
|
2020-02-15 04:03:18 +00:00
|
|
|
*
|
2020-03-12 19:58:36 +00:00
|
|
|
* @param {string} pageName Page title
|
|
|
|
* @param {number} oldId Revision ID
|
2021-04-07 20:46:36 +00:00
|
|
|
* @param {CommentItem} comment Comment
|
2020-08-19 20:03:41 +00:00
|
|
|
* @return {jQuery.Promise} Resolves with the pageName+oldId if the comment appears on the page.
|
|
|
|
* Rejects with error data if the comment is transcluded, or there are lint errors on the page.
|
2020-02-15 04:03:18 +00:00
|
|
|
*/
|
2021-04-07 20:46:36 +00:00
|
|
|
function checkCommentOnPage( pageName, oldId, comment ) {
|
|
|
|
var isNewTopic = comment.id.slice( 0, 4 ) === 'new|';
|
2021-03-24 18:41:39 +00:00
|
|
|
|
|
|
|
return getPageData( pageName, oldId, isNewTopic )
|
2020-02-15 04:03:18 +00:00
|
|
|
.then( function ( response ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var metadata = response.metadata,
|
2020-08-19 20:03:41 +00:00
|
|
|
lintErrors = response.linterrors,
|
|
|
|
transcludedFrom = response.transcludedfrom;
|
|
|
|
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( !isNewTopic ) {
|
2021-04-07 20:46:36 +00:00
|
|
|
// First look for data by the comment's ID. If not found, also look by name.
|
|
|
|
// Data by ID may not be found due to differences in headings (e.g. T273413, T275821),
|
|
|
|
// or if a comment's parent changes.
|
|
|
|
// Data by name might be combined from two or more comments, which would only allow us to
|
|
|
|
// treat them both as transcluded from unknown source, unless we check ID first.
|
2021-04-08 13:46:09 +00:00
|
|
|
var isTranscludedFrom = transcludedFrom[ comment.id ] || transcludedFrom[ comment.name ];
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( isTranscludedFrom === undefined ) {
|
|
|
|
// The comment wasn't found when generating the "transcludedfrom" data,
|
|
|
|
// so we don't know where the reply should be posted. Just give up.
|
|
|
|
return $.Deferred().reject( 'discussiontools-commentid-notfound-transcludedfrom', { errors: [ {
|
|
|
|
code: 'discussiontools-commentid-notfound-transcludedfrom',
|
|
|
|
html: mw.message( 'discussiontools-error-comment-disappeared' ).parse()
|
|
|
|
} ] } ).promise();
|
|
|
|
} else if ( isTranscludedFrom ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var mwTitle = isTranscludedFrom === true ? null : mw.Title.newFromText( isTranscludedFrom );
|
2021-03-24 18:41:39 +00:00
|
|
|
// If this refers to a template rather than a subpage, we never want to edit it
|
2021-04-08 13:46:09 +00:00
|
|
|
var follow = mwTitle && mwTitle.getNamespaceId() !== mw.config.get( 'wgNamespaceIds' ).template;
|
2021-03-24 18:41:39 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var transcludedErrMsg;
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( follow ) {
|
|
|
|
transcludedErrMsg = mw.message(
|
|
|
|
'discussiontools-error-comment-is-transcluded-title',
|
|
|
|
mwTitle.getPrefixedText()
|
|
|
|
).parse();
|
|
|
|
} else {
|
|
|
|
transcludedErrMsg = mw.message(
|
|
|
|
'discussiontools-error-comment-is-transcluded',
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-edit' ).text()
|
|
|
|
).parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.Deferred().reject( 'comment-is-transcluded', { errors: [ {
|
|
|
|
data: {
|
|
|
|
transcludedFrom: isTranscludedFrom,
|
|
|
|
follow: follow
|
|
|
|
},
|
|
|
|
code: 'comment-is-transcluded',
|
|
|
|
html: transcludedErrMsg
|
|
|
|
} ] } ).promise();
|
2020-03-09 20:53:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( lintErrors.length ) {
|
|
|
|
// We currently only request the first error
|
2021-04-08 13:46:09 +00:00
|
|
|
var lintType = lintErrors[ 0 ].category;
|
2021-03-24 18:41:39 +00:00
|
|
|
|
|
|
|
return $.Deferred().reject( 'lint', { errors: [ {
|
|
|
|
code: 'lint',
|
|
|
|
html: mw.message( 'discussiontools-error-lint',
|
|
|
|
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Lint_errors/' + lintType,
|
|
|
|
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help_talk:Lint_errors/' + lintType,
|
|
|
|
mw.util.getUrl( pageName, { action: 'edit', lintid: lintErrors[ 0 ].lintId } ) ).parse()
|
|
|
|
} ] } ).promise();
|
|
|
|
}
|
2020-03-09 20:53:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( !metadata.canEdit ) {
|
|
|
|
return $.Deferred().reject( 'permissions-error', { errors: [ {
|
|
|
|
code: 'permissions-error',
|
|
|
|
html: metadata.notices[ 'permissions-error' ]
|
2020-06-03 22:19:44 +00:00
|
|
|
} ] } ).promise();
|
2020-04-15 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:03:18 +00:00
|
|
|
return {
|
2020-08-19 20:03:41 +00:00
|
|
|
pageName: pageName,
|
|
|
|
oldId: oldId
|
2020-02-15 04:03:18 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
function getCheckboxesPromise( pageName, oldId ) {
|
2020-03-04 16:19:19 +00:00
|
|
|
return getPageData(
|
2020-08-19 20:03:41 +00:00
|
|
|
pageName,
|
|
|
|
oldId
|
|
|
|
).then( function ( pageData ) {
|
|
|
|
var data = pageData.metadata,
|
2020-03-04 16:19:19 +00:00
|
|
|
checkboxesDef = {};
|
|
|
|
|
|
|
|
mw.messages.set( data.checkboxesMessages );
|
|
|
|
|
|
|
|
// Only show the watch checkbox for now
|
|
|
|
if ( 'wpWatchthis' in data.checkboxesDef ) {
|
|
|
|
checkboxesDef.wpWatchthis = data.checkboxesDef.wpWatchthis;
|
|
|
|
}
|
2020-12-15 19:34:57 +00:00
|
|
|
return mw.loader.using( 'ext.visualEditor.targetLoader' ).then( function () {
|
|
|
|
return mw.libs.ve.targetLoader.createCheckboxFields( checkboxesDef );
|
|
|
|
} );
|
2020-03-04 16:19:19 +00:00
|
|
|
// TODO: createCheckboxField doesn't make links in the label open in a new
|
|
|
|
// window as that method currently lives in ve.utils
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:34:02 +00:00
|
|
|
function initTopicSubscriptions( $container ) {
|
|
|
|
$container.find( '.ext-discussiontools-section-subscribe' ).on( 'click', function () {
|
|
|
|
var element = this,
|
|
|
|
api = getApi(),
|
|
|
|
subscribe = element.classList.contains( 'oo-ui-icon-star' ),
|
|
|
|
commentName = this.getAttribute( 'data-mw-comment-name' ),
|
2021-04-22 16:18:08 +00:00
|
|
|
heading = $( this ).closest( '.ext-discussiontools-section' )[ 0 ],
|
2021-02-17 22:34:02 +00:00
|
|
|
section = utils.getHeadlineNodeAndOffset( heading ).node.id,
|
|
|
|
title = mw.config.get( 'wgRelevantPageName' ) + '#' + section;
|
|
|
|
|
|
|
|
if ( !commentName ) {
|
|
|
|
// This should never happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Disable button while pending
|
|
|
|
api.postWithToken( 'csrf', {
|
|
|
|
action: 'discussiontoolssubscribe',
|
|
|
|
page: title,
|
|
|
|
commentname: commentName,
|
|
|
|
subscribe: subscribe
|
|
|
|
}, { contentType: 'multipart/form-data' } ).then( function ( response2 ) {
|
|
|
|
return OO.getProp( response2, 'discussiontoolssubscribe' ) || {};
|
|
|
|
} ).then( function ( result ) {
|
|
|
|
element.classList.remove( 'oo-ui-icon-star', 'oo-ui-icon-unStar', 'oo-ui-image-progressive' );
|
|
|
|
if ( result.subscribe ) {
|
|
|
|
element.classList.add( 'oo-ui-icon-unStar', 'oo-ui-image-progressive' );
|
|
|
|
} else {
|
|
|
|
element.classList.add( 'oo-ui-icon-star' );
|
|
|
|
}
|
|
|
|
mw.notify(
|
|
|
|
mw.msg(
|
|
|
|
result.subscribe ?
|
|
|
|
'discussiontools-topicsubscription-notify-subscribed-body' :
|
|
|
|
'discussiontools-topicsubscription-notify-unsubscribed-body'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
title: mw.msg(
|
|
|
|
result.subscribe ?
|
|
|
|
'discussiontools-topicsubscription-notify-subscribed-title' :
|
|
|
|
'discussiontools-topicsubscription-notify-unsubscribed-title'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:57:57 +00:00
|
|
|
function init( $container, state ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var pageExists = !!mw.config.get( 'wgRelevantArticleId' ),
|
2021-01-25 22:21:34 +00:00
|
|
|
controllers = [],
|
|
|
|
activeController = null,
|
2020-09-16 12:07:27 +00:00
|
|
|
// Loads later to avoid circular dependency
|
|
|
|
CommentController = require( './CommentController.js' ),
|
2020-08-29 12:00:51 +00:00
|
|
|
NewTopicController = require( './NewTopicController.js' ),
|
2020-11-18 18:44:44 +00:00
|
|
|
threadItemsById = {},
|
|
|
|
threadItems = [];
|
2019-11-05 13:55:01 +00:00
|
|
|
|
|
|
|
$pageContainer = $container;
|
2021-04-08 13:46:09 +00:00
|
|
|
var parser = new Parser( $pageContainer[ 0 ] );
|
2019-11-24 15:39:52 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var pageThreads = [];
|
|
|
|
var commentNodes = $pageContainer[ 0 ].querySelectorAll( '[data-mw-comment]' );
|
2020-11-18 18:44:44 +00:00
|
|
|
threadItems.length = commentNodes.length;
|
2020-04-29 16:43:11 +00:00
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
// The page can be served from the HTTP cache (Varnish), containing data-mw-comment generated
|
|
|
|
// by an older version of our PHP code. Code below must be able to handle that.
|
2021-04-19 18:34:55 +00:00
|
|
|
// See CommentFormatter::addDiscussionTools() in PHP.
|
2020-10-21 15:52:04 +00:00
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
// Iterate over commentNodes backwards so replies are always deserialized before their parents.
|
2021-04-08 13:46:09 +00:00
|
|
|
var i, comment;
|
2020-09-16 12:07:27 +00:00
|
|
|
for ( i = commentNodes.length - 1; i >= 0; i-- ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var hash = JSON.parse( commentNodes[ i ].getAttribute( 'data-mw-comment' ) );
|
2020-10-27 12:18:50 +00:00
|
|
|
comment = ThreadItem.static.newFromJSON( hash, threadItemsById );
|
2021-02-12 19:16:13 +00:00
|
|
|
if ( !comment.name ) {
|
|
|
|
comment.name = parser.computeName( comment );
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
threadItemsById[ comment.id ] = comment;
|
2020-09-16 12:07:27 +00:00
|
|
|
|
|
|
|
if ( comment.type === 'comment' ) {
|
2021-01-25 22:21:34 +00:00
|
|
|
controllers.push(
|
2021-02-12 19:16:13 +00:00
|
|
|
new CommentController( $pageContainer, $( commentNodes[ i ] ), comment, parser )
|
2021-01-25 22:21:34 +00:00
|
|
|
);
|
2020-09-16 12:07:27 +00:00
|
|
|
} else {
|
|
|
|
// Use unshift as we are in a backwards loop
|
|
|
|
pageThreads.unshift( comment );
|
|
|
|
}
|
2020-11-18 18:44:44 +00:00
|
|
|
threadItems[ i ] = comment;
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|
2019-11-05 13:55:01 +00:00
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
// Recalculate legacy IDs
|
2021-02-12 19:16:13 +00:00
|
|
|
parser.threadItemsByName = {};
|
2020-10-21 15:52:04 +00:00
|
|
|
parser.threadItemsById = {};
|
2020-11-18 18:44:44 +00:00
|
|
|
// In the forward order this time, as the IDs for indistinguishable comments depend on it
|
|
|
|
for ( i = 0; i < threadItems.length; i++ ) {
|
|
|
|
comment = threadItems[ i ];
|
2021-02-12 19:16:13 +00:00
|
|
|
|
|
|
|
if ( !parser.threadItemsByName[ comment.name ] ) {
|
|
|
|
parser.threadItemsByName[ comment.name ] = [];
|
|
|
|
}
|
|
|
|
parser.threadItemsByName[ comment.name ].push( comment );
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var newId = parser.computeId( comment );
|
2020-11-18 18:44:44 +00:00
|
|
|
parser.threadItemsById[ newId ] = comment;
|
|
|
|
if ( newId !== comment.id ) {
|
2020-10-21 15:52:04 +00:00
|
|
|
comment.id = newId;
|
|
|
|
threadItemsById[ newId ] = comment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:04:18 +00:00
|
|
|
if ( featuresEnabled.newtopictool && mw.user.options.get( 'discussiontools-newtopictool' ) ) {
|
2020-12-23 15:26:21 +00:00
|
|
|
if ( newTopicController ) {
|
|
|
|
// Stop the torn down controller from re-appearing
|
|
|
|
newTopicController.$replyLink.off( 'click keypress', newTopicController.onReplyLinkClickHandler );
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2021-04-08 13:46:09 +00:00
|
|
|
var $addSectionTab = $( '#ca-addsection' );
|
2020-12-23 15:26:21 +00:00
|
|
|
// TODO If the page doesn't exist yet, we'll need to handle the interface differently,
|
|
|
|
// for now just don't enable the tool there
|
|
|
|
if ( $addSectionTab.length && pageExists ) {
|
|
|
|
// Disable VisualEditor's new section editor (in wikitext mode / NWE), to allow our own
|
|
|
|
$addSectionTab.off( '.ve-target' );
|
2021-02-12 19:16:13 +00:00
|
|
|
newTopicController = new NewTopicController( $pageContainer, $addSectionTab.find( 'a' ), parser );
|
2021-01-25 22:21:34 +00:00
|
|
|
controllers.push( newTopicController );
|
2020-12-23 15:26:21 +00:00
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:34:02 +00:00
|
|
|
if ( featuresEnabled.topicsubscription && mw.user.options.get( 'discussiontools-topicsubscription' ) ) {
|
|
|
|
initTopicSubscriptions( $container );
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
// Hook up each link to open a reply widget
|
|
|
|
//
|
|
|
|
// TODO: Allow users to use multiple reply widgets simultaneously.
|
|
|
|
// Currently submitting a reply from one widget would also destroy the other ones.
|
|
|
|
controllers.forEach( function ( c ) {
|
|
|
|
c.on( 'link-click', function () {
|
|
|
|
// If the reply widget is already open, activate it.
|
|
|
|
// Reply links are also made unclickable using 'pointer-events' in CSS, but that doesn't happen
|
|
|
|
// for new section links, because we don't have a good way of visually disabling them.
|
|
|
|
// (And it also doesn't work on IE 11.)
|
|
|
|
if ( activeController === c ) {
|
|
|
|
c.showAndFocus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:41:35 +00:00
|
|
|
// If this is a new topic link, and a reply widget is open, attempt to close it first.
|
|
|
|
if ( activeController && c instanceof NewTopicController ) {
|
|
|
|
activeController.replyWidget.tryTeardown().then( function () {
|
|
|
|
activeController = c;
|
|
|
|
c.setup();
|
|
|
|
} );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
// If another reply widget is open (or opening), do nothing.
|
|
|
|
if ( activeController ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
activeController = c;
|
|
|
|
c.setup();
|
|
|
|
} ).on( 'teardown', function () {
|
|
|
|
activeController = null;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
// For debugging (now unused in the code)
|
2019-11-05 13:55:01 +00:00
|
|
|
mw.dt.pageThreads = pageThreads;
|
|
|
|
|
2021-02-17 12:30:03 +00:00
|
|
|
if ( state.repliedTo === 'new|' + mw.config.get( 'wgRelevantPageName' ) ) {
|
2021-02-17 12:22:40 +00:00
|
|
|
// Highlight the last comment on the page
|
2021-04-08 13:46:09 +00:00
|
|
|
var lastComment = threadItems[ threadItems.length - 1 ];
|
2021-02-17 12:22:40 +00:00
|
|
|
highlight( lastComment );
|
|
|
|
|
|
|
|
// If it's the only comment under its heading, highlight the heading too.
|
|
|
|
// (It might not be if the new discussion topic was posted without a title: T272666.)
|
|
|
|
if (
|
|
|
|
lastComment.parent &&
|
|
|
|
lastComment.parent.type === 'heading' &&
|
|
|
|
lastComment.parent.replies.length === 1
|
|
|
|
) {
|
|
|
|
highlight( lastComment.parent );
|
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
mw.hook( 'postEdit' ).fire( {
|
|
|
|
message: mw.msg( 'discussiontools-postedit-confirmation-topicadded', mw.user )
|
|
|
|
} );
|
|
|
|
|
|
|
|
} else if ( state.repliedTo ) {
|
2019-11-24 15:39:52 +00:00
|
|
|
// Find the comment we replied to, then highlight the last reply
|
2021-04-08 13:46:09 +00:00
|
|
|
var repliedToComment = threadItemsById[ state.repliedTo ];
|
2019-11-24 15:39:52 +00:00
|
|
|
highlight( repliedToComment.replies[ repliedToComment.replies.length - 1 ] );
|
2019-11-13 13:57:57 +00:00
|
|
|
|
2020-11-30 23:45:15 +00:00
|
|
|
mw.hook( 'postEdit' ).fire( {
|
|
|
|
message: mw.msg( 'discussiontools-postedit-confirmation-published', mw.user )
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
// Preload page metadata.
|
2020-02-15 04:03:18 +00:00
|
|
|
// TODO: Isn't this too early to load it? We will only need it if the user tries replying...
|
|
|
|
getPageData(
|
|
|
|
mw.config.get( 'wgRelevantPageName' ),
|
2020-02-15 04:49:58 +00:00
|
|
|
mw.config.get( 'wgCurRevisionId' )
|
2020-02-15 04:03:18 +00:00
|
|
|
);
|
2019-11-05 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
function update( data, comment, pageName, replyWidget ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var api = getApi(),
|
2020-08-19 20:03:41 +00:00
|
|
|
pageUpdated = $.Deferred();
|
|
|
|
|
2020-10-22 19:52:05 +00:00
|
|
|
// We posted a new comment, clear the cache, because wgCurRevisionId will not change if we posted
|
|
|
|
// to a transcluded page (T266275)
|
|
|
|
pageDataCache[ mw.config.get( 'wgRelevantPageName' ) ][ mw.config.get( 'wgCurRevisionId' ) ] = null;
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
replyWidget.teardown();
|
|
|
|
// TODO: Tell controller to teardown all other open widgets
|
|
|
|
|
|
|
|
// Update page state
|
|
|
|
if ( pageName === mw.config.get( 'wgRelevantPageName' ) ) {
|
|
|
|
// We can use the result from the VisualEditor API
|
|
|
|
$pageContainer.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
|
|
|
|
api.post( {
|
|
|
|
action: 'purge',
|
|
|
|
titles: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} ).then( function () {
|
|
|
|
return api.get( {
|
|
|
|
action: 'parse',
|
2020-10-22 13:03:40 +00:00
|
|
|
// HACK: 'useskin' triggers a different code path that runs our OutputPageBeforeHTML hook,
|
|
|
|
// adding our reply links in the HTML (T266195)
|
|
|
|
useskin: mw.config.get( 'skin' ),
|
2020-10-23 11:02:18 +00:00
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
2020-11-18 18:19:06 +00:00
|
|
|
// HACK: Always display reply links afterwards, ignoring preferences etc., in case this was
|
|
|
|
// a page view with reply links forced with ?dtenable=1 or otherwise
|
|
|
|
dtenable: '1',
|
2020-08-19 20:03:41 +00:00
|
|
|
prop: [ 'text', 'modules', 'jsconfigvars' ],
|
|
|
|
page: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} );
|
|
|
|
} ).then( function ( parseResp ) {
|
|
|
|
$pageContainer.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…
|
|
|
|
window.location = mw.util.getUrl( pageName );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// User logged in if module loaded.
|
|
|
|
if ( mw.loader.getState( 'mediawiki.page.watch.ajax' ) === 'ready' ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var watch = require( 'mediawiki.page.watch.ajax' );
|
2020-08-28 14:50:42 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
watch.updateWatchLink(
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-watch a, #ca-unwatch a' ),
|
2020-08-28 14:50:42 +00:00
|
|
|
data.watched ? 'unwatch' : 'watch',
|
|
|
|
'idle',
|
|
|
|
data.watchlistexpiry
|
2020-08-19 20:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pageUpdated.then( function () {
|
|
|
|
// Re-initialize and highlight the new reply.
|
|
|
|
mw.dt.initState.repliedTo = comment.id;
|
|
|
|
|
|
|
|
// We need our init code to run after everyone else's handlers for this hook,
|
|
|
|
// so that all changes to the page layout have been completed (e.g. collapsible elements),
|
|
|
|
// and we can measure things and display the highlight in the right place.
|
|
|
|
mw.hook( 'wikipage.content' ).remove( mw.dt.init );
|
|
|
|
mw.hook( 'wikipage.content' ).fire( $pageContainer );
|
|
|
|
// The hooks have "memory" so calling add() after fire() actually fires the handler,
|
|
|
|
// and calling add() before fire() would actually fire it twice.
|
|
|
|
mw.hook( 'wikipage.content' ).add( mw.dt.init );
|
|
|
|
|
|
|
|
logger( {
|
|
|
|
action: 'saveSuccess',
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
revision_id: data.newrevid
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:01 +00:00
|
|
|
module.exports = {
|
|
|
|
init: init,
|
2020-08-19 20:03:41 +00:00
|
|
|
update: update,
|
|
|
|
checkCommentOnPage: checkCommentOnPage,
|
2020-10-23 11:02:18 +00:00
|
|
|
getCheckboxesPromise: getCheckboxesPromise,
|
|
|
|
getApi: getApi
|
2019-11-05 13:55:01 +00:00
|
|
|
};
|