diff options
Diffstat (limited to 'vendor/oojs/oojs-ui/src/widgets/ToggleButtonWidget.js')
-rw-r--r-- | vendor/oojs/oojs-ui/src/widgets/ToggleButtonWidget.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/vendor/oojs/oojs-ui/src/widgets/ToggleButtonWidget.js b/vendor/oojs/oojs-ui/src/widgets/ToggleButtonWidget.js new file mode 100644 index 00000000..bb36f083 --- /dev/null +++ b/vendor/oojs/oojs-ui/src/widgets/ToggleButtonWidget.js @@ -0,0 +1,114 @@ +/** + * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a + * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be + * configured with {@link OO.ui.IconElement icons}, {@link OO.ui.IndicatorElement indicators}, + * {@link OO.ui.TitledElement titles}, {@link OO.ui.FlaggedElement styling flags}, + * and {@link OO.ui.LabelElement labels}. Please see + * the [OOjs UI documentation][1] on MediaWiki for more information. + * + * @example + * // Toggle buttons in the 'off' and 'on' state. + * var toggleButton1 = new OO.ui.ToggleButtonWidget( { + * label: 'Toggle Button off' + * } ); + * var toggleButton2 = new OO.ui.ToggleButtonWidget( { + * label: 'Toggle Button on', + * value: true + * } ); + * // Append the buttons to the DOM. + * $( 'body' ).append( toggleButton1.$element, toggleButton2.$element ); + * + * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons + * + * @class + * @extends OO.ui.ToggleWidget + * @mixins OO.ui.ButtonElement + * @mixins OO.ui.IconElement + * @mixins OO.ui.IndicatorElement + * @mixins OO.ui.LabelElement + * @mixins OO.ui.TitledElement + * @mixins OO.ui.FlaggedElement + * @mixins OO.ui.TabIndexedElement + * + * @constructor + * @param {Object} [config] Configuration options + * @cfg {boolean} [value=false] The toggle button’s initial on/off + * state. By default, the button is in the 'off' state. + */ +OO.ui.ToggleButtonWidget = function OoUiToggleButtonWidget( config ) { + // Configuration initialization + config = config || {}; + + // Parent constructor + OO.ui.ToggleButtonWidget.super.call( this, config ); + + // Mixin constructors + OO.ui.ButtonElement.call( this, config ); + OO.ui.IconElement.call( this, config ); + OO.ui.IndicatorElement.call( this, config ); + OO.ui.LabelElement.call( this, config ); + OO.ui.TitledElement.call( this, $.extend( {}, config, { $titled: this.$button } ) ); + OO.ui.FlaggedElement.call( this, config ); + OO.ui.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$button } ) ); + + // Events + this.connect( this, { click: 'onAction' } ); + + // Initialization + this.$button.append( this.$icon, this.$label, this.$indicator ); + this.$element + .addClass( 'oo-ui-toggleButtonWidget' ) + .append( this.$button ); +}; + +/* Setup */ + +OO.inheritClass( OO.ui.ToggleButtonWidget, OO.ui.ToggleWidget ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.ButtonElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.IconElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.IndicatorElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.LabelElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.TitledElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.FlaggedElement ); +OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.TabIndexedElement ); + +/* Methods */ + +/** + * Handle the button action being triggered. + * + * @private + */ +OO.ui.ToggleButtonWidget.prototype.onAction = function () { + this.setValue( !this.value ); +}; + +/** + * @inheritdoc + */ +OO.ui.ToggleButtonWidget.prototype.setValue = function ( value ) { + value = !!value; + if ( value !== this.value ) { + // Might be called from parent constructor before ButtonElement constructor + if ( this.$button ) { + this.$button.attr( 'aria-pressed', value.toString() ); + } + this.setActive( value ); + } + + // Parent method + OO.ui.ToggleButtonWidget.super.prototype.setValue.call( this, value ); + + return this; +}; + +/** + * @inheritdoc + */ +OO.ui.ToggleButtonWidget.prototype.setButtonElement = function ( $button ) { + if ( this.$button ) { + this.$button.removeAttr( 'aria-pressed' ); + } + OO.ui.ButtonElement.prototype.setButtonElement.call( this, $button ); + this.$button.attr( 'aria-pressed', this.value.toString() ); +}; |