All files / src/ce/selections ve.ce.TableSelection.js

96.36% Statements 53/55
85.71% Branches 12/14
90.9% Functions 10/11
96.36% Lines 53/55

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 154 155 156 157 158 159 160 161 162 163 164                          1x   200x 200x         1x       1x             1x 4x 4x           1x 16x 16x 16x 16x   16x 16x 16x 16x   16x 16x 16x       16x 16x     16x   16x 16x 16x 16x                                   16x                 16x 6x   10x           1x   6x 6x               1x 6x 6x   6x 6x   6x 1x   5x               1x 22x 22x           1x             1x 399x           1x 1x 1x   1x         1x  
/*!
 * VisualEditor Table Selection class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * @class
 * @extends ve.ce.Selection
 * @constructor
 * @param {ve.ce.Surface} surface
 * @param {ve.dm.Selection} model
 */
ve.ce.TableSelection = function VeCeTableSelection() {
	// Parent constructor
	ve.ce.TableSelection.super.apply( this, arguments );
	this.directionality = null;
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.TableSelection, ve.ce.Selection );
 
/* Static Properties */
 
ve.ce.TableSelection.static.name = 'table';
 
/* Method */
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.getSelectionRects = function () {
	const boundingRect = this.getSelectionBoundingRect();
	return boundingRect ? [ boundingRect ] : [];
};
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.getSelectionBoundingRect = function () {
	const surface = this.getSurface(),
		tableNode = this.getTableNode(),
		nodes = tableNode.getCellNodesFromSelection( this.getModel() ),
		surfaceRect = surface.getSurface().getBoundingClientRect();
 
	let top = Infinity;
	let bottom = -Infinity;
	let left = Infinity;
	let right = -Infinity;
 
	const cellElements = [];
	nodes.forEach( ( node ) => {
		cellElements.push( ...node.$element.toArray() );
	} );
 
	// Compute a bounding box for the given cell elements
	cellElements.forEach( ( cellElement ) => {
		Iif ( !cellElement ) {
			return null;
		}
		const cellOffset = cellElement.getBoundingClientRect();
 
		top = Math.min( top, cellOffset.top );
		bottom = Math.max( bottom, cellOffset.bottom );
		left = Math.min( left, cellOffset.left );
		right = Math.max( right, cellOffset.right );
	} );
 
	// Browser tweaks to adjust for border-collapse:collapse
	/* istanbul ignore next */
	if ( !ve.test ) {
		switch ( $.client.profile().layout ) {
			case 'webkit':
				right += 1;
				bottom += 1;
				break;
			case 'gecko':
				left -= 1;
				top -= 1;
				break;
		}
	}
 
	const boundingRect = {
		top,
		bottom,
		left,
		right,
		width: right - left,
		height: bottom - top
	};
 
	if ( !boundingRect || !surfaceRect ) {
		return null;
	}
	return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
};
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.getSelectionFocusRect = function () {
	// Table selections render the focus at 'from', instead of 'to'.
	const fromSelection = new this.constructor( this.getModel().collapseToFrom(), this.surface );
	return fromSelection.getSelectionBoundingRect();
};
 
/**
 * Get the bounding rectangle of the parent table
 *
 * @return {Object|null} Selection rectangle, with keys top, bottom, left, right, width, height
 */
ve.ce.TableSelection.prototype.getTableBoundingRect = function () {
	const surface = this.getSurface(),
		tableNode = this.getTableNode();
 
	const surfaceRect = surface.getSurface().getBoundingClientRect();
	const boundingRect = tableNode.$element[ 0 ].getBoundingClientRect();
 
	if ( !boundingRect || !surfaceRect ) {
		return null;
	}
	return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
};
 
/**
 * Get the selection's table node
 *
 * @return {ve.ce.TableNode} Table node
 */
ve.ce.TableSelection.prototype.getTableNode = function () {
	const doc = this.getSurface().getDocument();
	return doc.getBranchNodeFromOffset( this.getModel().tableRange.start + 1 );
};
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.isFocusedNode = function () {
	return true;
};
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.isNativeCursor = function () {
	return false;
};
 
/**
 * @inheritdoc
 */
ve.ce.TableSelection.prototype.getDirectionality = function ( doc ) {
	Eif ( !this.directionality ) {
		this.directionality = doc.getDirectionalityFromRange( this.getModel().tableRange );
	}
	return this.directionality;
};
 
/* Registration */
 
ve.ce.selectionFactory.register( ve.ce.TableSelection );