2021-08-16 16:17:14 +00:00
|
|
|
/**
|
2022-07-12 09:55:54 +00:00
|
|
|
* List of template parameters, each of which can be added or removed using a
|
|
|
|
* checkbox.
|
|
|
|
*
|
|
|
|
* This is modelled after {@see OO.ui.OutlineSelectWidget}. Currently we use
|
|
|
|
* the SelectWidget in multi-select mode, and selection maps to checked
|
|
|
|
* checkboxes.
|
2021-08-16 16:17:14 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.SelectWidget
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes OO.ui.mixin.TabIndexedElement
|
|
|
|
* @mixes ve.ui.MWAriaDescribe
|
2021-08-16 16:17:14 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2021-09-09 09:29:04 +00:00
|
|
|
* @param {Object} config
|
|
|
|
* @cfg {ve.ui.MWTransclusionOutlineParameterWidget[]} items
|
2022-07-18 14:52:20 +00:00
|
|
|
* @property {string|null} activeParameter Name of the currently selected parameter
|
2022-08-02 09:08:23 +00:00
|
|
|
* @property {number} stickyHeaderHeight
|
2021-08-16 16:17:14 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget = function VeUiMWTransclusionOutlineParameterSelectWidget( config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.super.call( this, ve.extendObject( config, {
|
2021-09-10 19:18:59 +00:00
|
|
|
classes: [ 've-ui-mwTransclusionOutlineParameterSelectWidget' ],
|
2021-08-16 16:17:14 +00:00
|
|
|
multiselect: true
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
// Mixin constructors
|
2021-09-09 09:29:04 +00:00
|
|
|
OO.ui.mixin.TabIndexedElement.call( this, {
|
|
|
|
tabIndex: this.isEmpty() ? -1 : 0
|
|
|
|
} );
|
2021-10-31 10:08:13 +00:00
|
|
|
ve.ui.MWAriaDescribe.call( this, config );
|
2021-08-16 16:17:14 +00:00
|
|
|
|
2021-08-31 10:27:06 +00:00
|
|
|
this.$element
|
|
|
|
.on( {
|
|
|
|
focus: this.bindDocumentKeyDownListener.bind( this ),
|
2022-07-15 13:07:00 +00:00
|
|
|
blur: this.onBlur.bind( this )
|
2021-11-05 08:10:21 +00:00
|
|
|
} );
|
2022-06-30 07:19:43 +00:00
|
|
|
|
2022-07-18 14:52:20 +00:00
|
|
|
this.activeParameter = null;
|
2022-08-02 09:08:23 +00:00
|
|
|
this.stickyHeaderHeight = 0;
|
2021-08-16 16:17:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, OO.ui.SelectWidget );
|
|
|
|
OO.mixinClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, OO.ui.mixin.TabIndexedElement );
|
2021-10-31 10:08:13 +00:00
|
|
|
OO.mixinClass( ve.ui.MWTransclusionOutlineParameterSelectWidget, ve.ui.MWAriaDescribe );
|
2021-08-16 16:17:14 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
2021-09-07 15:53:37 +00:00
|
|
|
/**
|
|
|
|
* This is fired instead of the "choose" event from the {@see OO.ui.SelectWidget} base class when
|
2022-07-15 12:16:34 +00:00
|
|
|
* pressing space on a parameter to toggle it or scroll it into view, without losing the focus.
|
2021-09-07 15:53:37 +00:00
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.ui.MWTransclusionOutlineParameterSelectWidget#templateParameterSpaceDown
|
2021-09-07 15:53:37 +00:00
|
|
|
* @param {ve.ui.MWTransclusionOutlineParameterWidget} item
|
|
|
|
* @param {boolean} selected
|
|
|
|
*/
|
|
|
|
|
2021-08-26 14:29:42 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {string} config.data Parameter name
|
|
|
|
* @param {string} config.label
|
2022-02-14 15:18:57 +00:00
|
|
|
* @param {boolean} [config.required=false] Required parameters can't be unchecked
|
|
|
|
* @param {boolean} [config.selected=false] If the parameter is currently used (checked)
|
2021-08-26 14:29:42 +00:00
|
|
|
* @return {ve.ui.MWTransclusionOutlineParameterWidget}
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.static.createItem = function ( config ) {
|
|
|
|
return new ve.ui.MWTransclusionOutlineParameterWidget( config );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2021-08-16 16:17:14 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.mixin.GroupElement
|
|
|
|
* @param {ve.ui.MWTransclusionOutlineParameterWidget[]} items
|
|
|
|
* @param {number} [index]
|
|
|
|
* @return {ve.ui.MWTransclusionOutlineParameterSelectWidget}
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.addItems = function ( items, index ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
items.forEach( ( item ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
item.connect( this, {
|
2021-08-31 08:00:42 +00:00
|
|
|
change: [ 'onCheckboxChange', item ]
|
2021-08-16 16:17:14 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-09-09 09:29:04 +00:00
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.addItems.call( this, items, index );
|
|
|
|
this.setTabIndex( this.isEmpty() ? -1 : 0 );
|
|
|
|
return this;
|
2021-08-16 16:17:14 +00:00
|
|
|
};
|
|
|
|
|
2022-08-02 09:08:23 +00:00
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.ensureVisibilityOfFirstCheckedParameter = function () {
|
|
|
|
// TODO: Replace with {@see OO.ui.SelectWidget.findFirstSelectedItem} when available
|
2024-05-21 14:22:56 +00:00
|
|
|
const firstChecked = this.findSelectedItems()[ 0 ];
|
2022-08-02 09:08:23 +00:00
|
|
|
if ( firstChecked ) {
|
|
|
|
firstChecked.ensureVisibility( this.stickyHeaderHeight );
|
|
|
|
}
|
2022-03-11 11:29:21 +00:00
|
|
|
};
|
|
|
|
|
2021-09-01 09:15:37 +00:00
|
|
|
/**
|
2022-07-19 14:31:38 +00:00
|
|
|
* @param {string|null} [paramName] Parameter name to set, e.g. "param1". Omit to remove setting.
|
2021-09-01 09:15:37 +00:00
|
|
|
*/
|
2022-07-18 14:52:20 +00:00
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.setActiveParameter = function ( paramName ) {
|
2022-07-19 14:31:38 +00:00
|
|
|
// Note: We know unnamed parameter placeholders never have an item here
|
2024-05-21 14:22:56 +00:00
|
|
|
const newItem = paramName ? this.findItemFromData( paramName ) : null;
|
2022-07-15 13:40:15 +00:00
|
|
|
// Unhighlight when called with no parameter name
|
|
|
|
this.highlightItem( newItem );
|
|
|
|
|
2022-07-19 14:31:38 +00:00
|
|
|
paramName = paramName || null;
|
2022-07-18 14:52:20 +00:00
|
|
|
if ( this.activeParameter === paramName ) {
|
2022-07-09 06:54:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const currentItem = this.activeParameter ? this.findItemFromData( this.activeParameter ) : null;
|
2022-07-18 14:52:20 +00:00
|
|
|
this.activeParameter = paramName;
|
2022-07-09 06:54:35 +00:00
|
|
|
|
|
|
|
if ( currentItem ) {
|
2022-07-18 14:36:37 +00:00
|
|
|
currentItem.toggleActivePageIndicator( false );
|
2022-07-09 06:54:35 +00:00
|
|
|
}
|
|
|
|
if ( newItem ) {
|
2022-07-18 14:36:37 +00:00
|
|
|
newItem.toggleActivePageIndicator( true );
|
2021-09-01 09:15:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-11 08:58:36 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.SelectWidget
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.highlightItem = function ( item ) {
|
2022-08-02 10:13:06 +00:00
|
|
|
if ( item ) {
|
2022-08-02 09:08:23 +00:00
|
|
|
item.ensureVisibility( this.stickyHeaderHeight );
|
2022-07-11 08:58:36 +00:00
|
|
|
}
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.highlightItem.call( this, item );
|
|
|
|
};
|
|
|
|
|
2021-08-26 14:29:42 +00:00
|
|
|
/**
|
|
|
|
* @param {string} paramName
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.markParameterAsUnused = function ( paramName ) {
|
|
|
|
// There is no OO.ui.SelectWidget.unselectItemByData(), we need to do this manually
|
2022-08-01 07:39:47 +00:00
|
|
|
/** @type {ve.ui.MWTransclusionOutlineParameterWidget} */
|
2024-05-21 14:22:56 +00:00
|
|
|
const item = paramName ? this.findItemFromData( paramName ) : null;
|
2021-08-26 14:29:42 +00:00
|
|
|
if ( item ) {
|
|
|
|
item.setSelected( false );
|
2022-07-18 13:55:51 +00:00
|
|
|
// An unused parameter can't be the active (set) one; it doesn't exist in the content pane
|
2022-08-01 07:39:47 +00:00
|
|
|
if ( this.activeParameter === paramName ) {
|
|
|
|
this.activeParameter = null;
|
|
|
|
item.toggleActivePageIndicator( false );
|
2022-07-18 13:55:51 +00:00
|
|
|
}
|
2021-08-26 14:29:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-16 16:17:14 +00:00
|
|
|
/**
|
|
|
|
* @private
|
2021-08-26 14:29:42 +00:00
|
|
|
* @param {ve.ui.MWTransclusionOutlineParameterWidget} item
|
2021-08-16 16:17:14 +00:00
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
2021-08-26 14:29:42 +00:00
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onCheckboxChange = function ( item, value ) {
|
2021-08-16 16:17:14 +00:00
|
|
|
// This extra check shouldn't be necessary, but better be safe than sorry
|
2021-08-26 14:29:42 +00:00
|
|
|
if ( item.isSelected() !== value ) {
|
2021-08-16 16:17:14 +00:00
|
|
|
// Note: This should have been named `toggle…` as it toggles the item's selection
|
2021-08-26 14:29:42 +00:00
|
|
|
this.chooseItem( item );
|
2021-08-16 16:17:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-31 15:07:17 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.SelectWidget
|
|
|
|
*/
|
2022-07-21 08:29:50 +00:00
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onFocus = function ( event ) {
|
2022-07-25 14:19:06 +00:00
|
|
|
if ( event.target !== this.$element[ 0 ] || this.findHighlightedItem() ) {
|
|
|
|
return;
|
2021-08-31 15:07:17 +00:00
|
|
|
}
|
2022-07-25 14:19:06 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let index = 0;
|
2022-07-25 14:19:06 +00:00
|
|
|
if ( event.relatedTarget ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const toolbarClass = 've-ui-mwTransclusionOutlineControlsWidget',
|
2022-07-25 14:19:06 +00:00
|
|
|
// The only elements below a parameter list can be another part or the toolbar
|
|
|
|
selector = '.ve-ui-mwTransclusionOutlinePartWidget, .' + toolbarClass,
|
|
|
|
$fromPart = $( event.relatedTarget ).closest( selector ),
|
|
|
|
$toPart = $( event.target ).closest( selector );
|
|
|
|
// When shift+tabbing into the list, highlight the last parameter
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
if ( $fromPart.hasClass( toolbarClass ) || $fromPart.index() > $toPart.index() ) {
|
|
|
|
index = this.getItemCount() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.highlightItem( this.items[ index ] );
|
|
|
|
|
2022-07-13 13:27:24 +00:00
|
|
|
// Don't call the parent. It makes assumptions what should be done here.
|
2021-08-31 15:07:17 +00:00
|
|
|
};
|
|
|
|
|
2021-08-16 16:17:14 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.SelectWidget
|
|
|
|
* @param {jQuery.Event} e
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires OO.ui.SelectWidget#choose
|
2021-08-16 16:17:14 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onMouseDown = function ( e ) {
|
|
|
|
if ( e.which === OO.ui.MouseButtons.LEFT ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const item = this.findTargetItem( e );
|
2021-08-31 08:05:25 +00:00
|
|
|
// Same as pressing enter, see below.
|
2021-08-16 16:17:14 +00:00
|
|
|
if ( item && item.isSelected() ) {
|
2021-09-10 15:10:24 +00:00
|
|
|
this.emit( 'choose', item, item.isSelected() );
|
2021-08-16 16:17:14 +00:00
|
|
|
|
|
|
|
// Don't call the parent, i.e. can't click to unselect the item
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.onMouseDown.call( this, e );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.SelectWidget
|
|
|
|
* @param {KeyboardEvent} e
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires OO.ui.SelectWidget#choose
|
|
|
|
* @fires ve.ui.MWTransclusionOutlineParameterSelectWidget#templateParameterSpaceDown
|
2021-08-16 16:17:14 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onDocumentKeyDown = function ( e ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let item;
|
2021-08-16 16:17:14 +00:00
|
|
|
|
|
|
|
switch ( e.keyCode ) {
|
|
|
|
case OO.ui.Keys.HOME:
|
|
|
|
item = this.items[ 0 ];
|
|
|
|
if ( item ) {
|
|
|
|
this.highlightItem( item );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OO.ui.Keys.END:
|
|
|
|
item = this.items[ this.items.length - 1 ];
|
|
|
|
if ( item ) {
|
|
|
|
this.highlightItem( item );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OO.ui.Keys.SPACE:
|
|
|
|
item = this.findHighlightedItem();
|
|
|
|
if ( item ) {
|
2021-09-28 07:55:47 +00:00
|
|
|
// Warning, this intentionally doesn't call .chooseItem() because we don't want this
|
|
|
|
// to fire a "choose" event!
|
2021-09-14 10:02:46 +00:00
|
|
|
if ( item.isSelected() ) {
|
|
|
|
this.unselectItem( item );
|
|
|
|
} else {
|
|
|
|
this.selectItem( item );
|
|
|
|
}
|
2022-07-15 12:16:34 +00:00
|
|
|
this.emit( 'templateParameterSpaceDown', item, item.isSelected() );
|
2021-08-16 16:17:14 +00:00
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
case OO.ui.Keys.ENTER:
|
|
|
|
item = this.findHighlightedItem();
|
2021-08-31 08:05:25 +00:00
|
|
|
// Same as clicking with the mouse, see above.
|
2021-08-16 16:17:14 +00:00
|
|
|
if ( item && item.isSelected() ) {
|
2021-09-10 15:10:24 +00:00
|
|
|
this.emit( 'choose', item, item.isSelected() );
|
2021-08-16 16:17:14 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Don't call the parent, i.e. can't use enter to unselect the item
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.super.prototype.onDocumentKeyDown.call( this, e );
|
|
|
|
};
|
2022-07-15 13:07:00 +00:00
|
|
|
|
|
|
|
ve.ui.MWTransclusionOutlineParameterSelectWidget.prototype.onBlur = function () {
|
|
|
|
this.highlightItem();
|
|
|
|
this.unbindDocumentKeyDownListener();
|
|
|
|
};
|