2013-03-21 19:18:52 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWCategoryMetaItem class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright 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
|
2024-05-21 14:22:56 +00:00
|
|
|
const 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 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let domElement;
|
|
|
|
const category = dataElement.attributes.category || '';
|
2022-01-19 16:01:44 +00:00
|
|
|
if ( converter.isForPreview() ) {
|
|
|
|
domElement = doc.createElement( 'a' );
|
2024-05-21 14:22:56 +00:00
|
|
|
const title = mw.Title.newFromText( category );
|
2022-01-19 16:01:44 +00:00
|
|
|
domElement.setAttribute( 'href', title.getUrl() );
|
|
|
|
domElement.appendChild( doc.createTextNode( title.getMainText() ) );
|
|
|
|
} else {
|
|
|
|
domElement = doc.createElement( 'link' );
|
2024-05-21 14:22:56 +00:00
|
|
|
const sortkey = dataElement.attributes.sortkey || '';
|
2022-01-19 16:01:44 +00:00
|
|
|
domElement.setAttribute( 'rel', 'mw:PageProp/Category' );
|
|
|
|
|
|
|
|
// Parsoid: WikiLinkHandler::renderCategory
|
2024-05-21 14:22:56 +00:00
|
|
|
let href = mw.libs.ve.encodeParsoidResourceName( category );
|
2022-01-19 16:01:44 +00:00
|
|
|
if ( sortkey !== '' ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
href += '#' + sortkey.replace( /[%? [\]#|<>]/g, ( match ) => encodeURIComponent( match ) );
|
2022-01-19 16:01:44 +00:00
|
|
|
}
|
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 ];
|
|
|
|
};
|
|
|
|
|
2023-05-30 12:59:52 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.isDiffComparable = function ( element, other ) {
|
|
|
|
// Don't try to compare different categories. Even fixing a typo in a category name
|
|
|
|
// results in one category being removed and another added, which we shoud show.
|
|
|
|
return element.type === other.type && element.attributes.category === other.attributes.category;
|
|
|
|
};
|
|
|
|
|
2023-05-30 13:14:08 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.describeChange = function ( key, change ) {
|
|
|
|
if ( key === 'sortkey' ) {
|
|
|
|
if ( !change.from ) {
|
|
|
|
return ve.htmlMsg( 'visualeditor-changedesc-mwcategory-sortkey-set', this.wrapText( 'ins', change.to ) );
|
|
|
|
} else if ( !change.to ) {
|
|
|
|
return ve.htmlMsg( 'visualeditor-changedesc-mwcategory-sortkey-unset', this.wrapText( 'del', change.from ) );
|
|
|
|
} else {
|
|
|
|
return ve.htmlMsg( 'visualeditor-changedesc-mwcategory-sortkey-changed',
|
|
|
|
this.wrapText( 'del', change.from ),
|
|
|
|
this.wrapText( 'ins', change.to )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
2023-11-02 16:42:31 +00:00
|
|
|
return ve.dm.MWCategoryMetaItem.super.static.describeChange.apply( this, arguments );
|
2023-05-30 13:14:08 +00:00
|
|
|
};
|
|
|
|
|
2013-03-21 19:18:52 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWCategoryMetaItem );
|
2022-01-19 16:01:44 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
ve.ui.metaListDiffRegistry.register( 'mwCategory', ( diffElement, diffQueue, documentNode /* , documentSpacerNode */ ) => {
|
2022-01-19 16:01:44 +00:00
|
|
|
diffQueue = diffElement.processQueue( diffQueue );
|
|
|
|
|
|
|
|
if ( !diffQueue.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const catLinks = document.createElement( 'div' );
|
2022-01-19 16:01:44 +00:00
|
|
|
catLinks.setAttribute( 'class', 'catlinks' );
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const headerLink = document.createElement( 'a' );
|
2022-01-19 16:01:44 +00:00
|
|
|
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' ) ) );
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const list = document.createElement( 'ul' );
|
2022-01-19 16:01:44 +00:00
|
|
|
catLinks.appendChild( list );
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const catSpacerNode = document.createElement( 'span' );
|
2022-01-19 16:01:44 +00:00
|
|
|
catSpacerNode.appendChild( document.createTextNode( ' … ' ) );
|
|
|
|
|
|
|
|
// Wrap each item in the queue in an <li>
|
2024-04-30 16:44:25 +00:00
|
|
|
diffQueue.forEach( ( diffItem ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const listItem = document.createElement( 'li' );
|
2022-01-19 16:01:44 +00:00
|
|
|
diffElement.renderQueue(
|
|
|
|
[ diffItem ],
|
|
|
|
listItem, catSpacerNode
|
|
|
|
);
|
|
|
|
list.appendChild( listItem );
|
|
|
|
} );
|
|
|
|
|
|
|
|
documentNode.appendChild( catLinks );
|
|
|
|
} );
|