| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
( function () { |
| 22 |
const hasOwn = Object.hasOwnProperty, |
| 23 |
specCache = {}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
ve.dm.MWTransclusionModel = function VeDmMWTransclusionModel( doc ) { |
| 37 |
|
| 38 |
OO.EventEmitter.call( this ); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
this.doc = doc; |
| 49 |
this.parts = []; |
| 50 |
this.uid = 0; |
| 51 |
this.templateDataApiRequests = []; |
| 52 |
this.changeQueue = []; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
OO.mixinClass( ve.dm.MWTransclusionModel, OO.EventEmitter ); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment, forceType ) { |
| 92 |
const deferred = ve.createDeferred(), |
| 93 |
baseNodeClass = ve.dm.MWTransclusionNode; |
| 94 |
|
| 95 |
const insertNode = ( isInline, generatedContents ) => { |
| 96 |
const type = isInline ? baseNodeClass.static.inlineType : baseNodeClass.static.blockType, |
| 97 |
data = [ |
| 98 |
{ |
| 99 |
type: type, |
| 100 |
attributes: { |
| 101 |
mw: this.getPlainObject() |
| 102 |
} |
| 103 |
}, |
| 104 |
{ type: '/' + type } |
| 105 |
]; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if ( generatedContents ) { |
| 110 |
const nodeClass = ve.dm.modelRegistry.lookup( type ); |
| 111 |
const store = surfaceFragment.getDocument().getStore(); |
| 112 |
const hash = OO.getHash( [ nodeClass.static.getHashObjectForRendering( data[ 0 ] ), undefined ] ); |
| 113 |
store.hash( generatedContents, hash ); |
| 114 |
} |
| 115 |
|
| 116 |
surfaceFragment.insertContent( data ); |
| 117 |
|
| 118 |
deferred.resolve(); |
| 119 |
}; |
| 120 |
|
| 121 |
if ( forceType ) { |
| 122 |
insertNode( forceType === 'inline' ); |
| 123 |
} else { |
| 124 |
ve.init.target.parseWikitextFragment( |
| 125 |
baseNodeClass.static.getWikitext( this.getPlainObject() ), |
| 126 |
true, |
| 127 |
surfaceFragment.getDocument() |
| 128 |
).then( ( response ) => { |
| 129 |
if ( ve.getProp( response, 'visualeditor', 'result' ) === 'success' ) { |
| 130 |
|
| 131 |
|
| 132 |
let contentNodes = $.parseHTML( response.visualeditor.content, surfaceFragment.getDocument().getHtmlDocument() ) || []; |
| 133 |
contentNodes = ve.ce.MWTransclusionNode.static.filterRendering( contentNodes ); |
| 134 |
insertNode( |
| 135 |
baseNodeClass.static.isHybridInline( contentNodes, ve.dm.converter ), |
| 136 |
contentNodes |
| 137 |
); |
| 138 |
} else { |
| 139 |
|
| 140 |
insertNode( true ); |
| 141 |
} |
| 142 |
}, () => { |
| 143 |
insertNode( true ); |
| 144 |
} ); |
| 145 |
} |
| 146 |
return deferred.promise(); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
ve.dm.MWTransclusionModel.prototype.updateTransclusionNode = function ( surfaceModel, node ) { |
| 156 |
const obj = this.getPlainObject(); |
| 157 |
|
| 158 |
if ( obj !== null ) { |
| 159 |
surfaceModel.getLinearFragment( node.getOuterRange(), true ) |
| 160 |
.changeAttributes( { mw: obj } ); |
| 161 |
} else { |
| 162 |
surfaceModel.getLinearFragment( node.getOuterRange(), true ) |
| 163 |
.removeContent(); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ve.dm.MWTransclusionModel.prototype.load = function ( data ) { |
| 174 |
const promises = []; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if ( data.params && data.target ) { |
| 179 |
data = { parts: [ { template: data } ] }; |
| 180 |
} |
| 181 |
|
| 182 |
if ( Array.isArray( data.parts ) ) { |
| 183 |
for ( let i = 0; i < data.parts.length; i++ ) { |
| 184 |
const part = data.parts[ i ]; |
| 185 |
if ( part.template ) { |
| 186 |
const deferred = ve.createDeferred(); |
| 187 |
promises.push( deferred.promise() ); |
| 188 |
this.changeQueue.push( { |
| 189 |
add: ve.dm.MWTemplateModel.newFromData( this, part.template ), |
| 190 |
deferred: deferred |
| 191 |
} ); |
| 192 |
} else if ( typeof part === 'string' ) { |
| 193 |
const deferred = ve.createDeferred(); |
| 194 |
promises.push( deferred.promise() ); |
| 195 |
this.changeQueue.push( { |
| 196 |
add: new ve.dm.MWTransclusionContentModel( this, part ), |
| 197 |
deferred: deferred |
| 198 |
} ); |
| 199 |
} |
| 200 |
} |
| 201 |
setTimeout( this.processChangeQueue.bind( this ) ); |
| 202 |
} |
| 203 |
|
| 204 |
return ve.promiseAll( promises ); |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
ve.dm.MWTransclusionModel.prototype.resolveChangeQueue = function ( queue ) { |
| 217 |
const resolveQueue = []; |
| 218 |
|
| 219 |
for ( let i = 0; i < queue.length; i++ ) { |
| 220 |
const item = queue[ i ]; |
| 221 |
let remove = 0; |
| 222 |
|
| 223 |
if ( item.add instanceof ve.dm.MWTemplateModel ) { |
| 224 |
const title = item.add.getTemplateDataQueryTitle(); |
| 225 |
if ( hasOwn.call( specCache, title ) && specCache[ title ] ) { |
| 226 |
item.add.getSpec().setTemplateData( specCache[ title ] ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
let index = item.index; |
| 232 |
|
| 233 |
const existing = this.parts.indexOf( item.add ); |
| 234 |
if ( existing !== -1 ) { |
| 235 |
this.removePart( item.add ); |
| 236 |
if ( index && existing + 1 < index ) { |
| 237 |
index--; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if ( index === undefined && item.remove ) { |
| 242 |
index = this.parts.indexOf( item.remove ); |
| 243 |
if ( index !== -1 ) { |
| 244 |
remove = 1; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if ( index === undefined || index === -1 ) { |
| 249 |
index = this.parts.length; |
| 250 |
} |
| 251 |
|
| 252 |
this.parts.splice( index, remove, item.add ); |
| 253 |
if ( item.add ) { |
| 254 |
|
| 255 |
|
| 256 |
item.add.connect( this, { change: [ 'emit', 'change' ] } ); |
| 257 |
} |
| 258 |
if ( item.remove ) { |
| 259 |
item.remove.disconnect( this ); |
| 260 |
} |
| 261 |
this.emit( 'replace', item.remove || null, item.add, index ); |
| 262 |
|
| 263 |
|
| 264 |
if ( item.deferred ) { |
| 265 |
resolveQueue.push( item.deferred ); |
| 266 |
} |
| 267 |
} |
| 268 |
this.emit( 'change' ); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
resolveQueue.forEach( ( queueItem ) => { |
| 274 |
queueItem.resolve(); |
| 275 |
} ); |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ve.dm.MWTransclusionModel.prototype.processChangeQueue = function () { |
| 282 |
const templateNamespaceId = mw.config.get( 'wgNamespaceIds' ).template, |
| 283 |
titles = []; |
| 284 |
|
| 285 |
if ( !this.changeQueue.length ) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
const queue = this.changeQueue.slice(); |
| 290 |
|
| 291 |
|
| 292 |
this.changeQueue.length = 0; |
| 293 |
|
| 294 |
|
| 295 |
for ( let i = 0; i < queue.length; i++ ) { |
| 296 |
const item = queue[ i ]; |
| 297 |
if ( item.add instanceof ve.dm.MWTemplateModel ) { |
| 298 |
const title = item.add.getTemplateDataQueryTitle(), |
| 299 |
mwTitle = title ? mw.Title.newFromText( title, templateNamespaceId ) : null; |
| 300 |
if ( |
| 301 |
|
| 302 |
mwTitle && |
| 303 |
|
| 304 |
!hasOwn.call( specCache, title ) && |
| 305 |
|
| 306 |
titles.indexOf( title ) === -1 |
| 307 |
) { |
| 308 |
titles.push( title ); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
if ( !titles.length ) { |
| 315 |
setTimeout( this.resolveChangeQueue.bind( this, queue ) ); |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
this.templateDataApiRequests.push( this.callTemplateDataApi( titles, queue ) ); |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
ve.dm.MWTransclusionModel.prototype.callTemplateDataApi = function ( titles, queue ) { |
| 329 |
const xhr = ve.init.target.getContentApi( this.doc ).get( { |
| 330 |
action: 'templatedata', |
| 331 |
titles: titles, |
| 332 |
lang: mw.config.get( 'wgUserLanguage' ), |
| 333 |
includeMissingTitles: '1', |
| 334 |
redirects: '1' |
| 335 |
} ).done( this.cacheTemplateDataApiResponse.bind( this ) ); |
| 336 |
xhr.always( |
| 337 |
this.markRequestAsDone.bind( this, xhr ), |
| 338 |
this.resolveChangeQueue.bind( this, queue ) |
| 339 |
); |
| 340 |
return xhr; |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
ve.dm.MWTransclusionModel.prototype.cacheTemplateDataApiResponse = function ( data ) { |
| 349 |
if ( !data || !data.pages ) { |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
for ( const id in data.pages ) { |
| 355 |
const title = data.pages[ id ].title; |
| 356 |
|
| 357 |
if ( data.pages[ id ].missing ) { |
| 358 |
|
| 359 |
|
| 360 |
const missingTitle = {}; |
| 361 |
missingTitle[ title ] = { missing: true }; |
| 362 |
ve.init.platform.linkCache.setMissing( missingTitle ); |
| 363 |
} else if ( data.pages[ id ].notemplatedata && !OO.isPlainObject( data.pages[ id ].params ) ) { |
| 364 |
|
| 365 |
|
| 366 |
specCache[ title ] = null; |
| 367 |
} else { |
| 368 |
specCache[ title ] = data.pages[ id ]; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
const aliasMap = data.redirects || []; |
| 374 |
|
| 375 |
if ( data.normalized ) { |
| 376 |
ve.batchPush( aliasMap, data.normalized ); |
| 377 |
} |
| 378 |
|
| 379 |
for ( let i = 0; i < aliasMap.length; i++ ) { |
| 380 |
|
| 381 |
|
| 382 |
if ( hasOwn.call( specCache, aliasMap[ i ].to ) ) { |
| 383 |
specCache[ aliasMap[ i ].from ] = specCache[ aliasMap[ i ].to ]; |
| 384 |
} |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
ve.dm.MWTransclusionModel.prototype.markRequestAsDone = function ( apiPromise ) { |
| 393 |
|
| 394 |
const index = this.templateDataApiRequests.indexOf( apiPromise ); |
| 395 |
if ( index !== -1 ) { |
| 396 |
this.templateDataApiRequests.splice( index, 1 ); |
| 397 |
} |
| 398 |
}; |
| 399 |
|
| 400 |
ve.dm.MWTransclusionModel.prototype.abortAllApiRequests = function () { |
| 401 |
for ( let i = 0; i < this.templateDataApiRequests.length; i++ ) { |
| 402 |
this.templateDataApiRequests[ i ].abort(); |
| 403 |
} |
| 404 |
this.templateDataApiRequests.length = 0; |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
ve.dm.MWTransclusionModel.prototype.getPlainObject = function () { |
| 413 |
const parts = []; |
| 414 |
|
| 415 |
for ( let i = 0; i < this.parts.length; i++ ) { |
| 416 |
const part = this.parts[ i ]; |
| 417 |
const serialization = part.serialize(); |
| 418 |
if ( serialization !== undefined && serialization !== '' ) { |
| 419 |
parts.push( serialization ); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
return parts.length ? { parts: parts } : null; |
| 424 |
}; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
ve.dm.MWTransclusionModel.prototype.nextUniquePartId = function () { |
| 431 |
return 'part_' + this.uid++; |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
ve.dm.MWTransclusionModel.prototype.replacePart = function ( remove, add ) { |
| 444 |
const deferred = ve.createDeferred(); |
| 445 |
if ( |
| 446 |
!( remove instanceof ve.dm.MWTransclusionPartModel ) || |
| 447 |
!( add instanceof ve.dm.MWTransclusionPartModel ) |
| 448 |
) { |
| 449 |
throw new Error( 'Invalid transclusion part' ); |
| 450 |
} |
| 451 |
this.changeQueue.push( { remove: remove, add: add, deferred: deferred } ); |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
setTimeout( this.processChangeQueue.bind( this ) ); |
| 456 |
|
| 457 |
return deferred.promise(); |
| 458 |
}; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
ve.dm.MWTransclusionModel.prototype.addPart = function ( part, index ) { |
| 469 |
const deferred = ve.createDeferred(); |
| 470 |
if ( !( part instanceof ve.dm.MWTransclusionPartModel ) ) { |
| 471 |
throw new Error( 'Invalid transclusion part' ); |
| 472 |
} |
| 473 |
this.changeQueue.push( { add: part, index: index, deferred: deferred } ); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
setTimeout( this.processChangeQueue.bind( this ) ); |
| 478 |
|
| 479 |
return deferred.promise(); |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
ve.dm.MWTransclusionModel.prototype.removePart = function ( part ) { |
| 487 |
const index = this.parts.indexOf( part ); |
| 488 |
if ( index !== -1 ) { |
| 489 |
this.parts.splice( index, 1 ); |
| 490 |
part.disconnect( this ); |
| 491 |
this.emit( 'replace', part, null ); |
| 492 |
} |
| 493 |
}; |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
ve.dm.MWTransclusionModel.prototype.isEmpty = function () { |
| 499 |
return this.parts.every( ( part ) => part instanceof ve.dm.MWTemplatePlaceholderModel ); |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
ve.dm.MWTransclusionModel.prototype.isSingleTemplate = function () { |
| 506 |
return this.parts.length === 1 && ( |
| 507 |
this.parts[ 0 ] instanceof ve.dm.MWTemplateModel || |
| 508 |
this.parts[ 0 ] instanceof ve.dm.MWTemplatePlaceholderModel |
| 509 |
); |
| 510 |
}; |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
ve.dm.MWTransclusionModel.prototype.getParts = function () { |
| 516 |
return this.parts; |
| 517 |
}; |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
ve.dm.MWTransclusionModel.prototype.getPartFromId = function ( id ) { |
| 526 |
if ( !id ) { |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
const partId = id.split( '/', 1 )[ 0 ]; |
| 533 |
|
| 534 |
for ( let i = 0; i < this.parts.length; i++ ) { |
| 535 |
if ( this.parts[ i ].getId() === partId ) { |
| 536 |
return this.parts[ i ]; |
| 537 |
} |
| 538 |
} |
| 539 |
}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
ve.dm.MWTransclusionModel.prototype.getIndex = function ( model ) { |
| 550 |
const parts = this.parts; |
| 551 |
|
| 552 |
let index = 0; |
| 553 |
for ( let i = 0; i < parts.length; i++ ) { |
| 554 |
const part = parts[ i ]; |
| 555 |
if ( part === model ) { |
| 556 |
return index; |
| 557 |
} |
| 558 |
index++; |
| 559 |
if ( part instanceof ve.dm.MWTemplateModel ) { |
| 560 |
const names = part.getOrderedParameterNames(); |
| 561 |
for ( let j = 0; j < names.length; j++ ) { |
| 562 |
if ( part.getParameter( names[ j ] ) === model ) { |
| 563 |
return index; |
| 564 |
} |
| 565 |
index++; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
return -1; |
| 570 |
}; |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
ve.dm.MWTransclusionModel.prototype.addPromptedParameters = function () { |
| 576 |
this.parts.forEach( ( part ) => { |
| 577 |
if ( part instanceof ve.dm.MWTemplateModel ) { |
| 578 |
part.addPromptedParameters(); |
| 579 |
} |
| 580 |
} ); |
| 581 |
}; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
ve.dm.MWTransclusionModel.prototype.containsValuableData = function () { |
| 587 |
return this.parts.some( ( part ) => part.containsValuableData() ); |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
ve.dm.MWTransclusionModel.prototype.reset = function () { |
| 594 |
this.parts = []; |
| 595 |
this.uid = 0; |
| 596 |
this.templateDataApiRequests = []; |
| 597 |
this.changeQueue = []; |
| 598 |
}; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
mw.log.deprecate( ve.dm.MWTransclusionModel.prototype, 'abortRequests', |
| 603 |
ve.dm.MWTransclusionModel.prototype.abortAllApiRequests, |
| 604 |
'Use "abortAllApiRequests" instead.' |
| 605 |
); |
| 606 |
|
| 607 |
}() ); |