Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWTransclusionOutlineParameterSelectWidget.js

raw symbols SCIP
1 /**
2 * List of template parameters, each of which can be added or removed using a
3 * checkbox.
4 *
5 * This is modelled after {@see OO.ui.OutlineSelectWidget}. Currently we use
6 * the SelectWidget in multi-select mode, and selection maps to checked
7 * checkboxes.
8 *
9 * @class
10 * @extends OO.ui.SelectWidget
11 * @mixes OO.ui.mixin.TabIndexedElement
12 * @mixes ve.ui.MWAriaDescribe
13 *
14 * @constructor
15 * @param {Object} config
16 * @param {ve.ui.MWTransclusionOutlineParameterWidget[]} config.items
17 * @property {string|null} activeParameter Name of the currently selected parameter
18 * @property {number} stickyHeaderHeight
19 */
20 ve.ui.MWTransclusionOutlineParameterSelectWidget = function VeUiMWTransclusionOutlineParameterSelectWidget( config ) {
21 // Parent constructor
22 ve.ui.MWTransclusionOutlineParameterSelectWidget.super.call( this, ve.extendObject( config, {
23 classes: [ 've-ui-mwTransclusionOutlineParameterSelectWidget' ],
24 multiselect: true
25 } ) );
26
27 // Mixin constructors
28 OO.ui.mixin.TabIndexedElement.call( this, {
29 tabIndex: this.isEmpty() ? -1 : 0
30 } );
31 ve.ui.MWAriaDescribe.call( this, config );
32
33 this.$element
34 .on( {
35 focus: this.bindDocumentKeyDownListener.bind( this ),
36 blur: this.onBlur.bind( this )
37 } );
38
39 this.activeParameter = null;
40 this.stickyHeaderHeight = 0;
41 };
42
43 /* Inheritance */
44
45 OO.inheritClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, OO.ui.SelectWidget );
46 OO.mixinClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, OO.ui.mixin.TabIndexedElement );
47 OO.mixinClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, ve.ui.MWAriaDescribe );
48
49 /* Events */
50
51 /**
52 * This is fired instead of the "choose" event from the {@see OO.ui.SelectWidget} base class when
53 * pressing space on a parameter to toggle it or scroll it into view, without losing the focus.
54 *
55 * @event ve.ui.MWTransclusionOutlineParameterSelectWidget#templateParameterSpaceDown
56 * @param {ve.ui.MWTransclusionOutlineParameterWidget} item
57 * @param {boolean} selected
58 */
59
60 /* Static Methods */
61
62 /**
63 * @param {Object} config
64 * @param {string} config.data Parameter name
65 * @param {string} config.label
66 * @param {boolean} [config.required=false] Required parameters can't be unchecked
67 * @param {boolean} [config.selected=false] If the parameter is currently used (checked)
68 * @return {ve.ui.MWTransclusionOutlineParameterWidget}
69 */
70 ve.ui.MWTransclusionOutlineParameterSelectWidget.static.createItem = function ( config ) {
71 return new ve.ui.MWTransclusionOutlineParameterWidget( config );
72 };
73
74 /* Methods */
75
76 /**
77 * @inheritDoc OO.ui.mixin.GroupElement
78 * @param {ve.ui.MWTransclusionOutlineParameterWidget[]} items
79 * @param {number} [index]
80 * @return {ve.ui.MWTransclusionOutlineParameterSelectWidget}
81 */
82 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.addItems = function ( items, index ) {
83 items.forEach( ( item ) => {
84 item.connect( this, {
85 change: [ 'onCheckboxChange', item ]
86 } );
87 } );
88
89 ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.addItems.call( this, items, index );
90 this.setTabIndex( this.isEmpty() ? -1 : 0 );
91 return this;
92 };
93
94 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.ensureVisibilityOfFirstCheckedParameter = function () {
95 // TODO: Replace with {@see OO.ui.SelectWidget.findFirstSelectedItem} when available
96 const firstChecked = this.findSelectedItems()[ 0 ];
97 if ( firstChecked ) {
98 firstChecked.ensureVisibility( this.stickyHeaderHeight );
99 }
100 };
101
102 /**
103 * @param {string|null} [paramName] Parameter name to set, e.g. "param1". Omit to remove setting.
104 */
105 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.setActiveParameter = function ( paramName ) {
106 // Note: We know unnamed parameter placeholders never have an item here
107 const newItem = paramName ? this.findItemFromData( paramName ) : null;
108 // Unhighlight when called with no parameter name
109 this.highlightItem( newItem );
110
111 paramName = paramName || null;
112 if ( this.activeParameter === paramName ) {
113 return;
114 }
115
116 const currentItem = this.activeParameter ? this.findItemFromData( this.activeParameter ) : null;
117 this.activeParameter = paramName;
118
119 if ( currentItem ) {
120 currentItem.toggleActivePageIndicator( false );
121 }
122 if ( newItem ) {
123 newItem.toggleActivePageIndicator( true );
124 }
125 };
126
127 /**
128 * @inheritDoc OO.ui.SelectWidget
129 */
130 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.highlightItem = function ( item ) {
131 if ( item ) {
132 item.ensureVisibility( this.stickyHeaderHeight );
133 }
134 ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.highlightItem.call( this, item );
135 };
136
137 /**
138 * @param {string} paramName
139 */
140 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.markParameterAsUnused = function ( paramName ) {
141 // There is no OO.ui.SelectWidget.unselectItemByData(), we need to do this manually
142 /** @type {ve.ui.MWTransclusionOutlineParameterWidget} */
143 const item = paramName ? this.findItemFromData( paramName ) : null;
144 if ( item ) {
145 item.setSelected( false );
146 // An unused parameter can't be the active (set) one; it doesn't exist in the content pane
147 if ( this.activeParameter === paramName ) {
148 this.activeParameter = null;
149 item.toggleActivePageIndicator( false );
150 }
151 }
152 };
153
154 /**
155 * @private
156 * @param {ve.ui.MWTransclusionOutlineParameterWidget} item
157 * @param {boolean} value
158 */
159 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onCheckboxChange = function ( item, value ) {
160 // This extra check shouldn't be necessary, but better be safe than sorry
161 if ( item.isSelected() !== value ) {
162 // Note: This should have been named `toggle…` as it toggles the item's selection
163 this.chooseItem( item );
164 }
165 };
166
167 /**
168 * @inheritDoc OO.ui.SelectWidget
169 */
170 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onFocus = function ( event ) {
171 if ( event.target !== this.$element[ 0 ] || this.findHighlightedItem() ) {
172 return;
173 }
174
175 let index = 0;
176 if ( event.relatedTarget ) {
177 const toolbarClass = 've-ui-mwTransclusionOutlineControlsWidget',
178 // The only elements below a parameter list can be another part or the toolbar
179 selector = '.ve-ui-mwTransclusionOutlinePartWidget, .' + toolbarClass,
180 $fromPart = $( event.relatedTarget ).closest( selector ),
181 $toPart = $( event.target ).closest( selector );
182 // When shift+tabbing into the list, highlight the last parameter
183 // eslint-disable-next-line no-jquery/no-class-state
184 if ( $fromPart.hasClass( toolbarClass ) || $fromPart.index() > $toPart.index() ) {
185 index = this.getItemCount() - 1;
186 }
187 }
188 this.highlightItem( this.items[ index ] );
189
190 // Don't call the parent. It makes assumptions what should be done here.
191 };
192
193 /**
194 * @inheritDoc OO.ui.SelectWidget
195 * @param {jQuery.Event} e
196 * @fires OO.ui.SelectWidget#choose
197 */
198 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onMouseDown = function ( e ) {
199 if ( e.which === OO.ui.MouseButtons.LEFT ) {
200 const item = this.findTargetItem( e );
201 // Same as pressing enter, see below.
202 if ( item && item.isSelected() ) {
203 this.emit( 'choose', item, item.isSelected() );
204
205 // Don't call the parent, i.e. can't click to unselect the item
206 return false;
207 }
208 }
209
210 ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.onMouseDown.call( this, e );
211 };
212
213 /**
214 * @inheritDoc OO.ui.SelectWidget
215 * @param {KeyboardEvent} e
216 * @fires OO.ui.SelectWidget#choose
217 * @fires ve.ui.MWTransclusionOutlineParameterSelectWidget#templateParameterSpaceDown
218 */
219 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onDocumentKeyDown = function ( e ) {
220 let item;
221
222 switch ( e.keyCode ) {
223 case OO.ui.Keys.HOME:
224 item = this.items[ 0 ];
225 if ( item ) {
226 this.highlightItem( item );
227 }
228 break;
229 case OO.ui.Keys.END:
230 item = this.items[ this.items.length - 1 ];
231 if ( item ) {
232 this.highlightItem( item );
233 }
234 break;
235 case OO.ui.Keys.SPACE:
236 item = this.findHighlightedItem();
237 if ( item ) {
238 // Warning, this intentionally doesn't call .chooseItem() because we don't want this
239 // to fire a "choose" event!
240 if ( item.isSelected() ) {
241 this.unselectItem( item );
242 } else {
243 this.selectItem( item );
244 }
245 this.emit( 'templateParameterSpaceDown', item, item.isSelected() );
246 }
247 e.preventDefault();
248 break;
249 case OO.ui.Keys.ENTER:
250 item = this.findHighlightedItem();
251 // Same as clicking with the mouse, see above.
252 if ( item && item.isSelected() ) {
253 this.emit( 'choose', item, item.isSelected() );
254 e.preventDefault();
255
256 // Don't call the parent, i.e. can't use enter to unselect the item
257 return false;
258 }
259 break;
260 }
261
262 ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.onDocumentKeyDown.call( this, e );
263 };
264
265 ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onBlur = function () {
266 this.highlightItem();
267 this.unbindDocumentKeyDownListener();
268 };