2013-03-21 19:18:52 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWCategoryMetaItem class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-03-21 19:18:52 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel category meta item.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.MetaItem
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} element Reference to element in meta-linmod
|
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWCategoryMetaItem = function VeDmMWCategoryMetaItem() {
|
2013-03-21 19:18:52 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.super.apply( this, arguments );
|
2013-03-21 19:18:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWCategoryMetaItem, ve.dm.MetaItem );
|
2013-03-21 19:18:52 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.name = 'mwCategory';
|
2013-03-21 19:18:52 +00:00
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.group = 'mwCategory';
|
2013-04-11 21:49:17 +00:00
|
|
|
|
2013-03-21 19:18:52 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.matchTagNames = [ 'link' ];
|
|
|
|
|
2013-12-06 21:12:44 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.matchRdfaTypes = [ 'mw:PageProp/Category' ];
|
2013-03-21 19:18:52 +00:00
|
|
|
|
|
|
|
ve.dm.MWCategoryMetaItem.static.toDataElement = function ( domElements ) {
|
2022-12-21 15:49:20 +00:00
|
|
|
// Parsoid: LinkHandlerUtils::serializeAsWikiLink
|
2015-08-19 17:33:02 +00:00
|
|
|
var href = domElements[ 0 ].getAttribute( 'href' ),
|
2022-12-21 15:49:20 +00:00
|
|
|
titleAndFragment = href.match( /^(.*?)(?:#(.*))?\s*$/ );
|
2013-03-21 19:18:52 +00:00
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
2022-12-21 15:49:20 +00:00
|
|
|
category: mw.libs.ve.parseParsoidResourceName( titleAndFragment[ 1 ] ).title,
|
|
|
|
sortkey: titleAndFragment[ 2 ] ? decodeURIComponent( titleAndFragment[ 2 ] ) : ''
|
2013-03-21 19:18:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-19 16:01:44 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.toDomElements = function ( dataElement, doc, converter ) {
|
|
|
|
var domElement;
|
|
|
|
var category = dataElement.attributes.category || '';
|
|
|
|
if ( converter.isForPreview() ) {
|
|
|
|
domElement = doc.createElement( 'a' );
|
|
|
|
var title = mw.Title.newFromText( category );
|
|
|
|
domElement.setAttribute( 'href', title.getUrl() );
|
|
|
|
domElement.appendChild( doc.createTextNode( title.getMainText() ) );
|
|
|
|
} else {
|
|
|
|
domElement = doc.createElement( 'link' );
|
|
|
|
var sortkey = dataElement.attributes.sortkey || '';
|
|
|
|
domElement.setAttribute( 'rel', 'mw:PageProp/Category' );
|
|
|
|
|
|
|
|
// Parsoid: WikiLinkHandler::renderCategory
|
|
|
|
var href = mw.libs.ve.encodeParsoidResourceName( category );
|
|
|
|
if ( sortkey !== '' ) {
|
|
|
|
href += '#' + sortkey.replace( /[%? [\]#|<>]/g, function ( match ) {
|
|
|
|
return encodeURIComponent( match );
|
|
|
|
} );
|
|
|
|
}
|
2022-12-21 15:49:20 +00:00
|
|
|
|
2022-01-19 16:01:44 +00:00
|
|
|
domElement.setAttribute( 'href', href );
|
|
|
|
}
|
2013-03-21 19:18:52 +00:00
|
|
|
return [ domElement ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWCategoryMetaItem );
|
2022-01-19 16:01:44 +00:00
|
|
|
|
|
|
|
ve.ui.metaListDiffRegistry.register( 'mwCategory', function ( diffElement, diffQueue, documentNode /* , documentSpacerNode */ ) {
|
|
|
|
diffQueue = diffElement.processQueue( diffQueue );
|
|
|
|
|
|
|
|
if ( !diffQueue.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var catLinks = document.createElement( 'div' );
|
|
|
|
catLinks.setAttribute( 'class', 'catlinks' );
|
|
|
|
|
|
|
|
var headerLink = document.createElement( 'a' );
|
|
|
|
headerLink.appendChild( document.createTextNode( ve.msg( 'pagecategories', diffQueue.length ) ) );
|
|
|
|
headerLink.setAttribute( 'href', ve.msg( 'pagecategorieslink' ) );
|
|
|
|
|
|
|
|
catLinks.appendChild( headerLink );
|
|
|
|
catLinks.appendChild( document.createTextNode( ve.msg( 'colon-separator' ) ) );
|
|
|
|
|
|
|
|
var list = document.createElement( 'ul' );
|
|
|
|
catLinks.appendChild( list );
|
|
|
|
|
|
|
|
var catSpacerNode = document.createElement( 'span' );
|
|
|
|
catSpacerNode.appendChild( document.createTextNode( ' … ' ) );
|
|
|
|
|
|
|
|
// Wrap each item in the queue in an <li>
|
|
|
|
diffQueue.forEach( function ( diffItem ) {
|
|
|
|
var listItem = document.createElement( 'li' );
|
|
|
|
diffElement.renderQueue(
|
|
|
|
[ diffItem ],
|
|
|
|
listItem, catSpacerNode
|
|
|
|
);
|
|
|
|
list.appendChild( listItem );
|
|
|
|
} );
|
|
|
|
|
|
|
|
documentNode.appendChild( catLinks );
|
|
|
|
} );
|