All files / src/ui/contexts ve.ui.TableLineContext.js

41.37% Statements 24/58
11.11% Branches 2/18
14.28% Functions 1/7
42.85% Lines 24/56

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                      1x 368x     368x     368x 368x 368x     368x                 368x 368x     368x         368x     368x         368x       368x         1x       1x           1x           1x                     1x                                     1x                 1x                   1x                     1x             1x                                                                                      
/*!
 * VisualEditor UserInterface Table Context class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Context menu for editing tables.
 *
 * Three are usually generated for column, row and (on mobile) table-wide actions separately.
 *
 * @class
 * @extends ve.ui.Context
 *
 * @constructor
 * @param {ve.ce.TableNode} tableNode
 * @param {string} itemGroup Tool group to use, 'col', 'row', or 'table'
 * @param {Object} [config] Configuration options
 */
ve.ui.TableLineContext = function VeUiTableLineContext( tableNode, itemGroup, config ) {
	config = config || {};
 
	// Parent constructor
	ve.ui.TableLineContext.super.call( this, tableNode.surface.getSurface(), config );
 
	// Properties
	this.tableNode = tableNode;
	this.itemGroup = itemGroup;
	this.icon = new OO.ui.IconWidget( {
		icon: this.constructor.static.icons[ itemGroup ]
	} );
	this.popup = new OO.ui.PopupWidget( {
		classes: [ 've-ui-tableLineContext-menu' ],
		$container: this.surface.$element,
		$floatableContainer: this.icon.$element,
		position: this.constructor.static.positions[ this.itemGroup ],
		width: null
	} );
 
	// Events
	this.icon.$element.on( 'mousedown', this.onIconMouseDown.bind( this ) );
	this.onDocumentMouseDownHandler = this.onDocumentMouseDown.bind( this );
 
	// Initialization
	const labels = {
		col: ve.msg( 'visualeditor-table-context-col' ),
		row: ve.msg( 'visualeditor-table-context-row' ),
		table: ve.msg( 'visualeditor-toolbar-table' )
	};
	this.icon.$element
		.attr( 'role', 'button' )
		.attr( 'aria-label', labels[ itemGroup ] );
	this.popup.$body.append( this.$group );
	// The following classes are used here:
	// * ve-ui-tableLineContext-col
	// * ve-ui-tableLineContext-row
	// * ve-ui-tableLineContext-table
	this.$element
		.addClass( 've-ui-tableLineContext ve-ui-tableLineContext-' + itemGroup )
		.append( this.icon.$element, this.popup.$element );
	// Visibility is handled by the table overlay
	this.toggle( true );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.TableLineContext, ve.ui.Context );
 
/* Static Properties */
 
ve.ui.TableLineContext.static.groups = {
	col: [ 'insertColumnBefore', 'insertColumnAfter', 'moveColumnBefore', 'moveColumnAfter', 'deleteColumn' ],
	row: [ 'insertRowBefore', 'insertRowAfter', 'moveRowBefore', 'moveRowAfter', 'deleteRow' ],
	table: [ 'tableProperties', 'toggleTableEditing', 'deleteTable' ]
};
 
ve.ui.TableLineContext.static.icons = {
	col: 'expand',
	row: 'next',
	table: 'table'
};
 
ve.ui.TableLineContext.static.positions = {
	col: 'below',
	row: 'after',
	table: 'after'
};
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.TableLineContext.prototype.getRelatedSources = function () {
	const items = this.constructor.static.groups[ this.itemGroup ];
 
	if ( !this.relatedSources ) {
		this.relatedSources = [];
 
		for ( let i = 0, l = items.length; i < l; i++ ) {
			this.relatedSources.push( {
				type: 'item',
				name: items[ i ]
			} );
		}
	}
	return this.relatedSources;
};
 
/**
 * @inheritdoc
 */
ve.ui.TableLineContext.prototype.onContextItemCommand = function () {
	this.toggleMenu( false );
};
 
/**
 * Handle mouse down events on the icon
 *
 * @param {jQuery.Event} e Mouse down event
 */
ve.ui.TableLineContext.prototype.onIconMouseDown = function ( e ) {
	e.preventDefault();
	this.toggleMenu( undefined, true );
};
 
/**
 * Handle document mouse down events
 *
 * @param {jQuery.Event} e Mouse down event
 */
ve.ui.TableLineContext.prototype.onDocumentMouseDown = function ( e ) {
	if ( !$( e.target ).closest( this.$element ).length ) {
		this.toggleMenu( false, true );
	}
};
 
/**
 * Handle model select events
 *
 * @param {ve.dm.Selection} selection
 */
ve.ui.TableLineContext.prototype.onModelSelect = function () {
	this.toggleMenu();
};
 
/**
 * @inheritdoc
 */
ve.ui.TableLineContext.prototype.toggleMenu = function ( show, restoreEditing ) {
	show = show === undefined ? !this.popup.isVisible() : !!show;
 
	const surfaceModel = this.surface.getModel();
	const surfaceView = this.surface.getView();
 
	// Remember whether the table was in editing mode, because some itemGroups
	// will force it into editing mode so their commands can work on a
	// TableSelection.
	this.wasEditing = !!this.tableNode.editingFragment;
 
	// Kick the table into/out of editing mode if needed:
	if ( this.itemGroup !== 'table' ) {
		if ( show ) {
			this.tableNode.setEditing( false );
		} else if ( restoreEditing && surfaceModel.getSelection() instanceof ve.dm.TableSelection ) {
			this.tableNode.setEditing( this.wasEditing );
		}
	}
 
	// Set up the close-if-anything-happens handlers:
	if ( show ) {
		surfaceModel.connect( this, { select: 'onModelSelect' } );
		surfaceView.$document.on( 'mousedown', this.onDocumentMouseDownHandler );
		surfaceView.deactivate();
		const dir = surfaceView.getSelectionDirectionality();
		// eslint-disable-next-line mediawiki/class-doc
		this.$element
			.removeClass( 've-ui-dir-block-rtl ve-ui-dir-block-ltr' )
			.addClass( 've-ui-dir-block-' + dir );
	} else {
		surfaceModel.disconnect( this );
		surfaceView.$document.off( 'mousedown', this.onDocumentMouseDownHandler );
		surfaceView.activate();
	}
 
	// Parent method - call after selection has been possibly modified above
	ve.ui.TableLineContext.super.prototype.toggleMenu.call( this, show );
 
	// Display the popup with correct positioning after the parent method fills in its contents
	// (or hide it).
	this.popup.toggle( show );
};