2012-05-31 23:50:16 +00:00
|
|
|
/**
|
|
|
|
* DataModel annotation for a link.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.dm.Annotation}
|
|
|
|
*/
|
|
|
|
ve.dm.LinkAnnotation = function() {
|
|
|
|
// Inheritance
|
|
|
|
ve.dm.Annotation.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Members */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converters.
|
|
|
|
*
|
|
|
|
* @see {ve.dm.Converter}
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.dm.LinkAnnotation.converters = {
|
2012-06-07 00:47:27 +00:00
|
|
|
'domElementTypes': ['a'],
|
|
|
|
'toDomElement': function( subType, annotation ) {
|
2012-06-19 22:45:01 +00:00
|
|
|
var link = document.createElement( 'a' );
|
|
|
|
if ( subType === 'wikiLink' ) {
|
|
|
|
link.setAttribute( 'rel', 'mw:wikiLink' );
|
|
|
|
// Set href to /title
|
|
|
|
link.setAttribute( 'href', '/' + annotation.data.title );
|
|
|
|
} else if ( subType === 'extLink' ) {
|
|
|
|
link.setAttribute( 'rel', 'mw:extLink' );
|
2012-06-08 22:17:18 +00:00
|
|
|
link.setAttribute( 'href', annotation.data.href );
|
2012-06-07 22:02:25 +00:00
|
|
|
}
|
2012-06-19 22:45:01 +00:00
|
|
|
if ( annotation.data.mw ) {
|
|
|
|
link.setAttribute( 'data-mw', annotation.data.mw );
|
|
|
|
}
|
|
|
|
return link;
|
2012-05-31 23:50:16 +00:00
|
|
|
},
|
2012-06-07 00:47:27 +00:00
|
|
|
'toDataAnnotation': function( tag, element ) {
|
2012-06-12 01:02:21 +00:00
|
|
|
var rel = element.getAttribute( 'rel' ) || '',
|
2012-06-19 22:45:01 +00:00
|
|
|
subType = rel.split( ':' )[1] || 'unknown',
|
2012-06-14 01:54:24 +00:00
|
|
|
mwattr = element.getAttribute( 'data-mw' ),
|
|
|
|
mwdata = $.parseJSON( mwattr ) || {},
|
|
|
|
href = element.getAttribute( 'href' ),
|
2012-06-12 01:02:21 +00:00
|
|
|
retval = {
|
2012-06-19 22:45:01 +00:00
|
|
|
'type': 'link/' + subType,
|
|
|
|
'data': {}
|
2012-06-14 01:54:24 +00:00
|
|
|
};
|
2012-06-19 22:45:01 +00:00
|
|
|
if ( subType === 'wikiLink' ) {
|
2012-06-19 23:03:41 +00:00
|
|
|
retval.data.title = mwdata.sHref ||
|
2012-06-19 22:45:01 +00:00
|
|
|
// Trim leading slash from href
|
|
|
|
href.replace( /^\//, '' );
|
|
|
|
} else if ( subType === 'extLink' ) {
|
|
|
|
retval.data.href = href;
|
|
|
|
}
|
2012-06-12 01:02:21 +00:00
|
|
|
if ( mwattr ) {
|
|
|
|
retval.data.mw = mwattr;
|
|
|
|
}
|
|
|
|
return retval;
|
2012-05-31 23:50:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.annotationFactory.register( 'link', ve.dm.LinkAnnotation );
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
|