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

70.96% Statements 22/31
41.66% Branches 5/12
75% Functions 3/4
70.96% Lines 22/31

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                                1x   1x     1x         1x       1x   1x   1x   1x             1x 24x           1x   1x     1x 1x 1x 1x           1x 1x 1x                                       1x               1x           1x  
/*!
 * VisualEditor SumCellsContextItem class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Show the sum and average of numeric values across a table selection.
 *
 * @class
 * @extends ve.ui.LinearContextItem
 *
 * @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.SumCellsContextItem = function VeUiSumCellsContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.SumCellsContextItem.super.call( this, context, model, config );
 
	// Initialization
	this.$element.addClass( 've-ui-sumCellsContextItem' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.SumCellsContextItem, ve.ui.LinearContextItem );
 
/* Static Properties */
 
ve.ui.SumCellsContextItem.static.name = 'sumCells';
 
ve.ui.SumCellsContextItem.static.icon = 'mathematics';
 
ve.ui.SumCellsContextItem.static.editable = false;
 
ve.ui.SumCellsContextItem.static.embeddable = false;
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.SumCellsContextItem.static.isCompatibleWith = function ( model ) {
	return model instanceof ve.dm.Node && model.isCellable();
};
 
/**
 * @inheritdoc
 */
ve.ui.SumCellsContextItem.prototype.setup = function () {
	// Parent method
	ve.ui.SumCellsContextItem.super.prototype.setup.apply( this, arguments );
 
	// If not disabled, selection must be table and spanning multiple matrix cells
	var count = 0,
		selection = this.getFragment().getSurface().getSelection(),
		documentModel = this.getFragment().getDocument(),
		documentView = this.context.getSurface().getView().getDocument();
 
	// There's some situations involving transclusion table cells which
	// can make us have a LinearSelection here, so make sure this will
	// work:
	var sum;
	Eif ( selection instanceof ve.dm.TableSelection ) {
		var cells = selection.getMatrixCells( documentModel, true );
		Iif ( cells.length > 1 ) {
			sum = cells.reduce( function ( s, cell ) {
				var number;
				if ( !cell.isPlaceholder() ) {
					// Get text from view rendering to catch numbers in alien nodes, etc.
					var viewCell = documentView.getBranchNodeFromOffset( cell.node.getRange().start );
					number = ve.init.platform.parseNumber(
						viewCell.$element.text()
					);
					if ( !isNaN( number ) ) {
						count++;
						return s + number;
					}
				}
				return s;
			}, 0 );
		}
	}
 
	// Only show if more than one numeric value was selected
	Iif ( count > 1 ) {
		this.setLabel(
			ve.msg( 'visualeditor-table-sum',
				ve.init.platform.formatNumber( sum ),
				ve.init.platform.formatNumber( sum / count )
			)
		);
	} else {
		this.$element.detach();
	}
};
 
/* Registration */
 
ve.ui.contextItemFactory.register( ve.ui.SumCellsContextItem );