2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTransclusionModel class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @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
|
2013-10-15 11:58:04 +00:00
|
|
|
* @mixins OO.EventEmitter
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel = function VeDmMWTransclusionModel() {
|
|
|
|
// Mixin constructors
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.EventEmitter.call( this );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.parts = [];
|
|
|
|
this.uid = 0;
|
2013-06-28 21:38:40 +00:00
|
|
|
this.requests = [];
|
2013-06-29 02:37:42 +00:00
|
|
|
this.queue = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.mixinClass( ve.dm.MWTransclusionModel, OO.EventEmitter );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2013-12-02 20:10:55 +00:00
|
|
|
* @event replace
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} removed Removed part
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} added Added part
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-09 19:05:57 +00:00
|
|
|
/**
|
|
|
|
* @event change
|
|
|
|
*/
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag
Changes:
* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget
Configuration:
If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:
[
{ "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
{ "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
{ "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
{ "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]
...or...
[
{
"name": "any-name",
"icon": "any-ooui-icon",
"template": "Any template",
"title": "Any title text"
}
]
The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.
Depends on Ic353f91 in oojs-ui
Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +00:00
|
|
|
/**
|
2014-04-10 18:55:24 +00:00
|
|
|
* Insert transclusion at the end of a surface fragment.
|
Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag
Changes:
* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget
Configuration:
If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:
[
{ "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
{ "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
{ "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
{ "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]
...or...
[
{
"name": "any-name",
"icon": "any-ooui-icon",
"template": "Any template",
"title": "Any title text"
}
]
The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.
Depends on Ic353f91 in oojs-ui
Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +00:00
|
|
|
*
|
2014-04-10 18:55:24 +00:00
|
|
|
* @param {ve.dm.SurfaceFragment} surfaceModel Surface fragment to insert at
|
Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag
Changes:
* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget
Configuration:
If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:
[
{ "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
{ "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
{ "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
{ "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]
...or...
[
{
"name": "any-name",
"icon": "any-ooui-icon",
"template": "Any template",
"title": "Any title text"
}
]
The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.
Depends on Ic353f91 in oojs-ui
Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +00:00
|
|
|
*/
|
2014-04-10 18:55:24 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment ) {
|
|
|
|
surfaceFragment
|
|
|
|
.insertContent( [
|
|
|
|
{
|
|
|
|
'type': 'mwTransclusionInline',
|
|
|
|
'attributes': {
|
|
|
|
'mw': this.getPlainObject()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/mwTransclusionInline' }
|
|
|
|
] );
|
Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag
Changes:
* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget
Configuration:
If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:
[
{ "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
{ "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
{ "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
{ "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]
...or...
[
{
"name": "any-name",
"icon": "any-ooui-icon",
"template": "Any template",
"title": "Any title text"
}
]
The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.
Depends on Ic353f91 in oojs-ui
Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +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 ) {
|
|
|
|
surfaceModel.getFragment( node.getOuterRange(), true )
|
|
|
|
.changeAttributes( { 'mw': obj } );
|
|
|
|
} else {
|
|
|
|
surfaceModel.getFragment( node.getOuterRange(), true )
|
|
|
|
.removeContent();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Load from transclusion data, and fetch spec from server.
|
|
|
|
*
|
|
|
|
* @param {Object} data Transclusion data
|
|
|
|
* @returns {jQuery.Promise} Promise, resolved when spec is loaded
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.load = function ( data ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
var i, len, part, deferred,
|
|
|
|
promises = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Convert single part format to multi-part format
|
2013-09-17 23:51:40 +00:00
|
|
|
// Parsoid doesn't use this format any more, but we accept it for backwards compatibility
|
2013-06-11 19:16:04 +00:00
|
|
|
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 ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
deferred = $.Deferred();
|
|
|
|
promises.push( deferred.promise() );
|
|
|
|
this.queue.push( {
|
|
|
|
'add': ve.dm.MWTemplateModel.newFromData( this, part.template ),
|
|
|
|
'deferred': deferred
|
|
|
|
} );
|
2013-06-11 19:16:04 +00:00
|
|
|
} else if ( typeof part === 'string' ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
deferred = $.Deferred();
|
|
|
|
promises.push( deferred.promise() );
|
|
|
|
this.queue.push( {
|
|
|
|
'add': new ve.dm.MWTransclusionContentModel( this, part, 'data' ),
|
|
|
|
'deferred': deferred
|
|
|
|
} );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-29 02:37:42 +00:00
|
|
|
setTimeout( ve.bind( this.fetch, this ) );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
|
|
|
|
return $.when.apply( $, promises );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-29 02:37:42 +00:00
|
|
|
* Process one or more queue items.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2013-06-29 02:37:42 +00:00
|
|
|
* @param {Object[]} queue List of objects containing parts to add and optionally indexes to add
|
2013-07-25 02:02:50 +00:00
|
|
|
* them at, if no index is given parts will be added at the end
|
2013-12-02 20:10:55 +00:00
|
|
|
* @fires replace For each item added
|
2013-12-09 19:05:57 +00:00
|
|
|
* @fires change
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.process = function ( queue ) {
|
2014-02-05 19:21:19 +00:00
|
|
|
var i, len, item, title, index, remove, existing;
|
2013-06-29 02:37:42 +00:00
|
|
|
|
|
|
|
for ( i = 0, len = queue.length; i < len; i++ ) {
|
2014-01-22 20:09:53 +00:00
|
|
|
remove = 0;
|
2013-06-29 02:37:42 +00:00
|
|
|
item = queue[i];
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
if ( item.add instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
title = item.add.getTitle();
|
2013-06-29 02:37:42 +00:00
|
|
|
if ( hasOwn.call( specCache, title ) && specCache[title] ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
item.add.getSpec().extend( specCache[title] );
|
2013-06-29 02:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2014-02-05 19:11:41 +00:00
|
|
|
// Use specified index
|
2014-01-22 20:09:53 +00:00
|
|
|
index = item.index;
|
2014-02-05 19:21:19 +00:00
|
|
|
// Auto-remove if already existing, preserving index
|
|
|
|
existing = ve.indexOf( item.add, this.parts );
|
|
|
|
if ( existing !== -1 ) {
|
|
|
|
this.removePart( item.add );
|
2014-02-28 00:04:15 +00:00
|
|
|
if ( index && existing + 1 < index ) {
|
2014-02-05 19:21:19 +00:00
|
|
|
index--;
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 19:11:41 +00:00
|
|
|
// Derive index from removal if given
|
|
|
|
if ( index === undefined && item.remove ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
index = ve.indexOf( item.remove, this.parts );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
remove = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 19:11:41 +00:00
|
|
|
// Use last index as a last resort
|
2013-12-02 20:10:55 +00:00
|
|
|
if ( index === undefined || index === -1 ) {
|
|
|
|
index = this.parts.length;
|
|
|
|
}
|
2014-02-05 19:11:41 +00:00
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
this.parts.splice( index, remove, item.add );
|
2013-12-09 19:05:57 +00:00
|
|
|
if ( item.add ) {
|
|
|
|
item.add.connect( this, { 'change': [ 'emit', 'change' ] } );
|
|
|
|
}
|
|
|
|
if ( item.remove ) {
|
|
|
|
item.remove.disconnect( this );
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
this.emit( 'replace', item.remove || null, item.add );
|
2014-01-22 20:09:53 +00:00
|
|
|
|
2013-07-12 01:23:52 +00:00
|
|
|
// Resolve promises
|
|
|
|
if ( item.deferred ) {
|
|
|
|
item.deferred.resolve();
|
|
|
|
}
|
2013-06-29 02:37:42 +00:00
|
|
|
}
|
2013-12-09 19:05:57 +00:00
|
|
|
this.emit( 'change' );
|
2013-06-29 02:37:42 +00:00
|
|
|
};
|
|
|
|
|
2013-07-31 22:53:29 +00:00
|
|
|
/** */
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTransclusionModel.prototype.fetch = function () {
|
|
|
|
if ( !this.queue.length ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2013-06-29 02:37:42 +00:00
|
|
|
var i, len, item, title, request,
|
|
|
|
titles = [],
|
|
|
|
specs = {},
|
|
|
|
queue = this.queue.slice();
|
|
|
|
|
|
|
|
// Clear shared queue for future calls
|
|
|
|
this.queue.length = 0;
|
|
|
|
|
|
|
|
// Get unique list of template titles that aren't already loaded
|
|
|
|
for ( i = 0, len = queue.length; i < len; i++ ) {
|
|
|
|
item = queue[i];
|
2013-12-02 20:10:55 +00:00
|
|
|
if ( item.add instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
title = item.add.getTitle();
|
2013-06-29 02:37:42 +00:00
|
|
|
if (
|
|
|
|
// Skip titles that don't have a resolvable href
|
|
|
|
title &&
|
|
|
|
// Skip titles outside the template namespace
|
|
|
|
title.charAt( 0 ) !== ':' &&
|
|
|
|
// Skip already cached data
|
|
|
|
!hasOwn.call( specCache, title ) &&
|
|
|
|
// Skip duplicate titles in the same batch
|
|
|
|
ve.indexOf( title, titles ) === -1
|
|
|
|
) {
|
|
|
|
titles.push( title );
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass server for empty lists
|
|
|
|
if ( !titles.length ) {
|
2013-06-29 02:37:42 +00:00
|
|
|
setTimeout( ve.bind( this.process, this, queue ) );
|
|
|
|
return;
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request template specs from server
|
2014-05-23 09:57:58 +00:00
|
|
|
request = ve.init.target.constructor.static.apiRequest( {
|
2014-01-05 04:44:13 +00:00
|
|
|
'action': 'templatedata',
|
|
|
|
'titles': titles.join( '|' ),
|
2014-05-23 17:15:33 +00:00
|
|
|
'lang': mw.config.get( 'wgUserLanguage' ),
|
|
|
|
'redirects': '1'
|
2013-06-11 19:16:04 +00:00
|
|
|
} )
|
|
|
|
.done( function ( data ) {
|
2014-05-23 17:15:33 +00:00
|
|
|
var i, len, id, aliasMap = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2014-05-23 17:15:33 +00:00
|
|
|
// Follow redirects
|
|
|
|
if ( data.redirects ) {
|
|
|
|
aliasMap = data.redirects;
|
|
|
|
}
|
|
|
|
// Follow MW's normalisation
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( data.normalized ) {
|
2014-05-23 17:15:33 +00:00
|
|
|
aliasMap.push.apply( aliasMap, data.normalized );
|
|
|
|
}
|
|
|
|
// Cross-reference aliased titles.
|
|
|
|
for ( i = 0, len = aliasMap.length; i < len; i++ ) {
|
|
|
|
// Only define the alias if the target exists, otherwise
|
|
|
|
// we create a new property with an invalid "undefined" value.
|
|
|
|
if ( hasOwn.call( specs, aliasMap[i].to ) ) {
|
|
|
|
specs[aliasMap[i].from] = specs[aliasMap[i].to];
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-23 17:15:33 +00:00
|
|
|
|
2013-06-29 02:37:42 +00:00
|
|
|
// Prevent asking again for templates that have no specs
|
|
|
|
for ( i = 0, len = titles.length; i < len; i++ ) {
|
|
|
|
title = titles[i];
|
|
|
|
if ( !specs[title] ) {
|
|
|
|
specs[title] = null;
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-29 02:37:42 +00:00
|
|
|
|
|
|
|
ve.extendObject( specCache, specs );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
} )
|
2013-06-29 02:37:42 +00:00
|
|
|
.always( ve.bind( function () {
|
|
|
|
// Prune completed request
|
|
|
|
var index = ve.indexOf( request, this.requests );
|
2013-06-28 21:38:40 +00:00
|
|
|
if ( index !== -1 ) {
|
2013-06-29 02:37:42 +00:00
|
|
|
this.requests.splice( index, 1 );
|
2013-06-28 21:38:40 +00:00
|
|
|
}
|
2013-06-29 02:37:42 +00:00
|
|
|
// Actually add queued items
|
|
|
|
this.process( queue );
|
|
|
|
}, this ) );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2013-06-29 02:37:42 +00:00
|
|
|
this.requests.push( request );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2013-06-28 21:38:40 +00:00
|
|
|
/**
|
|
|
|
* Abort any pending requests.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
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 () {
|
2013-07-12 00:23:33 +00:00
|
|
|
var i, len, part, serialization,
|
2013-06-11 19:16:04 +00:00
|
|
|
obj = { 'parts': [] };
|
|
|
|
|
|
|
|
for ( i = 0, len = this.parts.length; i < len; i++ ) {
|
|
|
|
part = this.parts[i];
|
2013-07-12 00:23:33 +00:00
|
|
|
serialization = part.serialize();
|
|
|
|
if ( serialization !== undefined ) {
|
|
|
|
obj.parts.push( serialization );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 16:23:29 +00:00
|
|
|
if ( obj.parts.length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2013-12-09 19:05:57 +00:00
|
|
|
/**
|
|
|
|
* Get the wikitext for this transclusion.
|
|
|
|
*
|
|
|
|
* @returns {string} Wikitext like `{{foo|1=bar|baz=quux}}`
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getWikitext = function () {
|
|
|
|
var i, len,
|
|
|
|
wikitext = '';
|
|
|
|
|
|
|
|
for ( i = 0, len = this.parts.length; i < len; i++ ) {
|
|
|
|
wikitext += this.parts[i].getWikitext();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wikitext;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @returns {number} Unique ID
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getUniquePartId = function () {
|
|
|
|
return this.uid++;
|
|
|
|
};
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
/**
|
|
|
|
* Replace part.
|
|
|
|
*
|
|
|
|
* Replace asynchonously.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @returns {jQuery.Promise} Promise, resolved when part is added
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.replacePart = function ( remove, add ) {
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
if (
|
|
|
|
!( remove instanceof ve.dm.MWTransclusionPartModel ) ||
|
|
|
|
!( add instanceof ve.dm.MWTransclusionPartModel )
|
|
|
|
) {
|
|
|
|
throw new Error( 'Invalid transclusion part' );
|
|
|
|
}
|
|
|
|
this.queue.push( { 'remove': remove, 'add': add, 'deferred': deferred } );
|
|
|
|
|
|
|
|
// 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
|
|
|
|
setTimeout( ve.bind( this.fetch, this ) );
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
2013-06-14 00:46:45 +00:00
|
|
|
/**
|
|
|
|
* Add part.
|
|
|
|
*
|
2013-06-29 02:37:42 +00:00
|
|
|
* Added asynchronously, but order is preserved.
|
|
|
|
*
|
2013-06-14 00:46:45 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Part to add
|
2013-06-29 02:37:42 +00:00
|
|
|
* @param {number} [index] Specific index to add content at, defaults to the end
|
|
|
|
* @throws {Error} If part is not valid
|
2013-07-12 01:23:52 +00:00
|
|
|
* @returns {jQuery.Promise} Promise, resolved when part is added
|
2013-06-14 00:46:45 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.addPart = function ( part, index ) {
|
2013-07-12 01:23:52 +00:00
|
|
|
var deferred = $.Deferred();
|
2013-06-29 02:37:42 +00:00
|
|
|
if ( !( part instanceof ve.dm.MWTransclusionPartModel ) ) {
|
|
|
|
throw new Error( 'Invalid transclusion part' );
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
this.queue.push( { 'add': part, 'index': index, 'deferred': deferred } );
|
2013-07-12 01:23:52 +00:00
|
|
|
|
2013-06-29 02:37:42 +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
|
|
|
|
setTimeout( ve.bind( this.fetch, this ) );
|
2013-07-12 01:23:52 +00:00
|
|
|
|
|
|
|
return deferred.promise();
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a part.
|
|
|
|
*
|
2013-06-14 00:46:45 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part Part to remove
|
2013-12-02 20:10:55 +00:00
|
|
|
* @fires replace
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.removePart = function ( part ) {
|
|
|
|
var index = ve.indexOf( part, this.parts );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.parts.splice( index, 1 );
|
2013-12-09 19:05:57 +00:00
|
|
|
part.disconnect( this );
|
2013-12-02 20:10:55 +00:00
|
|
|
this.emit( 'replace', part, null );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all parts.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
* @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,
|
2014-02-24 21:49:48 +00:00
|
|
|
// For ids from ve.dm.MWParameterModel, compare against the part id
|
2013-06-18 19:39:21 +00:00
|
|
|
// 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-12-02 20:10:55 +00:00
|
|
|
/**
|
|
|
|
* Get the index of a part or parameter.
|
|
|
|
*
|
|
|
|
* Indexes are linear depth-first addresses in the transclusion tree.
|
|
|
|
*
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel|ve.dm.MWParameterModel} model Part or parameter
|
2013-12-02 20:10:55 +00:00
|
|
|
* @returns {number} Page index of model
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionModel.prototype.getIndex = function ( model ) {
|
|
|
|
var i, iLen, j, jLen, part, names,
|
|
|
|
parts = this.parts,
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
for ( i = 0, iLen = parts.length; i < iLen; i++ ) {
|
|
|
|
part = parts[i];
|
|
|
|
if ( part === model ) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
if ( part instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
names = part.getParameterNames();
|
|
|
|
for ( j = 0, jLen = names.length; j < jLen; j++ ) {
|
|
|
|
if ( part.getParameter( names[j] ) === model ) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
}() );
|