Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor TableLineContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for a table line (row or column) toolset.
9 *
10 * @class
11 * @abstract
12 * @extends ve.ui.ContextItem
13 *
14 * @param {ve.ui.TableLineContext} context Context the item is in
15 * @param {ve.dm.Model} model Model the item is related to
16 * @param {Function} tool Tool class the item is based on
17 * @param {Object} [config] Configuration options
18 */
19 ve.ui.TableLineContextItem = function VeUiTableLineContextItem() {
20 // Parent constructor
21 ve.ui.TableLineContextItem.super.apply( this, arguments );
22
23 this.actionButton = new OO.ui.ButtonWidget( {
24 framed: false,
25 classes: [ 've-ui-tableLineContextItem-actionButton' ]
26 } );
27
28 this.actionButton.connect( this, { click: 'onActionButtonClick' } );
29
30 // Initialization
31 this.$element
32 .addClass( 've-ui-tableLineContextItem' )
33 .append( this.actionButton.$element );
34 };
35
36 /* Inheritance */
37
38 OO.inheritClass( ve.ui.TableLineContextItem, ve.ui.ContextItem );
39
40 /* Static Properties */
41
42 ve.ui.TableLineContextItem.static.name = 'tableLine';
43
44 /**
45 * Title to use for context item action
46 *
47 * @static
48 * @property {string}
49 * @inheritable
50 */
51 ve.ui.TableLineContextItem.static.title = null;
52
53 /* Methods */
54
55 /**
56 * Handle action button click events.
57 *
58 * @localdoc Executes the command related to #static-commandName on the context's surface
59 *
60 * @protected
61 * @fires ve.ui.ContextItem#command
62 */
63 ve.ui.TableLineContextItem.prototype.onActionButtonClick = function () {
64 const command = this.getCommand();
65
66 if ( command ) {
67 command.execute( this.context.getSurface() );
68 this.emit( 'command' );
69 }
70 };
71
72 /**
73 * Get the title of the tool, used by the button label
74 *
75 * @return {jQuery|string|OO.ui.HtmlSnippet|Function} Tool title
76 */
77 ve.ui.TableLineContextItem.prototype.getTitle = function () {
78 return this.constructor.static.title;
79 };
80
81 /**
82 * @inheritdoc
83 */
84 ve.ui.TableLineContextItem.prototype.setup = function () {
85 // Parent method
86 ve.ui.TableLineContextItem.super.prototype.setup.call( this );
87
88 this.actionButton
89 .setIcon( this.constructor.static.icon )
90 .setLabel( this.getTitle() );
91 };
92
93 /* Specific tools */
94
95 ( function () {
96 const modes = [ 'row', 'col' ],
97 sides = [ 'before', 'after' ],
98 modeNames = { row: 'Row', col: 'Column' },
99 sideNames = { before: 'Before', after: 'After' };
100
101 modes.forEach( ( mode ) => {
102 const modeName = modeNames[ mode ];
103 let className;
104
105 sides.forEach( ( side ) => {
106 const sideName = sideNames[ side ];
107
108 className = 'Insert' + modeName + sideName + 'ContextItem';
109 // The following classes are used here:
110 // * ve.ui.InsertColumnBeforeContextItem
111 // * ve.ui.InsertColumnAfterContextItem
112 // * ve.ui.InsertRowBeforeContextItem
113 // * ve.ui.InsertRowAfterContextItem
114 ve.ui[ className ] = function VeUiInsertRowOrColumnContextItem() {
115 ve.ui.TableLineContextItem.apply( this, arguments );
116 };
117 OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
118 ve.ui[ className ].static.name = 'insert' + modeName + sideName;
119 ve.ui[ className ].static.group = 'table-' + mode;
120 ve.ui[ className ].static.icon = 'tableAdd' + modeName + sideName;
121 // Messages used here:
122 // * visualeditor-table-insert-col-before
123 // * visualeditor-table-insert-col-after
124 // * visualeditor-table-insert-row-before
125 // * visualeditor-table-insert-row-after
126 ve.ui[ className ].static.title =
127 OO.ui.deferMsg( 'visualeditor-table-insert-' + mode + '-' + side );
128 ve.ui[ className ].static.commandName = 'insert' + modeName + sideName;
129 ve.ui.contextItemFactory.register( ve.ui[ className ] );
130
131 className = 'Move' + modeName + sideName + 'ContextItem';
132 // The following classes are used here:
133 // * ve.ui.MoveColumnBeforeContextItem
134 // * ve.ui.MoveColumnAfterContextItem
135 // * ve.ui.MoveRowBeforeContextItem
136 // * ve.ui.MoveRowAfterContextItem
137 ve.ui[ className ] = function VeUiMoveRowOrColumnContextItem() {
138 ve.ui.TableLineContextItem.apply( this, arguments );
139 };
140 OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
141 ve.ui[ className ].static.name = 'move' + modeName + sideName;
142 ve.ui[ className ].static.group = 'table-' + mode;
143 ve.ui[ className ].static.icon = 'tableMove' + modeName + sideName;
144 // Messages used here:
145 // * visualeditor-table-move-col-before
146 // * visualeditor-table-move-col-after
147 // * visualeditor-table-move-row-before
148 // * visualeditor-table-move-row-after
149 ve.ui[ className ].static.title =
150 OO.ui.deferMsg( 'visualeditor-table-move-' + mode + '-' + side );
151 ve.ui[ className ].static.commandName = 'move' + modeName + sideName;
152 ve.ui[ className ].prototype.setup = function () {
153 // Parent method
154 ve.ui.TableLineContextItem.prototype.setup.call( this );
155
156 const selection = this.context.getSurface().getModel().getSelection();
157 const documentModel = this.context.getSurface().getModel().getDocument();
158
159 if ( !( selection instanceof ve.dm.TableSelection ) ) {
160 this.actionButton.setDisabled( true );
161 return;
162 }
163
164 if ( side === 'before' ) {
165 this.actionButton.setDisabled(
166 ( mode === 'row' && selection.startRow === 0 ) ||
167 ( mode === 'col' && selection.startCol === 0 )
168 );
169 } else {
170 const matrix = selection.getTableNode( documentModel ).getMatrix();
171 this.actionButton.setDisabled(
172 ( mode === 'row' && selection.endRow === matrix.getRowCount() - 1 ) ||
173 ( mode === 'col' && selection.endCol === matrix.getMaxColCount() - 1 )
174 );
175 }
176 };
177 ve.ui.contextItemFactory.register( ve.ui[ className ] );
178 } );
179
180 className = 'Delete' + modeName + 'ContextItem';
181 // The following classes are used here:
182 // * ve.ui.DeleteColumnContextItem
183 // * ve.ui.DeleteRowContextItem
184 ve.ui[ className ] = function VeUiDeleteRowOrColumnContextItem() {
185 ve.ui.TableLineContextItem.apply( this, arguments );
186
187 this.actionButton.setFlags( { destructive: true } );
188 };
189 OO.inheritClass( ve.ui[ className ], ve.ui.TableLineContextItem );
190 ve.ui[ className ].static.name = 'delete' + modeName;
191 ve.ui[ className ].static.group = 'table-' + mode;
192 ve.ui[ className ].static.icon = 'trash';
193 ve.ui[ className ].static.commandName = 'delete' + modeName;
194 ve.ui[ className ].prototype.getTitle = function () {
195 const selection = this.context.getSurface().getModel().getSelection();
196
197 let count;
198 if ( !( selection instanceof ve.dm.TableSelection ) ) {
199 count = 0;
200 } else if ( mode === 'row' ) {
201 count = selection.getRowCount();
202 } else {
203 count = selection.getColCount();
204 }
205
206 // The following messages are used here:
207 // * visualeditor-table-delete-col
208 // * visualeditor-table-delete-row
209 return ve.msg( 'visualeditor-table-delete-' + mode, count );
210 };
211 ve.ui.contextItemFactory.register( ve.ui[ className ] );
212 } );
213
214 ve.ui.TablePropertiesContextItem = function VeUiTablePropertiesContextItem() {
215 ve.ui.TableLineContextItem.apply( this, arguments );
216
217 this.actionButton.setFlags( { progressive: true } );
218 };
219 OO.inheritClass( ve.ui.TablePropertiesContextItem, ve.ui.TableLineContextItem );
220 ve.ui.TablePropertiesContextItem.static.name = 'tableProperties';
221 ve.ui.TablePropertiesContextItem.static.group = 'table';
222 ve.ui.TablePropertiesContextItem.static.icon = 'edit';
223 ve.ui.TablePropertiesContextItem.static.commandName = 'table';
224 ve.ui.TablePropertiesContextItem.static.title = OO.ui.deferMsg( 'visualeditor-table-contextitem-properties' );
225 ve.ui.TablePropertiesContextItem.prototype.onActionButtonClick = function () {
226 // Tweak the selection here:
227 const command = this.context.getSurface().commandRegistry.lookup( 'exitTableCell' );
228 if ( command ) {
229 command.execute( this.context.getSurface() );
230 }
231 return ve.ui.TablePropertiesContextItem.super.prototype.onActionButtonClick.apply( this, arguments );
232 };
233 ve.ui.contextItemFactory.register( ve.ui.TablePropertiesContextItem );
234
235 ve.ui.DeleteTableContextItem = function VeUiDeleteTableContextItem() {
236 ve.ui.TableLineContextItem.apply( this, arguments );
237
238 this.actionButton.setFlags( { destructive: true } );
239 };
240 OO.inheritClass( ve.ui.DeleteTableContextItem, ve.ui.TableLineContextItem );
241 ve.ui.DeleteTableContextItem.static.name = 'deleteTable';
242 ve.ui.DeleteTableContextItem.static.group = 'table';
243 ve.ui.DeleteTableContextItem.static.icon = 'trash';
244 ve.ui.DeleteTableContextItem.static.commandName = 'deleteTable';
245 ve.ui.DeleteTableContextItem.static.title = OO.ui.deferMsg( 'visualeditor-contextitemwidget-label-remove' );
246 ve.ui.contextItemFactory.register( ve.ui.DeleteTableContextItem );
247
248 ve.ui.ToggleTableSelectionContextItem = function VeUiToggleTableSelectionContextItem() {
249 ve.ui.TableLineContextItem.apply( this, arguments );
250
251 this.actionButton.setFlags( { progressive: true } );
252 };
253 OO.inheritClass( ve.ui.ToggleTableSelectionContextItem, ve.ui.TableLineContextItem );
254 ve.ui.ToggleTableSelectionContextItem.static.name = 'toggleTableEditing';
255 ve.ui.ToggleTableSelectionContextItem.static.group = 'table';
256 ve.ui.ToggleTableSelectionContextItem.static.icon = 'table';
257 ve.ui.ToggleTableSelectionContextItem.prototype.getCommand = function () {
258 const commandName = this.context.wasEditing ? 'exitTableCell' : 'enterTableCell';
259 return this.context.getSurface().commandRegistry.lookup( commandName );
260 };
261 ve.ui.ToggleTableSelectionContextItem.prototype.getTitle = function () {
262 const selection = this.context.getSurface().getModel().getSelection();
263
264 let mode = 'cells';
265 if ( selection instanceof ve.dm.TableSelection ) {
266 mode = 'contents';
267 }
268 // The following messages are used here:
269 // * visualeditor-table-contextitem-selectionmode-cells
270 // * visualeditor-table-contextitem-selectionmode-contents
271 return ve.msg( 'visualeditor-table-contextitem-selectionmode-' + mode );
272 };
273 ve.ui.contextItemFactory.register( ve.ui.ToggleTableSelectionContextItem );
274
275 }() );