2019-11-05 13:55:01 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-10 21:46:22 +00:00
|
|
|
var
|
2022-10-11 12:30:37 +00:00
|
|
|
$pageContainer, linksController,
|
2022-03-01 15:04:24 +00:00
|
|
|
pageThreads,
|
2022-02-02 16:18:01 +00:00
|
|
|
lastControllerScrollOffset,
|
2021-01-25 14:15:35 +00:00
|
|
|
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
|
2022-06-12 13:54:26 +00:00
|
|
|
createMemoryStorage = require( './createMemoryStorage.js' ),
|
|
|
|
storage = createMemoryStorage( mw.storage.session ),
|
2020-10-21 15:52:04 +00:00
|
|
|
Parser = require( './Parser.js' ),
|
2022-02-19 06:31:34 +00:00
|
|
|
ThreadItemSet = require( './ThreadItemSet.js' ),
|
2021-06-09 23:22:28 +00:00
|
|
|
CommentDetails = require( './CommentDetails.js' ),
|
2021-06-24 16:21:31 +00:00
|
|
|
ReplyLinksController = require( './ReplyLinksController.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' ),
|
2022-03-01 14:25:27 +00:00
|
|
|
highlighter = require( './highlighter.js' ),
|
2022-03-01 15:54:52 +00:00
|
|
|
topicSubscriptions = require( './topicsubscriptions.js' ),
|
2022-10-11 12:30:37 +00:00
|
|
|
mobile = require( './mobile.js' ),
|
2022-03-01 15:04:24 +00:00
|
|
|
pageHandlersSetup = false,
|
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
|
|
|
|
2021-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Get an MW API instance
|
|
|
|
*
|
|
|
|
* @return {mw.Api} API instance
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
2022-05-12 01:38:31 +00:00
|
|
|
function getPageData( pageName, oldId ) {
|
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;
|
2022-05-12 01:38:31 +00:00
|
|
|
if ( oldId ) {
|
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( {
|
2022-02-04 23:21:29 +00:00
|
|
|
action: 'discussiontoolspageinfo',
|
2020-09-16 11:19:42 +00:00
|
|
|
page: pageName,
|
|
|
|
oldid: oldId
|
|
|
|
} ).then( function ( response ) {
|
2022-02-04 23:21:29 +00:00
|
|
|
return OO.getProp( response, 'discussiontoolspageinfo', 'transcludedfrom' ) || {};
|
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 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-20 17:06:24 +00:00
|
|
|
* Check if a given thread item 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-12-20 17:06:24 +00:00
|
|
|
* @param {ThreadItem} threadItem Thread item
|
2021-06-09 23:22:28 +00:00
|
|
|
* @return {jQuery.Promise} Resolved with a CommentDetails object if the comment appears on the page.
|
2020-08-19 20:03:41 +00:00
|
|
|
* 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-12-20 17:06:24 +00:00
|
|
|
function checkThreadItemOnPage( pageName, oldId, threadItem ) {
|
|
|
|
var isNewTopic = threadItem.id === utils.NEW_TOPIC_COMMENT_ID;
|
2021-03-24 18:41:39 +00:00
|
|
|
|
2022-05-12 01:38:31 +00:00
|
|
|
return getPageData( pageName, oldId )
|
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-12-20 17:06:24 +00:00
|
|
|
// First look for data by the thread item's ID. If not found, also look by name.
|
2021-04-07 20:46:36 +00:00
|
|
|
// Data by ID may not be found due to differences in headings (e.g. T273413, T275821),
|
2021-12-20 17:06:24 +00:00
|
|
|
// or if a thread item's parent changes.
|
|
|
|
// Data by name might be combined from two or more thread items, which would only allow us to
|
2021-04-07 20:46:36 +00:00
|
|
|
// treat them both as transcluded from unknown source, unless we check ID first.
|
2021-12-20 17:06:24 +00:00
|
|
|
var isTranscludedFrom = transcludedFrom[ threadItem.id ];
|
2021-12-16 00:23:58 +00:00
|
|
|
if ( isTranscludedFrom === undefined ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
isTranscludedFrom = transcludedFrom[ threadItem.name ];
|
2021-12-16 00:23:58 +00:00
|
|
|
}
|
2021-03-24 18:41:39 +00:00
|
|
|
if ( isTranscludedFrom === undefined ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
// The thread item wasn't found when generating the "transcludedfrom" data,
|
2021-03-24 18:41:39 +00:00
|
|
|
// 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',
|
2022-02-02 22:41:41 +00:00
|
|
|
html: mw.message( 'discussiontools-error-comment-disappeared' ).parse() +
|
|
|
|
'<br>' +
|
|
|
|
mw.message( 'discussiontools-error-comment-disappeared-reload' ).parse()
|
2021-03-24 18:41:39 +00:00
|
|
|
} ] } ).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();
|
2022-05-12 23:49:55 +00:00
|
|
|
} else if ( metadata.canEdit ) {
|
|
|
|
// If the user can edit, advise them to use the edit button
|
2021-03-24 18:41:39 +00:00
|
|
|
transcludedErrMsg = mw.message(
|
|
|
|
'discussiontools-error-comment-is-transcluded',
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-edit' ).text()
|
|
|
|
).parse();
|
2022-05-12 23:49:55 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, tell them why they can't edit
|
|
|
|
transcludedErrMsg = metadata.notices[ 'permissions-error' ];
|
2021-03-24 18:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-03 22:32:35 +00:00
|
|
|
return new CommentDetails( pageName, oldId, metadata.notices );
|
2020-02-15 04:03:18 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Get a promise which resolves with editor checkbox data
|
|
|
|
*
|
|
|
|
* @param {string} pageName Page title
|
|
|
|
* @param {number} oldId Revision ID
|
|
|
|
* @return {jQuery.Promise} See ve.init.mw.ArticleTargetLoader#createCheckboxFields
|
|
|
|
*/
|
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;
|
2021-09-21 09:41:32 +00:00
|
|
|
// Override the label with a more verbose one to distinguish this from topic subscriptions (T290712)
|
|
|
|
checkboxesDef.wpWatchthis[ 'label-message' ] = 'discussiontools-replywidget-watchthis';
|
2020-03-04 16:19:19 +00:00
|
|
|
}
|
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-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Initialize Discussion Tools features
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container Page container
|
|
|
|
* @param {Object<string,Mixed>} [state] Page state data object
|
|
|
|
* @param {string} [state.repliedTo] The comment ID that was just replied to
|
|
|
|
*/
|
2019-11-13 13:57:57 +00:00
|
|
|
function init( $container, state ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
var
|
|
|
|
activeCommentId = null,
|
2021-01-25 22:21:34 +00:00
|
|
|
activeController = null,
|
2020-09-16 12:07:27 +00:00
|
|
|
// Loads later to avoid circular dependency
|
|
|
|
CommentController = require( './CommentController.js' ),
|
2022-02-19 06:31:34 +00:00
|
|
|
NewTopicController = require( './NewTopicController.js' );
|
2019-11-05 13:55:01 +00:00
|
|
|
|
2022-12-01 20:48:59 +00:00
|
|
|
// We may be re-initializing after posting a new comment, so clear the cache, because
|
|
|
|
// wgCurRevisionId will not change if we posted to a transcluded page (T266275).
|
|
|
|
// This also applies when another tool has posted a comment and reloaded page contents (T323661).
|
|
|
|
pageDataCache = {};
|
|
|
|
|
2020-12-15 19:45:05 +00:00
|
|
|
// Lazy-load postEdit module, may be required later (on desktop)
|
|
|
|
mw.loader.using( 'mediawiki.action.view.postEdit' );
|
|
|
|
|
2019-11-05 13:55:01 +00:00
|
|
|
$pageContainer = $container;
|
2021-06-24 16:21:31 +00:00
|
|
|
linksController = new ReplyLinksController( $pageContainer );
|
2022-02-19 06:31:34 +00:00
|
|
|
var parser = new Parser( require( './parser/data.json' ) );
|
2019-11-24 15:39:52 +00:00
|
|
|
|
2022-10-12 15:22:50 +00:00
|
|
|
var commentNodes = $pageContainer[ 0 ].querySelectorAll( '[data-mw-thread-id]' );
|
|
|
|
pageThreads = ThreadItemSet.static.newFromJSON( mw.config.get( 'wgDiscussionToolsPageThreads' ) || [], $pageContainer[ 0 ], parser );
|
2020-10-21 15:52:04 +00:00
|
|
|
|
2021-10-13 19:59:47 +00:00
|
|
|
if ( featuresEnabled.topicsubscription ) {
|
2022-06-20 17:02:12 +00:00
|
|
|
topicSubscriptions.initTopicSubscriptions( $container, pageThreads );
|
2021-02-17 22:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 12:30:37 +00:00
|
|
|
if ( OO.ui.isMobile() && mw.config.get( 'skin' ) === 'minerva' ) {
|
|
|
|
mobile.init( $container );
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Setup comment controllers for each comment, and the new topic controller
|
|
|
|
*
|
|
|
|
* @param {string} commentId Comment ID, or NEW_TOPIC_COMMENT_ID constant
|
|
|
|
* @param {jQuery} $link Add section link for new topic controller
|
|
|
|
* @param {string} [mode] Optionally force a mode, 'visual' or 'source'
|
|
|
|
* @param {boolean} [hideErrors] Suppress errors, e.g. when restoring auto-save
|
2022-02-22 23:10:43 +00:00
|
|
|
* @param {boolean} [suppressNotifications] Don't notify the user if recovering auto-save
|
2021-12-20 15:23:46 +00:00
|
|
|
*/
|
2022-02-22 23:10:43 +00:00
|
|
|
function setupController( commentId, $link, mode, hideErrors, suppressNotifications ) {
|
2021-08-05 19:05:29 +00:00
|
|
|
var commentController, $addSectionLink;
|
2022-02-02 16:18:01 +00:00
|
|
|
|
2021-06-24 16:21:31 +00:00
|
|
|
if ( commentId === utils.NEW_TOPIC_COMMENT_ID ) {
|
2021-08-05 19:05:29 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2022-12-05 23:56:09 +00:00
|
|
|
$addSectionLink = $( '#ca-addsection' ).find( 'a' );
|
2021-08-05 19:05:29 +00:00
|
|
|
// When opening new topic tool using any link, always activate the link in page tabs too
|
|
|
|
$link = $link.add( $addSectionLink );
|
2022-03-01 15:04:24 +00:00
|
|
|
commentController = new NewTopicController( $pageContainer, pageThreads );
|
2021-06-24 16:21:31 +00:00
|
|
|
} else {
|
2022-03-01 15:04:24 +00:00
|
|
|
commentController = new CommentController( $pageContainer, pageThreads.findCommentById( commentId ), pageThreads );
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeCommentId = commentId;
|
|
|
|
activeController = commentController;
|
|
|
|
linksController.setActiveLink( $link );
|
|
|
|
|
2022-02-02 16:15:50 +00:00
|
|
|
commentController.on( 'teardown', function ( teardownMode ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
activeCommentId = null;
|
|
|
|
activeController = null;
|
2022-02-02 16:18:01 +00:00
|
|
|
|
|
|
|
if ( teardownMode !== 'refresh' ) {
|
|
|
|
linksController.clearActiveLink();
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
|
2022-02-02 16:15:50 +00:00
|
|
|
if ( teardownMode === 'abandoned' ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
linksController.focusLink( $link );
|
|
|
|
}
|
|
|
|
} );
|
2022-02-02 16:18:01 +00:00
|
|
|
commentController.on( 'reloadPage', function () {
|
2022-03-08 23:09:30 +00:00
|
|
|
mw.dt.initState.newCommentIds = commentController.newComments.map( function ( cmt ) {
|
|
|
|
return cmt.id;
|
|
|
|
} );
|
2022-02-02 16:18:01 +00:00
|
|
|
// Teardown active reply widget(s)
|
|
|
|
commentController.replyWidgetPromise.then( function ( replyWidget ) {
|
|
|
|
lastControllerScrollOffset = $( commentController.newListItem ).offset().top;
|
|
|
|
replyWidget.teardown( 'refresh' );
|
2022-02-08 15:47:28 +00:00
|
|
|
// Only fetch the last now "good" revision, on which we know the parent comment still exists.
|
|
|
|
// As we poll frequently, this will almost always be the lastet revision.
|
|
|
|
refreshPageContents( commentController.oldId );
|
2022-02-02 16:18:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2021-06-24 16:21:31 +00:00
|
|
|
|
2022-02-22 23:10:43 +00:00
|
|
|
commentController.setup( mode, hideErrors, suppressNotifications );
|
2022-02-02 16:18:01 +00:00
|
|
|
if ( lastControllerScrollOffset ) {
|
|
|
|
$( document.documentElement ).scrollTop(
|
|
|
|
$( document.documentElement ).scrollTop() +
|
|
|
|
( $( commentController.newListItem ).offset().top - lastControllerScrollOffset )
|
|
|
|
);
|
|
|
|
lastControllerScrollOffset = null;
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2021-06-24 16:21:31 +00:00
|
|
|
linksController.on( 'link-click', function ( commentId, $link ) {
|
|
|
|
// 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 ( activeCommentId === commentId ) {
|
|
|
|
activeController.showAndFocus();
|
|
|
|
return;
|
|
|
|
}
|
2021-01-25 22:21:34 +00:00
|
|
|
|
2021-06-24 16:21:31 +00:00
|
|
|
// If this is a new topic link, and a reply widget is open, attempt to close it first.
|
2021-11-08 19:03:40 +00:00
|
|
|
var teardownPromise;
|
2021-06-24 16:21:31 +00:00
|
|
|
if ( activeController && commentId === utils.NEW_TOPIC_COMMENT_ID ) {
|
2021-11-08 19:03:40 +00:00
|
|
|
teardownPromise = activeController.replyWidget.tryTeardown();
|
2021-06-24 16:21:31 +00:00
|
|
|
} else {
|
2021-11-08 19:03:40 +00:00
|
|
|
teardownPromise = $.Deferred().resolve();
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
2021-01-25 22:41:35 +00:00
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
teardownPromise.then( function () {
|
2021-01-25 22:21:34 +00:00
|
|
|
// If another reply widget is open (or opening), do nothing.
|
|
|
|
if ( activeController ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
setupController( commentId, $link );
|
2021-01-25 22:21:34 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-06-24 16:21:31 +00:00
|
|
|
// Restore autosave
|
2021-11-08 19:03:40 +00:00
|
|
|
( function () {
|
2022-10-10 20:56:52 +00:00
|
|
|
if ( mw.config.get( 'wgAction' ) !== 'view' ) {
|
|
|
|
// Don't do anything when we're editing/previewing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
var mode, $link;
|
2022-03-01 15:04:24 +00:00
|
|
|
for ( var i = 0; i < pageThreads.threadItems.length; i++ ) {
|
|
|
|
var comment = pageThreads.threadItems[ i ];
|
2021-11-08 19:03:40 +00:00
|
|
|
if ( storage.get( 'reply/' + comment.id + '/saveable' ) ) {
|
|
|
|
mode = storage.get( 'reply/' + comment.id + '/mode' );
|
|
|
|
$link = $( commentNodes[ i ] );
|
2022-02-22 23:10:43 +00:00
|
|
|
setupController( comment.id, $link, mode, true, !state.firstLoad );
|
2021-11-08 19:03:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
2021-11-08 19:03:40 +00:00
|
|
|
if ( storage.get( 'reply/' + utils.NEW_TOPIC_COMMENT_ID + '/saveable' ) ) {
|
|
|
|
mode = storage.get( 'reply/' + utils.NEW_TOPIC_COMMENT_ID + '/mode' );
|
2022-02-22 23:10:43 +00:00
|
|
|
setupController( utils.NEW_TOPIC_COMMENT_ID, $( [] ), mode, true, !state.firstLoad );
|
2021-11-08 19:03:40 +00:00
|
|
|
} else if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) {
|
|
|
|
setupController( utils.NEW_TOPIC_COMMENT_ID, $( [] ) );
|
|
|
|
}
|
|
|
|
}() );
|
2021-06-24 16:21:31 +00:00
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
// For debugging (now unused in the code)
|
2022-03-01 15:04:24 +00:00
|
|
|
mw.dt.pageThreads = pageThreads;
|
2019-11-05 13:55:01 +00:00
|
|
|
|
2021-11-18 14:56:55 +00:00
|
|
|
var promise = OO.ui.isMobile() && mw.loader.getState( 'mobile.init' ) ?
|
2021-06-17 15:37:05 +00:00
|
|
|
mw.loader.using( 'mobile.init' ) :
|
|
|
|
$.Deferred().resolve().promise();
|
|
|
|
|
|
|
|
promise.then( function () {
|
2022-03-01 14:44:21 +00:00
|
|
|
if ( state.repliedTo ) {
|
2022-03-01 15:54:52 +00:00
|
|
|
highlighter.highlightPublishedComment( pageThreads, state.repliedTo );
|
2021-06-17 15:37:05 +00:00
|
|
|
|
2022-03-01 14:44:21 +00:00
|
|
|
if ( state.repliedTo === utils.NEW_TOPIC_COMMENT_ID ) {
|
2021-06-17 15:37:05 +00:00
|
|
|
mw.hook( 'postEdit' ).fire( {
|
2022-03-01 14:44:21 +00:00
|
|
|
message: mw.msg( 'discussiontools-postedit-confirmation-topicadded', mw.user )
|
2021-06-17 15:37:05 +00:00
|
|
|
} );
|
2022-03-01 14:44:21 +00:00
|
|
|
} else {
|
|
|
|
if ( OO.ui.isMobile() ) {
|
|
|
|
mw.notify( mw.msg( 'discussiontools-postedit-confirmation-published', mw.user ) );
|
|
|
|
} else {
|
|
|
|
// postEdit is currently desktop only
|
|
|
|
mw.hook( 'postEdit' ).fire( {
|
|
|
|
message: mw.msg( 'discussiontools-postedit-confirmation-published', mw.user )
|
|
|
|
} );
|
2021-06-17 15:37:05 +00:00
|
|
|
}
|
2022-03-01 14:44:21 +00:00
|
|
|
}
|
2022-03-08 23:09:30 +00:00
|
|
|
} else if ( state.newCommentIds ) {
|
|
|
|
highlighter.highlightNewComments( pageThreads, true, state.newCommentIds );
|
2021-06-17 15:37:05 +00:00
|
|
|
}
|
2021-04-29 18:03:37 +00:00
|
|
|
|
2021-11-18 14:56:29 +00:00
|
|
|
// Check topic subscription states if the user has automatic subscriptions enabled
|
|
|
|
// and has recently edited this page.
|
|
|
|
if ( featuresEnabled.autotopicsub && mw.user.options.get( 'discussiontools-autotopicsub' ) ) {
|
2022-03-01 15:54:52 +00:00
|
|
|
topicSubscriptions.updateAutoSubscriptionStates( $container, pageThreads, state.repliedTo );
|
2021-08-19 20:35:32 +00:00
|
|
|
}
|
2021-11-18 14:56:29 +00:00
|
|
|
} );
|
2021-08-19 20:35:32 +00:00
|
|
|
|
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
|
|
|
);
|
2021-04-29 18:03:37 +00:00
|
|
|
|
2022-03-01 15:04:24 +00:00
|
|
|
// Page-level handlers only need to be setup once
|
|
|
|
if ( !pageHandlersSetup ) {
|
|
|
|
$( window ).on( 'popstate', function () {
|
2022-06-20 20:48:24 +00:00
|
|
|
// Delay with setTimeout() because "the Document's target element" (corresponding to the :target
|
|
|
|
// selector in CSS) is not yet updated to match the URL when handling a 'popstate' event.
|
|
|
|
setTimeout( function () {
|
|
|
|
highlighter.highlightTargetComment( pageThreads, true );
|
|
|
|
} );
|
2022-03-01 15:04:24 +00:00
|
|
|
} );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( 'body' ).on( 'click', function ( e ) {
|
|
|
|
if ( utils.isUnmodifiedLeftClick( e ) ) {
|
|
|
|
highlighter.clearHighlightTargetComment( pageThreads );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
pageHandlersSetup = true;
|
|
|
|
}
|
2022-02-23 22:41:47 +00:00
|
|
|
if ( state.firstLoad ) {
|
|
|
|
highlighter.highlightTargetComment( pageThreads );
|
|
|
|
}
|
2019-11-05 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 23:23:14 +00:00
|
|
|
/**
|
|
|
|
* Update the contents of the page with the data from an action=parse API response.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container Page container
|
|
|
|
* @param {Object} data Data from action=parse API
|
|
|
|
*/
|
|
|
|
function updatePageContents( $container, data ) {
|
2022-09-28 23:23:13 +00:00
|
|
|
$container.find( '.mw-parser-output' ).first().replaceWith( data.parse.text );
|
2022-04-21 14:29:56 +00:00
|
|
|
|
2022-12-09 22:32:54 +00:00
|
|
|
mw.util.clearSubtitle();
|
|
|
|
mw.util.addSubtitle( data.parse.subtitle );
|
2022-04-21 14:29:56 +00:00
|
|
|
|
2022-08-24 12:10:11 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
if ( $( '#catlinks' ).length ) {
|
|
|
|
var $categories = $( $.parseHTML( data.parse.categorieshtml ) );
|
|
|
|
mw.hook( 'wikipage.categories' ).fire( $categories );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#catlinks' ).replaceWith( $categories );
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:23:14 +00:00
|
|
|
mw.config.set( data.parse.jsconfigvars );
|
|
|
|
mw.loader.load( data.parse.modulestyles );
|
|
|
|
mw.loader.load( data.parse.modules );
|
2022-02-02 16:43:44 +00:00
|
|
|
|
|
|
|
mw.config.set( {
|
|
|
|
wgCurRevisionId: data.parse.revid,
|
|
|
|
wgRevisionId: data.parse.revid
|
|
|
|
} );
|
|
|
|
|
2022-08-24 12:10:11 +00:00
|
|
|
// TODO: update displaytitle, lastmodified
|
2022-02-01 23:23:14 +00:00
|
|
|
// 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???
|
|
|
|
|
|
|
|
// 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( $container );
|
|
|
|
// 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 );
|
2022-06-03 01:44:05 +00:00
|
|
|
|
|
|
|
mw.hook( 'wikipage.tableOfContents' ).fire(
|
|
|
|
data.parse.showtoc ? data.parse.sections : []
|
|
|
|
);
|
2022-08-22 11:15:34 +00:00
|
|
|
|
|
|
|
// Copied from ve.init.mw.DesktopArticleTarget.prototype.saveComplete
|
|
|
|
// TODO: Upstream this to core/skins, triggered by a hook (wikipage.content?)
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2022-12-05 23:56:09 +00:00
|
|
|
$( '#t-permalink' ).add( '#coll-download-as-rl' ).find( 'a' ).each( function () {
|
2022-08-25 15:11:25 +00:00
|
|
|
var permalinkUrl = new URL( this.href );
|
|
|
|
permalinkUrl.searchParams.set( 'oldid', data.parse.revid );
|
|
|
|
$( this ).attr( 'href', permalinkUrl.toString() );
|
2022-08-22 11:15:34 +00:00
|
|
|
} );
|
2022-08-25 15:11:25 +00:00
|
|
|
|
|
|
|
var url = new URL( location.href );
|
|
|
|
url.searchParams.delete( 'oldid' );
|
|
|
|
|
|
|
|
// If there are any other query parameters left, re-use that URL object.
|
|
|
|
// Otherwise use the canonical style view url (T44553, T102363).
|
|
|
|
var keys = [];
|
|
|
|
url.searchParams.forEach( function ( val, key ) {
|
|
|
|
keys.push( key );
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !keys.length || ( keys.length === 1 && keys[ 0 ] === 'title' ) ) {
|
|
|
|
var viewUrl = new URL( mw.util.getUrl( mw.config.get( 'wgRelevantPageName' ) ), document.baseURI );
|
|
|
|
viewUrl.hash = location.hash;
|
|
|
|
history.pushState( null, '', viewUrl );
|
|
|
|
} else {
|
|
|
|
history.pushState( null, '', url );
|
|
|
|
}
|
2022-02-01 23:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the latest revision of the page and display its contents.
|
|
|
|
*
|
2022-02-08 15:47:28 +00:00
|
|
|
* @param {number} [oldId] Revision ID to fetch, latest if not specified
|
2022-02-01 23:23:14 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves when the refresh is complete
|
|
|
|
*/
|
2022-02-08 15:47:28 +00:00
|
|
|
function refreshPageContents( oldId ) {
|
2022-08-24 12:05:07 +00:00
|
|
|
// This should approximately match the API call in ApiVisualEditorEditor#parseWikitext
|
2022-02-01 23:23:14 +00:00
|
|
|
return getApi().get( {
|
|
|
|
action: 'parse',
|
|
|
|
// 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' ),
|
2022-06-27 13:40:41 +00:00
|
|
|
mobileformat: OO.ui.isMobile(),
|
2022-02-01 23:23:14 +00:00
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
|
|
|
// 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',
|
2022-08-24 12:05:07 +00:00
|
|
|
prop: [ 'text', 'revid', 'categorieshtml', 'sections', 'displaytitle', 'subtitle', 'modules', 'jsconfigvars' ],
|
2022-02-08 15:47:28 +00:00
|
|
|
page: !oldId ? mw.config.get( 'wgRelevantPageName' ) : undefined,
|
|
|
|
oldid: oldId || undefined
|
2022-02-01 23:23:14 +00:00
|
|
|
} ).then( function ( parseResp ) {
|
|
|
|
updatePageContents( $pageContainer, parseResp );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Update the page after a comment is published/saved
|
|
|
|
*
|
|
|
|
* @param {Object} data Edit API response data
|
2021-12-20 17:06:24 +00:00
|
|
|
* @param {ThreadItem} threadItem Parent thread item
|
2021-12-20 15:23:46 +00:00
|
|
|
* @param {string} pageName Page title
|
|
|
|
* @param {mw.dt.ReplyWidget} replyWidget ReplyWidget
|
|
|
|
*/
|
2021-12-20 17:06:24 +00:00
|
|
|
function update( data, threadItem, pageName, replyWidget ) {
|
2022-04-06 16:37:29 +00:00
|
|
|
function logSaveSuccess() {
|
|
|
|
logger( {
|
|
|
|
action: 'saveSuccess',
|
|
|
|
timing: mw.now() - replyWidget.saveInitiated,
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
revision_id: data.newrevid
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-07-29 06:12:10 +00:00
|
|
|
var pageExists = !!mw.config.get( 'wgRelevantArticleId' );
|
|
|
|
if ( !pageExists ) {
|
|
|
|
// The page didn't exist before this update, so reload it. We'd handle
|
|
|
|
// setting up the content just fine (assuming there's a
|
|
|
|
// mw-parser-output), but fixing up the UI tabs/behavior is outside
|
|
|
|
// our scope.
|
2021-08-06 00:31:50 +00:00
|
|
|
replyWidget.unbindBeforeUnloadHandler();
|
|
|
|
replyWidget.clearStorage();
|
|
|
|
replyWidget.setPending( true );
|
2021-12-20 17:06:24 +00:00
|
|
|
window.location = mw.util.getUrl( pageName, { dtrepliedto: threadItem.id } );
|
2022-04-06 16:37:29 +00:00
|
|
|
logSaveSuccess();
|
2021-07-29 06:12:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-06 00:31:50 +00:00
|
|
|
replyWidget.teardown();
|
|
|
|
linksController.teardown();
|
|
|
|
linksController = null;
|
|
|
|
// TODO: Tell controller to teardown all other open widgets
|
|
|
|
|
2022-02-01 23:23:14 +00:00
|
|
|
// Highlight the new reply after re-initializing
|
|
|
|
mw.dt.initState.repliedTo = threadItem.id;
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
// Update page state
|
2022-02-01 23:23:14 +00:00
|
|
|
var pageUpdated = $.Deferred();
|
2020-08-19 20:03:41 +00:00
|
|
|
if ( pageName === mw.config.get( 'wgRelevantPageName' ) ) {
|
|
|
|
// We can use the result from the VisualEditor API
|
2022-02-01 23:23:14 +00:00
|
|
|
updatePageContents( $pageContainer, {
|
|
|
|
parse: {
|
|
|
|
text: data.content,
|
2022-04-21 14:29:56 +00:00
|
|
|
subtitle: data.contentSub,
|
2022-08-24 12:10:11 +00:00
|
|
|
categorieshtml: data.categorieshtml,
|
2022-02-01 23:23:14 +00:00
|
|
|
jsconfigvars: data.jsconfigvars,
|
2022-02-02 16:43:44 +00:00
|
|
|
revid: data.newrevid,
|
2022-02-01 23:23:14 +00:00
|
|
|
// Note: VE API merges 'modules' and 'modulestyles'
|
|
|
|
modules: data.modules,
|
2022-06-03 01:44:05 +00:00
|
|
|
modulestyles: [],
|
|
|
|
// Note: VE API drops 'showtoc' and changes 'sections' depending on it
|
|
|
|
showtoc: true,
|
|
|
|
sections: data.sections
|
2022-02-01 23:23:14 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
mw.config.set( {
|
|
|
|
wgCurRevisionId: data.newrevid,
|
|
|
|
wgRevisionId: data.newrevid
|
|
|
|
} );
|
|
|
|
|
|
|
|
pageUpdated.resolve();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// We saved to another page, we must purge and then fetch the current page
|
2022-02-01 23:23:14 +00:00
|
|
|
var api = getApi();
|
2020-08-19 20:03:41 +00:00
|
|
|
api.post( {
|
|
|
|
action: 'purge',
|
|
|
|
titles: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} ).then( function () {
|
2022-02-01 23:23:14 +00:00
|
|
|
return refreshPageContents();
|
|
|
|
} ).then( function () {
|
2020-08-19 20:03:41 +00:00
|
|
|
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…
|
2021-12-20 17:06:24 +00:00
|
|
|
window.location = mw.util.getUrl( pageName, { dtrepliedto: threadItem.id } );
|
2022-04-06 16:37:29 +00:00
|
|
|
// We're confident the saving portion succeeded, so still log this:
|
|
|
|
logSaveSuccess();
|
2020-08-19 20:03:41 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-01-10 22:30:09 +00:00
|
|
|
watch.updateWatchLink(
|
|
|
|
mw.Title.newFromText( pageName ),
|
|
|
|
data.watched ? 'unwatch' : 'watch',
|
|
|
|
'idle',
|
2020-08-28 14:50:42 +00:00
|
|
|
data.watchlistexpiry
|
2020-08-19 20:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:37:29 +00:00
|
|
|
pageUpdated.then( logSaveSuccess );
|
2020-08-19 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:01 +00:00
|
|
|
module.exports = {
|
|
|
|
init: init,
|
2020-08-19 20:03:41 +00:00
|
|
|
update: update,
|
2022-02-01 23:23:14 +00:00
|
|
|
updatePageContents: updatePageContents,
|
|
|
|
refreshPageContents: refreshPageContents,
|
2021-12-20 17:06:24 +00:00
|
|
|
checkThreadItemOnPage: checkThreadItemOnPage,
|
2020-10-23 11:02:18 +00:00
|
|
|
getCheckboxesPromise: getCheckboxesPromise,
|
2022-02-01 22:55:57 +00:00
|
|
|
getApi: getApi,
|
|
|
|
storage: storage
|
2019-11-05 13:55:01 +00:00
|
|
|
};
|