| 1 |
( function () { |
| 2 |
const api = new mw.Api(); |
| 3 |
const util = require( 'mediawiki.util' ); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function showEditSummary( $formNode, response ) { |
| 13 |
const $summaryPreview = $formNode.find( '.mw-summary-preview' ).empty(); |
| 14 |
const parse = response.parse; |
| 15 |
|
| 16 |
if ( !parse || !parse.parsedsummary ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$summaryPreview.append( |
| 21 |
mw.message( 'summary-preview' ).parse(), |
| 22 |
' ', |
| 23 |
$( '<span>' ).addClass( 'comment' ).html( parenthesesWrap( parse.parsedsummary ) ) |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
function parenthesesWrap( str ) { |
| 35 |
if ( str === '' ) { |
| 36 |
return str; |
| 37 |
} |
| 38 |
|
| 39 |
return mw.message( 'parentheses' ).escaped() |
| 40 |
|
| 41 |
|
| 42 |
.replace( '$1', () => str ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
function showIndicators( indicators ) { |
| 52 |
|
| 53 |
indicators = $.map( indicators, ( indicator, name ) => $( '<div>' ) |
| 54 |
.addClass( 'mw-indicator' ) |
| 55 |
.attr( 'id', mw.util.escapeIdForAttribute( 'mw-indicator-' + name ) ) |
| 56 |
.html( indicator ) |
| 57 |
.get( 0 ) ); |
| 58 |
if ( indicators.length ) { |
| 59 |
mw.hook( 'wikipage.indicators' ).fire( $( indicators ) ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
const newList = []; |
| 65 |
indicators.forEach( ( indicator ) => { |
| 66 |
newList.push( indicator, document.createTextNode( '\n' ) ); |
| 67 |
} ); |
| 68 |
|
| 69 |
$( '.mw-indicators' ).empty().append( newList ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
function showTemplates( templates ) { |
| 81 |
|
| 82 |
|
| 83 |
const $parent = $( '.templatesUsed' ); |
| 84 |
|
| 85 |
|
| 86 |
let $explanation = $parent.find( '.mw-templatesUsedExplanation p' ); |
| 87 |
if ( $explanation.length === 0 ) { |
| 88 |
$explanation = $( '<p>' ); |
| 89 |
$parent.append( $( '<div>' ) |
| 90 |
.addClass( 'mw-templatesUsedExplanation' ) |
| 91 |
.append( $explanation ) ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
let $list = $parent.find( 'ul' ); |
| 97 |
if ( $list.length === 0 ) { |
| 98 |
$list = $( '<ul>' ).addClass( [ 'mw-editfooter-list', 'mw-collapsible', 'mw-made-collapsible' ] ); |
| 99 |
$parent.append( $list ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( templates.length === 0 ) { |
| 103 |
$explanation.msg( 'templatesusedpreview', 0 ); |
| 104 |
$list.empty(); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
$parent.addClass( 'mw-preview-loading-elements-loading' ); |
| 110 |
const batchSize = 50; |
| 111 |
const requests = []; |
| 112 |
for ( let batch = 0; batch < templates.length; batch += batchSize ) { |
| 113 |
|
| 114 |
const titles = templates |
| 115 |
.slice( batch, batch + batchSize ) |
| 116 |
.map( ( template ) => template.title ); |
| 117 |
requests.push( api.post( { |
| 118 |
action: 'query', |
| 119 |
format: 'json', |
| 120 |
formatversion: 2, |
| 121 |
titles: titles, |
| 122 |
prop: 'info', |
| 123 |
|
| 124 |
inprop: 'linkclasses|protection', |
| 125 |
intestactions: 'edit' |
| 126 |
} ) ); |
| 127 |
} |
| 128 |
$.when( ...requests ).done( function () { |
| 129 |
const templatesAllInfo = []; |
| 130 |
|
| 131 |
for ( let r = 0; r < arguments.length; r++ ) { |
| 132 |
|
| 133 |
const response = arguments[ r ][ 0 ] || arguments[ r ]; |
| 134 |
const templatesInfo = ( response.query && response.query.pages ) || []; |
| 135 |
templatesInfo.forEach( ( ti ) => { |
| 136 |
templatesAllInfo.push( { |
| 137 |
title: mw.Title.newFromText( ti.title ), |
| 138 |
apiData: ti |
| 139 |
} ); |
| 140 |
} ); |
| 141 |
} |
| 142 |
|
| 143 |
templatesAllInfo.sort( ( t1, t2 ) => { |
| 144 |
|
| 145 |
if ( t1.title.getNamespaceId() !== t2.title.getNamespaceId() ) { |
| 146 |
return t1.title.getNamespaceId() - t2.title.getNamespaceId(); |
| 147 |
} else { |
| 148 |
return t1.title.getMain() === t2.title.getMain() ? |
| 149 |
0 : |
| 150 |
t1.title.getMain() < t2.title.getMain() ? -1 : 1; |
| 151 |
} |
| 152 |
} ); |
| 153 |
|
| 154 |
|
| 155 |
const $listNew = $( '<ul>' ); |
| 156 |
addItemToTemplateListPromise( $listNew, templatesAllInfo, 0 ) |
| 157 |
.then( () => { |
| 158 |
$list.html( $listNew.html() ); |
| 159 |
} ); |
| 160 |
$explanation.msg( 'templatesusedpreview', templatesAllInfo.length ); |
| 161 |
} ).always( () => { |
| 162 |
$parent.removeClass( 'mw-preview-loading-elements-loading' ); |
| 163 |
} ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
function addItemToTemplateListPromise( $list, templatesInfo, templateIndex ) { |
| 179 |
return addItemToTemplateList( $list, templatesInfo[ templateIndex ] ).then( () => { |
| 180 |
if ( templatesInfo[ templateIndex + 1 ] !== undefined ) { |
| 181 |
return addItemToTemplateListPromise( $list, templatesInfo, templateIndex + 1 ); |
| 182 |
} |
| 183 |
} ); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
function addItemToTemplateList( $list, template ) { |
| 195 |
const editable = template.apiData.ns >= 0; |
| 196 |
const canEdit = editable && template.apiData.actions.edit !== undefined; |
| 197 |
const linkClasses = template.apiData.linkclasses || []; |
| 198 |
if ( template.apiData.missing !== undefined && template.apiData.known === undefined ) { |
| 199 |
linkClasses.push( 'new' ); |
| 200 |
} |
| 201 |
const $baseLink = $( '<a>' ) |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
.addClass( linkClasses ); |
| 208 |
const $link = $baseLink.clone() |
| 209 |
.attr( 'href', template.title.getUrl() ) |
| 210 |
.text( template.title.getPrefixedText() ); |
| 211 |
|
| 212 |
if ( editable ) { |
| 213 |
const $editLink = $baseLink.clone() |
| 214 |
.attr( 'href', template.title.getUrl( { action: 'edit' } ) ) |
| 215 |
.append( mw.message( canEdit ? 'editlink' : 'viewsourcelink' ).escaped() ); |
| 216 |
|
| 217 |
const wordSep = mw.message( 'word-separator' ).escaped(); |
| 218 |
return getRestrictionsText( template.apiData.protection || [] ) |
| 219 |
.then( ( restrictionsList ) => { |
| 220 |
|
| 221 |
const editLinkParens = parenthesesWrap( $editLink[ 0 ].outerHTML ); |
| 222 |
const $li = $( '<li>' ).append( $link, wordSep, editLinkParens, wordSep, restrictionsList ); |
| 223 |
$list.append( $li ); |
| 224 |
} ); |
| 225 |
} else { |
| 226 |
$list.append( $( '<li>' ).append( $link ) ); |
| 227 |
return $.Deferred().resolve( '' ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
function getRestrictionsText( restrictions ) { |
| 241 |
let msg = ''; |
| 242 |
if ( !restrictions ) { |
| 243 |
return $.Deferred().resolve( msg ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
const restrictionLevels = []; |
| 248 |
restrictions.forEach( ( r ) => { |
| 249 |
if ( r.type !== 'edit' ) { |
| 250 |
return; |
| 251 |
} |
| 252 |
if ( r.level === 'sysop' ) { |
| 253 |
msg = mw.msg( 'template-protected' ); |
| 254 |
} else if ( r.level === 'autoconfirmed' ) { |
| 255 |
msg = mw.msg( 'template-semiprotected' ); |
| 256 |
} else { |
| 257 |
restrictionLevels.push( r.level ); |
| 258 |
} |
| 259 |
} ); |
| 260 |
|
| 261 |
|
| 262 |
if ( msg !== '' ) { |
| 263 |
return $.Deferred().resolve( msg ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
const msgs = []; |
| 269 |
restrictionLevels.forEach( ( level ) => { |
| 270 |
msgs.push( 'restriction-level-' + level ); |
| 271 |
} ); |
| 272 |
if ( msgs.length === 0 ) { |
| 273 |
return $.Deferred().resolve( '' ); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
return api.loadMessagesIfMissing( msgs ).then( () => { |
| 278 |
const localizedMessages = msgs.map( |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
( m ) => mw.message( m ).parse() |
| 283 |
); |
| 284 |
|
| 285 |
return parenthesesWrap( localizedMessages.join( mw.message( 'comma-separator' ).escaped() ) ); |
| 286 |
} ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
function showLanguageLinks( langLinks ) { |
| 297 |
const newList = langLinks.map( ( langLink ) => { |
| 298 |
const bcp47 = mw.language.bcp47( langLink.lang ); |
| 299 |
|
| 300 |
return $( '<li>' ) |
| 301 |
.addClass( 'interlanguage-link interwiki-' + langLink.lang ) |
| 302 |
.append( $( '<a>' ) |
| 303 |
.attr( { |
| 304 |
href: langLink.url, |
| 305 |
title: langLink.title + ' - ' + langLink.langname, |
| 306 |
lang: bcp47, |
| 307 |
hreflang: bcp47 |
| 308 |
} ) |
| 309 |
.text( langLink.autonym ) |
| 310 |
); |
| 311 |
} ); |
| 312 |
const $list = $( '#p-lang ul' ), |
| 313 |
$parent = $list.parent(); |
| 314 |
$list.detach().empty().append( newList ).prependTo( $parent ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
function showPreviewNotes( config, response ) { |
| 325 |
const arrow = $( document.body ).css( 'direction' ) === 'rtl' ? '←' : '→'; |
| 326 |
const $previewHeader = $( '<div>' ) |
| 327 |
.addClass( 'previewnote' ) |
| 328 |
.append( $( '<h2>' ) |
| 329 |
.attr( 'id', 'mw-previewheader' ) |
| 330 |
|
| 331 |
.append( config.previewHeader ) |
| 332 |
); |
| 333 |
|
| 334 |
const warningContentElement = $( '<div>' ) |
| 335 |
.append( |
| 336 |
|
| 337 |
config.previewNote, |
| 338 |
' ', |
| 339 |
$( '<span>' ) |
| 340 |
.addClass( 'mw-continue-editing' ) |
| 341 |
.append( $( '<a>' ) |
| 342 |
.attr( 'href', '#' + config.$formNode.attr( 'id' ) ) |
| 343 |
.text( arrow + ' ' + mw.msg( 'continue-editing' ) ) |
| 344 |
), |
| 345 |
response.parse.parsewarningshtml.map( ( warning ) => $( '<p>' ).append( warning ) ) |
| 346 |
)[ 0 ]; |
| 347 |
const warningMessageElement = util.messageBox( |
| 348 |
warningContentElement, |
| 349 |
'warning' |
| 350 |
); |
| 351 |
$previewHeader.append( warningMessageElement ); |
| 352 |
config.$previewNode.prepend( $previewHeader ); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
function showError( config, $message ) { |
| 363 |
const errorContentElement = $( '<div>' ) |
| 364 |
.append( |
| 365 |
$( '<strong>' ).text( mw.msg( 'previewerrortext' ) ), |
| 366 |
$message |
| 367 |
)[ 0 ]; |
| 368 |
const errorMessageElement = util.messageBox( errorContentElement, 'error' ); |
| 369 |
errorMessageElement.classList.add( 'mw-page-preview-error' ); |
| 370 |
config.$previewNode.hide().before( errorMessageElement ); |
| 371 |
if ( config.$diffNode ) { |
| 372 |
config.$diffNode.hide(); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
function handleParseResponse( config, response ) { |
| 384 |
let $content; |
| 385 |
|
| 386 |
|
| 387 |
if ( response.parse.jsconfigvars ) { |
| 388 |
mw.config.set( response.parse.jsconfigvars ); |
| 389 |
} |
| 390 |
if ( response.parse.modules ) { |
| 391 |
mw.loader.load( response.parse.modules.concat( |
| 392 |
response.parse.modulestyles |
| 393 |
) ); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
showIndicators( response.parse.indicators ); |
| 398 |
|
| 399 |
|
| 400 |
if ( response.parse.displaytitle ) { |
| 401 |
$( '#firstHeadingTitle' ).html( response.parse.displaytitle ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
if ( response.parse.categorieshtml ) { |
| 406 |
$content = $( $.parseHTML( response.parse.categorieshtml ) ); |
| 407 |
mw.hook( 'wikipage.categories' ).fire( $content ); |
| 408 |
$( '.catlinks[data-mw="interface"]' ).replaceWith( $content ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
if ( response.parse.sections ) { |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
mw.hook( 'wikipage.tableOfContents' ).fire( |
| 422 |
response.parse.hidetoc ? [] : response.parse.sections |
| 423 |
); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
if ( response.parse.templates ) { |
| 428 |
showTemplates( response.parse.templates ); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
if ( response.parse.limitreporthtml ) { |
| 433 |
$( '.limitreport' ).html( response.parse.limitreporthtml ) |
| 434 |
.find( '.mw-collapsible' ).makeCollapsible(); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) { |
| 439 |
showLanguageLinks( response.parse.langlinks ); |
| 440 |
} |
| 441 |
|
| 442 |
if ( !response.parse.text ) { |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
config.$previewNode.children( '.mw-parser-output' ).remove(); |
| 448 |
|
| 449 |
config.$previewNode.find( '.previewnote' ).remove(); |
| 450 |
|
| 451 |
if ( config.isLivePreview ) { |
| 452 |
showPreviewNotes( config, response ); |
| 453 |
} |
| 454 |
|
| 455 |
$content = $( $.parseHTML( response.parse.text ) ); |
| 456 |
|
| 457 |
config.$previewNode.append( $content ).show(); |
| 458 |
|
| 459 |
mw.hook( 'wikipage.content' ).fire( $content ); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
function getParseRequest( config, section ) { |
| 471 |
const params = { |
| 472 |
formatversion: 2, |
| 473 |
action: 'parse', |
| 474 |
summary: config.summary, |
| 475 |
prop: '' |
| 476 |
}; |
| 477 |
params[ config.titleParam ] = config.title; |
| 478 |
|
| 479 |
if ( !config.showDiff ) { |
| 480 |
params[ config.textParam ] = config.$textareaNode.textSelection( 'getContents' ); |
| 481 |
Object.assign( params, { |
| 482 |
prop: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|sections|templates|langlinks|limitreporthtml|parsewarningshtml', |
| 483 |
pst: true, |
| 484 |
preview: true, |
| 485 |
sectionpreview: section !== '', |
| 486 |
disableeditsection: true, |
| 487 |
useskin: mw.config.get( 'skin' ), |
| 488 |
uselang: mw.config.get( 'wgUserLanguage' ) |
| 489 |
} ); |
| 490 |
if ( mw.config.get( 'wgUserVariant' ) ) { |
| 491 |
params.variant = mw.config.get( 'wgUserVariant' ); |
| 492 |
} |
| 493 |
} |
| 494 |
if ( section === 'new' ) { |
| 495 |
params.section = 'new'; |
| 496 |
params.sectiontitle = params.summary; |
| 497 |
delete params.summary; |
| 498 |
} |
| 499 |
|
| 500 |
Object.assign( params, config.parseParams ); |
| 501 |
|
| 502 |
return api.post( params, { headers: { 'Promise-Non-Write-API-Action': 'true' } } ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
function getDiffTable() { |
| 511 |
return $( '<table>' ).addClass( 'diff' ).append( |
| 512 |
$( '<col>' ).addClass( 'diff-marker' ), |
| 513 |
$( '<col>' ).addClass( 'diff-content' ), |
| 514 |
$( '<col>' ).addClass( 'diff-marker' ), |
| 515 |
$( '<col>' ).addClass( 'diff-content' ), |
| 516 |
$( '<thead>' ).append( |
| 517 |
$( '<tr>' ).addClass( 'diff-title' ).append( |
| 518 |
$( '<td>' ) |
| 519 |
.attr( 'colspan', 2 ) |
| 520 |
.addClass( 'diff-otitle diff-side-deleted' ) |
| 521 |
.text( mw.msg( 'currentrev' ) ), |
| 522 |
$( '<td>' ) |
| 523 |
.attr( 'colspan', 2 ) |
| 524 |
.addClass( 'diff-ntitle diff-side-added' ) |
| 525 |
.text( mw.msg( 'yourtext' ) ) |
| 526 |
) |
| 527 |
), |
| 528 |
$( '<tbody>' ) |
| 529 |
); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
function handleDiffResponse( config, response ) { |
| 540 |
const $table = getDiffTable(); |
| 541 |
config.$diffNode |
| 542 |
.hide() |
| 543 |
.empty() |
| 544 |
.append( $table ); |
| 545 |
|
| 546 |
const diff = response.compare.bodies; |
| 547 |
if ( diff.main ) { |
| 548 |
$table.find( 'tbody' ).html( diff.main ); |
| 549 |
mw.hook( 'wikipage.diff' ).fire( $table ); |
| 550 |
} else { |
| 551 |
|
| 552 |
const $tableCell = $( '<td>' ) |
| 553 |
.attr( 'colspan', 4 ) |
| 554 |
.addClass( 'diff-notice' ) |
| 555 |
.append( |
| 556 |
$( '<div>' ) |
| 557 |
.addClass( 'mw-diff-empty' ) |
| 558 |
.text( mw.msg( 'diff-empty' ) ) |
| 559 |
); |
| 560 |
$table.find( 'tbody' ) |
| 561 |
.empty() |
| 562 |
.append( |
| 563 |
$( '<tr>' ).append( $tableCell ) |
| 564 |
); |
| 565 |
} |
| 566 |
config.$diffNode.show(); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
function getDiffRequest( config, section, pageExists ) { |
| 579 |
let contents = config.$textareaNode.textSelection( 'getContents' ), |
| 580 |
sectionTitle = config.summary; |
| 581 |
|
| 582 |
if ( section === 'new' ) { |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
sectionTitle = sectionTitle === '' ? '' : '== ' + sectionTitle + ' ==\n\n'; |
| 597 |
|
| 598 |
|
| 599 |
contents = sectionTitle + contents; |
| 600 |
} |
| 601 |
|
| 602 |
const params = { |
| 603 |
action: 'compare', |
| 604 |
fromtitle: config.title, |
| 605 |
totitle: config.title, |
| 606 |
toslots: 'main', |
| 607 |
|
| 608 |
|
| 609 |
'totext-main': contents.replace( /\s+$/, '' ), |
| 610 |
'tocontentmodel-main': mw.config.get( 'wgPageContentModel' ), |
| 611 |
topst: true, |
| 612 |
slots: 'main', |
| 613 |
uselang: mw.config.get( 'wgUserLanguage' ) |
| 614 |
}; |
| 615 |
if ( mw.config.get( 'wgUserVariant' ) ) { |
| 616 |
params.variant = mw.config.get( 'wgUserVariant' ); |
| 617 |
} |
| 618 |
if ( section ) { |
| 619 |
params[ 'tosection-main' ] = section; |
| 620 |
} |
| 621 |
if ( !pageExists ) { |
| 622 |
params.fromslots = 'main'; |
| 623 |
params[ 'fromcontentmodel-main' ] = mw.config.get( 'wgPageContentModel' ); |
| 624 |
params[ 'fromtext-main' ] = ''; |
| 625 |
} |
| 626 |
return api.post( params ); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
function getLoadingSelectors() { |
| 637 |
return [ |
| 638 |
|
| 639 |
'.mw-indicators', |
| 640 |
'#firstHeading', |
| 641 |
'#wikiPreview', |
| 642 |
'#wikiDiff', |
| 643 |
'#catlinks', |
| 644 |
'#p-lang', |
| 645 |
|
| 646 |
'.templatesUsed', |
| 647 |
'.limitreport', |
| 648 |
'.mw-summary-preview', |
| 649 |
'.hiddencats' |
| 650 |
]; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
function doPreview( config ) { |
| 700 |
config = Object.assign( { |
| 701 |
$previewNode: $( '#wikiPreview' ), |
| 702 |
$diffNode: $( '#wikiDiff' ), |
| 703 |
$formNode: $( '#editform' ), |
| 704 |
$textareaNode: $( '#wpTextbox1' ), |
| 705 |
$spinnerNode: $( '.mw-spinner-preview' ), |
| 706 |
summary: null, |
| 707 |
showDiff: false, |
| 708 |
isLivePreview: false, |
| 709 |
previewHeader: null, |
| 710 |
previewNote: null, |
| 711 |
title: mw.config.get( 'wgPageName' ), |
| 712 |
titleParam: 'title', |
| 713 |
textParam: 'text', |
| 714 |
parseParams: null, |
| 715 |
responseHandler: null, |
| 716 |
createSpinner: false, |
| 717 |
loadingSelectors: getLoadingSelectors() |
| 718 |
}, config ); |
| 719 |
|
| 720 |
const section = config.$formNode.find( '[name="wpSection"]' ).val(); |
| 721 |
|
| 722 |
if ( !config.$textareaNode || config.$textareaNode.length === 0 ) { |
| 723 |
return; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
if ( !config.summary ) { |
| 728 |
const $summaryWidget = $( '#wpSummaryWidget' ); |
| 729 |
if ( $summaryWidget.length ) { |
| 730 |
config.summary = OO.ui.infuse( $summaryWidget ).getValue(); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if ( config.isLivePreview ) { |
| 735 |
|
| 736 |
$( '.mw-newarticletext, .mw-page-preview-error' ).remove(); |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
config.$previewNode.show(); |
| 741 |
|
| 742 |
|
| 743 |
config.$previewNode[ 0 ].scrollIntoView(); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
if ( config.$spinnerNode && config.$spinnerNode.length ) { |
| 748 |
config.$spinnerNode.show(); |
| 749 |
} else if ( config.createSpinner ) { |
| 750 |
if ( mw.loader.getState( 'jquery.spinner' ) === 'ready' ) { |
| 751 |
config.$spinnerNode = $.createSpinner( { |
| 752 |
size: 'large', |
| 753 |
type: 'block' |
| 754 |
} ) |
| 755 |
.addClass( 'mw-spinner-preview' ) |
| 756 |
.insertBefore( config.$previewNode ); |
| 757 |
} else { |
| 758 |
mw.log.warn( 'createSpinner requires the module jquery.spinner' ); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
const $loadingElements = $( config.loadingSelectors.join( ',' ) ); |
| 764 |
$loadingElements.addClass( [ 'mw-preview-loading-elements', 'mw-preview-loading-elements-loading' ] ); |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
const tempUserNamePromise = mw.user.acquireTempUserName(); |
| 769 |
|
| 770 |
let diffRequest; |
| 771 |
|
| 772 |
const parseRequest = tempUserNamePromise.then( () => getParseRequest( config, section ) ); |
| 773 |
|
| 774 |
if ( config.showDiff ) { |
| 775 |
config.$previewNode.hide(); |
| 776 |
|
| 777 |
|
| 778 |
if ( config.$diffNode.length === 0 && config.$previewNode.length > 0 ) { |
| 779 |
const rtlDir = $( '#wpTextbox1' ).attr( 'dir' ) === 'rtl'; |
| 780 |
const alignStart = rtlDir ? 'right' : 'left'; |
| 781 |
config.$diffNode = $( '<div>' ) |
| 782 |
.attr( 'id', 'wikiDiff' ) |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
.addClass( 'diff-editfont-' + mw.user.options.get( 'editfont' ) ) |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
.addClass( 'diff-contentalign-' + alignStart ); |
| 792 |
config.$previewNode.after( config.$diffNode ); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
mw.hook( 'wikipage.tableOfContents' ).fire( [] ); |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
diffRequest = parseRequest.then( ( parseResponse ) => getDiffRequest( config, section, parseResponse.parse.pageid !== 0 ) ); |
| 804 |
|
| 805 |
} else if ( config.$diffNode ) { |
| 806 |
config.$diffNode.hide(); |
| 807 |
} |
| 808 |
|
| 809 |
return $.when( parseRequest, diffRequest ) |
| 810 |
.done( ( parseResponse, diffResponse ) => { |
| 811 |
if ( config.responseHandler ) { |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
if ( config.showDiff ) { |
| 819 |
config.responseHandler( config, parseResponse[ 0 ], diffResponse[ 0 ] ); |
| 820 |
} else { |
| 821 |
config.responseHandler( config, parseResponse[ 0 ] ); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
showEditSummary( config.$formNode, parseResponse[ 0 ] ); |
| 826 |
|
| 827 |
if ( config.showDiff ) { |
| 828 |
handleDiffResponse( config, diffResponse[ 0 ] ); |
| 829 |
} else { |
| 830 |
handleParseResponse( config, parseResponse[ 0 ] ); |
| 831 |
} |
| 832 |
|
| 833 |
mw.hook( 'wikipage.editform' ).fire( config.$formNode ); |
| 834 |
} ) |
| 835 |
.fail( ( _code, result ) => { |
| 836 |
if ( config.isLivePreview ) { |
| 837 |
|
| 838 |
showError( config, api.getErrorMessage( result ) ); |
| 839 |
} |
| 840 |
} ) |
| 841 |
.always( () => { |
| 842 |
if ( config.$spinnerNode && config.$spinnerNode.length ) { |
| 843 |
config.$spinnerNode.hide(); |
| 844 |
} |
| 845 |
$loadingElements.removeClass( 'mw-preview-loading-elements-loading' ); |
| 846 |
} ); |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
module.exports = { |
| 859 |
doPreview: doPreview, |
| 860 |
getLoadingSelectors: getLoadingSelectors |
| 861 |
}; |
| 862 |
|
| 863 |
}() ); |