Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.CompletionWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface CompletionWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Widget that displays autocompletion suggestions
9 *
10 * @class
11 * @extends OO.ui.Widget
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to complete into
15 * @param {Object} [config] Configuration options
16 * @param {Object} [config.validate] Validation pattern passed to TextInputWidgets
17 * @param {boolean} [config.readOnly=false] Prevent changes to the value of the widget.
18 */
19 ve.ui.CompletionWidget = function VeUiCompletionWidget( surface, config ) {
20 this.surface = surface;
21 this.surfaceModel = surface.getModel();
22
23 // Configuration
24 config = config || {
25 anchor: false
26 };
27
28 // Parent constructor
29 ve.ui.CompletionWidget.super.call( this, config );
30
31 this.$tabIndexed = this.$element;
32
33 const $doc = surface.getView().getDocument().getDocumentNode().$element;
34 this.popup = new OO.ui.PopupWidget( {
35 anchor: false,
36 align: 'forwards',
37 hideWhenOutOfView: false,
38 autoFlip: false,
39 width: null,
40 $container: config.$popupContainer || this.surface.$element,
41 containerPadding: config.popupPadding
42 } );
43 this.input = new OO.ui.TextInputWidget();
44 this.menu = new OO.ui.MenuSelectWidget( {
45 widget: this,
46 $input: $doc.add( this.input.$input )
47 } );
48 // This may be better semantically as a MenuSectionOptionWidget,
49 // but that causes all subsequent options to be indented.
50 this.header = new OO.ui.MenuOptionWidget( {
51 classes: [ 've-ui-completionWidget-header' ],
52 disabled: true
53 } );
54 this.noResults = new OO.ui.MenuOptionWidget( {
55 label: ve.msg( 'visualeditor-completionwidget-noresults' ),
56 classes: [ 've-ui-completionWidget-noresults' ],
57 disabled: true
58 } );
59
60 // Events
61 this.menu.connect( this, {
62 choose: 'onMenuChoose',
63 toggle: 'onMenuToggle'
64 } );
65 this.input.connect( this, { change: 'update' } );
66
67 this.popup.$element.prepend( this.input.$element );
68 this.popup.$body.append(
69 this.menu.$element
70 );
71
72 // Setup
73 this.$element.addClass( 've-ui-completionWidget' )
74 .append(
75 this.popup.$element
76 );
77 };
78
79 /* Inheritance */
80
81 OO.inheritClass( ve.ui.CompletionWidget, OO.ui.Widget );
82
83 /**
84 * Setup the completion widget
85 *
86 * @param {ve.ui.Action} action Action which opened the widget
87 * @param {boolean} [isolateInput] Isolate input from the surface
88 */
89 ve.ui.CompletionWidget.prototype.setup = function ( action, isolateInput ) {
90 const range = this.surfaceModel.getSelection().getCoveringRange();
91 this.action = action;
92 this.isolateInput = !!isolateInput;
93 this.sequenceLength = this.action.getSequenceLength();
94 this.initialOffset = range.end - this.sequenceLength;
95
96 this.input.toggle( this.isolateInput );
97 if ( this.isolateInput ) {
98 this.wasActive = !this.surface.getView().isDeactivated();
99 this.surface.getView().deactivate();
100 this.input.setValue( '' );
101 setTimeout( () => {
102 this.input.focus();
103 }, 1 );
104 } else {
105 this.wasActive = false;
106 }
107
108 this.update();
109
110 this.surfaceModel.connect( this, { select: 'onModelSelect' } );
111 };
112
113 /**
114 * Teardown the completion widget
115 */
116 ve.ui.CompletionWidget.prototype.teardown = function () {
117 this.tearingDown = true;
118 this.popup.toggle( false );
119 this.surfaceModel.disconnect( this );
120 if ( this.wasActive ) {
121 this.surface.getView().activate();
122 }
123 this.action = undefined;
124 this.tearingDown = false;
125 };
126
127 /**
128 * Update the completion widget after the input has changed
129 */
130 ve.ui.CompletionWidget.prototype.update = function () {
131 const direction = this.surface.getDir(),
132 range = this.getCompletionRange(),
133 boundingRect = this.surface.getView().getSelection( new ve.dm.LinearSelection( range ) ).getSelectionBoundingRect(),
134 style = {
135 top: boundingRect.bottom
136 };
137
138 let input;
139 if ( this.isolateInput ) {
140 input = this.input.getValue();
141 } else {
142 const data = this.surfaceModel.getDocument().data;
143 input = data.getText( false, range );
144 }
145
146 if ( direction === 'rtl' ) {
147 // This works because this.$element is a 0x0px box, with the menu positioned relative to it.
148 // If this style was applied to the menu, we'd need to do some math here to align the right
149 // edge of the menu with the right edge of the selection.
150 style.left = boundingRect.right;
151 } else {
152 style.left = boundingRect.left;
153 }
154 this.$element.css( style );
155
156 this.updateMenu( input );
157 this.action.getSuggestions( input ).then( ( suggestions ) => {
158 if ( !this.action ) {
159 // Check widget hasn't been torn down
160 return;
161 }
162 this.menu.clearItems();
163 let menuItems = suggestions.map( this.action.getMenuItemForSuggestion.bind( this.action ) );
164 menuItems = this.action.updateMenuItems( menuItems );
165 this.menu.addItems( menuItems );
166 this.menu.highlightItem( this.menu.findFirstSelectableItem() );
167 this.updateMenu( input, suggestions );
168 } );
169 };
170
171 /**
172 * Update the widget's menu with the latest suggestions
173 *
174 * @param {string} input Input text
175 * @param {Array} suggestions Suggestions
176 */
177 ve.ui.CompletionWidget.prototype.updateMenu = function ( input, suggestions ) {
178 // Update the header based on the input
179 const label = this.action.getHeaderLabel( input, suggestions );
180 if ( label !== undefined ) {
181 this.header.setLabel( label );
182 }
183 if ( this.header.getLabel() !== null ) {
184 this.menu.addItems( [ this.header ], 0 );
185 } else {
186 this.menu.removeItems( [ this.header ] );
187 }
188 if ( !this.isolateInput ) {
189 // If there is a header or menu items, show the menu
190 if ( this.menu.items.length ) {
191 this.menu.toggle( true );
192 this.popup.toggle( true );
193 // Menu may have changed size, so recalculate position
194 this.popup.updateDimensions();
195 } else {
196 this.popup.toggle( false );
197 }
198 } else {
199 if ( !this.menu.items.length ) {
200 this.menu.addItems( [ this.noResults ], 0 );
201 }
202 this.menu.toggle( true );
203 this.popup.toggle( true );
204 this.popup.updateDimensions();
205 }
206 };
207
208 /**
209 * Handle choose events from the menu
210 *
211 * @param {OO.ui.MenuOptionWidget} item Chosen option
212 */
213 ve.ui.CompletionWidget.prototype.onMenuChoose = function ( item ) {
214 this.action.chooseItem( item, this.getCompletionRange( true ) );
215
216 this.teardown();
217 };
218
219 /**
220 * Handle toggle events from the menu
221 *
222 * @param {boolean} visible Menu is visible
223 */
224 ve.ui.CompletionWidget.prototype.onMenuToggle = function ( visible ) {
225 if ( !visible && !this.tearingDown ) {
226 // Menu was hidden by the user (e.g. pressed ESC) - trigger a teardown
227 this.teardown();
228 }
229 };
230
231 /**
232 * Handle select events from the document model
233 *
234 * @param {ve.dm.Selection} selection Selection
235 */
236 ve.ui.CompletionWidget.prototype.onModelSelect = function () {
237 const range = this.getCompletionRange();
238
239 const countMatches = () => {
240 let matches = this.menu.getItems().length;
241 if ( this.header.getLabel() !== null ) {
242 matches--;
243 }
244 if ( this.action.constructor.static.alwaysIncludeInput ) {
245 matches--;
246 }
247 return matches;
248 };
249
250 if ( !range || range.isBackwards() || this.action.shouldAbandon( this.surfaceModel.getDocument().data.getText( false, range ), countMatches() ) ) {
251 this.teardown();
252 } else {
253 this.update();
254 }
255 };
256
257 /**
258 * Get the range where the user has entered text in the document since opening the widget
259 *
260 * @param {boolean} [withSequence] Include the triggering sequence text in the range
261 * @return {ve.Range|null} Range, null if not valid
262 */
263 ve.ui.CompletionWidget.prototype.getCompletionRange = function ( withSequence ) {
264 const range = this.surfaceModel.getSelection().getCoveringRange();
265 if ( !range || !this.action ) {
266 return null;
267 }
268 return new ve.Range( this.initialOffset + ( withSequence ? 0 : this.sequenceLength ), range.end );
269 };