2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTransclusionModel class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
2014-10-07 00:06:02 +00:00
|
|
|
|
2021-06-24 07:53:46 +00:00
|
|
|
/**
|
|
|
|
* Object literal
|
2023-01-16 15:06:40 +00:00
|
|
|
*
|
2021-06-24 07:53:46 +00:00
|
|
|
* @class ve.dm.MWTransclusionPartInstruction
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @property {ve.dm.MWTransclusionPartModel} [remove]
|
|
|
|
* @property {ve.dm.MWTransclusionPartModel} [add]
|
|
|
|
* @property {number} [index]
|
|
|
|
* @property {jQuery.Deferred} [deferred]
|
|
|
|
*/
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
( function () {
|
2014-06-26 10:41:18 +00:00
|
|
|
var hasOwn = Object.hasOwnProperty,
|
|
|
|
specCache = {};
|
|
|
|
|
|
|
|
/**
|
2021-06-03 16:25:15 +00:00
|
|
|
* Represents a MediaWiki transclusion, i.e. a sequence of one or more template invocations that
|
2021-06-04 12:43:21 +00:00
|
|
|
* strictly belong to each other (e.g. because they are unbalanced), possibly mixed with raw
|
2021-06-24 07:53:46 +00:00
|
|
|
* wikitext snippets. These individual "parts" are subclasses of ve.dm.MWTransclusionPartModel.
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
|
|
|
* @class
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes OO.EventEmitter
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2018-05-04 13:30:10 +00:00
|
|
|
* @param {ve.dm.Document} doc Document to use associate with API requests
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2018-05-04 13:30:10 +00:00
|
|
|
ve.dm.MWTransclusionModel = function VeDmMWTransclusionModel( doc ) {
|
2014-06-26 10:41:18 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
2021-06-24 07:53:46 +00:00
|
|
|
/**
|
|
|
|
* @property {ve.dm.MWTransclusionPartModel[]} parts
|
|
|
|
* @property {number} uid
|
|
|
|
* @property {jQuery.Promise[]} templateDataApiRequests Currently running API requests. The only
|
|
|
|
* reason to keep these around is to be able to abort them earlier when the template dialog
|
|
|
|
* closes or resets.
|
|
|
|
* @property {Object[]} changeQueue
|
|
|
|
*/
|
2018-05-04 13:30:10 +00:00
|
|
|
this.doc = doc;
|
2014-06-26 10:41:18 +00:00
|
|
|
this.parts = [];
|
|
|
|
this.uid = 0;
|
2021-07-06 13:43:13 +00:00
|
|
|
this.templateDataApiRequests = [];
|
|
|
|
this.changeQueue = [];
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.mixinClass( ve.dm.MWTransclusionModel, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2021-07-15 15:07:21 +00:00
|
|
|
* Emitted when a part is added, removed, replaced (e.g. a placeholder with an actual template),
|
|
|
|
* or an existing part changed position.
|
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.dm.MWTransclusionPartInstruction#replace
|
2014-06-26 10:41:18 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} removed Removed part
|
2021-07-15 15:07:21 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} added Added or moved part
|
|
|
|
* @param {number} [newPosition] Position the part was added or moved to
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-07-15 15:07:21 +00:00
|
|
|
* Emitted when anything changed, including any changes in the content of the parts.
|
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.dm.MWTransclusionPartInstruction#change
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert transclusion at the end of a surface fragment.
|
|
|
|
*
|
2016-05-15 11:08:13 +00:00
|
|
|
* If forceType is not specified and this is used in async mode, users of this method
|
|
|
|
* should ensure the surface is not accessible while the type is being evaluated.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.SurfaceFragment} surfaceFragment Surface fragment after which to insert.
|
2022-02-14 15:18:57 +00:00
|
|
|
* @param {string} [forceType] Force the type to 'inline' or 'block'. If not
|
2016-05-15 11:08:13 +00:00
|
|
|
* specified it will be evaluated asynchronously.
|
|
|
|
* @return {jQuery.Promise} Promise which resolves when the node has been inserted. If
|
|
|
|
* forceType was specified this will be instant.
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2016-05-15 11:08:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment, forceType ) {
|
|
|
|
var model = this,
|
2019-11-02 05:06:28 +00:00
|
|
|
deferred = ve.createDeferred(),
|
2017-02-07 21:35:06 +00:00
|
|
|
baseNodeClass = ve.dm.MWTransclusionNode;
|
2016-05-15 11:08:13 +00:00
|
|
|
|
2017-02-07 21:35:06 +00:00
|
|
|
function insertNode( isInline, generatedContents ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var type = isInline ? baseNodeClass.static.inlineType : baseNodeClass.static.blockType,
|
2016-05-26 16:10:52 +00:00
|
|
|
data = [
|
|
|
|
{
|
|
|
|
type: type,
|
|
|
|
attributes: {
|
|
|
|
mw: model.getPlainObject()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ type: '/' + type }
|
|
|
|
];
|
|
|
|
|
2017-02-07 21:35:06 +00:00
|
|
|
// If we just fetched the generated contents, put them in the store
|
|
|
|
// so we don't do a duplicate API call later.
|
|
|
|
if ( generatedContents ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var nodeClass = ve.dm.modelRegistry.lookup( type );
|
|
|
|
var store = surfaceFragment.getDocument().getStore();
|
|
|
|
var hash = OO.getHash( [ nodeClass.static.getHashObjectForRendering( data[ 0 ] ), undefined ] );
|
2018-03-06 12:44:37 +00:00
|
|
|
store.hash( generatedContents, hash );
|
2017-02-07 21:35:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 16:56:52 +00:00
|
|
|
surfaceFragment.insertContent( data );
|
|
|
|
|
2016-05-15 11:08:13 +00:00
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( forceType ) {
|
|
|
|
insertNode( forceType === 'inline' );
|
|
|
|
} else {
|
2018-04-06 11:30:57 +00:00
|
|
|
ve.init.target.parseWikitextFragment(
|
|
|
|
baseNodeClass.static.getWikitext( this.getPlainObject() ),
|
|
|
|
true,
|
|
|
|
surfaceFragment.getDocument()
|
|
|
|
).then( function ( response ) {
|
2016-10-28 00:22:30 +00:00
|
|
|
if ( ve.getProp( response, 'visualeditor', 'result' ) === 'success' ) {
|
2019-11-04 12:16:14 +00:00
|
|
|
// This method is only ever run by a client, so it is okay to use jQuery
|
|
|
|
// eslint-disable-next-line no-undef
|
2021-06-21 08:10:27 +00:00
|
|
|
var contentNodes = $.parseHTML( response.visualeditor.content, surfaceFragment.getDocument().getHtmlDocument() ) || [];
|
2016-10-28 00:22:30 +00:00
|
|
|
contentNodes = ve.ce.MWTransclusionNode.static.filterRendering( contentNodes );
|
|
|
|
insertNode(
|
2017-02-07 21:35:06 +00:00
|
|
|
baseNodeClass.static.isHybridInline( contentNodes, ve.dm.converter ),
|
|
|
|
contentNodes
|
2016-10-28 00:22:30 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Request failed - just assume inline
|
2016-05-15 11:08:13 +00:00
|
|
|
insertNode( true );
|
2016-10-28 00:22:30 +00:00
|
|
|
}
|
|
|
|
}, function () {
|
|
|
|
insertNode( true );
|
|
|
|
} );
|
2016-05-15 11:08:13 +00:00
|
|
|
}
|
|
|
|
return deferred.promise();
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update transclusion node in a document.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Surface} surfaceModel Surface model of main document
|
|
|
|
* @param {ve.dm.MWTransclusionNode} node Transclusion node to update
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.updateTransclusionNode = function ( surfaceModel, node ) {
|
|
|
|
var obj = this.getPlainObject();
|
|
|
|
|
|
|
|
if ( obj !== null ) {
|
2014-09-30 16:34:33 +00:00
|
|
|
surfaceModel.getLinearFragment( node.getOuterRange(), true )
|
2014-08-22 20:50:48 +00:00
|
|
|
.changeAttributes( { mw: obj } );
|
2014-06-26 10:41:18 +00:00
|
|
|
} else {
|
2014-09-30 16:34:33 +00:00
|
|
|
surfaceModel.getLinearFragment( node.getOuterRange(), true )
|
2014-06-26 10:41:18 +00:00
|
|
|
.removeContent();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load from transclusion data, and fetch spec from server.
|
|
|
|
*
|
|
|
|
* @param {Object} data Transclusion data
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {jQuery.Promise} Promise, resolved when spec is loaded
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.load = function ( data ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var promises = [];
|
2014-06-26 10:41:18 +00:00
|
|
|
|
|
|
|
// Convert single part format to multi-part format
|
|
|
|
// Parsoid doesn't use this format any more, but we accept it for backwards compatibility
|
|
|
|
if ( data.params && data.target ) {
|
2014-08-22 20:50:48 +00:00
|
|
|
data = { parts: [ { template: data } ] };
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2014-09-24 00:58:09 +00:00
|
|
|
if ( Array.isArray( data.parts ) ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < data.parts.length; i++ ) {
|
|
|
|
var part = data.parts[ i ];
|
2021-10-13 12:57:45 +00:00
|
|
|
var deferred;
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( part.template ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
deferred = ve.createDeferred();
|
2014-06-26 10:41:18 +00:00
|
|
|
promises.push( deferred.promise() );
|
2021-07-06 13:43:13 +00:00
|
|
|
this.changeQueue.push( {
|
2014-08-22 20:50:48 +00:00
|
|
|
add: ve.dm.MWTemplateModel.newFromData( this, part.template ),
|
|
|
|
deferred: deferred
|
2014-06-26 10:41:18 +00:00
|
|
|
} );
|
|
|
|
} else if ( typeof part === 'string' ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
deferred = ve.createDeferred();
|
2014-06-26 10:41:18 +00:00
|
|
|
promises.push( deferred.promise() );
|
2021-07-06 13:43:13 +00:00
|
|
|
this.changeQueue.push( {
|
2015-01-11 17:38:36 +00:00
|
|
|
add: new ve.dm.MWTransclusionContentModel( this, part ),
|
2014-08-22 20:50:48 +00:00
|
|
|
deferred: deferred
|
2014-06-26 10:41:18 +00:00
|
|
|
} );
|
2014-04-10 18:55:24 +00:00
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
2021-07-06 13:43:13 +00:00
|
|
|
setTimeout( this.processChangeQueue.bind( this ) );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
|
2019-11-02 16:29:11 +00:00
|
|
|
return ve.promiseAll( promises );
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process one or more queue items.
|
|
|
|
*
|
2021-07-06 13:43:13 +00:00
|
|
|
* @private
|
2021-06-24 07:53:46 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartInstruction[]} queue List of objects containing parts to add and optionally
|
|
|
|
* indexes to add them at, if no index is given parts will be added at the end
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWTransclusionPartInstruction#replace For each item added
|
|
|
|
* @fires ve.dm.MWTransclusionPartInstruction#change
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2021-07-06 13:43:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.resolveChangeQueue = function ( queue ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var resolveQueue = [];
|
2014-06-26 10:41:18 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
for ( var i = 0; i < queue.length; i++ ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var item = queue[ i ],
|
|
|
|
remove = 0;
|
2014-06-26 10:41:18 +00:00
|
|
|
|
|
|
|
if ( item.add instanceof ve.dm.MWTemplateModel ) {
|
2021-09-17 14:13:19 +00:00
|
|
|
var title = item.add.getTemplateDataQueryTitle();
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( hasOwn.call( specCache, title ) && specCache[ title ] ) {
|
2021-07-01 09:00:44 +00:00
|
|
|
item.add.getSpec().setTemplateData( specCache[ title ] );
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
// Use specified index
|
2021-06-21 08:10:27 +00:00
|
|
|
var index = item.index;
|
2014-06-26 10:41:18 +00:00
|
|
|
// Auto-remove if already existing, preserving index
|
2021-06-21 08:10:27 +00:00
|
|
|
var existing = this.parts.indexOf( item.add );
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( existing !== -1 ) {
|
|
|
|
this.removePart( item.add );
|
|
|
|
if ( index && existing + 1 < index ) {
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Derive index from removal if given
|
|
|
|
if ( index === undefined && item.remove ) {
|
2015-03-10 12:50:42 +00:00
|
|
|
index = this.parts.indexOf( item.remove );
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( index !== -1 ) {
|
|
|
|
remove = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Use last index as a last resort
|
|
|
|
if ( index === undefined || index === -1 ) {
|
|
|
|
index = this.parts.length;
|
|
|
|
}
|
2013-06-29 02:37:42 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
this.parts.splice( index, remove, item.add );
|
|
|
|
if ( item.add ) {
|
2021-08-20 12:52:39 +00:00
|
|
|
// This forwards change events from the nested ve.dm.MWTransclusionPartModel upwards.
|
2021-07-15 15:07:21 +00:00
|
|
|
// The array syntax is a way to call `this.emit( 'change' )`.
|
2014-08-22 20:50:48 +00:00
|
|
|
item.add.connect( this, { change: [ 'emit', 'change' ] } );
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
|
|
|
if ( item.remove ) {
|
|
|
|
item.remove.disconnect( this );
|
|
|
|
}
|
2021-07-15 15:07:21 +00:00
|
|
|
this.emit( 'replace', item.remove || null, item.add, index );
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
// Resolve promises
|
|
|
|
if ( item.deferred ) {
|
2014-12-09 23:11:41 +00:00
|
|
|
resolveQueue.push( item.deferred );
|
2013-06-29 02:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
this.emit( 'change' );
|
2014-12-09 23:11:41 +00:00
|
|
|
|
|
|
|
// We need to go back and resolve the deferreds after emitting change.
|
|
|
|
// Otherwise we get silly situations like a single change event being
|
|
|
|
// guaranteed after the transclusion loaded promise gets resolved.
|
2021-10-13 12:57:45 +00:00
|
|
|
resolveQueue.forEach( function ( queueItem ) {
|
|
|
|
queueItem.resolve();
|
|
|
|
} );
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.processChangeQueue = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
var templateNamespaceId = mw.config.get( 'wgNamespaceIds' ).template,
|
2021-07-06 13:47:32 +00:00
|
|
|
titles = [];
|
2015-08-19 18:05:01 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
if ( !this.changeQueue.length ) {
|
2014-06-26 10:41:18 +00:00
|
|
|
return;
|
2014-02-05 19:21:19 +00:00
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
var queue = this.changeQueue.slice();
|
2014-06-26 10:41:18 +00:00
|
|
|
|
|
|
|
// Clear shared queue for future calls
|
2021-07-06 13:43:13 +00:00
|
|
|
this.changeQueue.length = 0;
|
2014-06-26 10:41:18 +00:00
|
|
|
|
|
|
|
// Get unique list of template titles that aren't already loaded
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < queue.length; i++ ) {
|
|
|
|
var item = queue[ i ];
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( item.add instanceof ve.dm.MWTemplateModel ) {
|
2021-09-17 14:13:19 +00:00
|
|
|
var title = item.add.getTemplateDataQueryTitle(),
|
2021-08-26 12:40:05 +00:00
|
|
|
mwTitle = title ? mw.Title.newFromText( title, templateNamespaceId ) : null;
|
2014-06-26 10:41:18 +00:00
|
|
|
if (
|
|
|
|
// Skip titles that don't have a resolvable href
|
2021-08-26 12:40:05 +00:00
|
|
|
mwTitle &&
|
2014-06-26 10:41:18 +00:00
|
|
|
// Skip already cached data
|
|
|
|
!hasOwn.call( specCache, title ) &&
|
|
|
|
// Skip duplicate titles in the same batch
|
2015-03-10 12:50:42 +00:00
|
|
|
titles.indexOf( title ) === -1
|
2014-06-26 10:41:18 +00:00
|
|
|
) {
|
|
|
|
titles.push( title );
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 19:11:41 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
// Bypass server for empty lists
|
|
|
|
if ( !titles.length ) {
|
2021-07-06 13:43:13 +00:00
|
|
|
setTimeout( this.resolveChangeQueue.bind( this, queue ) );
|
2014-06-26 10:41:18 +00:00
|
|
|
return;
|
2013-12-09 19:05:57 +00:00
|
|
|
}
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
this.templateDataApiRequests.push( this.callTemplateDataApi( titles, queue ) );
|
2014-10-07 00:06:02 +00:00
|
|
|
};
|
|
|
|
|
2021-07-02 15:52:42 +00:00
|
|
|
/**
|
2021-07-06 13:43:13 +00:00
|
|
|
* @private
|
2021-07-02 15:52:42 +00:00
|
|
|
* @param {string[]} titles
|
2021-06-24 07:53:46 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartInstruction[]} queue
|
2021-07-02 15:52:42 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
2021-07-06 13:43:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.callTemplateDataApi = function ( titles, queue ) {
|
2018-05-04 13:30:10 +00:00
|
|
|
var xhr = ve.init.target.getContentApi( this.doc ).get( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'templatedata',
|
2016-09-30 16:07:10 +00:00
|
|
|
titles: titles,
|
2014-08-22 20:50:48 +00:00
|
|
|
lang: mw.config.get( 'wgUserLanguage' ),
|
2020-12-15 11:52:55 +00:00
|
|
|
includeMissingTitles: '1',
|
2014-08-22 20:50:48 +00:00
|
|
|
redirects: '1'
|
2021-07-06 13:43:13 +00:00
|
|
|
} ).done( this.cacheTemplateDataApiResponse.bind( this ) );
|
2021-07-06 13:50:09 +00:00
|
|
|
xhr.always(
|
2021-07-06 13:43:13 +00:00
|
|
|
this.markRequestAsDone.bind( this, xhr ),
|
|
|
|
this.resolveChangeQueue.bind( this, queue )
|
2021-07-06 13:50:09 +00:00
|
|
|
);
|
2015-01-24 00:22:17 +00:00
|
|
|
return xhr;
|
2014-10-07 00:06:02 +00:00
|
|
|
};
|
2014-05-23 17:15:33 +00:00
|
|
|
|
2021-07-02 15:52:42 +00:00
|
|
|
/**
|
2021-07-06 13:43:13 +00:00
|
|
|
* @private
|
2021-07-02 15:52:42 +00:00
|
|
|
* @param {Object} [data]
|
2021-06-24 07:53:46 +00:00
|
|
|
* @param {Object.<number,ve.dm.MWTemplatePageMetadata>} [data.pages]
|
2021-07-02 15:52:42 +00:00
|
|
|
*/
|
2021-07-06 13:43:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.cacheTemplateDataApiResponse = function ( data ) {
|
2021-08-05 08:59:22 +00:00
|
|
|
if ( !data || !data.pages ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep spec data on hand for future use
|
|
|
|
for ( var id in data.pages ) {
|
|
|
|
var title = data.pages[ id ].title;
|
|
|
|
|
|
|
|
if ( data.pages[ id ].missing ) {
|
|
|
|
// Remember templates that don't exist in the link cache
|
|
|
|
// { title: { missing: true|false }
|
|
|
|
var missingTitle = {};
|
|
|
|
missingTitle[ title ] = { missing: true };
|
|
|
|
ve.init.platform.linkCache.setMissing( missingTitle );
|
|
|
|
} else if ( data.pages[ id ].notemplatedata && !OO.isPlainObject( data.pages[ id ].params ) ) {
|
|
|
|
// (T243868) Prevent asking again for templates that have neither user-provided specs
|
|
|
|
// nor automatically detected params
|
|
|
|
specCache[ title ] = null;
|
|
|
|
} else {
|
|
|
|
specCache[ title ] = data.pages[ id ];
|
2014-10-07 00:06:02 +00:00
|
|
|
}
|
2021-08-05 08:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Follow redirects
|
|
|
|
var aliasMap = data.redirects || [];
|
|
|
|
// Follow MW's normalisation
|
|
|
|
if ( data.normalized ) {
|
|
|
|
ve.batchPush( aliasMap, data.normalized );
|
|
|
|
}
|
|
|
|
// Cross-reference aliased titles.
|
|
|
|
for ( var i = 0; i < aliasMap.length; i++ ) {
|
|
|
|
// Only define the alias if the target exists, otherwise
|
|
|
|
// we create a new property with an invalid "undefined" value.
|
|
|
|
if ( hasOwn.call( specCache, aliasMap[ i ].to ) ) {
|
|
|
|
specCache[ aliasMap[ i ].from ] = specCache[ aliasMap[ i ].to ];
|
2014-10-07 00:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-02 15:52:42 +00:00
|
|
|
/**
|
2021-07-06 13:43:13 +00:00
|
|
|
* @private
|
2021-07-02 15:52:42 +00:00
|
|
|
* @param {jQuery.Promise} apiPromise
|
|
|
|
*/
|
2021-07-06 13:43:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.markRequestAsDone = function ( apiPromise ) {
|
2014-10-07 00:06:02 +00:00
|
|
|
// Prune completed request
|
2021-07-06 13:43:13 +00:00
|
|
|
var index = this.templateDataApiRequests.indexOf( apiPromise );
|
2014-10-07 00:06:02 +00:00
|
|
|
if ( index !== -1 ) {
|
2021-07-06 13:43:13 +00:00
|
|
|
this.templateDataApiRequests.splice( index, 1 );
|
2014-10-07 00:06:02 +00:00
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.abortAllApiRequests = function () {
|
|
|
|
for ( var i = 0; i < this.templateDataApiRequests.length; i++ ) {
|
|
|
|
this.templateDataApiRequests[ i ].abort();
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
2021-07-06 13:43:13 +00:00
|
|
|
this.templateDataApiRequests.length = 0;
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get plain object representation of template transclusion.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object|null} Plain object representation, or null if empty
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getPlainObject = function () {
|
2021-07-06 13:43:13 +00:00
|
|
|
var parts = [];
|
2014-06-26 10:41:18 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < this.parts.length; i++ ) {
|
|
|
|
var part = this.parts[ i ];
|
|
|
|
var serialization = part.serialize();
|
2014-10-29 01:08:41 +00:00
|
|
|
if ( serialization !== undefined && serialization !== '' ) {
|
2021-07-06 13:43:13 +00:00
|
|
|
parts.push( serialization );
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 21:38:40 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
return parts.length ? { parts: parts } : null;
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2013-06-20 16:23:29 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
/**
|
2022-04-19 11:46:34 +00:00
|
|
|
* @return {string} Next part ID, starting from "part_0", guaranteed to be unique for this
|
|
|
|
* transclusion
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2021-12-22 12:03:01 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.nextUniquePartId = function () {
|
2022-04-19 11:46:34 +00:00
|
|
|
return 'part_' + this.uid++;
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-12-09 16:47:13 +00:00
|
|
|
* Replace asynchronously.
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} remove Part to remove
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} add Part to add
|
|
|
|
* @throws {Error} If part to remove is not valid
|
|
|
|
* @throws {Error} If part to add is not valid
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {jQuery.Promise} Promise, resolved when part is added
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.replacePart = function ( remove, add ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
var deferred = ve.createDeferred();
|
2014-06-26 10:41:18 +00:00
|
|
|
if (
|
|
|
|
!( remove instanceof ve.dm.MWTransclusionPartModel ) ||
|
|
|
|
!( add instanceof ve.dm.MWTransclusionPartModel )
|
|
|
|
) {
|
|
|
|
throw new Error( 'Invalid transclusion part' );
|
|
|
|
}
|
2021-07-06 13:43:13 +00:00
|
|
|
this.changeQueue.push( { remove: remove, add: add, deferred: deferred } );
|
2013-12-09 19:05:57 +00:00
|
|
|
|
2021-07-06 13:43:13 +00:00
|
|
|
// Fetch on next yield to process items in the queue together, subsequent calls will
|
2014-06-26 10:41:18 +00:00
|
|
|
// have no effect because the queue will be clear
|
2021-07-06 13:43:13 +00:00
|
|
|
setTimeout( this.processChangeQueue.bind( this ) );
|
2013-12-09 19:05:57 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Added asynchronously, but order is preserved.
|
|
|
|
*
|
2022-04-19 11:56:42 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part
|
2014-06-26 10:41:18 +00:00
|
|
|
* @param {number} [index] Specific index to add content at, defaults to the end
|
|
|
|
* @throws {Error} If part is not valid
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {jQuery.Promise} Promise, resolved when part is added
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addPart = function ( part, index ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
var deferred = ve.createDeferred();
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( !( part instanceof ve.dm.MWTransclusionPartModel ) ) {
|
|
|
|
throw new Error( 'Invalid transclusion part' );
|
|
|
|
}
|
2021-07-06 13:43:13 +00:00
|
|
|
this.changeQueue.push( { add: part, index: index, deferred: deferred } );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
// Fetch on next yield to process items in the queue together, subsequent calls to fetch will
|
|
|
|
// have no effect because the queue will be clear
|
2021-07-06 13:43:13 +00:00
|
|
|
setTimeout( this.processChangeQueue.bind( this ) );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-04-19 11:56:42 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWTransclusionPartInstruction#replace
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.removePart = function ( part ) {
|
2015-03-10 12:50:42 +00:00
|
|
|
var index = this.parts.indexOf( part );
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.parts.splice( index, 1 );
|
|
|
|
part.disconnect( this );
|
|
|
|
this.emit( 'replace', part, null );
|
2013-06-14 00:46:45 +00:00
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
2021-10-06 13:37:02 +00:00
|
|
|
/**
|
2021-10-06 15:57:10 +00:00
|
|
|
* @return {boolean} True if the transclusion is literally empty or contains only placeholders
|
2021-10-06 13:37:02 +00:00
|
|
|
*/
|
2021-10-06 15:57:10 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.isEmpty = function () {
|
|
|
|
return this.parts.every( function ( part ) {
|
|
|
|
return part instanceof ve.dm.MWTemplatePlaceholderModel;
|
|
|
|
} );
|
2021-10-06 13:37:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-06 15:57:10 +00:00
|
|
|
* @return {boolean} True if this is a single template or template placeholder
|
2021-10-06 13:37:02 +00:00
|
|
|
*/
|
2021-10-06 15:57:10 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.isSingleTemplate = function () {
|
|
|
|
return this.parts.length === 1 && (
|
|
|
|
this.parts[ 0 ] instanceof ve.dm.MWTemplateModel ||
|
|
|
|
this.parts[ 0 ] instanceof ve.dm.MWTemplatePlaceholderModel
|
|
|
|
);
|
2021-10-06 13:37:02 +00:00
|
|
|
};
|
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
/**
|
2022-04-19 11:56:42 +00:00
|
|
|
* @return {ve.dm.MWTransclusionPartModel[]} All parts in this transclusion
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getParts = function () {
|
|
|
|
return this.parts;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matching is performed against the first section of the `id`, delimited by a '/'.
|
|
|
|
*
|
2021-10-08 07:37:01 +00:00
|
|
|
* @param {string} [id] Any id, including slash-delimited template parameter ids
|
|
|
|
* @return {ve.dm.MWTransclusionPartModel|undefined} Part with matching ID, if found
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getPartFromId = function ( id ) {
|
2021-10-08 07:37:01 +00:00
|
|
|
if ( !id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
// For ids from ve.dm.MWParameterModel, compare against the part id
|
|
|
|
// of the parameter instead of the entire model id (e.g. "part_1" instead of "part_1/foo").
|
2021-09-16 08:42:13 +00:00
|
|
|
var partId = id.split( '/', 1 )[ 0 ];
|
2014-06-26 10:41:18 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < this.parts.length; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.parts[ i ].getId() === partId ) {
|
|
|
|
return this.parts[ i ];
|
2014-06-26 10:41:18 +00:00
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index of a part or parameter.
|
|
|
|
*
|
|
|
|
* Indexes are linear depth-first addresses in the transclusion tree.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel|ve.dm.MWParameterModel} model Part or parameter
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {number} Page index of model
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getIndex = function ( model ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var parts = this.parts,
|
2014-06-26 10:41:18 +00:00
|
|
|
index = 0;
|
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < parts.length; i++ ) {
|
|
|
|
var part = parts[ i ];
|
2014-06-26 10:41:18 +00:00
|
|
|
if ( part === model ) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
if ( part instanceof ve.dm.MWTemplateModel ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var names = part.getOrderedParameterNames();
|
|
|
|
for ( var j = 0; j < names.length; j++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( part.getParameter( names[ j ] ) === model ) {
|
2014-06-26 10:41:18 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
index++;
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 10:41:18 +00:00
|
|
|
return -1;
|
|
|
|
};
|
2013-12-02 20:10:55 +00:00
|
|
|
|
2022-06-27 11:48:16 +00:00
|
|
|
/**
|
2016-05-13 19:43:47 +00:00
|
|
|
* Add missing required and suggested parameters to each transclusion.
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addPromptedParameters = function () {
|
2022-06-27 11:48:16 +00:00
|
|
|
this.parts.forEach( function ( part ) {
|
|
|
|
if ( part instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
part.addPromptedParameters();
|
|
|
|
}
|
|
|
|
} );
|
2016-05-13 19:43:47 +00:00
|
|
|
};
|
|
|
|
|
2021-09-27 09:16:14 +00:00
|
|
|
/**
|
|
|
|
* @return {boolean} True if any transclusion part contains meaningful, non-default user input
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.containsValuableData = function () {
|
|
|
|
return this.parts.some( function ( part ) {
|
|
|
|
return part.containsValuableData();
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-05-07 10:40:36 +00:00
|
|
|
/**
|
|
|
|
* Resets the model's state.
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.reset = function () {
|
|
|
|
this.parts = [];
|
|
|
|
this.uid = 0;
|
2021-07-06 13:43:13 +00:00
|
|
|
this.templateDataApiRequests = [];
|
|
|
|
this.changeQueue = [];
|
2021-05-07 10:40:36 +00:00
|
|
|
};
|
|
|
|
|
2021-08-27 14:50:20 +00:00
|
|
|
// Temporary compatibility for https://github.com/femiwiki/Sanctions/pull/118. Remove when not
|
|
|
|
// needed any more.
|
|
|
|
mw.log.deprecate( ve.dm.MWTransclusionModel.prototype, 'abortRequests',
|
|
|
|
ve.dm.MWTransclusionModel.prototype.abortAllApiRequests,
|
|
|
|
'Use "abortAllApiRequests" instead.'
|
|
|
|
);
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
}() );
|