2021-05-17 15:43:49 +00:00
|
|
|
/*!
|
2021-06-18 09:48:33 +00:00
|
|
|
* VisualEditor user interface MWTemplateOutlineParameterCheckboxLayout class.
|
2021-05-17 15:43:49 +00:00
|
|
|
*
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for checkbox and label
|
|
|
|
*
|
|
|
|
* @class
|
2021-08-10 14:42:21 +00:00
|
|
|
* @extends OO.ui.Widget
|
2021-05-17 15:43:49 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {Object} config
|
|
|
|
* @cfg {string} data Parameter name
|
|
|
|
* @cfg {string} label
|
|
|
|
* @cfg {boolean} [required]
|
|
|
|
* @cfg {boolean} [selected]
|
2021-05-17 15:43:49 +00:00
|
|
|
*/
|
2021-06-18 09:48:33 +00:00
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout = function VeUiMWTemplateOutlineParameterCheckboxLayout( config ) {
|
2021-08-10 14:42:21 +00:00
|
|
|
this.checkbox = new OO.ui.CheckboxInputWidget( {
|
2021-05-17 15:43:49 +00:00
|
|
|
title: config.required ? ve.msg( 'visualeditor-dialog-transclusion-required-parameter' ) : null,
|
|
|
|
disabled: config.required,
|
|
|
|
selected: config.selected || config.required
|
|
|
|
} )
|
|
|
|
// FIXME: pass-through binding like [ 'emit', 'toggle' ]?
|
2021-08-09 16:35:53 +00:00
|
|
|
.connect( this, { change: 'onCheckboxChange' } );
|
2021-08-10 14:42:21 +00:00
|
|
|
this.checkbox.$input.on( 'keydown', this.onKeyDown.bind( this ) );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
|
|
|
// Parent constructor
|
2021-08-10 14:42:21 +00:00
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout.super.call( this, config );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
2021-08-09 16:35:53 +00:00
|
|
|
// Mixin constructors
|
2021-08-10 14:42:21 +00:00
|
|
|
OO.ui.mixin.LabelElement.call( this, $.extend( { $label: $( '<label>' ) }, config ) );
|
|
|
|
OO.ui.mixin.TabIndexedElement.call( this, ve.extendObject( config, {
|
|
|
|
tabIndex: this.checkbox.isDisabled() ? 0 : -1
|
|
|
|
} ) );
|
2021-08-09 16:35:53 +00:00
|
|
|
|
2021-05-17 15:43:49 +00:00
|
|
|
// Initialization
|
2021-08-09 16:35:53 +00:00
|
|
|
this.$element
|
|
|
|
.addClass( 've-ui-mwTransclusionOutlineItem' )
|
2021-08-10 14:42:21 +00:00
|
|
|
.append( this.checkbox.$element, this.$label )
|
2021-08-09 16:35:53 +00:00
|
|
|
.on( 'click', this.onClick.bind( this ) )
|
|
|
|
.on( 'keydown', this.onKeyDown.bind( this ) );
|
2021-05-17 15:43:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2021-08-10 14:42:21 +00:00
|
|
|
OO.inheritClass( ve.ui.MWTemplateOutlineParameterCheckboxLayout, OO.ui.Widget );
|
|
|
|
OO.mixinClass( ve.ui.MWTemplateOutlineParameterCheckboxLayout, OO.ui.mixin.LabelElement );
|
2021-08-09 16:35:53 +00:00
|
|
|
OO.mixinClass( ve.ui.MWTemplateOutlineParameterCheckboxLayout, OO.ui.mixin.TabIndexedElement );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event change
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {string} paramName
|
2021-06-29 19:45:09 +00:00
|
|
|
* @param {boolean} checked New checkbox state
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event select
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {string} paramName
|
2021-06-29 19:45:09 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-17 15:43:49 +00:00
|
|
|
/* Methods */
|
2021-06-29 19:45:09 +00:00
|
|
|
|
2021-08-09 16:35:53 +00:00
|
|
|
/**
|
|
|
|
* @fires select
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout.prototype.onClick = function () {
|
2021-08-10 14:42:21 +00:00
|
|
|
this.setSelected( true );
|
2021-08-09 16:35:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout.prototype.onKeyDown = function ( e ) {
|
2021-08-10 14:42:21 +00:00
|
|
|
if ( e.keyCode === OO.ui.Keys.SPACE ) {
|
|
|
|
// FIXME: Focus should stay in the sidebar
|
|
|
|
} else if ( e.keyCode === OO.ui.Keys.ENTER ) {
|
|
|
|
this.setSelected( true );
|
2021-08-09 16:35:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a checkbox input widget change event {@see OO.ui.CheckboxInputWidget}.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
* @fires change
|
|
|
|
*/
|
2021-08-09 16:35:53 +00:00
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout.prototype.onCheckboxChange = function ( value ) {
|
2021-06-18 15:23:35 +00:00
|
|
|
this.emit( 'change', this.getData(), value );
|
2021-05-17 15:43:49 +00:00
|
|
|
};
|
|
|
|
|
2021-08-10 14:42:21 +00:00
|
|
|
/**
|
|
|
|
* @param {boolean} state Selected state
|
|
|
|
* @param {boolean} internal Used for internal calls to suppress events
|
|
|
|
*/
|
2021-06-18 15:23:35 +00:00
|
|
|
ve.ui.MWTemplateOutlineParameterCheckboxLayout.prototype.setSelected = function ( state, internal ) {
|
2021-08-10 14:42:21 +00:00
|
|
|
if ( !this.checkbox.isDisabled() ) {
|
|
|
|
this.checkbox.setSelected( state, internal );
|
|
|
|
}
|
|
|
|
if ( !internal ) {
|
|
|
|
// Note: Must be fired even if the checkbox was selected before, for proper focus behavior
|
|
|
|
this.emit( 'select', this.getData() );
|
|
|
|
}
|
2021-05-17 15:43:49 +00:00
|
|
|
};
|