all files / src/ui/contextitems/ ve.ui.MergeCellsContextItem.js

94.44% Statements 17/18
45.45% Branches 5/11
100% Functions 3/3
94.44% Lines 17/18
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                                                                          23×                                                      
/*!
 * VisualEditor MergeCellsContextItem class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Context item for mergeable cels.
 *
 * @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.MergeCellsContextItem = function VeUiMergeCellsContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.MergeCellsContextItem.super.call( this, context, model, config );
 
	// Initialization
	this.$element.addClass( 've-ui-mergeCellsContextItem' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.MergeCellsContextItem, ve.ui.LinearContextItem );
 
/* Static Properties */
 
ve.ui.MergeCellsContextItem.static.name = 'mergeCells';
 
ve.ui.MergeCellsContextItem.static.icon = 'tableMergeCells';
 
ve.ui.MergeCellsContextItem.static.label = OO.ui.deferMsg( 'visualeditor-table-merge-cells' );
 
ve.ui.MergeCellsContextItem.static.commandName = 'mergeCells';
 
ve.ui.MergeCellsContextItem.static.deletable = false;
 
ve.ui.MergeCellsContextItem.static.embeddable = false;
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.MergeCellsContextItem.static.isCompatibleWith = function ( model ) {
	return model instanceof ve.dm.Node && model.isCellable();
};
 
/**
 * @inheritdoc
 */
ve.ui.MergeCellsContextItem.prototype.setup = function () {
	// If not disabled, selection must be table and spanning multiple matrix cells
	var selection = this.getFragment().getSurface().getSelection(),
		documentModel = this.getFragment().getDocument(),
		// There's some situations involving transclusion table cells which
		// can make us have a LinearSelection here, so make sure this will
		// work:
		isMergeable = ( selection instanceof ve.dm.TableSelection ) &&
			selection.isMergeable( documentModel ) &&
			!this.isReadOnly();
 
	Eif ( !isMergeable ) {
		// Ideally we would check this in isCompatibleWith, but only the model node is available there
		this.$element.detach();
	} else {
		this.editButton.setLabel(
			isMergeable && selection.isSingleCell( documentModel ) ?
				ve.msg( 'visualeditor-table-merge-cells-unmerge' ) :
				ve.msg( 'visualeditor-table-merge-cells-merge' )
		);
	}
};
 
/* Registration */
 
ve.ui.contextItemFactory.register( ve.ui.MergeCellsContextItem );