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

70.97% Statements 22/31
41.67% Branches 5/12
75% Functions 3/4
70.97% 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                                1x   1x     1x         1x       1x   1x   1x   1x   1x             1x 23x           1x     1x 1x 1x 1x         1x 1x 1x                                       1x               1x           1x  
/*!
 * VisualEditor SumCellsContextItem class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Show the sum and average of numeric values across a table selection.
 *
 * @class
 * @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.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.deletable = 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 () {
	// If not disabled, selection must be table and spanning multiple matrix cells
	var sum, cells, viewCell,
		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:
	Eif ( selection instanceof ve.dm.TableSelection ) {
		cells = selection.getMatrixCells( documentModel, true );
		Iif ( cells.length > 1 ) {
			sum = cells.reduce( function ( sum, cell ) {
				var number;
				if ( !cell.isPlaceholder() ) {
					// Get text from view rendering to catch numbers in alien nodes, etc.
					viewCell = documentView.getBranchNodeFromOffset( cell.node.getRange().start );
					number = ve.init.platform.parseNumber(
						viewCell.$element.text()
					);
					if ( !isNaN( number ) ) {
						count++;
						return sum + number;
					}
				}
				return sum;
			}, 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 );