2013-03-21 19:18:52 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWCategoryMetaItem class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
ve.dm.MWCategoryMetaItem = function VeDmMWCategoryMetaItem( element ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.dm.MetaItem.call( this, element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.dm.MWCategoryMetaItem, ve.dm.MetaItem );
|
|
|
|
|
|
|
|
/* 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' ];
|
|
|
|
|
|
|
|
ve.dm.MWCategoryMetaItem.static.matchRdfaTypes = [ 'mw:WikiLink/Category' ];
|
|
|
|
|
|
|
|
ve.dm.MWCategoryMetaItem.static.toDataElement = function ( domElements ) {
|
|
|
|
var firstDomElement = domElements[0],
|
|
|
|
href = firstDomElement.getAttribute( 'href' ),
|
|
|
|
/*jshint regexp:false */
|
2013-05-13 18:10:02 +00:00
|
|
|
matches = href.match( /^((?:\.\.?\/)*)(.*?)(?:#(.*))?$/ ),
|
2013-03-21 19:18:52 +00:00
|
|
|
rawSortkey = matches[3] || '';
|
|
|
|
return {
|
2013-05-28 11:31:41 +00:00
|
|
|
'type': 'mwCategory',
|
2013-03-21 19:18:52 +00:00
|
|
|
'attributes': {
|
|
|
|
'hrefPrefix': matches[1],
|
|
|
|
'category': decodeURIComponent( matches[2] ).replace( /_/g, ' ' ),
|
|
|
|
'origCategory': matches[2],
|
|
|
|
'sortkey': decodeURIComponent( rawSortkey ).replace( /_/g, ' ' ),
|
|
|
|
'origSortkey': rawSortkey
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-04-02 20:23:16 +00:00
|
|
|
ve.dm.MWCategoryMetaItem.static.toDomElements = function ( dataElement, doc ) {
|
2013-03-21 19:18:52 +00:00
|
|
|
var href,
|
2013-04-02 20:23:16 +00:00
|
|
|
domElement = doc.createElement( 'link' ),
|
2013-04-09 04:44:59 +00:00
|
|
|
hrefPrefix = dataElement.attributes.hrefPrefix || '',
|
|
|
|
category = dataElement.attributes.category || '',
|
|
|
|
sortkey = dataElement.attributes.sortkey || '',
|
|
|
|
origCategory = dataElement.attributes.origCategory || '',
|
|
|
|
origSortkey = dataElement.attributes.origSortkey || '',
|
2013-03-21 19:18:52 +00:00
|
|
|
normalizedOrigCategory = decodeURIComponent( origCategory ).replace( /_/g, ' ' ),
|
|
|
|
normalizedOrigSortkey = decodeURIComponent( origSortkey );
|
|
|
|
if ( normalizedOrigSortkey === sortkey ) {
|
|
|
|
sortkey = origSortkey;
|
|
|
|
} else {
|
|
|
|
sortkey = encodeURIComponent( sortkey );
|
|
|
|
}
|
|
|
|
if ( normalizedOrigCategory === category ) {
|
|
|
|
category = origCategory;
|
|
|
|
} else {
|
|
|
|
category = encodeURIComponent( category );
|
|
|
|
}
|
|
|
|
domElement.setAttribute( 'rel', 'mw:WikiLink/Category' );
|
|
|
|
href = hrefPrefix + category;
|
|
|
|
if ( sortkey !== '' ) {
|
|
|
|
href += '#' + sortkey;
|
|
|
|
}
|
|
|
|
domElement.setAttribute( 'href', href );
|
|
|
|
return [ domElement ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWCategoryMetaItem );
|