All files / src/ui/contextitems ve.ui.LinkContextItem.js

82.6% Statements 38/46
41.66% Branches 5/12
80% Functions 4/5
82.6% Lines 38/46

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                1x   11x     11x   11x 11x                                           11x         11x             11x   11x 11x             1x       1x   1x   1x   1x   1x   1x   1x   1x   1x             1x 11x           1x 11x 11x               11x 11x 11x 11x   11x               1x 11x 11x           11x 11x     11x                   1x                           1x  
/*!
 * VisualEditor LinkContextItem class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Context item for a link.
 *
 * @class
 * @extends ve.ui.AnnotationContextItem
 *
 * @param {ve.ui.LinearContext} context Context the item is in
 * @param {ve.dm.Model} model Model the item is related to
 * @param {Object} [config] Configuration options
 */
ve.ui.LinkContextItem = function VeUiLinkContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.LinkContextItem.super.call( this, context, model, config );
 
	// Initialization
	this.$element.addClass( 've-ui-linkContextItem' );
 
	this.labelPreview = new OO.ui.LabelWidget();
	Iif ( this.context.isMobile() ) {
		this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append(
			$( '<div>' ).addClass( 've-ui-linkContextItem-label-body' ).append(
				new OO.ui.LabelWidget( {
					classes: [ 've-ui-linkContextItem-label-label' ],
					label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' )
				} ).$element,
				$( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element )
			)
		);
		this.$innerBody = $( '<div>' ).addClass( 've-ui-linkContextItem-inner-body' );
		this.$body.append(
			this.$labelLayout,
			new OO.ui.LabelWidget( {
				classes: [ 've-ui-linkContextItem-link-label' ],
				label: OO.ui.deferMsg( 'visualeditor-linkinspector-title' )
			} ).$element,
			this.$innerBody
		);
		// Sub-classes should now append content to $innerBody
		this.$body = this.$innerBody;
	} else {
		this.labelButton = new OO.ui.ButtonWidget( {
			label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-change' ),
			framed: false,
			flags: [ 'progressive' ]
		} );
		this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append(
			$( '<div>' ).addClass( 've-ui-linkContextItem-label-label' ).append(
				new OO.ui.IconWidget( { icon: 'quotes' } ).$element,
				new OO.ui.LabelWidget( { label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' ) } ).$element
			),
			$( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element )
		);
		this.labelButton.connect( this, { click: 'onLabelButtonClick' } );
 
		Eif ( !this.isReadOnly() ) {
			this.$labelLayout.append( $( '<div>' ).addClass( 've-ui-linkContextItem-label-action' ).append( this.labelButton.$element ) );
		}
	}
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LinkContextItem, ve.ui.AnnotationContextItem );
 
/* Static Properties */
 
ve.ui.LinkContextItem.static.name = 'link';
 
ve.ui.LinkContextItem.static.icon = 'link';
 
ve.ui.LinkContextItem.static.label = OO.ui.deferMsg( 'visualeditor-linkinspector-title' );
 
ve.ui.LinkContextItem.static.modelClasses = [ ve.dm.LinkAnnotation ];
 
ve.ui.LinkContextItem.static.embeddable = false;
 
ve.ui.LinkContextItem.static.commandName = 'link';
 
ve.ui.LinkContextItem.static.clearable = true;
 
ve.ui.LinkContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-linkcontext-remove' );
 
ve.ui.LinkContextItem.static.clearIcon = 'unLink';
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.LinkContextItem.prototype.getDescription = function () {
	return this.model.getHref();
};
 
/**
 * @inheritdoc
 */
ve.ui.LinkContextItem.prototype.renderBody = function () {
	var htmlDoc = this.context.getSurface().getModel().getDocument().getHtmlDocument();
	var $link = $( '<a>' )
		.addClass( 've-ui-linkContextItem-link' )
		.text( this.getDescription() )
		.attr( {
			target: '_blank',
			rel: 'noopener'
		} );
	// T322704
	ve.setAttributeSafe( $link[ 0 ], 'href', ve.resolveUrl( this.model.getHref(), htmlDoc ), '#' );
	this.$body.empty().append( $link );
	Eif ( !this.context.isMobile() ) {
		this.$body.append( this.$labelLayout );
	}
	this.updateLabelPreview();
};
 
/**
 * Set the preview of the label
 *
 * @protected
 */
ve.ui.LinkContextItem.prototype.updateLabelPreview = function () {
	var surfaceModel = this.context.getSurface().getModel(),
		annotationView = this.getAnnotationView();
 
	// annotationView is a potentially old view node from when the context was
	// first focused in the document. If the annotation model has been changed
	// as well, this may be a problem.
	var label;
	Eif ( annotationView ) {
		label = surfaceModel.getFragment().expandLinearSelection( 'annotation', annotationView.getModel() ).getText();
	}
 
	this.labelPreview.setLabel( label || ve.msg( 'visualeditor-linkcontext-label-fallback' ) );
};
 
/**
 * Handle label-edit button click events.
 *
 * @localdoc Selects the contents of the link annotation
 *
 * @protected
 */
ve.ui.LinkContextItem.prototype.onLabelButtonClick = function () {
	var surface = this.context.getSurface().getView(),
		annotationView = this.getAnnotationView();
 
	surface.selectNodeContents(
		annotationView.$element[ 0 ],
		this.context.isMobile() ? 'end' : undefined
	);
 
	ve.track( 'activity.' + this.constructor.static.name, { action: 'context-label' } );
};
 
/* Registration */
 
ve.ui.contextItemFactory.register( ve.ui.LinkContextItem );