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

57.39% Statements 66/115
0% Branches 0/24
16.67% Functions 3/18
57.39% Lines 66/115
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278                                                                                                                                                                                                                                                                                                                                                                                                                                       
/*!
 * VisualEditor TableLineContextItem class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Context item for a table line (row or column) toolset.
 *
 * @class
 * @abstract
 * @extends ve.ui.ContextItem
 *
 * @param {ve.ui.Context} context Context item is in
 * @param {ve.dm.Model} model Model the item is related to
 * @param {Function} tool Tool class the item is based on
 * @param {Object} config Configuration options
 */
ve.ui.TableLineContextItem = function VeUiTableLineContextItem() {
	// Parent constructor
	ve.ui.TableLineContextItem.super.apply( this, arguments );
 
	this.actionButton = new OO.ui.ButtonWidget( {
		framed: false,
		classes: [ 've-ui-tableLineContextItem-actionButton' ]
	} );
 
	this.actionButton.connect( this, { click: 'onActionButtonClick' } );
 
	// Initialization
	this.$element
		.addClass( 've-ui-tableLineContextItem' )
		.append( this.actionButton.$element );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.TableLineContextItem, ve.ui.ContextItem );
 
/* Static Properties */
 
ve.ui.TableLineContextItem.static.name = 'tableLine';
 
/**
 * Title to use for context item action
 *
 * @static
 * @property {string}
 * @inheritable
 */
ve.ui.TableLineContextItem.static.title = null;
 
/* Methods */
 
/**
 * Handle action button click events.
 *
 * @localdoc Executes the command related to #static-commandName on the context's surface
 *
 * @protected
 */
ve.ui.TableLineContextItem.prototype.onActionButtonClick = function () {
	var command = this.getCommand();
 
	if ( command ) {
		command.execute( this.context.getSurface() );
		this.emit( 'command' );
	}
};
 
/**
 * Get the title of the tool, used by the button label
 *
 * @return {jQuery|string|OO.ui.HtmlSnippet|Function} Tool title
 */
ve.ui.TableLineContextItem.prototype.getTitle = function () {
	return this.constructor.static.title;
};
 
/**
 * @inheritdoc
 */
ve.ui.TableLineContextItem.prototype.setup = function () {
	// Parent method
	ve.ui.TableLineContextItem.super.prototype.setup.call( this );
 
	this.actionButton
		.setIcon( this.constructor.static.icon )
		.setLabel( this.getTitle() );
};
 
/* Specific tools */
 
