Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contextitems/ve.ui.MergeCellsContextItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor MergeCellsContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for mergeable cels.
9 *
10 * @class
11 * @extends ve.ui.LinearContextItem
12 *
13 * @param {ve.ui.LinearContext} context Context the item is in
14 * @param {ve.dm.Model} model Model the item is related to
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MergeCellsContextItem = function VeUiMergeCellsContextItem( context, model, config ) {
18 // Parent constructor
19 ve.ui.MergeCellsContextItem.super.call( this, context, model, config );
20
21 this.dimensions = new OO.ui.LabelWidget( {
22 classes: [ 've-ui-mergeCellsContextItem-dimensions' ]
23 } );
24 if ( this.context.isMobile() ) {
25 // Use desktop-style label-only button, as otherwise the "edit" button
26 // gets collapsed to just the edit icon.
27 this.editButton
28 .setIcon( null )
29 .setInvisibleLabel( false );
30 }
31
32 // Initialization
33 this.$element.addClass( 've-ui-mergeCellsContextItem' );
34 this.$title.append( this.dimensions.$element );
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( ve.ui.MergeCellsContextItem, ve.ui.LinearContextItem );
40
41 /* Static Properties */
42
43 ve.ui.MergeCellsContextItem.static.name = 'mergeCells';
44
45 ve.ui.MergeCellsContextItem.static.icon = 'tableMergeCells';
46
47 ve.ui.MergeCellsContextItem.static.label = OO.ui.deferMsg( 'visualeditor-table-merge-cells' );
48
49 ve.ui.MergeCellsContextItem.static.commandName = 'mergeCells';
50
51 ve.ui.MergeCellsContextItem.static.embeddable = false;
52
53 /* Methods */
54
55 /**
56 * @inheritdoc
57 */
58 ve.ui.MergeCellsContextItem.static.isCompatibleWith = function ( model ) {
59 return model instanceof ve.dm.Node && model.isCellable();
60 };
61
62 /**
63 * @inheritdoc
64 */
65 ve.ui.MergeCellsContextItem.prototype.setup = function () {
66 // Parent method
67 ve.ui.MergeCellsContextItem.super.prototype.setup.apply( this, arguments );
68
69 // If not disabled, selection must be table and spanning multiple matrix cells
70 const selection = this.getFragment().getSurface().getSelection(),
71 documentModel = this.getFragment().getDocument(),
72 // There's some situations involving transclusion table cells which
73 // can make us have a LinearSelection here, so make sure this will
74 // work:
75 isMergeable = ( selection instanceof ve.dm.TableSelection ) &&
76 selection.isMergeable( documentModel ) &&
77 !this.isReadOnly();
78
79 if ( !isMergeable ) {
80 // Ideally we would check this in isCompatibleWith, but only the model node is available there
81 this.$element.detach();
82 } else {
83 this.dimensions.setLabel(
84 ve.msg( 'visualeditor-table-selection-dimensions',
85 selection.getColCount(),
86 selection.getRowCount()
87 )
88 );
89 this.editButton.setLabel(
90 selection.isSingleCell( documentModel ) ?
91 ve.msg( 'visualeditor-table-merge-cells-unmerge' ) :
92 ve.msg( 'visualeditor-table-merge-cells-merge' )
93 );
94 }
95 };
96
97 /* Registration */
98
99 ve.ui.contextItemFactory.register( ve.ui.MergeCellsContextItem );