All files / src/ce/annotations ve.ce.LinkAnnotation.js

100% Statements 14/14
100% Branches 0/0
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                  1x   117x     117x   117x         117x   117x   117x         1x   1x       1x   1x             1x 117x                                         1x  
/*!
 * VisualEditor ContentEditable LinkAnnotation class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * ContentEditable link annotation.
 *
 * @class
 * @extends ve.ce.Annotation
 * @mixins ve.ce.NailedAnnotation
 * @constructor
 * @param {ve.dm.LinkAnnotation} model Model to observe
 * @param {ve.ce.ContentBranchNode} [parentNode] Node rendering this annotation
 * @param {Object} [config] Configuration options
 */
ve.ce.LinkAnnotation = function VeCeLinkAnnotation( model, parentNode, config ) {
	// Parent constructor
	ve.ce.LinkAnnotation.super.call( this, model, parentNode, ve.extendObject( { $element: $( '<a>' ) }, config ) );
 
	// Mixin constructor
	ve.ce.NailedAnnotation.call( this );
 
	this.$element.addClass( 've-ce-linkAnnotation' )
		.prop( {
			title: this.constructor.static.getDescription( this.model )
		} );
	// T322704
	ve.setAttributeSafe( this.$element[ 0 ], 'href', ve.resolveUrl( this.model.getHref(), this.getModelHtmlDocument() ), '#' );
 
	this.$element.on( 'click', this.onClick.bind( this ) );
	// Deprecated, use this.$element
	this.$anchor = this.$element;
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.LinkAnnotation, ve.ce.Annotation );
 
OO.mixinClass( ve.ce.LinkAnnotation, ve.ce.NailedAnnotation );
 
/* Static Properties */
 
ve.ce.LinkAnnotation.static.name = 'link';
 
ve.ce.LinkAnnotation.static.tagName = 'span';
 
/* Static Methods */
 
/**
 * @inheritdoc
 */
ve.ce.LinkAnnotation.static.getDescription = function ( model ) {
	return model.getHref();
};
 
/* Methods */
 
/* istanbul ignore next */
/**
 * Handle click events.
 *
 * @param {jQuery.Event} e Mouse click event
 */
ve.ce.LinkAnnotation.prototype.onClick = function ( e ) {
	if ( e.which === OO.ui.MouseButtons.LEFT && ( e.ctrlKey || e.metaKey ) ) {
		window.open( this.$element.prop( 'href' ) );
		// Prevent multiple windows being opened, or other action being performed (e.g. middle click paste)
		e.preventDefault();
	}
};
 
/* Registration */
 
ve.ce.annotationFactory.register( ve.ce.LinkAnnotation );