( function () {
 
	var className,
		modes = [ 'row', 'col' ],
		sides = [ 'before', 'after' ],
		modeNames = { row: 'Row', col: 'Column' },
		sideNames = { before: 'Before', after: 'After' };
 
	modes.forEach( function ( mode ) {
		var modeName = modeNames[ mode ];
 
		sides.forEach( function ( side ) {
			var sideName = sideNames[ side ];
 
			// Classes created here:
			// * ve.ui.InsertColumnBeforeContextItem
			// * ve.ui.InsertColumnAfterContextItem
			// * ve.ui.InsertRowBeforeContextItem
			// * ve.ui.InsertRowAfterContextItem
			className = 'Insert' + modeName + sideName + 'ContextItem';
			ve.ui[ className ] = function VeUiInsertRowOrColumnContextItem() {
				ve.ui.TableLineContextItem.apply( this, arguments );
			};
			OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
			ve.ui[ className ].static.name = 'insert' + modeName + sideName;
			ve.ui[ className ].static.group = 'table-' + mode;
			ve.ui[ className ].static.icon = 'tableAdd' + modeName + sideName;
			// Messages used here:
			// * visualeditor-table-insert-col-before
			// * visualeditor-table-insert-col-after
			// * visualeditor-table-insert-row-before
			// * visualeditor-table-insert-row-after
			ve.ui[ className ].static.title =
				OO.ui.deferMsg( 'visualeditor-table-insert-' + mode + '-' + side );
			ve.ui[ className ].static.commandName = 'insert' + modeName + sideName;
			ve.ui.contextItemFactory.register( ve.ui[ className ] );
 
			// Classes created here:
			// * ve.ui.MoveColumnBeforeContextItem
			// * ve.ui.MoveColumnAfterContextItem
			// * ve.ui.MoveRowBeforeContextItem
			// * ve.ui.MoveRowAfterContextItem
			className = 'Move' + modeName + sideName + 'ContextItem';
			ve.ui[ className ] = function VeUiMoveRowOrColumnContextItem() {
				ve.ui.TableLineContextItem.apply( this, arguments );
			};
			OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
			ve.ui[ className ].static.name = 'move' + modeName + sideName;
			ve.ui[ className ].static.group = 'table-' + mode;
			ve.ui[ className ].static.icon = 'tableMove' + modeName + sideName;
			// Messages used here:
			// * visualeditor-table-move-col-before
			// * visualeditor-table-move-col-after
			// * visualeditor-table-move-row-before
			// * visualeditor-table-move-row-after
			ve.ui[ className ].static.title =
				OO.ui.deferMsg( 'visualeditor-table-move-' + mode + '-' + side );
			ve.ui[ className ].static.commandName = 'move' + modeName + sideName;
			ve.ui[ className ].prototype.setup = function () {
				var selection, documentModel, matrix;
 
				// Parent method
				ve.ui.TableLineContextItem.prototype.setup.call( this );
 
				selection = this.context.getSurface().getModel().getSelection();
				documentModel = this.context.getSurface().getModel().getDocument();
 
				if ( !( selection instanceof ve.dm.TableSelection ) ) {
					this.actionButton.setDisabled( true );
					return;
				}
 
				if ( side === 'before' ) {
					this.actionButton.setDisabled(
						( mode === 'row' && selection.startRow === 0 ) ||
						( mode === 'col' && selection.startCol === 0 )
					);
				} else {
					matrix = selection.getTableNode( documentModel ).getMatrix();
					this.actionButton.setDisabled(
						( mode === 'row' && selection.endRow === matrix.getRowCount() - 1 ) ||
						( mode === 'col' && selection.endCol === matrix.getMaxColCount() - 1 )
					);
				}
			};
			ve.ui.contextItemFactory.register( ve.ui[ className ] );
		} );
 
		// Classes created here:
		// * ve.ui.DeleteColumnContextItem
		// * ve.ui.DeleteRowContextItem
		className = 'Delete' + modeName + 'ContextItem';
		ve.ui[ className ] = function VeUiDeleteRowOrColumnContextItem() {
			ve.ui.TableLineContextItem.apply( this, arguments );
 
			this.actionButton.setFlags( { destructive: true } );
		};
		OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
		ve.ui[ className ].static.name = 'delete' + modeName;
		ve.ui[ className ].static.group = 'table-' + mode;
		ve.ui[ className ].static.icon = 'trash';
		ve.ui[ className ].static.commandName = 'delete' + modeName;
		ve.ui[ className ].prototype.getTitle = function () {
			var count,
				selection = this.context.getSurface().getModel().getSelection();
 
			if ( !( selection instanceof ve.dm.TableSelection ) ) {
				count = 0;
			} else if ( mode === 'row' ) {
				count = selection.getRowCount();
			} else {
				count = selection.getColCount();
			}
 
			// The following messages are used here:
			// * visualeditor-table-delete-col
			// * visualeditor-table-delete-row
			return ve.msg( 'visualeditor-table-delete-' + mode, count );
		};
		ve.ui.contextItemFactory.register( ve.ui[ className ] );
 
	} );
 
	ve.ui.TablePropertiesContextItem = function VeUiTablePropertiesContextItem() {
		ve.ui.TableLineContextItem.apply( this, arguments );
 
		this.actionButton.setFlags( { progressive: true } );
	};
	OO.inheritClass( ve.ui.TablePropertiesContextItem, ve.ui.TableLineContextItem );
	ve.ui.TablePropertiesContextItem.static.name = 'tableProperties';
	ve.ui.TablePropertiesContextItem.static.group = 'table';
	ve.ui.TablePropertiesContextItem.static.icon = 'edit';
	ve.ui.TablePropertiesContextItem.static.commandName = 'table';
	ve.ui.TablePropertiesContextItem.static.title = OO.ui.deferMsg( 'visualeditor-table-contextitem-properties' );
	ve.ui.TablePropertiesContextItem.prototype.onActionButtonClick = function () {
		// Tweak the selection here:
		var command = this.context.getSurface().commandRegistry.lookup( 'exitTableCell' );
		if ( command ) {
			command.execute( this.context.getSurface() );
		}
		return ve.ui.TablePropertiesContextItem.super.prototype.onActionButtonClick.apply( this, arguments );
	};
	ve.ui.contextItemFactory.register( ve.ui.TablePropertiesContextItem );
 
	ve.ui.DeleteTableContextItem = function VeUiDeleteTableContextItem() {
		ve.ui.TableLineContextItem.apply( this, arguments );
 
		this.actionButton.setFlags( { destructive: true } );
	};
	OO.inheritClass( ve.ui.DeleteTableContextItem, ve.ui.TableLineContextItem );
	ve.ui.DeleteTableContextItem.static.name = 'deleteTable';
	ve.ui.DeleteTableContextItem.static.group = 'table';
	ve.ui.DeleteTableContextItem.static.icon = 'trash';
	ve.ui.DeleteTableContextItem.static.commandName = 'deleteTable';
	ve.ui.DeleteTableContextItem.static.title = OO.ui.deferMsg( 'visualeditor-contextitemwidget-label-remove' );
	ve.ui.contextItemFactory.register( ve.ui.DeleteTableContextItem );
 
	ve.ui.ToggleTableSelectionContextItem = function VeUiToggleTableSelectionContextItem() {
		ve.ui.TableLineContextItem.apply( this, arguments );
 
		this.actionButton.setFlags( { progressive: true } );
	};
	OO.inheritClass( ve.ui.ToggleTableSelectionContextItem, ve.ui.TableLineContextItem );
	ve.ui.ToggleTableSelectionContextItem.static.name = 'toggleTableEditing';
	ve.ui.ToggleTableSelectionContextItem.static.group = 'table';
	ve.ui.ToggleTableSelectionContextItem.static.icon = 'table';
	ve.ui.ToggleTableSelectionContextItem.prototype.getCommand = function () {
		var commandName = this.context.wasEditing ? 'exitTableCell' : 'enterTableCell';
		return this.context.getSurface().commandRegistry.lookup( commandName );
	};
	ve.ui.ToggleTableSelectionContextItem.prototype.getTitle = function () {
		var mode = 'cells',
			selection = this.context.getSurface().getModel().getSelection();
		if ( selection instanceof ve.dm.TableSelection ) {
			mode = 'contents';
		}
		// The following messages are used here:
		// * visualeditor-table-contextitem-selectionmode-cells
		// * visualeditor-table-contextitem-selectionmode-contents
		return ve.msg( 'visualeditor-table-contextitem-selectionmode-' + mode );
	};
	ve.ui.contextItemFactory.register( ve.ui.ToggleTableSelectionContextItem );
 
}() );