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' ) || {},
|
2023-06-13 22:55:08 +00:00
|
|
|
MemoryStorage = require( './MemoryStorage.js' ),
|
|
|
|
STORAGE_EXPIRY = 60 * 60 * 24 * 30,
|
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-29 15:35:37 +00:00
|
|
|
HeadingItem = require( './HeadingItem.js' ),
|
2021-06-24 16:21:31 +00:00
|
|
|
ReplyLinksController = require( './ReplyLinksController.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' ),
|
2023-10-05 09:55:00 +00:00
|
|
|
permalinks = require( './permalinks.js' ),
|
2022-03-01 15:04:24 +00:00
|
|
|
pageHandlersSetup = false,
|
2023-05-21 06:57:49 +00:00
|
|
|
pageDataCache = {},
|
|
|
|
defaultEditMode = mw.user.options.get( 'discussiontools-editmode' ) || mw.config.get( 'wgDiscussionToolsFallbackEditMode' ),
|
|
|
|
defaultVisual = defaultEditMode === 'visual',
|
2023-10-11 12:04:39 +00:00
|
|
|
enable2017Wikitext = featuresEnabled.sourcemodetoolbar,
|
|
|
|
overflowMenu = require( './overflowMenu.js' );
|
2019-11-05 13:55:01 +00:00
|
|
|
|
2023-04-13 23:27:17 +00:00
|
|
|
var mobile = null;
|
|
|
|
if ( OO.ui.isMobile() && mw.config.get( 'skin' ) === 'minerva' ) {
|
|
|
|
mobile = require( './mobile.js' );
|
|
|
|
}
|
|
|
|
|
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
|
2021-06-29 15:35:37 +00:00
|
|
|
* @param {Object} [apiParams] Additional parameters for the API
|
2020-02-15 04:03:18 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
2021-06-29 15:35:37 +00:00
|
|
|
function getPageData( pageName, oldId, apiParams ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var api = getApi();
|
2021-06-29 15:35:37 +00:00
|
|
|
apiParams = apiParams || {};
|
2020-10-23 11:02:18 +00:00
|
|
|
|
2020-02-15 04:03:18 +00:00
|
|
|
pageDataCache[ pageName ] = pageDataCache[ pageName ] || {};
|
2021-06-29 15:35:37 +00:00
|
|
|
if ( pageDataCache[ pageName ][ oldId ] && $.isEmptyObject( apiParams ) ) {
|
2020-02-15 04:03:18 +00:00
|
|
|
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-06-29 15:35:37 +00:00
|
|
|
var veMetadataPromise = api.get( $.extend( {
|
2020-08-19 20:03:41 +00:00
|
|
|
action: 'visualeditor',
|
|
|
|
paction: 'metadata',
|
|
|
|
page: pageName
|
2021-06-29 15:35:37 +00:00
|
|
|
}, apiParams ) ).then( function ( response ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
return OO.getProp( response, 'visualeditor' ) || [];
|
|
|
|
} );
|
|
|
|
|
2021-06-29 15:35:37 +00:00
|
|
|
var promise = $.when( lintPromise, transcludedFromPromise, veMetadataPromise )
|
2020-08-19 20:03:41 +00:00
|
|
|
.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
|
|
|
} );
|
2021-06-29 15:35:37 +00:00
|
|
|
|
|
|
|
if ( $.isEmptyObject( apiParams ) ) {
|
|
|
|
pageDataCache[ pageName ][ oldId ] = promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
2020-02-15 04:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-06-29 15:35:37 +00:00
|
|
|
var defaultMode = mw.user.options.get( 'discussiontools-editmode' ) || mw.config.get( 'wgDiscussionToolsFallbackEditMode' );
|
|
|
|
var apiParams = null;
|
|
|
|
if ( isNewTopic ) {
|
|
|
|
apiParams = {
|
|
|
|
section: 'new',
|
|
|
|
editintro: threadItem.editintro,
|
|
|
|
preload: threadItem.preload,
|
|
|
|
preloadparams: threadItem.preloadparams,
|
|
|
|
paction: defaultMode === 'source' ? 'wikitext' : 'parse'
|
|
|
|
};
|
|
|
|
}
|
2021-03-24 18:41:39 +00:00
|
|
|
|
2021-06-29 15:35:37 +00:00
|
|
|
return getPageData( pageName, oldId, apiParams )
|
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
|
|
|
}
|
|
|
|
|
2023-04-27 22:44:33 +00:00
|
|
|
return new CommentDetails( pageName, oldId, metadata.notices, metadata.wouldautocreate, metadata.content, defaultMode );
|
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
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-05-21 06:57:49 +00:00
|
|
|
/**
|
|
|
|
* Get the resourceloader modules required for a mode of the reply widget
|
|
|
|
*
|
|
|
|
* @return {string[]}
|
|
|
|
*/
|
2023-10-13 10:58:12 +00:00
|
|
|
function getReplyWidgetModules() {
|
2023-05-21 06:57:49 +00:00
|
|
|
var veConf = mw.config.get( 'wgVisualEditorConfig' ),
|
2023-10-13 10:58:12 +00:00
|
|
|
modules = [ 'ext.discussionTools.ReplyWidget' ]
|
2023-05-21 06:57:49 +00:00
|
|
|
.concat( veConf.pluginModules.filter( mw.loader.getState ) );
|
|
|
|
|
|
|
|
if ( OO.ui.isMobile() ) {
|
2023-10-13 10:58:12 +00:00
|
|
|
modules = [
|
2023-05-21 06:57:49 +00:00
|
|
|
'ext.visualEditor.core.mobile',
|
|
|
|
'ext.visualEditor.mwextensions'
|
2023-10-13 10:58:12 +00:00
|
|
|
].concat( modules );
|
2023-05-21 06:57:49 +00:00
|
|
|
} else {
|
2023-10-13 10:58:12 +00:00
|
|
|
modules = [
|
2023-05-21 06:57:49 +00:00
|
|
|
'ext.visualEditor.core.desktop',
|
|
|
|
'ext.visualEditor.desktopTarget',
|
|
|
|
'ext.visualEditor.mwextensions.desktop'
|
2023-10-13 10:58:12 +00:00
|
|
|
].concat( modules );
|
2023-05-21 06:57:49 +00:00
|
|
|
}
|
2023-10-13 10:58:12 +00:00
|
|
|
return modules;
|
2023-05-21 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-08-24 15:36:44 +00:00
|
|
|
* @param {boolean} [state.tempUserCreated] Whether a temp user was just created
|
2021-12-20 15:23:46 +00:00
|
|
|
*/
|
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' );
|
|
|
|
|
2023-01-24 15:34:35 +00:00
|
|
|
if ( linksController ) {
|
|
|
|
linksController.teardown();
|
|
|
|
linksController = null;
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:01 +00:00
|
|
|
$pageContainer = $container;
|
2021-06-24 16:21:31 +00:00
|
|
|
linksController = new ReplyLinksController( $pageContainer );
|
2023-10-05 09:55:00 +00:00
|
|
|
permalinks.init( $pageContainer );
|
2023-01-05 22:47:36 +00:00
|
|
|
|
|
|
|
linksController.on( 'link-interact', function () {
|
|
|
|
// Preload page metadata when the user is about to use a link, to make the tool load faster.
|
|
|
|
// NOTE: As of January 2023, this is an EXPENSIVE API CALL. It must not be done on every
|
|
|
|
// pageview, as that would generate enough load to take down Wikimedia sites (T325477).
|
|
|
|
// It would be barely acceptable to do it on every *discussion* pageview, but we're trying
|
|
|
|
// to be better and only do it when really needed (T325598).
|
|
|
|
getPageData(
|
|
|
|
mw.config.get( 'wgRelevantPageName' ),
|
|
|
|
mw.config.get( 'wgCurRevisionId' )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-11 12:04:39 +00:00
|
|
|
overflowMenu.init( $container, pageThreads );
|
|
|
|
|
2023-04-13 23:27:17 +00:00
|
|
|
if ( mobile ) {
|
2023-09-28 11:00:36 +00:00
|
|
|
mobile.init( $container, pageThreads );
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 06:57:49 +00:00
|
|
|
if ( linksController.pageHasReplyLinks() || linksController.pageHasNewTopicLink() ) {
|
|
|
|
// Start loading reply widget code
|
|
|
|
// The worst-case here is that we might be on a page with no comments
|
|
|
|
// and the add-topic link suppressed, *but* which has valid links to
|
|
|
|
// trigger the new topic tool within the content. If that happens,
|
|
|
|
// the modules will still be loaded when those links are interacted with.
|
2023-10-13 10:58:12 +00:00
|
|
|
mw.loader.using( getReplyWidgetModules() );
|
2023-05-21 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Setup comment controllers for each comment, and the new topic controller
|
|
|
|
*
|
2021-06-29 15:35:37 +00:00
|
|
|
* @param {ThreadItem} comment
|
2021-12-20 15:23:46 +00:00
|
|
|
* @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
|
2023-06-13 22:55:08 +00:00
|
|
|
* @param {MemoryStorage} [storage] Storage object for autosave
|
2021-12-20 15:23:46 +00:00
|
|
|
*/
|
2023-06-13 22:55:08 +00:00
|
|
|
function setupController( comment, $link, mode, hideErrors, suppressNotifications, storage ) {
|
2021-08-05 19:05:29 +00:00
|
|
|
var commentController, $addSectionLink;
|
2022-02-02 16:18:01 +00:00
|
|
|
|
2023-06-13 22:55:08 +00:00
|
|
|
if ( !storage ) {
|
|
|
|
storage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + comment.id, STORAGE_EXPIRY );
|
|
|
|
}
|
2021-06-29 15:35:37 +00:00
|
|
|
if ( comment.id === 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 );
|
2023-06-13 22:55:08 +00:00
|
|
|
commentController = new NewTopicController( $pageContainer, comment, pageThreads, storage );
|
2021-06-24 16:21:31 +00:00
|
|
|
} else {
|
2023-06-13 22:55:08 +00:00
|
|
|
commentController = new CommentController( $pageContainer, comment, pageThreads, storage );
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:35:37 +00:00
|
|
|
activeCommentId = comment.id;
|
2021-06-24 16:21:31 +00:00
|
|
|
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-06-29 15:35:37 +00:00
|
|
|
function newTopicComment( data ) {
|
|
|
|
var comment = new HeadingItem( {}, 2 );
|
|
|
|
comment.id = utils.NEW_TOPIC_COMMENT_ID;
|
|
|
|
comment.isNewTopic = true;
|
|
|
|
$.extend( comment, data );
|
|
|
|
return comment;
|
|
|
|
}
|
|
|
|
|
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-29 15:35:37 +00:00
|
|
|
linksController.on( 'link-click', function ( commentId, $link, data ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
// 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.
|
|
|
|
if ( activeCommentId === commentId ) {
|
|
|
|
activeController.showAndFocus();
|
|
|
|
return;
|
|
|
|
}
|
2021-01-25 22:21:34 +00:00
|
|
|
|
2023-02-06 18:05:07 +00:00
|
|
|
var teardownPromise = $.Deferred().resolve();
|
|
|
|
if ( commentId === utils.NEW_TOPIC_COMMENT_ID ) {
|
|
|
|
// If this is a new topic link, and a reply widget is open, attempt to close it first.
|
|
|
|
if ( activeController ) {
|
2024-01-05 15:51:49 +00:00
|
|
|
teardownPromise = activeController.tryTeardown();
|
2023-02-06 18:05:07 +00:00
|
|
|
} else if ( OO.getProp( window, 've', 'init', 'target', 'tryTeardown' ) ) {
|
|
|
|
// If VE or 2017WTE is open, attempt to close it as well. (T317035#8590357)
|
|
|
|
// FIXME This should be generalized, using some global router or something,
|
|
|
|
// so that we don't try to open while something else is open on full screen.
|
|
|
|
// Another example is the MultimediaViewer extension.
|
|
|
|
teardownPromise = ve.init.target.tryTeardown() || $.Deferred().resolve();
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
2021-01-25 22:41:35 +00:00
|
|
|
|
2023-04-13 23:27:17 +00:00
|
|
|
if ( mobile ) {
|
|
|
|
teardownPromise = teardownPromise.then( function () {
|
|
|
|
return mobile.closeLedeSectionDialog();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
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-29 15:35:37 +00:00
|
|
|
var comment;
|
|
|
|
if ( commentId !== utils.NEW_TOPIC_COMMENT_ID ) {
|
2023-03-09 19:23:32 +00:00
|
|
|
comment = pageThreads.findCommentById( commentId );
|
2021-06-29 15:35:37 +00:00
|
|
|
} else {
|
|
|
|
comment = newTopicComment( data );
|
|
|
|
}
|
2023-11-07 17:59:03 +00:00
|
|
|
if ( comment ) {
|
|
|
|
setupController( comment, $link );
|
|
|
|
} else {
|
|
|
|
// We couldn't find the comment, so express that there's an issue
|
|
|
|
mw.notify( mw.msg( 'discussiontools-error-comment-disappeared' ) );
|
|
|
|
}
|
2021-01-25 22:21:34 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2024-02-23 00:20:27 +00:00
|
|
|
var mobilePromise = OO.ui.isMobile() && mw.loader.getState( 'mobile.init' ) ?
|
|
|
|
mw.loader.using( 'mobile.init' ) :
|
|
|
|
$.Deferred().resolve().promise();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-23 00:20:27 +00:00
|
|
|
for ( let i = 0; i < pageThreads.threadItems.length; i++ ) {
|
|
|
|
const comment = pageThreads.threadItems[ i ];
|
|
|
|
const replyStorage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + comment.id, STORAGE_EXPIRY );
|
|
|
|
if ( replyStorage.get( 'saveable' ) ) {
|
|
|
|
const mode = replyStorage.get( 'mode' );
|
|
|
|
const $link = $( commentNodes[ i ] );
|
|
|
|
// Wait for mobile section toggling code to be ready
|
|
|
|
mobilePromise.then( function () {
|
|
|
|
if ( OO.ui.isMobile() ) {
|
|
|
|
const urlFragment = mw.util.escapeIdForLink( comment.id );
|
|
|
|
// Force the section to expand on mobile (T338920)
|
|
|
|
location.hash = '#' + urlFragment;
|
|
|
|
}
|
|
|
|
// Wait for the 'hashchange' event to be handled by the mobile code
|
|
|
|
setTimeout( function () {
|
|
|
|
setupController( comment, $link, mode, true, !state.firstLoad, replyStorage );
|
|
|
|
} );
|
|
|
|
} );
|
2021-11-08 19:03:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
2024-02-23 00:20:27 +00:00
|
|
|
const newTopicStorage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + utils.NEW_TOPIC_COMMENT_ID, STORAGE_EXPIRY );
|
|
|
|
if ( newTopicStorage.get( 'saveable' ) || newTopicStorage.get( 'title' ) ) {
|
|
|
|
const mode = newTopicStorage.get( 'mode' );
|
|
|
|
setupController( newTopicComment(), $( [] ), mode, true, !state.firstLoad, newTopicStorage );
|
2021-11-08 19:03:40 +00:00
|
|
|
} else if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) {
|
2024-02-23 00:20:27 +00:00
|
|
|
const data = linksController.parseNewTopicLink( location.href );
|
2021-06-29 15:35:37 +00:00
|
|
|
setupController( newTopicComment( data ), $( [] ) );
|
2021-11-08 19:03:40 +00:00
|
|
|
}
|
|
|
|
}() );
|
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
|
|
|
|
2024-02-23 00:20:27 +00:00
|
|
|
mobilePromise.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( {
|
2023-08-24 15:36:44 +00:00
|
|
|
tempUserCreated: state.tempUserCreated,
|
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 {
|
2023-06-10 01:56:04 +00:00
|
|
|
mw.hook( 'postEdit' ).fire( {
|
2023-08-24 15:36:44 +00:00
|
|
|
tempUserCreated: state.tempUserCreated,
|
2023-06-10 01:56:04 +00:00
|
|
|
message: mw.msg( 'discussiontools-postedit-confirmation-published', mw.user )
|
|
|
|
} );
|
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
|
|
|
|
2023-06-26 15:13:09 +00:00
|
|
|
var dismissableNotificationPromise = null;
|
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 ) {
|
2023-10-03 20:03:59 +00:00
|
|
|
if ( utils.isUnmodifiedLeftClick( e ) && !e.target.closest( 'a' ) ) {
|
|
|
|
// Remove the highlight and the hash from the URL, unless clicking on another link
|
2022-03-01 15:04:24 +00:00
|
|
|
highlighter.clearHighlightTargetComment( pageThreads );
|
|
|
|
}
|
2023-06-26 15:13:09 +00:00
|
|
|
if ( dismissableNotificationPromise ) {
|
|
|
|
dismissableNotificationPromise.then( function ( notif ) {
|
|
|
|
notif.close();
|
|
|
|
} );
|
|
|
|
dismissableNotificationPromise = null;
|
|
|
|
}
|
2022-03-01 15:04:24 +00:00
|
|
|
} );
|
|
|
|
pageHandlersSetup = true;
|
|
|
|
}
|
2022-02-23 22:41:47 +00:00
|
|
|
if ( state.firstLoad ) {
|
2024-02-23 00:20:27 +00:00
|
|
|
mobilePromise.then( function () {
|
2023-10-23 13:15:04 +00:00
|
|
|
var findCommentQuery;
|
|
|
|
var isHeading = false;
|
2023-06-26 15:13:09 +00:00
|
|
|
var highlightResult = highlighter.highlightTargetComment( pageThreads );
|
2024-01-30 13:19:45 +00:00
|
|
|
|
|
|
|
// Hash contains a non-replaced space (should be underscore), maybe due to
|
|
|
|
// manual creation or a broken third party tool. Just replace the spaces
|
|
|
|
// and navigate to the new URL.
|
|
|
|
// Ideally we'd use history.replaceState but that wouldn't scroll the page.
|
|
|
|
// Only do the replacement if the original hash doesn't correspond to a target
|
|
|
|
// element, but the fixed hash does, to avoid affects on other apps which
|
|
|
|
// may use fragments with spaces.
|
|
|
|
if ( location.hash && !mw.util.getTargetFromFragment() && location.hash.indexOf( '%20' ) !== -1 ) {
|
|
|
|
var fixedHash = location.hash.slice( 1 ).replace( /%20/g, '_' );
|
|
|
|
if ( mw.util.getTargetFromFragment( fixedHash ) ) {
|
|
|
|
location.hash = fixedHash;
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 13:15:04 +00:00
|
|
|
if (
|
|
|
|
// Fragment doesn't correspond to an element on the page
|
|
|
|
location.hash &&
|
|
|
|
!mw.util.getTargetFromFragment() &&
|
|
|
|
// Not a DT comment
|
|
|
|
highlightResult.highlighted.length === 0 && highlightResult.requested.length === 0
|
|
|
|
) {
|
|
|
|
var fragment = location.hash.slice( 1 );
|
|
|
|
var ignorePatterns = [
|
2024-03-28 23:26:51 +00:00
|
|
|
// A leading '/' or '!/' usually means a application route, e.g. /media, or /editor.
|
2023-10-23 13:15:04 +00:00
|
|
|
// We can't rule out a heading title (T349498), but they are unlikely
|
2024-03-28 23:26:51 +00:00
|
|
|
/^!?\//,
|
2024-03-21 15:34:05 +00:00
|
|
|
// "top" is a magic value in the WHATWG spec that goes to the top of the
|
|
|
|
// document (unless there's an actual id=top element on the page), see:
|
|
|
|
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#scrolling-to-a-fragment:top-of-the-document-2
|
|
|
|
// There's a very rare edge case of actual headings named "top" that we'll
|
|
|
|
// be missing here, but they're far less common than the top-of-page usage.
|
|
|
|
/^top$/i,
|
2024-01-30 22:44:30 +00:00
|
|
|
// Gadget: ConvenientDiscussions
|
|
|
|
/^\d{12}_/,
|
2023-10-23 13:15:04 +00:00
|
|
|
// Gadget: RedWarn
|
|
|
|
/^noticeApplied-/
|
|
|
|
];
|
|
|
|
if ( ignorePatterns.every( function ( pattern ) {
|
|
|
|
return !pattern.test( fragment );
|
|
|
|
} ) ) {
|
|
|
|
findCommentQuery = {
|
2024-02-03 22:48:54 +00:00
|
|
|
heading: mw.util.percentDecodeFragment( fragment ).replace( / /g, '_' ),
|
2023-10-23 13:15:04 +00:00
|
|
|
page: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
};
|
|
|
|
isHeading = true;
|
|
|
|
}
|
|
|
|
} else if ( highlightResult.highlighted.length === 0 && highlightResult.requested.length === 1 ) {
|
|
|
|
findCommentQuery = {
|
2024-02-03 22:48:54 +00:00
|
|
|
idorname: mw.util.percentDecodeFragment( highlightResult.requested[ 0 ] )
|
2023-10-23 13:15:04 +00:00
|
|
|
};
|
|
|
|
isHeading = highlightResult.requested[ 0 ].slice( 0, 1 ) === 'h';
|
|
|
|
}
|
|
|
|
if ( findCommentQuery ) {
|
2023-06-26 15:13:09 +00:00
|
|
|
// TODO: Support multiple commentIds being requested and not all being found
|
2023-10-23 13:15:04 +00:00
|
|
|
var dtConf = require( './config.json' );
|
|
|
|
var findCommentRequest = dtConf.enablePermalinksFrontend ?
|
|
|
|
getApi().get( $.extend( {
|
|
|
|
action: 'discussiontoolsfindcomment'
|
|
|
|
}, findCommentQuery ) ) :
|
2024-01-10 17:24:02 +00:00
|
|
|
$.Deferred().resolve( [ {} ] ).promise();
|
2023-10-23 13:15:04 +00:00
|
|
|
dismissableNotificationPromise = $.when(
|
|
|
|
findCommentRequest,
|
|
|
|
mw.loader.using( 'mediawiki.notification' )
|
|
|
|
).then( function ( results ) {
|
|
|
|
var result = results[ 0 ];
|
|
|
|
var titles = [];
|
|
|
|
if ( result.discussiontoolsfindcomment ) {
|
|
|
|
titles = result.discussiontoolsfindcomment.map( function ( threadItemData ) {
|
2024-02-03 23:03:23 +00:00
|
|
|
// Only show items that appear on the current revision of their page
|
|
|
|
// and are not transcluded from another page
|
|
|
|
if ( threadItemData.couldredirect ) {
|
2023-10-23 13:15:04 +00:00
|
|
|
var title = mw.Title.newFromText(
|
|
|
|
threadItemData.title + '#' +
|
|
|
|
mw.util.escapeIdForLink( threadItemData.id )
|
2023-06-26 15:13:09 +00:00
|
|
|
);
|
2023-10-23 13:15:04 +00:00
|
|
|
return title;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} ).filter( function ( url ) {
|
|
|
|
return url;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( titles.length ) {
|
|
|
|
var $list = $( '<ul>' );
|
|
|
|
var $notification = $( '<div>' ).append(
|
|
|
|
$( '<p>' ).text( mw.message(
|
|
|
|
isHeading ?
|
|
|
|
'discussiontools-target-heading-found-moved' :
|
|
|
|
'discussiontools-target-comment-found-moved',
|
|
|
|
titles.length
|
|
|
|
).text() ),
|
|
|
|
$list
|
|
|
|
);
|
|
|
|
titles.forEach( function ( title ) {
|
|
|
|
$list.append(
|
|
|
|
$( '<li>' ).append(
|
|
|
|
$( '<a>' ).attr( 'href', title.getUrl() ).text( title.getPrefixedText() )
|
|
|
|
)
|
2023-06-26 15:13:09 +00:00
|
|
|
);
|
2023-10-23 13:15:04 +00:00
|
|
|
} );
|
|
|
|
mw.notification.notify(
|
|
|
|
$notification,
|
|
|
|
{ type: 'warn', autoHide: false }
|
|
|
|
);
|
|
|
|
// This notification should not be accidentally dismissable
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
} else {
|
2023-06-26 15:13:09 +00:00
|
|
|
return mw.notification.notify(
|
2023-10-23 13:15:04 +00:00
|
|
|
mw.message( isHeading ?
|
|
|
|
'discussiontools-target-heading-missing' :
|
|
|
|
'discussiontools-target-comment-missing'
|
|
|
|
).text(),
|
2023-06-26 15:13:09 +00:00
|
|
|
{ type: 'warn', autoHide: false }
|
|
|
|
);
|
2023-10-23 13:15:04 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2023-12-16 15:20:19 +00:00
|
|
|
if ( highlightResult.highlighted.length === 0 && ( highlightResult.requested.length > 1 || highlightResult.requestedSince ) ) {
|
2023-10-23 13:15:04 +00:00
|
|
|
dismissableNotificationPromise = mw.loader.using( 'mediawiki.notification' ).then( function () {
|
|
|
|
return mw.notification.notify(
|
|
|
|
mw.message( 'discussiontools-target-comments-missing' ).text(),
|
|
|
|
{ type: 'warn', autoHide: false }
|
|
|
|
);
|
|
|
|
} );
|
2023-06-26 15:13:09 +00:00
|
|
|
}
|
2023-08-15 16:04:17 +00:00
|
|
|
} );
|
2022-02-23 22:41:47 +00:00
|
|
|
}
|
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' ),
|
2024-03-25 18:26:53 +00:00
|
|
|
// Pass through dtenable query string param from original request
|
|
|
|
dtenable: new URLSearchParams( location.search ).get( 'dtenable' ) ? '1' : undefined,
|
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() {
|
2023-03-18 02:29:27 +00:00
|
|
|
mw.track( 'editAttemptStep', {
|
2022-04-06 16:37:29 +00:00
|
|
|
action: 'saveSuccess',
|
|
|
|
timing: mw.now() - replyWidget.saveInitiated,
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
revision_id: data.newrevid
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:07:55 +00:00
|
|
|
if (
|
|
|
|
( pageName === mw.config.get( 'wgRelevantPageName' ) && data.nocontent ) ||
|
|
|
|
data.tempusercreated
|
|
|
|
) {
|
|
|
|
// Reload if the page didn't exist before this update, or we just became logged in
|
|
|
|
// as a temporary user. 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 );
|
2023-09-04 15:17:06 +00:00
|
|
|
var params = { dtrepliedto: threadItem.id };
|
|
|
|
if ( data.tempusercreated ) {
|
|
|
|
params.dttempusercreated = '1';
|
|
|
|
}
|
|
|
|
window.location = data.tempusercreatedredirect || mw.util.getUrl( pageName, params );
|
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();
|
|
|
|
// 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;
|
2023-08-24 15:36:44 +00:00
|
|
|
mw.dt.initState.tempUserCreated = data.tempusercreated;
|
2022-02-01 23:23:14 +00:00
|
|
|
|
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,
|
2023-05-21 06:57:49 +00:00
|
|
|
getReplyWidgetModules: getReplyWidgetModules,
|
|
|
|
defaultVisual: defaultVisual,
|
|
|
|
enable2017Wikitext: enable2017Wikitext
|
2019-11-05 13:55:01 +00:00
|
|
|
};
|