| 1 |
'use strict'; |
| 2 |
|
| 3 |
const featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {}, |
| 4 |
MemoryStorage = require( './MemoryStorage.js' ), |
| 5 |
STORAGE_EXPIRY = 60 * 60 * 24 * 30, |
| 6 |
Parser = require( './Parser.js' ), |
| 7 |
ThreadItemSet = require( './ThreadItemSet.js' ), |
| 8 |
CommentDetails = require( './CommentDetails.js' ), |
| 9 |
HeadingItem = require( './HeadingItem.js' ), |
| 10 |
ReplyLinksController = require( './ReplyLinksController.js' ), |
| 11 |
utils = require( './utils.js' ), |
| 12 |
highlighter = require( './highlighter.js' ), |
| 13 |
topicSubscriptions = require( './topicsubscriptions.js' ), |
| 14 |
permalinks = require( './permalinks.js' ), |
| 15 |
defaultEditMode = mw.user.options.get( 'discussiontools-editmode' ) || mw.config.get( 'wgDiscussionToolsFallbackEditMode' ), |
| 16 |
defaultVisual = defaultEditMode === 'visual', |
| 17 |
enable2017Wikitext = featuresEnabled.sourcemodetoolbar, |
| 18 |
overflowMenu = require( './overflowMenu.js' ); |
| 19 |
let |
| 20 |
$pageContainer, linksController, |
| 21 |
pageThreads, |
| 22 |
lastControllerScrollOffset, |
| 23 |
pageDataCache = {}, |
| 24 |
pageHandlersSetup = false; |
| 25 |
|
| 26 |
let mobile = null; |
| 27 |
if ( OO.ui.isMobile() && mw.config.get( 'skin' ) === 'minerva' ) { |
| 28 |
mobile = require( './mobile.js' ); |
| 29 |
} |
| 30 |
if ( mw.loader.getState( 'ext.thanks' ) ) { |
| 31 |
require( './thanks.js' ); |
| 32 |
} |
| 33 |
|
| 34 |
mw.messages.set( require( './controller/contLangMessages.json' ) ); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
function getApi() { |
| 42 |
return new mw.Api( { |
| 43 |
parameters: { |
| 44 |
formatversion: 2, |
| 45 |
uselang: mw.config.get( 'wgUserLanguage' ) |
| 46 |
} |
| 47 |
} ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
function getPageData( pageName, oldId, apiParams ) { |
| 62 |
const api = getApi(); |
| 63 |
apiParams = apiParams || {}; |
| 64 |
|
| 65 |
pageDataCache[ pageName ] = pageDataCache[ pageName ] || {}; |
| 66 |
if ( pageDataCache[ pageName ][ oldId ] && $.isEmptyObject( apiParams ) ) { |
| 67 |
return pageDataCache[ pageName ][ oldId ]; |
| 68 |
} |
| 69 |
|
| 70 |
let lintPromise, transcludedFromPromise; |
| 71 |
if ( oldId ) { |
| 72 |
lintPromise = api.get( { |
| 73 |
action: 'query', |
| 74 |
list: 'linterrors', |
| 75 |
lntcategories: 'fostered', |
| 76 |
lntlimit: 1, |
| 77 |
lnttitle: pageName |
| 78 |
} ).then( ( response ) => OO.getProp( response, 'query', 'linterrors' ) || [] ); |
| 79 |
|
| 80 |
transcludedFromPromise = api.get( { |
| 81 |
action: 'discussiontoolspageinfo', |
| 82 |
page: pageName, |
| 83 |
oldid: oldId |
| 84 |
} ).then( ( response ) => OO.getProp( response, 'discussiontoolspageinfo', 'transcludedfrom' ) || {} ); |
| 85 |
} else { |
| 86 |
lintPromise = $.Deferred().resolve( [] ).promise(); |
| 87 |
transcludedFromPromise = $.Deferred().resolve( {} ).promise(); |
| 88 |
} |
| 89 |
|
| 90 |
const veMetadataPromise = api.get( Object.assign( { |
| 91 |
action: 'visualeditor', |
| 92 |
paction: 'metadata', |
| 93 |
page: pageName |
| 94 |
}, apiParams ) ).then( ( response ) => OO.getProp( response, 'visualeditor' ) || [] ); |
| 95 |
|
| 96 |
const promise = $.when( lintPromise, transcludedFromPromise, veMetadataPromise ) |
| 97 |
.then( ( linterrors, transcludedfrom, metadata ) => ( { |
| 98 |
linterrors: linterrors, |
| 99 |
transcludedfrom: transcludedfrom, |
| 100 |
metadata: metadata |
| 101 |
} ), () => { |
| 102 |
|
| 103 |
pageDataCache[ pageName ][ oldId ] = null; |
| 104 |
|
| 105 |
return $.Deferred().rejectWith( this, arguments ); |
| 106 |
} ); |
| 107 |
|
| 108 |
if ( $.isEmptyObject( apiParams ) ) { |
| 109 |
pageDataCache[ pageName ][ oldId ] = promise; |
| 110 |
} |
| 111 |
|
| 112 |
return promise; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
function checkThreadItemOnPage( pageName, oldId, threadItem ) { |
| 125 |
const isNewTopic = threadItem.id === utils.NEW_TOPIC_COMMENT_ID; |
| 126 |
const defaultMode = mw.user.options.get( 'discussiontools-editmode' ) || mw.config.get( 'wgDiscussionToolsFallbackEditMode' ); |
| 127 |
let apiParams = null; |
| 128 |
if ( isNewTopic ) { |
| 129 |
apiParams = { |
| 130 |
section: 'new', |
| 131 |
editintro: threadItem.editintro, |
| 132 |
preload: threadItem.preload, |
| 133 |
preloadparams: threadItem.preloadparams, |
| 134 |
paction: defaultMode === 'source' ? 'wikitext' : 'parse' |
| 135 |
}; |
| 136 |
} |
| 137 |
|
| 138 |
return getPageData( pageName, oldId, apiParams ) |
| 139 |
.then( ( response ) => { |
| 140 |
const metadata = response.metadata, |
| 141 |
lintErrors = response.linterrors, |
| 142 |
transcludedFrom = response.transcludedfrom; |
| 143 |
|
| 144 |
if ( !isNewTopic ) { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
let isTranscludedFrom = transcludedFrom[ threadItem.id ]; |
| 151 |
if ( isTranscludedFrom === undefined ) { |
| 152 |
isTranscludedFrom = transcludedFrom[ threadItem.name ]; |
| 153 |
} |
| 154 |
if ( isTranscludedFrom === undefined ) { |
| 155 |
|
| 156 |
|
| 157 |
return $.Deferred().reject( 'discussiontools-commentid-notfound-transcludedfrom', { errors: [ { |
| 158 |
code: 'discussiontools-commentid-notfound-transcludedfrom', |
| 159 |
html: mw.message( 'discussiontools-error-comment-disappeared' ).parse() + |
| 160 |
'<br>' + |
| 161 |
mw.message( 'discussiontools-error-comment-disappeared-reload' ).parse() |
| 162 |
} ] } ).promise(); |
| 163 |
} else if ( isTranscludedFrom ) { |
| 164 |
const mwTitle = isTranscludedFrom === true ? null : mw.Title.newFromText( isTranscludedFrom ); |
| 165 |
|
| 166 |
const follow = mwTitle && mwTitle.getNamespaceId() !== mw.config.get( 'wgNamespaceIds' ).template; |
| 167 |
|
| 168 |
let transcludedErrMsg; |
| 169 |
if ( follow ) { |
| 170 |
transcludedErrMsg = mw.message( |
| 171 |
'discussiontools-error-comment-is-transcluded-title', |
| 172 |
mwTitle.getPrefixedText() |
| 173 |
).parse(); |
| 174 |
} else if ( metadata.canEdit ) { |
| 175 |
|
| 176 |
transcludedErrMsg = mw.message( |
| 177 |
'discussiontools-error-comment-is-transcluded', |
| 178 |
|
| 179 |
$( '#ca-edit' ).text() |
| 180 |
).parse(); |
| 181 |
} else { |
| 182 |
|
| 183 |
transcludedErrMsg = metadata.notices[ 'permissions-error' ]; |
| 184 |
} |
| 185 |
|
| 186 |
return $.Deferred().reject( 'comment-is-transcluded', { errors: [ { |
| 187 |
data: { |
| 188 |
transcludedFrom: isTranscludedFrom, |
| 189 |
follow: follow |
| 190 |
}, |
| 191 |
code: 'comment-is-transcluded', |
| 192 |
html: transcludedErrMsg |
| 193 |
} ] } ).promise(); |
| 194 |
} |
| 195 |
|
| 196 |
if ( lintErrors.length ) { |
| 197 |
|
| 198 |
const lintType = lintErrors[ 0 ].category; |
| 199 |
|
| 200 |
return $.Deferred().reject( 'lint', { errors: [ { |
| 201 |
code: 'lint', |
| 202 |
html: mw.message( 'discussiontools-error-lint', |
| 203 |
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Lint_errors/' + lintType, |
| 204 |
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help_talk:Lint_errors/' + lintType, |
| 205 |
mw.util.getUrl( pageName, { action: 'edit', lintid: lintErrors[ 0 ].lintId } ) ).parse() |
| 206 |
} ] } ).promise(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ( !metadata.canEdit ) { |
| 211 |
return $.Deferred().reject( 'permissions-error', { errors: [ { |
| 212 |
code: 'permissions-error', |
| 213 |
html: metadata.notices[ 'permissions-error' ] |
| 214 |
} ] } ).promise(); |
| 215 |
} |
| 216 |
|
| 217 |
return new CommentDetails( pageName, oldId, metadata.notices, metadata.wouldautocreate, metadata.content, defaultMode ); |
| 218 |
} ); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
function getCheckboxesPromise( pageName, oldId ) { |
| 229 |
return getPageData( |
| 230 |
pageName, |
| 231 |
oldId |
| 232 |
).then( ( pageData ) => { |
| 233 |
const data = pageData.metadata, |
| 234 |
checkboxesDef = {}; |
| 235 |
|
| 236 |
mw.messages.set( data.checkboxesMessages ); |
| 237 |
|
| 238 |
|
| 239 |
if ( 'wpWatchthis' in data.checkboxesDef ) { |
| 240 |
checkboxesDef.wpWatchthis = data.checkboxesDef.wpWatchthis; |
| 241 |
|
| 242 |
checkboxesDef.wpWatchthis[ 'label-message' ] = 'discussiontools-replywidget-watchthis'; |
| 243 |
} |
| 244 |
return mw.loader.using( 'ext.visualEditor.targetLoader' ) |
| 245 |
.then( () => mw.libs.ve.targetLoader.createCheckboxFields( checkboxesDef ) ); |
| 246 |
|
| 247 |
|
| 248 |
} ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
function getReplyWidgetModules() { |
| 257 |
const modules = []; |
| 258 |
if ( OO.ui.isMobile() ) { |
| 259 |
modules.push( |
| 260 |
'ext.visualEditor.core.mobile', |
| 261 |
'ext.visualEditor.mwextensions' |
| 262 |
); |
| 263 |
} else { |
| 264 |
modules.push( |
| 265 |
'ext.visualEditor.core.desktop', |
| 266 |
'ext.visualEditor.desktopTarget', |
| 267 |
'ext.visualEditor.mwextensions.desktop' |
| 268 |
); |
| 269 |
} |
| 270 |
modules.push( 'ext.discussionTools.ReplyWidget' ); |
| 271 |
|
| 272 |
const veConf = mw.config.get( 'wgVisualEditorConfig' ); |
| 273 |
modules.push( ...veConf.pluginModules.filter( mw.loader.getState ) ); |
| 274 |
|
| 275 |
return modules; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
function init( $container, state ) { |
| 287 |
let |
| 288 |
activeCommentId = null, |
| 289 |
activeController = null; |
| 290 |
|
| 291 |
const CommentController = require( './CommentController.js' ), |
| 292 |
NewTopicController = require( './NewTopicController.js' ); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
pageDataCache = {}; |
| 298 |
|
| 299 |
|
| 300 |
mw.loader.using( 'mediawiki.action.view.postEdit' ); |
| 301 |
|
| 302 |
if ( linksController ) { |
| 303 |
linksController.teardown(); |
| 304 |
linksController = null; |
| 305 |
} |
| 306 |
|
| 307 |
$pageContainer = $container; |
| 308 |
linksController = new ReplyLinksController( $pageContainer ); |
| 309 |
permalinks.init( $pageContainer ); |
| 310 |
|
| 311 |
linksController.on( 'link-interact', () => { |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
getPageData( |
| 318 |
mw.config.get( 'wgRelevantPageName' ), |
| 319 |
mw.config.get( 'wgCurRevisionId' ) |
| 320 |
); |
| 321 |
} ); |
| 322 |
|
| 323 |
const parser = new Parser( require( './parser/data.json' ) ); |
| 324 |
|
| 325 |
const commentNodes = $pageContainer[ 0 ].querySelectorAll( '[data-mw-thread-id]' ); |
| 326 |
pageThreads = ThreadItemSet.static.newFromJSON( mw.config.get( 'wgDiscussionToolsPageThreads' ) || [], $pageContainer[ 0 ], parser ); |
| 327 |
|
| 328 |
if ( featuresEnabled.topicsubscription ) { |
| 329 |
topicSubscriptions.initTopicSubscriptions( $container, pageThreads ); |
| 330 |
} |
| 331 |
|
| 332 |
overflowMenu.init( $container, pageThreads ); |
| 333 |
|
| 334 |
if ( mobile ) { |
| 335 |
mobile.init( $container, pageThreads ); |
| 336 |
} |
| 337 |
|
| 338 |
if ( linksController.pageHasReplyLinks() || linksController.pageHasNewTopicLink() ) { |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
mw.loader.using( getReplyWidgetModules() ); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
function setupController( comment, $link, options, storage ) { |
| 359 |
if ( !storage ) { |
| 360 |
storage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + comment.id, STORAGE_EXPIRY ); |
| 361 |
} |
| 362 |
|
| 363 |
let commentController, $addSectionLink; |
| 364 |
if ( comment.id === utils.NEW_TOPIC_COMMENT_ID ) { |
| 365 |
|
| 366 |
$addSectionLink = $( '#ca-addsection' ).find( 'a' ); |
| 367 |
|
| 368 |
$link = $link.add( $addSectionLink ); |
| 369 |
commentController = new NewTopicController( $pageContainer, comment, pageThreads, storage ); |
| 370 |
} else { |
| 371 |
commentController = new CommentController( $pageContainer, comment, pageThreads, storage ); |
| 372 |
} |
| 373 |
|
| 374 |
activeCommentId = comment.id; |
| 375 |
activeController = commentController; |
| 376 |
linksController.setActiveLink( $link ); |
| 377 |
|
| 378 |
commentController.on( 'teardown', ( teardownMode ) => { |
| 379 |
activeCommentId = null; |
| 380 |
activeController = null; |
| 381 |
|
| 382 |
if ( teardownMode !== 'refresh' ) { |
| 383 |
linksController.clearActiveLink(); |
| 384 |
} |
| 385 |
|
| 386 |
if ( teardownMode === 'abandoned' ) { |
| 387 |
linksController.focusLink( $link ); |
| 388 |
} |
| 389 |
} ); |
| 390 |
commentController.on( 'reloadPage', () => { |
| 391 |
mw.dt.initState.newCommentIds = commentController.newComments.map( ( cmt ) => cmt.id ); |
| 392 |
|
| 393 |
commentController.replyWidgetPromise.then( ( replyWidget ) => { |
| 394 |
lastControllerScrollOffset = $( commentController.newListItem ).offset().top; |
| 395 |
replyWidget.teardown( 'refresh' ); |
| 396 |
|
| 397 |
|
| 398 |
refreshPageContents( commentController.oldId ); |
| 399 |
} ); |
| 400 |
} ); |
| 401 |
|
| 402 |
commentController.setup( options ); |
| 403 |
if ( lastControllerScrollOffset ) { |
| 404 |
$( document.documentElement ).scrollTop( |
| 405 |
$( document.documentElement ).scrollTop() + |
| 406 |
( $( commentController.newListItem ).offset().top - lastControllerScrollOffset ) |
| 407 |
); |
| 408 |
lastControllerScrollOffset = null; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
function newTopicComment( data ) { |
| 413 |
const comment = new HeadingItem( {}, 2 ); |
| 414 |
comment.id = utils.NEW_TOPIC_COMMENT_ID; |
| 415 |
comment.isNewTopic = true; |
| 416 |
Object.assign( comment, data ); |
| 417 |
return comment; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
linksController.on( 'link-click', ( commentId, $link, data ) => { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
if ( activeCommentId === commentId ) { |
| 429 |
activeController.showAndFocus(); |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
let teardownPromise = $.Deferred().resolve(); |
| 434 |
if ( commentId === utils.NEW_TOPIC_COMMENT_ID ) { |
| 435 |
|
| 436 |
if ( activeController ) { |
| 437 |
teardownPromise = activeController.tryTeardown(); |
| 438 |
} else if ( OO.getProp( window, 've', 'init', 'target', 'tryTeardown' ) ) { |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
teardownPromise = ve.init.target.tryTeardown() || $.Deferred().resolve(); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if ( mobile ) { |
| 448 |
teardownPromise = teardownPromise.then( () => mobile.closeLedeSectionDialog() ); |
| 449 |
} |
| 450 |
|
| 451 |
teardownPromise.then( () => { |
| 452 |
|
| 453 |
if ( activeController ) { |
| 454 |
return; |
| 455 |
} |
| 456 |
let comment; |
| 457 |
if ( commentId !== utils.NEW_TOPIC_COMMENT_ID ) { |
| 458 |
comment = pageThreads.findCommentById( commentId ); |
| 459 |
} else { |
| 460 |
comment = newTopicComment( data ); |
| 461 |
} |
| 462 |
if ( comment ) { |
| 463 |
setupController( comment, $link ); |
| 464 |
} else { |
| 465 |
|
| 466 |
mw.notify( mw.msg( 'discussiontools-error-comment-disappeared' ) ); |
| 467 |
} |
| 468 |
} ); |
| 469 |
} ); |
| 470 |
|
| 471 |
const mobilePromise = OO.ui.isMobile() && mw.loader.getState( 'mobile.init' ) ? |
| 472 |
mw.loader.using( 'mobile.init' ) : |
| 473 |
$.Deferred().resolve().promise(); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if ( mw.config.get( 'wgAction' ) === 'view' ) { |
| 478 |
pageThreads.threadItems.every( ( comment, i ) => { |
| 479 |
const replyStorage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + comment.id, STORAGE_EXPIRY ); |
| 480 |
if ( replyStorage.get( 'saveable' ) ) { |
| 481 |
const mode = replyStorage.get( 'mode' ); |
| 482 |
const $link = $( commentNodes[ i ] ); |
| 483 |
|
| 484 |
mobilePromise.then( () => { |
| 485 |
if ( OO.ui.isMobile() ) { |
| 486 |
const urlFragment = mw.util.escapeIdForLink( comment.id ); |
| 487 |
|
| 488 |
location.hash = '#' + urlFragment; |
| 489 |
} |
| 490 |
|
| 491 |
setTimeout( () => { |
| 492 |
setupController( comment, $link, { |
| 493 |
mode: mode, |
| 494 |
hideErrors: true, |
| 495 |
suppressNotifications: !state.firstLoad |
| 496 |
}, replyStorage ); |
| 497 |
} ); |
| 498 |
} ); |
| 499 |
return false; |
| 500 |
} |
| 501 |
return true; |
| 502 |
} ); |
| 503 |
const newTopicStorage = new MemoryStorage( mw.storage, 'mw-ext-DiscussionTools-reply/' + utils.NEW_TOPIC_COMMENT_ID, STORAGE_EXPIRY ); |
| 504 |
if ( newTopicStorage.get( 'saveable' ) || newTopicStorage.get( 'title' ) ) { |
| 505 |
const mode = newTopicStorage.get( 'mode' ); |
| 506 |
setupController( newTopicComment(), $( [] ), { |
| 507 |
mode: mode, |
| 508 |
hideErrors: true, |
| 509 |
suppressNotifications: !state.firstLoad |
| 510 |
}, newTopicStorage ); |
| 511 |
} else if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) { |
| 512 |
const data = linksController.parseNewTopicLink( location.href ); |
| 513 |
setupController( newTopicComment( data ), $( [] ) ); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
mw.dt.pageThreads = pageThreads; |
| 519 |
|
| 520 |
mobilePromise.then( () => { |
| 521 |
if ( state.repliedTo ) { |
| 522 |
highlighter.highlightPublishedComment( pageThreads, state.repliedTo ); |
| 523 |
|
| 524 |
if ( state.repliedTo === utils.NEW_TOPIC_COMMENT_ID ) { |
| 525 |
mw.hook( 'postEdit' ).fire( { |
| 526 |
tempUserCreated: state.tempUserCreated, |
| 527 |
message: mw.msg( 'discussiontools-postedit-confirmation-topicadded', mw.user ) |
| 528 |
} ); |
| 529 |
} else { |
| 530 |
mw.hook( 'postEdit' ).fire( { |
| 531 |
tempUserCreated: state.tempUserCreated, |
| 532 |
message: mw.msg( 'discussiontools-postedit-confirmation-published', mw.user ) |
| 533 |
} ); |
| 534 |
} |
| 535 |
} else if ( state.newCommentIds ) { |
| 536 |
highlighter.highlightNewComments( pageThreads, true, state.newCommentIds ); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if ( featuresEnabled.autotopicsub && mw.user.options.get( 'discussiontools-autotopicsub' ) ) { |
| 542 |
topicSubscriptions.updateAutoSubscriptionStates( $container, pageThreads, state.repliedTo ); |
| 543 |
} |
| 544 |
} ); |
| 545 |
|
| 546 |
let dismissableNotificationPromise = null; |
| 547 |
|
| 548 |
if ( !pageHandlersSetup ) { |
| 549 |
$( window ).on( 'popstate', () => { |
| 550 |
|
| 551 |
|
| 552 |
setTimeout( () => { |
| 553 |
highlighter.highlightTargetComment( pageThreads, true ); |
| 554 |
} ); |
| 555 |
} ); |
| 556 |
|
| 557 |
$( 'body' ).on( 'click', ( e ) => { |
| 558 |
if ( utils.isUnmodifiedLeftClick( e ) && !e.target.closest( 'a' ) ) { |
| 559 |
|
| 560 |
highlighter.clearHighlightTargetComment( pageThreads ); |
| 561 |
} |
| 562 |
if ( dismissableNotificationPromise ) { |
| 563 |
dismissableNotificationPromise.then( ( notif ) => { |
| 564 |
notif.close(); |
| 565 |
} ); |
| 566 |
dismissableNotificationPromise = null; |
| 567 |
} |
| 568 |
} ); |
| 569 |
pageHandlersSetup = true; |
| 570 |
} |
| 571 |
if ( state.firstLoad ) { |
| 572 |
mobilePromise.then( () => { |
| 573 |
let findCommentQuery; |
| 574 |
let isHeading = false; |
| 575 |
const highlightResult = highlighter.highlightTargetComment( pageThreads ); |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
if ( location.hash && !mw.util.getTargetFromFragment() && location.hash.indexOf( '%20' ) !== -1 ) { |
| 585 |
const fixedHash = location.hash.slice( 1 ).replace( /%20/g, '_' ); |
| 586 |
if ( mw.util.getTargetFromFragment( fixedHash ) ) { |
| 587 |
location.hash = fixedHash; |
| 588 |
} |
| 589 |
} |
| 590 |
if ( |
| 591 |
|
| 592 |
location.hash && |
| 593 |
!mw.util.getTargetFromFragment() && |
| 594 |
|
| 595 |
highlightResult.highlighted.length === 0 && highlightResult.requested.length === 0 |
| 596 |
) { |
| 597 |
const fragment = location.hash.slice( 1 ); |
| 598 |
const ignorePatterns = [ |
| 599 |
|
| 600 |
|
| 601 |
/^!?\//, |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
/^top$/i, |
| 608 |
|
| 609 |
/^\d{12}_/, |
| 610 |
|
| 611 |
/^noticeApplied-/ |
| 612 |
]; |
| 613 |
if ( ignorePatterns.every( ( pattern ) => !pattern.test( fragment ) ) ) { |
| 614 |
findCommentQuery = { |
| 615 |
heading: mw.util.percentDecodeFragment( fragment ).replace( / /g, '_' ), |
| 616 |
page: mw.config.get( 'wgRelevantPageName' ) |
| 617 |
}; |
| 618 |
isHeading = true; |
| 619 |
} |
| 620 |
} else if ( highlightResult.highlighted.length === 0 && highlightResult.requested.length === 1 ) { |
| 621 |
findCommentQuery = { |
| 622 |
idorname: mw.util.percentDecodeFragment( highlightResult.requested[ 0 ] ) |
| 623 |
}; |
| 624 |
isHeading = highlightResult.requested[ 0 ].slice( 0, 1 ) === 'h'; |
| 625 |
} |
| 626 |
if ( findCommentQuery ) { |
| 627 |
|
| 628 |
const dtConf = require( './config.json' ); |
| 629 |
const findCommentRequest = dtConf.enablePermalinksFrontend ? |
| 630 |
getApi().get( Object.assign( { |
| 631 |
action: 'discussiontoolsfindcomment' |
| 632 |
}, findCommentQuery ) ) : |
| 633 |
$.Deferred().resolve( [ {} ] ).promise(); |
| 634 |
dismissableNotificationPromise = $.when( |
| 635 |
findCommentRequest, |
| 636 |
mw.loader.using( 'mediawiki.notification' ) |
| 637 |
).then( ( results ) => { |
| 638 |
const result = results[ 0 ]; |
| 639 |
let titles = []; |
| 640 |
if ( result.discussiontoolsfindcomment ) { |
| 641 |
titles = result.discussiontoolsfindcomment.map( ( threadItemData ) => { |
| 642 |
|
| 643 |
|
| 644 |
if ( threadItemData.couldredirect ) { |
| 645 |
const title = mw.Title.newFromText( |
| 646 |
threadItemData.title + '#' + |
| 647 |
mw.util.escapeIdForLink( threadItemData.id ) |
| 648 |
); |
| 649 |
return title; |
| 650 |
} |
| 651 |
return null; |
| 652 |
} ).filter( ( url ) => url ); |
| 653 |
} |
| 654 |
if ( titles.length ) { |
| 655 |
const $list = $( '<ul>' ); |
| 656 |
const $notification = $( '<div>' ).append( |
| 657 |
$( '<p>' ).text( mw.message( |
| 658 |
isHeading ? |
| 659 |
'discussiontools-target-heading-found-moved' : |
| 660 |
'discussiontools-target-comment-found-moved', |
| 661 |
titles.length |
| 662 |
).text() ), |
| 663 |
$list |
| 664 |
); |
| 665 |
titles.forEach( ( title ) => { |
| 666 |
$list.append( |
| 667 |
$( '<li>' ).append( |
| 668 |
$( '<a>' ).attr( 'href', title.getUrl() ).text( title.getPrefixedText() ) |
| 669 |
) |
| 670 |
); |
| 671 |
} ); |
| 672 |
mw.notification.notify( |
| 673 |
$notification, |
| 674 |
{ type: 'warn', autoHide: false } |
| 675 |
); |
| 676 |
|
| 677 |
return $.Deferred().reject().promise(); |
| 678 |
} else { |
| 679 |
return mw.notification.notify( |
| 680 |
mw.message( isHeading ? |
| 681 |
'discussiontools-target-heading-missing' : |
| 682 |
'discussiontools-target-comment-missing' |
| 683 |
).text(), |
| 684 |
{ type: 'warn', autoHide: false } |
| 685 |
); |
| 686 |
} |
| 687 |
} ); |
| 688 |
} |
| 689 |
if ( highlightResult.highlighted.length === 0 && ( highlightResult.requested.length > 1 || highlightResult.requestedSince ) ) { |
| 690 |
dismissableNotificationPromise = mw.loader.using( 'mediawiki.notification' ).then( () => mw.notification.notify( |
| 691 |
mw.message( 'discussiontools-target-comments-missing' ).text(), |
| 692 |
{ type: 'warn', autoHide: false } |
| 693 |
) ); |
| 694 |
} |
| 695 |
} ); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
function updatePageContents( $container, data ) { |
| 706 |
$container.find( '.mw-parser-output' ).first().replaceWith( data.parse.text ); |
| 707 |
|
| 708 |
mw.util.clearSubtitle(); |
| 709 |
mw.util.addSubtitle( data.parse.subtitle ); |
| 710 |
|
| 711 |
|
| 712 |
if ( $( '#catlinks' ).length ) { |
| 713 |
const $categories = $( $.parseHTML( data.parse.categorieshtml ) ); |
| 714 |
mw.hook( 'wikipage.categories' ).fire( $categories ); |
| 715 |
|
| 716 |
$( '#catlinks' ).replaceWith( $categories ); |
| 717 |
} |
| 718 |
|
| 719 |
mw.config.set( data.parse.jsconfigvars ); |
| 720 |
mw.loader.load( data.parse.modulestyles ); |
| 721 |
mw.loader.load( data.parse.modules ); |
| 722 |
|
| 723 |
mw.config.set( { |
| 724 |
wgCurRevisionId: data.parse.revid, |
| 725 |
wgRevisionId: data.parse.revid |
| 726 |
} ); |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
mw.hook( 'wikipage.content' ).remove( mw.dt.init ); |
| 736 |
mw.hook( 'wikipage.content' ).fire( $container ); |
| 737 |
|
| 738 |
|
| 739 |
mw.hook( 'wikipage.content' ).add( mw.dt.init ); |
| 740 |
|
| 741 |
mw.hook( 'wikipage.tableOfContents' ).fire( |
| 742 |
data.parse.showtoc ? data.parse.sections : [] |
| 743 |
); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
$( '#t-permalink' ).add( '#coll-download-as-rl' ).find( 'a' ).each( ( i, link ) => { |
| 749 |
const permalinkUrl = new URL( link.href ); |
| 750 |
permalinkUrl.searchParams.set( 'oldid', data.parse.revid ); |
| 751 |
$( link ).attr( 'href', permalinkUrl.toString() ); |
| 752 |
} ); |
| 753 |
|
| 754 |
const url = new URL( location.href ); |
| 755 |
url.searchParams.delete( 'oldid' ); |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
const keys = []; |
| 760 |
url.searchParams.forEach( ( val, key ) => { |
| 761 |
keys.push( key ); |
| 762 |
} ); |
| 763 |
|
| 764 |
if ( !keys.length || ( keys.length === 1 && keys[ 0 ] === 'title' ) ) { |
| 765 |
const viewUrl = new URL( mw.util.getUrl( mw.config.get( 'wgRelevantPageName' ) ), document.baseURI ); |
| 766 |
viewUrl.hash = location.hash; |
| 767 |
history.pushState( null, '', viewUrl ); |
| 768 |
} else { |
| 769 |
history.pushState( null, '', url ); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
function refreshPageContents( oldId ) { |
| 780 |
|
| 781 |
return getApi().get( { |
| 782 |
action: 'parse', |
| 783 |
|
| 784 |
|
| 785 |
useskin: mw.config.get( 'skin' ), |
| 786 |
mobileformat: OO.ui.isMobile(), |
| 787 |
uselang: mw.config.get( 'wgUserLanguage' ), |
| 788 |
|
| 789 |
dtenable: new URLSearchParams( location.search ).get( 'dtenable' ) ? '1' : undefined, |
| 790 |
prop: [ 'text', 'revid', 'categorieshtml', 'sections', 'displaytitle', 'subtitle', 'modules', 'jsconfigvars' ], |
| 791 |
page: !oldId ? mw.config.get( 'wgRelevantPageName' ) : undefined, |
| 792 |
oldid: oldId || undefined |
| 793 |
} ).then( ( parseResp ) => { |
| 794 |
updatePageContents( $pageContainer, parseResp ); |
| 795 |
} ); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
function update( data, threadItem, pageName, replyWidget ) { |
| 807 |
function logSaveSuccess() { |
| 808 |
mw.track( 'editAttemptStep', { |
| 809 |
action: 'saveSuccess', |
| 810 |
timing: mw.now() - replyWidget.commentController.saveInitiated, |
| 811 |
|
| 812 |
revision_id: data.newrevid |
| 813 |
} ); |
| 814 |
} |
| 815 |
|
| 816 |
if ( |
| 817 |
( pageName === mw.config.get( 'wgRelevantPageName' ) && data.nocontent ) || |
| 818 |
data.tempusercreated |
| 819 |
) { |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
replyWidget.unbindBeforeUnloadHandler(); |
| 824 |
replyWidget.clearStorage(); |
| 825 |
replyWidget.setPending( true ); |
| 826 |
const params = { dtrepliedto: threadItem.id }; |
| 827 |
if ( data.tempusercreated ) { |
| 828 |
params.dttempusercreated = '1'; |
| 829 |
} |
| 830 |
window.location = data.tempusercreatedredirect || mw.util.getUrl( pageName, params ); |
| 831 |
logSaveSuccess(); |
| 832 |
return; |
| 833 |
} |
| 834 |
|
| 835 |
replyWidget.teardown(); |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
mw.dt.initState.repliedTo = threadItem.id; |
| 840 |
mw.dt.initState.tempUserCreated = data.tempusercreated; |
| 841 |
|
| 842 |
|
| 843 |
const pageUpdated = $.Deferred(); |
| 844 |
if ( pageName === mw.config.get( 'wgRelevantPageName' ) ) { |
| 845 |
|
| 846 |
updatePageContents( $pageContainer, { |
| 847 |
parse: { |
| 848 |
text: data.content, |
| 849 |
subtitle: data.contentSub, |
| 850 |
categorieshtml: data.categorieshtml, |
| 851 |
jsconfigvars: data.jsconfigvars, |
| 852 |
revid: data.newrevid, |
| 853 |
|
| 854 |
modules: data.modules, |
| 855 |
modulestyles: [], |
| 856 |
|
| 857 |
showtoc: true, |
| 858 |
sections: data.sections |
| 859 |
} |
| 860 |
} ); |
| 861 |
|
| 862 |
mw.config.set( { |
| 863 |
wgCurRevisionId: data.newrevid, |
| 864 |
wgRevisionId: data.newrevid |
| 865 |
} ); |
| 866 |
|
| 867 |
pageUpdated.resolve(); |
| 868 |
|
| 869 |
} else { |
| 870 |
|
| 871 |
const api = getApi(); |
| 872 |
api.post( { |
| 873 |
action: 'purge', |
| 874 |
titles: mw.config.get( 'wgRelevantPageName' ) |
| 875 |
} ).then( () => refreshPageContents() ).then( () => { |
| 876 |
pageUpdated.resolve(); |
| 877 |
} ).catch( () => { |
| 878 |
|
| 879 |
… |
| 880 |
window.location = mw.util.getUrl( pageName, { dtrepliedto: threadItem.id } ); |
| 881 |
|
| 882 |
logSaveSuccess(); |
| 883 |
} ); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
if ( mw.loader.getState( 'mediawiki.page.watch.ajax' ) === 'ready' ) { |
| 888 |
const watch = require( 'mediawiki.page.watch.ajax' ); |
| 889 |
|
| 890 |
watch.updateWatchLink( |
| 891 |
mw.Title.newFromText( pageName ), |
| 892 |
data.watched ? 'unwatch' : 'watch', |
| 893 |
'idle', |
| 894 |
data.watchlistexpiry |
| 895 |
); |
| 896 |
} |
| 897 |
|
| 898 |
pageUpdated.then( logSaveSuccess ); |
| 899 |
} |
| 900 |
|
| 901 |
module.exports = { |
| 902 |
init: init, |
| 903 |
update: update, |
| 904 |
updatePageContents: updatePageContents, |
| 905 |
refreshPageContents: refreshPageContents, |
| 906 |
checkThreadItemOnPage: checkThreadItemOnPage, |
| 907 |
getCheckboxesPromise: getCheckboxesPromise, |
| 908 |
getApi: getApi, |
| 909 |
getReplyWidgetModules: getReplyWidgetModules, |
| 910 |
defaultVisual: defaultVisual, |
| 911 |
enable2017Wikitext: enable2017Wikitext |
| 912 |
}; |