2013-04-10 18:15:15 +00:00
|
|
|
/*!
|
2013-06-11 19:16:04 +00:00
|
|
|
* VisualEditor DataModel MWTransclusionNode class.
|
2013-04-10 18:15:15 +00:00
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-04-10 18:15:15 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-06-11 19:16:04 +00:00
|
|
|
* DataModel MediaWiki transclusion node.
|
2013-04-10 18:15:15 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-05-29 01:35:42 +00:00
|
|
|
* @extends ve.dm.LeafNode
|
|
|
|
* @mixins ve.dm.GeneratedContentNode
|
2014-09-17 00:33:39 +00:00
|
|
|
* @mixins ve.dm.FocusableNode
|
2013-05-29 01:35:42 +00:00
|
|
|
*
|
2013-04-10 18:15:15 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.MWTransclusionNode = function VeDmMWTransclusionNode() {
|
2013-04-10 18:15:15 +00:00
|
|
|
// Parent constructor
|
2016-02-07 18:28:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.super.apply( this, arguments );
|
2013-05-29 01:35:42 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.GeneratedContentNode.call( this );
|
2014-09-17 00:33:39 +00:00
|
|
|
ve.dm.FocusableNode.call( this );
|
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
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.partsList = null;
|
|
|
|
|
|
|
|
// Events
|
2014-08-22 20:50:48 +00:00
|
|
|
this.connect( this, { attributeChange: 'onAttributeChange' } );
|
2013-04-10 18:15:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWTransclusionNode, ve.dm.LeafNode );
|
2013-05-29 01:35:42 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.dm.MWTransclusionNode, ve.dm.GeneratedContentNode );
|
2014-09-17 00:33:39 +00:00
|
|
|
|
|
|
|
OO.mixinClass( ve.dm.MWTransclusionNode, ve.dm.FocusableNode );
|
2013-04-10 18:15:15 +00:00
|
|
|
|
|
|
|
/* Static members */
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.name = 'mwTransclusion';
|
2013-04-10 18:15:15 +00:00
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.matchTagNames = null;
|
2013-04-10 18:15:15 +00:00
|
|
|
|
2015-05-16 14:27:09 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.matchRdfaTypes = [ 'mw:Transclusion' ];
|
|
|
|
|
|
|
|
// Transclusion nodes can contain other types, e.g. mw:PageProp/Category.
|
|
|
|
// Allow all other types (null) so they match to this node.
|
|
|
|
ve.dm.MWTransclusionNode.static.allowedRdfaTypes = null;
|
2013-04-10 18:15:15 +00:00
|
|
|
|
2015-06-16 12:51:35 +00:00
|
|
|
// HACK: This prevents any rules with higher specificity from matching,
|
|
|
|
// e.g. LanguageAnnotation which uses a match function
|
|
|
|
ve.dm.MWTransclusionNode.static.matchFunction = function () {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-06-24 16:27:35 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.enableAboutGrouping = true;
|
|
|
|
|
2019-01-17 12:58:29 +00:00
|
|
|
// We handle rendering ourselves, no need to render attributes from originalDomElements (T207325),
|
|
|
|
// except for data-parsoid/RESTBase ID (T207325)
|
|
|
|
ve.dm.MWTransclusionNode.static.preserveHtmlAttributes = function ( attribute ) {
|
|
|
|
return [ 'data-parsoid', 'id' ].indexOf( attribute ) !== -1;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.getHashObject = function ( dataElement ) {
|
2013-04-10 18:15:15 +00:00
|
|
|
return {
|
|
|
|
type: dataElement.type,
|
2013-04-20 16:08:23 +00:00
|
|
|
mw: dataElement.attributes.mw
|
2013-04-10 18:15:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-26 17:20:14 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.isDiffComparable = function ( element, other ) {
|
|
|
|
function getTemplateNames( parts ) {
|
|
|
|
return parts.map( function ( part ) {
|
|
|
|
return part.template ? part.template.target.wt : '';
|
|
|
|
} ).join( '|' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ve.dm.MWTransclusionNode.super.static.isDiffComparable.call( this, element, other ) &&
|
|
|
|
getTemplateNames( element.attributes.mw.parts ) === getTemplateNames( other.attributes.mw.parts );
|
|
|
|
};
|
|
|
|
|
2015-08-03 14:25:03 +00:00
|
|
|
/**
|
|
|
|
* Node type to use when the transclusion is inline
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.static.inlineType = 'mwTransclusionInline';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node type to use when the transclusion is a block
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.static.blockType = 'mwTransclusionBlock';
|
|
|
|
|
2017-05-04 16:58:30 +00:00
|
|
|
/**
|
|
|
|
* Node type to use when the transclusion is cellable
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {string}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.static.cellType = 'mwTransclusionTableCell';
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.toDataElement = function ( domElements, converter ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ),
|
2013-06-24 00:00:42 +00:00
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
|
2013-04-13 21:22:26 +00:00
|
|
|
isInline = this.isHybridInline( domElements, converter ),
|
2015-08-03 14:25:03 +00:00
|
|
|
type = isInline ? this.inlineType : this.blockType;
|
2013-04-13 21:22:26 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
var dataElement = {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: type,
|
|
|
|
attributes: {
|
|
|
|
mw: mwData,
|
|
|
|
originalMw: mwDataJSON
|
2013-04-20 16:08:23 +00:00
|
|
|
}
|
2013-04-10 18:15:15 +00:00
|
|
|
};
|
2013-06-18 14:23:37 +00:00
|
|
|
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( domElements.length === 1 && [ 'td', 'th' ].indexOf( domElements[ 0 ].nodeName.toLowerCase() ) !== -1 ) {
|
2017-05-04 16:58:30 +00:00
|
|
|
dataElement.type = this.cellType;
|
2015-08-03 14:25:03 +00:00
|
|
|
ve.dm.TableCellableNode.static.setAttributes( dataElement.attributes, domElements );
|
|
|
|
}
|
|
|
|
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( !domElements[ 0 ].getAttribute( 'data-ve-no-generated-contents' ) ) {
|
2015-10-02 10:39:40 +00:00
|
|
|
this.storeGeneratedContents( dataElement, domElements, converter.getStore() );
|
2013-12-11 10:33:15 +00:00
|
|
|
}
|
2013-06-18 14:23:37 +00:00
|
|
|
|
2013-04-10 18:15:15 +00:00
|
|
|
return dataElement;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var store = converter.getStore(),
|
2016-08-17 19:50:55 +00:00
|
|
|
originalMw = dataElement.attributes.originalMw,
|
2018-03-06 12:44:37 +00:00
|
|
|
originalDomElements = store.value( dataElement.originalDomElementsHash );
|
2013-06-18 14:23:37 +00:00
|
|
|
|
2015-02-23 03:23:32 +00:00
|
|
|
function wrapTextNode( node ) {
|
|
|
|
if ( node.nodeType === Node.TEXT_NODE ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var wrapper = doc.createElement( 'span' );
|
2015-02-23 03:23:32 +00:00
|
|
|
wrapper.appendChild( node );
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var els;
|
2013-06-18 14:23:37 +00:00
|
|
|
// If the transclusion is unchanged just send back the
|
|
|
|
// original DOM elements so selser can skip over it
|
2013-06-24 00:00:42 +00:00
|
|
|
if (
|
2016-08-17 19:50:55 +00:00
|
|
|
originalDomElements &&
|
2015-10-02 10:39:40 +00:00
|
|
|
originalMw && ve.compare( dataElement.attributes.mw, JSON.parse( originalMw ) )
|
2013-06-24 00:00:42 +00:00
|
|
|
) {
|
2015-10-02 10:39:40 +00:00
|
|
|
// originalDomElements is also used for CE rendering so return a copy
|
2016-08-17 19:50:55 +00:00
|
|
|
els = ve.copyDomElements( originalDomElements, doc );
|
2013-04-20 16:08:23 +00:00
|
|
|
} else {
|
2021-10-13 12:57:45 +00:00
|
|
|
var value;
|
2015-10-02 10:39:40 +00:00
|
|
|
if (
|
2018-04-28 13:43:18 +00:00
|
|
|
converter.doesModeNeedRendering() &&
|
2015-10-02 10:39:40 +00:00
|
|
|
// Use getHashObjectForRendering to get the rendering from the store
|
2018-03-06 12:44:37 +00:00
|
|
|
( value = store.value( store.hashOfValue( null, OO.getHash( [ this.getHashObjectForRendering( dataElement ), undefined ] ) ) ) )
|
2015-10-02 10:39:40 +00:00
|
|
|
) {
|
2015-02-23 03:23:32 +00:00
|
|
|
// For the clipboard use the current DOM contents so the user has something
|
|
|
|
// meaningful to paste into external applications
|
2016-08-17 19:50:55 +00:00
|
|
|
els = ve.copyDomElements( value, doc );
|
2015-08-19 17:33:02 +00:00
|
|
|
els[ 0 ] = wrapTextNode( els[ 0 ] );
|
2016-08-17 19:50:55 +00:00
|
|
|
} else if ( originalDomElements ) {
|
|
|
|
els = [ doc.createElement( originalDomElements[ 0 ].nodeName ) ];
|
2017-05-04 16:58:30 +00:00
|
|
|
} else if ( dataElement.type === this.cellType ) {
|
|
|
|
els = [ doc.createElement( dataElement.attributes.style === 'header' ? 'th' : 'td' ) ];
|
2013-07-11 02:05:28 +00:00
|
|
|
} else {
|
2013-12-13 17:39:12 +00:00
|
|
|
els = [ doc.createElement( 'span' ) ];
|
2013-07-11 02:05:28 +00:00
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
// All we need to send back to Parsoid is the original transclusion marker, with a
|
|
|
|
// reconstructed data-mw property.
|
2015-08-19 17:33:02 +00:00
|
|
|
els[ 0 ].setAttribute( 'typeof', 'mw:Transclusion' );
|
|
|
|
els[ 0 ].setAttribute( 'data-mw', JSON.stringify( dataElement.attributes.mw ) );
|
2015-02-16 16:00:50 +00:00
|
|
|
}
|
|
|
|
if ( converter.isForClipboard() ) {
|
2019-07-16 20:51:28 +00:00
|
|
|
// If the first element is a <link>, <meta> or <style> tag, e.g. a category or TemplateStyles,
|
|
|
|
// ensure it is not destroyed by copy-paste by replacing it with a span
|
|
|
|
if ( els[ 0 ].tagName === 'LINK' || els[ 0 ].tagName === 'META' || els[ 0 ].tagName === 'STYLE' ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var span = doc.createElement( 'span' );
|
2015-02-16 16:48:29 +00:00
|
|
|
span.setAttribute( 'typeof', 'mw:Transclusion' );
|
2015-08-19 17:33:02 +00:00
|
|
|
span.setAttribute( 'data-mw', els[ 0 ].getAttribute( 'data-mw' ) );
|
|
|
|
els[ 0 ] = span;
|
2015-02-16 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty spans can get thrown around by Chrome when pasting, so give them a space
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( els[ 0 ].innerHTML === '' ) {
|
|
|
|
els[ 0 ].appendChild( doc.createTextNode( '\u00a0' ) );
|
2015-02-16 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 16:00:50 +00:00
|
|
|
// Mark the data-mw element as not having valid generated contents with it in case it is
|
2013-12-11 10:33:15 +00:00
|
|
|
// inserted into another editor (e.g. via paste).
|
2015-08-19 17:33:02 +00:00
|
|
|
els[ 0 ].setAttribute( 'data-ve-no-generated-contents', true );
|
2015-02-16 16:00:50 +00:00
|
|
|
|
|
|
|
// ... and mark all but the first child as ignorable
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 1; i < els.length; i++ ) {
|
2015-02-16 16:00:50 +00:00
|
|
|
// Wrap plain text nodes so we can give them an attribute
|
2015-08-19 17:33:02 +00:00
|
|
|
els[ i ] = wrapTextNode( els[ i ] );
|
|
|
|
els[ i ].setAttribute( 'data-ve-ignore', 'true' );
|
2015-02-16 16:00:50 +00:00
|
|
|
}
|
2018-05-10 16:19:39 +00:00
|
|
|
} else if ( converter.isForPreview() ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
|
2018-05-10 16:19:39 +00:00
|
|
|
modelNode.setDocument( converter.internalList.getDocument() );
|
2021-06-21 08:10:27 +00:00
|
|
|
var viewNode = ve.ce.nodeFactory.createFromModel( modelNode );
|
2022-04-20 22:44:19 +00:00
|
|
|
// HACK: Node must be attached to check for rendering
|
|
|
|
viewNode.$element.appendTo( 'body' );
|
2018-05-10 16:19:39 +00:00
|
|
|
if ( !viewNode.hasRendering() ) {
|
2022-04-20 22:44:19 +00:00
|
|
|
viewNode.$element.detach();
|
2018-05-10 16:19:39 +00:00
|
|
|
viewNode.onSetup();
|
2021-10-19 14:32:00 +00:00
|
|
|
// HACK: Force the icon to render immediately
|
|
|
|
viewNode.updateInvisibleIconSync( true );
|
2018-05-10 16:19:39 +00:00
|
|
|
els = viewNode.$element.toArray();
|
|
|
|
}
|
2022-04-20 22:44:19 +00:00
|
|
|
viewNode.destroy();
|
|
|
|
viewNode.$element.detach();
|
2013-04-20 16:08:23 +00:00
|
|
|
}
|
2015-02-16 16:00:50 +00:00
|
|
|
return els;
|
2013-04-10 18:15:15 +00:00
|
|
|
};
|
|
|
|
|
2018-03-07 18:40:20 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.describeChanges = function ( attributeChanges ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var descriptions = [ ve.msg( 'visualeditor-changedesc-mwtransclusion' ) ];
|
2018-03-07 18:40:20 +00:00
|
|
|
|
|
|
|
// This method assumes that the behavior of isDiffComparable above remains
|
|
|
|
// the same, so it doesn't have to consider whether the actual template
|
|
|
|
// involved has changed.
|
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
function getLabel( par ) {
|
2018-03-07 18:40:20 +00:00
|
|
|
// If a parameter is an object with a wt key, we just want the value of that.
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( par && par.wt !== undefined ) {
|
2018-03-07 18:40:20 +00:00
|
|
|
// Can be `''`, and we're okay with that
|
2020-08-18 12:16:49 +00:00
|
|
|
return par.wt;
|
2018-03-07 18:40:20 +00:00
|
|
|
}
|
2020-08-18 12:16:49 +00:00
|
|
|
return par;
|
2018-03-07 18:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( attributeChanges.mw.from.parts.length === 1 && attributeChanges.mw.to.parts.length === 1 ) {
|
|
|
|
// Single-template transclusion, before and after. Relatively easy to summarize.
|
|
|
|
// TODO: expand this to well-represent transclusions that contain multiple templates.
|
|
|
|
|
|
|
|
// The bits of a template we care about are deeply-nested inside an
|
|
|
|
// attribute. We'll restructure this so that we can pretend template
|
|
|
|
// params are the direct attributes of the template.
|
2021-06-21 08:10:27 +00:00
|
|
|
var params = {};
|
2021-10-13 12:57:45 +00:00
|
|
|
var param;
|
2018-03-07 18:40:20 +00:00
|
|
|
for ( param in attributeChanges.mw.from.parts[ 0 ].template.params ) {
|
|
|
|
params[ param ] = { from: getLabel( attributeChanges.mw.from.parts[ 0 ].template.params[ param ] ) };
|
|
|
|
}
|
|
|
|
for ( param in attributeChanges.mw.to.parts[ 0 ].template.params ) {
|
|
|
|
params[ param ] = ve.extendObject(
|
|
|
|
{ to: getLabel( attributeChanges.mw.to.parts[ 0 ].template.params[ param ] ) },
|
|
|
|
params[ param ]
|
|
|
|
);
|
|
|
|
}
|
2021-10-13 12:57:45 +00:00
|
|
|
var paramChanges;
|
2018-03-07 18:40:20 +00:00
|
|
|
for ( param in params ) {
|
|
|
|
// All we know is that *something* changed, without the normal
|
|
|
|
// helpful just-being-given-the-changed-bits, so we have to filter
|
|
|
|
// this ourselves.
|
2018-05-28 13:56:53 +00:00
|
|
|
// Trim string values, and convert empty strings to undefined
|
2021-06-21 08:10:27 +00:00
|
|
|
var from = ( params[ param ].from || '' ).trim() || undefined,
|
|
|
|
to = ( params[ param ].to || '' ).trim() || undefined;
|
2018-05-28 13:56:53 +00:00
|
|
|
if ( from !== to ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var change = this.describeChange( param, { from: from, to: to } );
|
2018-03-07 18:40:20 +00:00
|
|
|
if ( change ) {
|
2018-11-05 15:17:47 +00:00
|
|
|
if ( !paramChanges ) {
|
|
|
|
paramChanges = document.createElement( 'ul' );
|
|
|
|
descriptions.push( paramChanges );
|
2018-03-07 18:40:20 +00:00
|
|
|
}
|
2021-06-21 08:10:27 +00:00
|
|
|
var listItem = document.createElement( 'li' );
|
2018-11-05 15:17:47 +00:00
|
|
|
if ( typeof change === 'string' ) {
|
|
|
|
listItem.appendChild( document.createTextNode( change ) );
|
2018-03-07 18:40:20 +00:00
|
|
|
} else {
|
2018-11-05 15:17:47 +00:00
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
change.forEach( function ( node ) {
|
|
|
|
listItem.appendChild( node );
|
|
|
|
} );
|
2018-03-07 18:40:20 +00:00
|
|
|
}
|
2018-11-05 15:17:47 +00:00
|
|
|
paramChanges.appendChild( listItem );
|
2018-03-07 18:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 16:46:29 +00:00
|
|
|
return descriptions;
|
2017-03-16 15:32:59 +00:00
|
|
|
};
|
|
|
|
|
2019-04-16 15:17:29 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc ve.dm.Node
|
|
|
|
*/
|
2016-02-07 18:28:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.cloneElement = function () {
|
2016-05-04 13:43:27 +00:00
|
|
|
// Parent method
|
|
|
|
var clone = ve.dm.MWTransclusionNode.super.static.cloneElement.apply( this, arguments );
|
2016-02-07 18:28:04 +00:00
|
|
|
delete clone.attributes.originalMw;
|
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2013-05-15 20:45:14 +00:00
|
|
|
/**
|
2013-12-09 19:05:57 +00:00
|
|
|
* Escape a template parameter. Helper function for #getWikitext.
|
|
|
|
*
|
|
|
|
* @static
|
2013-05-15 20:45:14 +00:00
|
|
|
* @param {string} param Parameter value
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Escaped parameter value
|
2013-05-15 20:45:14 +00:00
|
|
|
*/
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.escapeParameter = function ( param ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var input = param,
|
2015-01-31 00:41:37 +00:00
|
|
|
output = '',
|
|
|
|
inNowiki = false,
|
|
|
|
bracketStack = 0,
|
|
|
|
linkStack = 0;
|
|
|
|
|
2013-05-15 20:45:14 +00:00
|
|
|
while ( input.length > 0 ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var match = input.match( /(?:\[\[)|(?:\]\])|(?:\{\{)|(?:\}\})|\|+|<\/?nowiki>|<nowiki\s*\/>/ );
|
2013-05-15 20:45:14 +00:00
|
|
|
if ( !match ) {
|
|
|
|
output += input;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-06 19:11:02 +00:00
|
|
|
output += input.slice( 0, match.index );
|
2015-08-19 17:33:02 +00:00
|
|
|
input = input.slice( match.index + match[ 0 ].length );
|
2013-05-15 20:45:14 +00:00
|
|
|
if ( inNowiki ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( match[ 0 ] === '</nowiki>' ) {
|
2013-05-15 20:45:14 +00:00
|
|
|
inNowiki = false;
|
|
|
|
}
|
2022-01-13 11:10:02 +00:00
|
|
|
output += match[ 0 ];
|
2013-05-15 20:45:14 +00:00
|
|
|
} else {
|
2021-06-21 08:10:27 +00:00
|
|
|
var needsNowiki = true;
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( match[ 0 ] === '<nowiki>' ) {
|
2013-05-15 20:45:14 +00:00
|
|
|
inNowiki = true;
|
2013-06-27 19:12:44 +00:00
|
|
|
needsNowiki = false;
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( match[ 0 ] === '</nowiki>' || /<nowiki\s*\/>/.test( match[ 0 ] ) ) {
|
2013-06-27 19:12:44 +00:00
|
|
|
needsNowiki = false;
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( /(?:\[\[)/.test( match[ 0 ] ) ) {
|
2013-07-11 14:45:18 +00:00
|
|
|
linkStack++;
|
|
|
|
needsNowiki = false;
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( /(?:\]\])/.test( match[ 0 ] ) ) {
|
2013-07-11 14:45:18 +00:00
|
|
|
if ( linkStack > 0 ) {
|
|
|
|
linkStack--;
|
|
|
|
needsNowiki = false;
|
|
|
|
}
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( /(?:\{\{)/.test( match[ 0 ] ) ) {
|
2013-06-27 19:12:44 +00:00
|
|
|
bracketStack++;
|
|
|
|
needsNowiki = false;
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( /(?:\}\})/.test( match[ 0 ] ) ) {
|
2013-06-27 19:12:44 +00:00
|
|
|
if ( bracketStack > 0 ) {
|
|
|
|
bracketStack--;
|
|
|
|
needsNowiki = false;
|
|
|
|
}
|
2022-01-13 11:10:02 +00:00
|
|
|
} else if ( /\|+/.test( match[ 0 ] ) ) {
|
2013-07-11 14:45:18 +00:00
|
|
|
if ( bracketStack > 0 || linkStack > 0 ) {
|
2013-06-27 19:12:44 +00:00
|
|
|
needsNowiki = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( needsNowiki ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
output += '<nowiki>' + match[ 0 ] + '</nowiki>';
|
2013-06-27 19:12:44 +00:00
|
|
|
} else {
|
2015-08-19 17:33:02 +00:00
|
|
|
output += match[ 0 ];
|
2013-05-15 20:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2016-05-15 11:08:13 +00:00
|
|
|
/**
|
2021-06-17 15:54:14 +00:00
|
|
|
* Recreate the wikitext for this transclusion, possibly containing multiple template invocations,
|
|
|
|
* mixed with raw wikitext snippets.
|
2016-05-15 11:08:13 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {Object} content MW data content
|
2021-06-17 15:54:14 +00:00
|
|
|
* @return {string} Wikitext
|
2016-05-15 11:08:13 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.static.getWikitext = function ( content ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var wikitext = '';
|
2016-05-15 11:08:13 +00:00
|
|
|
|
|
|
|
// Normalize to multi template format
|
|
|
|
if ( content.params ) {
|
|
|
|
content = { parts: [ { template: content } ] };
|
|
|
|
}
|
|
|
|
// Build wikitext from content
|
2021-10-13 12:57:45 +00:00
|
|
|
for ( var i = 0, len = content.parts.length; i < len; i++ ) {
|
|
|
|
var part = content.parts[ i ];
|
2016-05-15 11:08:13 +00:00
|
|
|
if ( part.template ) {
|
|
|
|
// Template
|
2021-10-13 12:57:45 +00:00
|
|
|
var template = part.template;
|
2016-05-15 11:08:13 +00:00
|
|
|
wikitext += '{{' + template.target.wt;
|
2021-10-13 12:57:45 +00:00
|
|
|
for ( var param in template.params ) {
|
2016-05-15 11:08:13 +00:00
|
|
|
wikitext += '|' + param + '=' +
|
|
|
|
this.escapeParameter( template.params[ param ].wt );
|
|
|
|
}
|
|
|
|
wikitext += '}}';
|
|
|
|
} else {
|
|
|
|
// Plain wikitext
|
|
|
|
wikitext += part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wikitext;
|
|
|
|
};
|
|
|
|
|
2013-05-15 20:45:14 +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
|
|
|
/**
|
|
|
|
* Handle attribute change events.
|
|
|
|
*
|
|
|
|
* @param {string} key Attribute key
|
|
|
|
* @param {string} from Old value
|
|
|
|
* @param {string} to New value
|
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.prototype.onAttributeChange = function ( key ) {
|
|
|
|
if ( key === 'mw' ) {
|
|
|
|
this.partsList = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-01 23:15:47 +00:00
|
|
|
/**
|
|
|
|
* Check if transclusion contains only a single template.
|
|
|
|
*
|
2021-06-17 16:34:58 +00:00
|
|
|
* @param {string|string[]} [allowedTemplates] Names of templates to allow, omit to allow any template name
|
2014-05-10 18:26:28 +00:00
|
|
|
* @return {boolean} Transclusion only contains a single template, which is one of the ones in templates
|
2014-04-01 23:15:47 +00:00
|
|
|
*/
|
2021-06-17 16:34:58 +00:00
|
|
|
ve.dm.MWTransclusionNode.prototype.isSingleTemplate = function ( allowedTemplates ) {
|
2021-06-08 13:09:52 +00:00
|
|
|
var templateNS = mw.config.get( 'wgNamespaceIds' ).template,
|
2021-06-17 16:34:58 +00:00
|
|
|
parts = this.getPartsList();
|
2015-08-19 18:05:01 +00:00
|
|
|
|
2017-07-03 16:34:37 +00:00
|
|
|
function normalizeTemplateTitle( name ) {
|
|
|
|
var title = mw.Title.newFromText( name, templateNS );
|
2014-05-10 18:26:28 +00:00
|
|
|
return title ? title.getPrefixedText() : name;
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:34:58 +00:00
|
|
|
// Bail out as early as possible when no filter is given, or it's not a single part anyway
|
|
|
|
var isSingle = parts.length === 1;
|
|
|
|
if ( !isSingle || !allowedTemplates ) {
|
|
|
|
return isSingle;
|
2014-05-10 18:26:28 +00:00
|
|
|
}
|
2021-06-17 16:34:58 +00:00
|
|
|
|
|
|
|
var singlePart = parts[ 0 ];
|
|
|
|
// It's not a template but e.g. a parser function or raw wikitext content
|
|
|
|
if ( !singlePart.templatePage ) {
|
2021-06-08 13:09:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-17 16:34:58 +00:00
|
|
|
|
|
|
|
if ( typeof allowedTemplates === 'string' ) {
|
|
|
|
allowedTemplates = [ allowedTemplates ];
|
2014-05-10 18:26:28 +00:00
|
|
|
}
|
2021-06-17 16:34:58 +00:00
|
|
|
return allowedTemplates.some( function ( template ) {
|
|
|
|
return singlePart.templatePage === normalizeTemplateTitle( template );
|
2021-06-08 13:09:52 +00:00
|
|
|
} );
|
2014-04-01 23:15:47 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Get a simplified description of the transclusion's parts.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object[]} List of objects with either template or content properties
|
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
|
|
|
*/
|
|
|
|
ve.dm.MWTransclusionNode.prototype.getPartsList = function () {
|
|
|
|
if ( !this.partsList ) {
|
|
|
|
this.partsList = [];
|
2021-06-21 08:10:27 +00:00
|
|
|
var content = this.getAttribute( 'mw' );
|
|
|
|
for ( var i = 0; i < content.parts.length; i++ ) {
|
|
|
|
var part = content.parts[ i ];
|
2021-06-17 16:34:58 +00:00
|
|
|
// A template as serialized by {@see ve.dm.MWTemplateModel.serialize}
|
2017-06-14 22:41:22 +00:00
|
|
|
if ( part.template ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var href = part.template.target.href,
|
|
|
|
page = href ? mw.libs.ve.normalizeParsoidResourceName( href ) : null;
|
2017-06-14 22:41:22 +00:00
|
|
|
this.partsList.push( {
|
|
|
|
template: part.template.target.wt,
|
|
|
|
templatePage: page
|
|
|
|
} );
|
|
|
|
} else {
|
2021-06-17 16:34:58 +00:00
|
|
|
// Raw wikitext as serialized by {@see ve.dm.MWTransclusionContentModel.serialize}
|
2017-06-14 22:41:22 +00:00
|
|
|
this.partsList.push( { content: part } );
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.partsList;
|
|
|
|
};
|
|
|
|
|
2013-05-15 20:45:14 +00:00
|
|
|
/**
|
2021-06-17 15:54:14 +00:00
|
|
|
* Wrapper for static method, {@see ve.dm.MWTransclusionNode.static.getWikitext} above.
|
2013-05-28 23:14:43 +00:00
|
|
|
*
|
2021-06-17 15:54:14 +00:00
|
|
|
* @return {string} Wikitext
|
2013-05-15 20:45:14 +00:00
|
|
|
*/
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.MWTransclusionNode.prototype.getWikitext = function () {
|
2016-05-15 11:08:13 +00:00
|
|
|
return this.constructor.static.getWikitext( this.getAttribute( 'mw' ) );
|
2013-05-15 20:45:14 +00:00
|
|
|
};
|
|
|
|
|
2013-04-10 18:15:15 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWTransclusionNode );
|