All files / src/ui/actions ve.ui.AnnotationAction.js

51.66% Statements 31/60
41.66% Branches 5/12
42.85% Functions 3/7
52.63% Lines 30/57

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                                1x   8x         1x       1x   1x                     1x                       1x                               1x 8x 8x 8x   8x 8x 8x 3x   5x                                     8x               1x                                                 1x 3x 3x   3x 3x 3x 3x       3x 3x     3x 3x         1x  
/*!
 * VisualEditor UserInterface AnnotationAction class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Annotation action.
 *
 * @class
 * @extends ve.ui.Action
 *
 * @constructor
 * @param {ve.ui.Surface} surface Surface to act on
 * @param {string} [source]
 */
ve.ui.AnnotationAction = function VeUiAnnotationAction() {
	// Parent constructor
	ve.ui.AnnotationAction.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.AnnotationAction, ve.ui.Action );
 
/* Static Properties */
 
ve.ui.AnnotationAction.static.name = 'annotation';
 
ve.ui.AnnotationAction.static.methods = [ 'set', 'clear', 'toggle', 'clearAll' ];
 
/* Methods */
 
/**
 * Set an annotation.
 *
 * @param {string} name Annotation name, for example: 'textStyle/bold'
 * @param {Object} [data] Additional annotation data
 * @return {boolean} Action was executed
 */
ve.ui.AnnotationAction.prototype.set = function ( name, data ) {
	ve.track( 'activity.' + name, { action: 'set' } );
	return this.setInternal( name, data );
};
 
/**
 * Clear an annotation.
 *
 * @param {string} name Annotation name, for example: 'textStyle/bold'
 * @param {Object} [data] Additional annotation data
 * @return {boolean} Action was executed
 */
ve.ui.AnnotationAction.prototype.clear = function ( name, data ) {
	ve.track( 'activity.' + name, { action: 'clear' } );
	this.surface.getModel().getFragment().annotateContent( 'clear', name, data );
	return true;
};
 
/**
 * Toggle an annotation.
 *
 * If the selected text is completely covered with the annotation already the annotation will be
 * cleared. Otherwise the annotation will be set.
 *
 * @param {string} name Annotation name, for example: 'textStyle/bold'
 * @param {Object} [data] Additional annotation data
 * @return {boolean} Action was executed
 */
ve.ui.AnnotationAction.prototype.toggle = function ( name, data ) {
	var surfaceModel = this.surface.getModel(),
		fragment = surfaceModel.getFragment(),
		annotation = ve.dm.annotationFactory.create( name, data );
 
	if ( !fragment.getSelection().isCollapsed() ) {
		ve.track( 'activity.' + name, { action: 'toggle-selection' } );
		if ( !fragment.getAnnotations().containsComparable( annotation ) ) {
			this.setInternal( name, data );
		} else {
			fragment.annotateContent( 'clear', name );
		}
	} else Eif ( surfaceModel.sourceMode ) {
		return false;
	} else {
		ve.track( 'activity.' + name, { action: 'toggle-insertion' } );
		var insertionAnnotations = surfaceModel.getInsertionAnnotations();
		var existingAnnotations = insertionAnnotations.getAnnotationsByName( annotation.name );
		var removes = annotation.constructor.static.removes;
		if ( existingAnnotations.isEmpty() ) {
			var removesAnnotations = insertionAnnotations.filter( function ( ann ) {
				return removes.indexOf( ann.name ) !== -1;
			} );
			surfaceModel.removeInsertionAnnotations( removesAnnotations );
			surfaceModel.addInsertionAnnotations( annotation );
		} else {
			surfaceModel.removeInsertionAnnotations( existingAnnotations );
		}
	}
	return true;
};
 
/**
 * Clear all annotations.
 *
 * @return {boolean} Action was executed
 */
ve.ui.AnnotationAction.prototype.clearAll = function () {
	var surfaceModel = this.surface.getModel(),
		fragment = surfaceModel.getFragment(),
		annotations = fragment.getAnnotations( true );
 
	ve.track( 'activity.allAnnotations', { action: 'clear-all' } );
 
	var arr = annotations.get();
	// TODO: Allow multiple annotations to be set or cleared by ve.dm.SurfaceFragment, probably
	// using an annotation set and ideally building a single transaction
	for ( var i = 0, len = arr.length; i < len; i++ ) {
		fragment.annotateContent( 'clear', arr[ i ].name, arr[ i ].data );
	}
	surfaceModel.setInsertionAnnotations( null );
	return true;
};
 
/**
 * Internal implementation of set(). Do not use this, use set() instead.
 *
 * @private
 * @param {string} name Annotation name, for example: 'textStyle/bold'
 * @param {Object} [data] Additional annotation data
 * @return {boolean} Action was executed
 */
ve.ui.AnnotationAction.prototype.setInternal = function ( name, data ) {
	var fragment = this.surface.getModel().getFragment(),
		annotationClass = ve.dm.annotationFactory.lookup( name );
 
	Eif ( fragment.getSelection() instanceof ve.dm.LinearSelection ) {
		var trimmedFragment = fragment.trimLinearSelection();
		Eif ( !trimmedFragment.getSelection().isCollapsed() ) {
			fragment = trimmedFragment;
		}
	}
 
	var removes = annotationClass.static.removes;
	for ( var i = removes.length - 1; i >= 0; i-- ) {
		fragment.annotateContent( 'clear', removes[ i ] );
	}
	fragment.annotateContent( 'set', name, data );
	return true;
};
 
/* Registration */
 
ve.ui.actionFactory.register( ve.ui.AnnotationAction );