2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTransclusionModel class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global mw */
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
( function () {
|
2013-06-28 21:38:40 +00:00
|
|
|
var hasOwn = Object.hasOwnProperty,
|
|
|
|
specCache = {};
|
2013-06-20 22:09:42 +00:00
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* MediaWiki transclusion model.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins ve.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel = function VeDmMWTransclusionModel() {
|
|
|
|
// Mixin constructors
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.parts = [];
|
|
|
|
this.uid = 0;
|
2013-06-28 21:38:40 +00:00
|
|
|
this.requests = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.mixinClass( ve.dm.MWTransclusionModel, ve.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event add
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Added part
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event remove
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Removed part
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load from transclusion data, and fetch spec from server.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} data Transclusion data
|
|
|
|
* @returns {jQuery.Promise} Promise, resolved when spec is loaded
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.load = function ( data ) {
|
|
|
|
var i, len, key, part, template,
|
|
|
|
templates = [];
|
|
|
|
|
|
|
|
// Convert single part format to multi-part format
|
|
|
|
if ( data.params && data.target ) {
|
|
|
|
data = { 'parts': [ { 'template': data } ] };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ve.isArray( data.parts ) ) {
|
|
|
|
for ( i = 0, len = data.parts.length; i < len; i++ ) {
|
|
|
|
part = data.parts[i];
|
|
|
|
if ( part.template ) {
|
|
|
|
template = this.addTemplate( part.template.target );
|
|
|
|
for ( key in part.template.params ) {
|
|
|
|
template.addParameter( key, part.template.params[key].wt );
|
|
|
|
}
|
|
|
|
// Don't load specs for templates that don't have a resolvable target
|
|
|
|
if ( part.template.target.href ) {
|
|
|
|
templates.push( template );
|
|
|
|
}
|
|
|
|
} else if ( typeof part === 'string' ) {
|
|
|
|
this.addContent( part );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 00:25:52 +00:00
|
|
|
// Add fetched specs to #specs store when the promise is resolved
|
|
|
|
return this.fetchSpecs( templates ).done( function ( specs ) {
|
2013-06-28 21:38:40 +00:00
|
|
|
ve.extendObject( specCache, specs );
|
2013-06-24 00:25:52 +00:00
|
|
|
} );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch template specifications from server.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionModel[]} templates List of templates to load data for
|
|
|
|
* @returns {jQuery.Promise} Promise, resolved when spec is loaded
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.fetchSpecs = function ( templates ) {
|
2013-06-28 21:38:40 +00:00
|
|
|
var i, len, title, request,
|
|
|
|
requests = this.requests,
|
2013-06-24 00:25:52 +00:00
|
|
|
deferred = $.Deferred(),
|
2013-06-11 19:16:04 +00:00
|
|
|
specs = {},
|
|
|
|
titles = [];
|
|
|
|
|
2013-06-28 21:38:40 +00:00
|
|
|
// Get unique list of titles that aren't already loaded
|
2013-06-11 19:16:04 +00:00
|
|
|
for ( i = 0, len = templates.length; i < len; i++ ) {
|
|
|
|
title = templates[i].getTitle();
|
2013-06-28 21:38:40 +00:00
|
|
|
if ( !specCache[title] && ve.indexOf( title, titles ) === -1 ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
titles.push( title );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass server for empty lists
|
|
|
|
if ( !titles.length ) {
|
|
|
|
setTimeout( deferred.reject );
|
|
|
|
return deferred.promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request template specs from server
|
2013-06-28 21:38:40 +00:00
|
|
|
request = $.ajax( {
|
2013-06-11 19:16:04 +00:00
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'dataType': 'json',
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'templatedata',
|
|
|
|
'titles': titles.join( '|' )
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.done( function ( data ) {
|
|
|
|
var i, len, id, title;
|
|
|
|
|
|
|
|
if ( data && data.pages ) {
|
|
|
|
// Keep spec data on hand for future use
|
|
|
|
for ( id in data.pages ) {
|
|
|
|
specs[data.pages[id].title] = data.pages[id];
|
|
|
|
}
|
|
|
|
// Cross-reference under normalized titles
|
|
|
|
if ( data.normalized ) {
|
|
|
|
for ( i = 0, len = data.normalized.length; i < len; i++ ) {
|
2013-06-20 22:09:42 +00:00
|
|
|
// Only define the alias if the target exists, otherwise
|
|
|
|
// we create a new property with an invalid "undefined" value.
|
|
|
|
if ( hasOwn.call( specs, data.normalized[i].to ) ) {
|
|
|
|
specs[data.normalized[i].from] = specs[data.normalized[i].to];
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load into existing templates
|
|
|
|
for ( i = 0, len = templates.length; i < len; i++ ) {
|
|
|
|
title = templates[i].getTitle();
|
2013-06-20 22:09:42 +00:00
|
|
|
if ( hasOwn.call( specs, title ) ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
templates[i].getSpec().extend( specs[title] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deferred.resolve( specs );
|
|
|
|
} else {
|
|
|
|
deferred.reject( 'unavailable', arguments );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
deferred.reject( 'http', arguments );
|
2013-06-28 21:38:40 +00:00
|
|
|
} )
|
|
|
|
.always( function () {
|
|
|
|
// Prune requests when complete
|
|
|
|
var index = requests.indexOf( request );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
requests.splice( index, 1 );
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
} );
|
2013-06-28 21:38:40 +00:00
|
|
|
requests.push( request );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
2013-06-28 21:38:40 +00:00
|
|
|
/**
|
|
|
|
* Abort any pending requests.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.abortRequests = function () {
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
for ( i = 0, len = this.requests.length; i < len; i++ ) {
|
|
|
|
this.requests[i].abort();
|
|
|
|
}
|
|
|
|
this.requests.length = 0;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get plain object representation of template transclusion.
|
|
|
|
*
|
|
|
|
* @method
|
2013-06-20 16:23:29 +00:00
|
|
|
* @returns {Object|null} Plain object representation, or null if empty
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getPlainObject = function () {
|
|
|
|
var i, len, part, template, name, params,
|
|
|
|
obj = { 'parts': [] };
|
|
|
|
|
|
|
|
for ( i = 0, len = this.parts.length; i < len; i++ ) {
|
|
|
|
part = this.parts[i];
|
|
|
|
if ( part instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
template = { 'target': part.getTarget(), 'params': {} };
|
|
|
|
params = part.getParameters();
|
|
|
|
for ( name in params ) {
|
|
|
|
template.params[name] = { 'wt': params[name].getValue() };
|
|
|
|
}
|
|
|
|
obj.parts.push( { 'template': template } );
|
|
|
|
} else if ( part instanceof ve.dm.MWTransclusionContentModel ) {
|
|
|
|
obj.parts.push( part.getValue() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 16:23:29 +00:00
|
|
|
if ( obj.parts.length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
// Use single-part format when possible
|
|
|
|
if ( obj.parts.length === 1 ) {
|
2013-06-17 09:36:47 +00:00
|
|
|
obj = obj.parts[0].template;
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique ID for a part in the transclusion.
|
|
|
|
*
|
|
|
|
* This is used to give parts unique IDs, and returns a different value each time it's called.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {number} Unique ID
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getUniquePartId = function () {
|
|
|
|
return this.uid++;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add content part.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} value Content value
|
|
|
|
* @param {number} [index] Specific index to add content at
|
|
|
|
* @returns {ve.dm.MWTransclusionContentModel} Added content part
|
|
|
|
* @emits add
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addContent = function ( value, index ) {
|
|
|
|
var part = new ve.dm.MWTransclusionContentModel( this, value );
|
2013-06-14 00:46:45 +00:00
|
|
|
this.addPart( part, index );
|
2013-06-11 19:16:04 +00:00
|
|
|
return part;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add template part.
|
|
|
|
*
|
2013-06-28 21:38:40 +00:00
|
|
|
* Templates are added asynchronously.
|
|
|
|
*
|
2013-06-11 19:16:04 +00:00
|
|
|
* @method
|
2013-06-18 19:41:02 +00:00
|
|
|
* @param {Object} target Template target
|
|
|
|
* @param {string} target.wt Original wikitext of target
|
|
|
|
* @param {string} [target.href] Hypertext reference to target
|
|
|
|
* @param {number} [index] Specific index to add template at
|
2013-06-28 21:38:40 +00:00
|
|
|
* @returns {ve.dm.MWTemplateModel} Added template part
|
2013-06-11 19:16:04 +00:00
|
|
|
* @emits add
|
|
|
|
*/
|
2013-06-18 19:41:02 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.addTemplate = function ( target, index ) {
|
|
|
|
var part = new ve.dm.MWTemplateModel( this, target ),
|
2013-06-28 21:38:40 +00:00
|
|
|
title = part.getTitle(),
|
|
|
|
finish = ve.bind( this.addPart, this, part, index );
|
|
|
|
|
|
|
|
if ( hasOwn.call( specCache, title ) ) {
|
|
|
|
part.getSpec().extend( specCache[title] );
|
|
|
|
setTimeout( finish );
|
|
|
|
} else {
|
|
|
|
// Add fetched specs to #specs store when the promise is resolved
|
|
|
|
this.fetchSpecs( [ part ] )
|
|
|
|
.done( function ( specs ) {
|
|
|
|
ve.extendObject( specCache, specs );
|
|
|
|
} )
|
|
|
|
.always( finish );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
2013-06-14 00:46:45 +00:00
|
|
|
return part;
|
|
|
|
};
|
|
|
|
|
2013-06-18 21:24:16 +00:00
|
|
|
/**
|
|
|
|
* Add template placeholder part.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {number} [index] Specific index to add placeholder at
|
|
|
|
* @returns {ve.dm.MWTransclusionModel} Added template part
|
|
|
|
* @emits add
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addPlaceholder = function ( index ) {
|
|
|
|
var part = new ve.dm.MWTemplatePlaceholderModel( this );
|
|
|
|
|
|
|
|
this.addPart( part, index );
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
|
2013-06-14 00:46:45 +00:00
|
|
|
/**
|
|
|
|
* Add part.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Part to add
|
|
|
|
* @param {number} [index] Specific index to add content at
|
|
|
|
* @emits add
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addPart = function ( part, index ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
this.parts.splice( index === undefined ? this.parts.length : index, 0, part );
|
|
|
|
this.emit( 'add', part );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a part.
|
|
|
|
*
|
|
|
|
* @method
|
2013-06-14 00:46:45 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Part to remove
|
2013-06-11 19:16:04 +00:00
|
|
|
* @emits remove
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.removePart = function ( part ) {
|
|
|
|
var index = ve.indexOf( part, this.parts );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.parts.splice( index, 1 );
|
|
|
|
this.emit( 'remove', part );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all parts.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.MWTransclusionPartModel[]} Parts in transclusion
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getParts = function () {
|
|
|
|
return this.parts;
|
|
|
|
};
|
|
|
|
|
2013-06-14 00:46:45 +00:00
|
|
|
/**
|
|
|
|
* Get part by its ID.
|
|
|
|
*
|
2013-06-18 19:39:21 +00:00
|
|
|
* Matching is performed against the first section of the `id`, delimited by a '/'.
|
|
|
|
*
|
2013-06-14 00:46:45 +00:00
|
|
|
* @method
|
|
|
|
* @param {string} id Part ID
|
|
|
|
* @returns {ve.dm.MWTransclusionPartModel|null} Part with matching ID, if found
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getPartFromId = function ( id ) {
|
2013-06-18 19:39:21 +00:00
|
|
|
var i, len,
|
|
|
|
// For ids from ve.dm.MWTemplateParameterModel, compare against the part id
|
|
|
|
// of the parameter instead of the entire model id (e.g. "part_1" instead of "part_1/foo").
|
|
|
|
partId = id.split( '/' )[0];
|
2013-06-14 00:46:45 +00:00
|
|
|
|
|
|
|
for ( i = 0, len = this.parts.length; i < len; i++ ) {
|
2013-06-18 19:39:21 +00:00
|
|
|
if ( this.parts[i].getId() === partId ) {
|
2013-06-14 00:46:45 +00:00
|
|
|
return this.parts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
}() );
|