Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contexts/ve.ui.TableLineContext.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Table Context class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context menu for editing tables.
9 *
10 * Three are usually generated for column, row and (on mobile) table-wide actions separately.
11 *
12 * @class
13 * @extends ve.ui.Context
14 *
15 * @constructor
16 * @param {ve.ce.TableNode} tableNode
17 * @param {string} itemGroup Tool group to use, 'col', 'row', or 'table'
18 * @param {Object} [config] Configuration options
19 */
20 ve.ui.TableLineContext = function VeUiTableLineContext( tableNode, itemGroup, config ) {
21 config = config || {};
22
23 // Parent constructor
24 ve.ui.TableLineContext.super.call( this, tableNode.surface.getSurface(), config );
25
26 // Properties
27 this.tableNode = tableNode;
28 this.itemGroup = itemGroup;
29 this.icon = new OO.ui.IconWidget( {
30 icon: this.constructor.static.icons[ itemGroup ]
31 } );
32 this.popup = new OO.ui.PopupWidget( {
33 classes: [ 've-ui-tableLineContext-menu' ],
34 $container: this.surface.$element,
35 $floatableContainer: this.icon.$element,
36 position: this.constructor.static.positions[ this.itemGroup ],
37 width: null
38 } );
39
40 // Events
41 this.icon.$element.on( 'mousedown', this.onIconMouseDown.bind( this ) );
42 this.onDocumentMouseDownHandler = this.onDocumentMouseDown.bind( this );
43
44 // Initialization
45 const labels = {
46 col: ve.msg( 'visualeditor-table-context-col' ),
47 row: ve.msg( 'visualeditor-table-context-row' ),
48 table: ve.msg( 'visualeditor-toolbar-table' )
49 };
50 this.icon.$element
51 .attr( 'role', 'button' )
52 .attr( 'aria-label', labels[ itemGroup ] );
53 this.popup.$body.append( this.$group );
54 // The following classes are used here:
55 // * ve-ui-tableLineContext-col
56 // * ve-ui-tableLineContext-row
57 // * ve-ui-tableLineContext-table
58 this.$element
59 .addClass( 've-ui-tableLineContext ve-ui-tableLineContext-' + itemGroup )
60 .append( this.icon.$element, this.popup.$element );
61 // Visibility is handled by the table overlay
62 this.toggle( true );
63 };
64
65 /* Inheritance */
66
67 OO.inheritClass( ve.ui.TableLineContext, ve.ui.Context );
68
69 /* Static Properties */
70
71 ve.ui.TableLineContext.static.groups = {
72 col: [ 'insertColumnBefore', 'insertColumnAfter', 'moveColumnBefore', 'moveColumnAfter', 'deleteColumn' ],
73 row: [ 'insertRowBefore', 'insertRowAfter', 'moveRowBefore', 'moveRowAfter', 'deleteRow' ],
74 table: [ 'tableProperties', 'toggleTableEditing', 'deleteTable' ]
75 };
76
77 ve.ui.TableLineContext.static.icons = {
78 col: 'expand',
79 row: 'next',
80 table: 'table'
81 };
82
83 ve.ui.TableLineContext.static.positions = {
84 col: 'below',
85 row: 'after',
86 table: 'after'
87 };
88
89 /* Methods */
90
91 /**
92 * @inheritdoc
93 */
94 ve.ui.TableLineContext.prototype.getRelatedSources = function () {
95 const items = this.constructor.static.groups[ this.itemGroup ];
96
97 if ( !this.relatedSources ) {
98 this.relatedSources = [];
99
100 for ( let i = 0, l = items.length; i < l; i++ ) {
101 this.relatedSources.push( {
102 type: 'item',
103 name: items[ i ]
104 } );
105 }
106 }
107 return this.relatedSources;
108 };
109
110 /**
111 * @inheritdoc
112 */
113 ve.ui.TableLineContext.prototype.onContextItemCommand = function () {
114 this.toggleMenu( false );
115 };
116
117 /**
118 * Handle mouse down events on the icon
119 *
120 * @param {jQuery.Event} e Mouse down event
121 */
122 ve.ui.TableLineContext.prototype.onIconMouseDown = function ( e ) {
123 e.preventDefault();
124 this.toggleMenu( undefined, true );
125 };
126
127 /**
128 * Handle document mouse down events
129 *
130 * @param {jQuery.Event} e Mouse down event
131 */
132 ve.ui.TableLineContext.prototype.onDocumentMouseDown = function ( e ) {
133 if ( !$( e.target ).closest( this.$element ).length ) {
134 this.toggleMenu( false, true );
135 }
136 };
137
138 /**
139 * Handle model select events
140 *
141 * @param {ve.dm.Selection} selection
142 */
143 ve.ui.TableLineContext.prototype.onModelSelect = function () {
144 this.toggleMenu();
145 };
146
147 /**
148 * @inheritdoc
149 */
150 ve.ui.TableLineContext.prototype.toggleMenu = function ( show, restoreEditing ) {
151 show = show === undefined ? !this.popup.isVisible() : !!show;
152
153 const surfaceModel = this.surface.getModel();
154 const surfaceView = this.surface.getView();
155
156 // Remember whether the table was in editing mode, because some itemGroups
157 // will force it into editing mode so their commands can work on a
158 // TableSelection.
159 this.wasEditing = !!this.tableNode.editingFragment;
160
161 // Kick the table into/out of editing mode if needed:
162 if ( this.itemGroup !== 'table' ) {
163 if ( show ) {
164 this.tableNode.setEditing( false );
165 } else if ( restoreEditing && surfaceModel.getSelection() instanceof ve.dm.TableSelection ) {
166 this.tableNode.setEditing( this.wasEditing );
167 }
168 }
169
170 // Set up the close-if-anything-happens handlers:
171 if ( show ) {
172 surfaceModel.connect( this, { select: 'onModelSelect' } );
173 surfaceView.$document.on( 'mousedown', this.onDocumentMouseDownHandler );
174 surfaceView.deactivate();
175 const dir = surfaceView.getSelectionDirectionality();
176 // eslint-disable-next-line mediawiki/class-doc
177 this.$element
178 .removeClass( 've-ui-dir-block-rtl ve-ui-dir-block-ltr' )
179 .addClass( 've-ui-dir-block-' + dir );
180 } else {
181 surfaceModel.disconnect( this );
182 surfaceView.$document.off( 'mousedown', this.onDocumentMouseDownHandler );
183 surfaceView.activate();
184 }
185
186 // Parent method - call after selection has been possibly modified above
187 ve.ui.TableLineContext.super.prototype.toggleMenu.call( this, show );
188
189 // Display the popup with correct positioning after the parent method fills in its contents
190 // (or hide it).
191 this.popup.toggle( show );
192 };