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 ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
var href = domElements[ 0 ].getAttribute( 'href' ),
|
2020-03-20 17:08:24 +00:00
|
|
|
data = mw.libs.ve.parseParsoidResourceName( href ),
|
2018-02-28 02:01:05 +00:00
|
|
|
rawTitleAndFragment = data.rawTitle.match( /^(.*?)(?:#(.*))?$/ ),
|
|
|
|
titleAndFragment = data.title.match( /^(.*?)(?:#(.*))?$/ );
|
2013-03-21 19:18:52 +00:00
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
2018-02-28 02:01:05 +00:00
|
|
|
category: titleAndFragment[ 1 ],
|
|
|
|
origCategory: rawTitleAndFragment[ 1 ],
|
|
|
|
sortkey: titleAndFragment[ 2 ] || '',
|
|
|
|
origSortkey: rawTitleAndFragment[ 2 ] || ''
|
2013-03-21 19:18:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-04-02 20:23:16 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.toDomElements = function ( dataElement, doc ) {
|
2019-04-03 19:17:19 +00:00
|
|
|
var href, encodedCategory,
|
2013-04-02 20:23:16 +00:00
|
|
|
domElement = doc.createElement( 'link' ),
|
2013-04-09 04:44:59 +00:00
|
|
|
category = dataElement.attributes.category || '',
|
|
|
|
sortkey = dataElement.attributes.sortkey || '',
|
|
|
|
origCategory = dataElement.attributes.origCategory || '',
|
|
|
|
origSortkey = dataElement.attributes.origSortkey || '',
|
2020-03-20 17:08:24 +00:00
|
|
|
normalizedOrigCategory = mw.libs.ve.decodeURIComponentIntoArticleTitle( origCategory ),
|
|
|
|
normalizedOrigSortkey = mw.libs.ve.decodeURIComponentIntoArticleTitle( origSortkey );
|
2013-03-21 19:18:52 +00:00
|
|
|
if ( normalizedOrigSortkey === sortkey ) {
|
|
|
|
sortkey = origSortkey;
|
|
|
|
} else {
|
|
|
|
sortkey = encodeURIComponent( sortkey );
|
|
|
|
}
|
|
|
|
if ( normalizedOrigCategory === category ) {
|
2019-04-03 19:17:19 +00:00
|
|
|
encodedCategory = origCategory;
|
2013-03-21 19:18:52 +00:00
|
|
|
} else {
|
2019-04-03 19:17:19 +00:00
|
|
|
encodedCategory = encodeURIComponent( category );
|
2013-03-21 19:18:52 +00:00
|
|
|
}
|
2013-12-06 21:12:44 +00:00
|
|
|
domElement.setAttribute( 'rel', 'mw:PageProp/Category' );
|
2019-04-03 19:17:19 +00:00
|
|
|
href = './' + encodedCategory;
|
2013-03-21 19:18:52 +00:00
|
|
|
if ( sortkey !== '' ) {
|
|
|
|
href += '#' + sortkey;
|
|
|
|
}
|
|
|
|
domElement.setAttribute( 'href', href );
|
|
|
|
return [ domElement ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWCategoryMetaItem );
|