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

61.9% Statements 26/42
33.33% Branches 7/21
44.44% Functions 4/9
61.9% Lines 26/42

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                                  1x   13x     13x   13x                     13x         13x 13x     13x         1x       1x 1x 1x                 1x 13x                   1x                       1x                                                               1x 11x 11x 11x     11x         11x 11x   11x 11x     11x    
/*!
 * VisualEditor AnnotationContextItem class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Context item for an annotation.
 *
 * @class
 * @abstract
 * @extends ve.ui.LinearContextItem
 *
 * @param {ve.ui.Context} context Context item is in
 * @param {ve.dm.Model} model Model item is related to
 * @param {Object} config Configuration options
 */
ve.ui.AnnotationContextItem = function VeUiAnnotationContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.AnnotationContextItem.super.call( this, context, model, config );
 
	// Initialization
	this.$element.addClass( 've-ui-annotationContextItem' );
 
	Iif ( this.context.isMobile() ) {
		this.clearButton = new OO.ui.ButtonWidget( {
			framed: false,
			label: this.constructor.static.clearMsg,
			icon: this.constructor.static.clearIcon,
			flags: [ 'destructive' ]
		} );
		if ( this.isClearable() && !this.isReadOnly() ) {
			this.$foot.append( this.clearButton.$element );
		}
	} else {
		this.clearButton = new OO.ui.ButtonWidget( {
			title: this.constructor.static.clearMsg,
			icon: this.constructor.static.clearIcon,
			flags: [ 'destructive' ]
		} );
		Eif ( this.isClearable() && !this.isReadOnly() ) {
			this.actionButtons.addItems( [ this.clearButton ], 0 );
		}
	}
	this.clearButton.connect( this, { click: 'onClearButtonClick' } );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.AnnotationContextItem, ve.ui.LinearContextItem );
 
/* Static Properties */
 
ve.ui.AnnotationContextItem.static.clearable = true;
ve.ui.AnnotationContextItem.static.clearIcon = 'cancel';
ve.ui.AnnotationContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-clearbutton-tooltip' );
 
/* Methods */
 
/**
 * Check if item is clearable.
 *
 * @return {boolean} Item is clearable
 */
ve.ui.AnnotationContextItem.prototype.isClearable = function () {
	return this.constructor.static.clearable;
};
 
/**
 * Handle clear button click events.
 *
 * @localdoc Removes any modelClasses annotations from the current fragment
 *
 * @protected
 */
ve.ui.AnnotationContextItem.prototype.onClearButtonClick = function () {
	ve.track( 'activity.' + this.constructor.static.name, { action: 'context-clear' } );
	this.applyToAnnotations( function ( fragment, annotation ) {
		fragment.annotateContent( 'clear', annotation );
	} );
};
 
/**
 * Apply a callback to every modelClass annotation in the current fragment
 *
 * @param  {Function} callback Callback, will be passed fragment and annotation
 */
ve.ui.AnnotationContextItem.prototype.applyToAnnotations = function ( callback ) {
	var i, len,
		modelClasses = this.constructor.static.modelClasses,
		fragment = this.getFragment(),
		annotations = fragment.getAnnotations( true ).filter( function ( annotation ) {
			return ve.isInstanceOfAny( annotation, modelClasses );
		} ).get();
	if (
		!annotations.length &&
		fragment.getSelection().isCollapsed() &&
		fragment.getDocument().data.isContentOffset( fragment.getSelection().getRange().start )
	) {
		// Expand to nearest word and try again
		fragment = fragment.expandLinearSelection( 'word' );
 
		annotations = fragment.getAnnotations( true ).filter( function ( annotation ) {
			return ve.isInstanceOfAny( annotation, modelClasses );
		} ).get();
	}
	for ( i = 0, len = annotations.length; i < len; i++ ) {
		callback( fragment.expandLinearSelection( 'annotation', annotations[ i ] ), annotations[ i ] );
	}
};
 
/**
 * Find the CE view for the annotation related to this context item
 *
 * Problem: Going from the model to the view is difficult, as there can be many views of any given model.
 * Assumption: an active annotation context item must mean the focus is within the relevant annotation.
 *
 * @return {ve.ce.Annotation|undefined} The annotation view, if it's found, or undefined if not
 */
ve.ui.AnnotationContextItem.prototype.getAnnotationView = function () {
	var annotations = [],
		model = this.model,
		surfaceView = this.context.getSurface().getView();
 
	function isThisModel( annotationView ) {
		return model === annotationView.model;
	}
 
	// Use surfaceView.contexedAnnotations when available, i.e. when
	// the user clicked/tapped on the annotation.
	Eif ( surfaceView.contexedAnnotations ) {
		annotations = surfaceView.contexedAnnotations.filter( isThisModel );
	}
	Eif ( !annotations.length ) {
		annotations = surfaceView.annotationsAtModelSelection( isThisModel );
	}
 
	return annotations.length ? annotations[ 0 ] : undefined;
};