Extends
Constructor
new OO.ui.ToggleButtonWidget([config])
#
ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
Boolean value. Like other buttons
, toggle buttons can be
configured with icons
,
indicators
,
titles
, styling flags
,
and labels
. Please see
the OOUI documentation on MediaWiki for more information.
Example
// Toggle buttons in the 'off' and 'on' state.
const toggleButton1 = new OO.ui.ToggleButtonWidget( {
label: 'Toggle Button off'
} ),
toggleButton2 = new OO.ui.ToggleButtonWidget( {
label: 'Toggle Button on',
value: true
} );
// Append the buttons to the DOM.
$( document.body ).append( toggleButton1.$element, toggleButton2.$element );
Parameters:
Name | Type | Attributes | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object |
optional |
Configuration options Properties:
|
ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a Boolean value.
Properties
tagNamestatic
#
- Source:
cancelButtonMouseDownEvents
#
Cancel mouse down events.
This property is usually set to true
to prevent the focus from changing when the button is
clicked.
Classes such as DraggableElement
and
ButtonOptionWidget
use a value of false
so that dragging
behavior is possible and mousedown events can be handled by a parent widget.
Properties:
Type | Description |
---|---|
boolean |
flags
#
Initial value to pass to setFlags if no value is provided in config.
Properties:
- Mixes in:
- Source:
icon
#
The symbolic name of the icon (e.g., ‘remove’ or ‘menu’), or a map of symbolic names. A map
is used for i18n purposes and contains a default
icon name and additional names keyed by
language code. The default
name is used when no icon is keyed by the user's language.
Example of an i18n map:
{ default: 'bold-a', en: 'bold-b', de: 'bold-f' }
Note: the static property will be overridden if the #icon configuration is used.
Properties:
Type | Description |
---|---|
Object | string |
- Mixes in:
- Source:
iconTitle
#
The icon title, displayed when users move the mouse over the icon. The value can be text, a
function that returns title text, or null
for no title.
The static property will be overridden if the #iconTitle configuration is used.
Properties:
Type | Description |
---|---|
string
|
function
|
null
|
- Mixes in:
- Source:
indicator
#
Symbolic name of the indicator (e.g. ‘required’ or ‘down’). The static property will be overridden if the #indicator configuration is used.
Properties:
Type | Description |
---|---|
string
|
null
|
- Mixes in:
- Source:
indicatorTitle
#
A text string used as the indicator title, a function that returns title text, or null
for no title. The static property will be overridden if the #indicatorTitle configuration is
used.
Properties:
Type | Description |
---|---|
string
|
function
|
null
|
- Mixes in:
- Source:
A text string used as the indicator title, a function that returns title text, or null
for no title.
label
#
The label text. The label can be specified as a plaintext string, a function that will
produce a string (will be resolved on construction time), or null
for no label. The static
value will be overridden if a label is specified with the #label config option.
Properties:
Type | Description |
---|---|
string
|
function
|
null
|
- Mixes in:
- Source:
title
#
The title text, a function that returns text, or null
for no title. The value of the static
property is overridden if the #title config option is used.
If the element has a default title (e.g. <input type=file>
), null
will allow that title to be
shown. Use empty string to suppress it.
Properties:
Type | Description |
---|---|
string
|
function
|
null
|
- Inherited from:
- Mixes in:
- Source:
null
for no title.
Methods
getClosestScrollableElementContainer() → {HTMLElement}
#
Get closest scrollable container.
- Inherited from:
- Source:
Returns:
Closest scrollable container
- Type
- HTMLElement
getData() → {any}
#
Get element data.
- Inherited from:
- Source:
Returns:
Element data
- Type
- any
getElementDocument() → {HTMLDocument}
#
getElementGroup() → {OO.ui.mixin.GroupElement|null
}
#
null
}
#
Get group element is in.
- Inherited from:
- Source:
Returns:
Group element, null if none
- Type
-
OO.ui.mixin.GroupElement
|
null
getElementId() → {string}
#
Ensure that the element has an 'id' attribute, setting it to an unique value if it's missing, and return its value.
- Inherited from:
- Source:
Returns:
- Type
- string
Ensure that the element has an 'id' attribute, setting it to an unique value if it's missing, and return its value.
getElementWindow() → {Window}
#
getInputId() → {string|null
}
#
null
}
#
Get an ID of a labelable node which is part of this widget, if any, to be used for <label for>
value.
If this function returns null, the widget should have a meaningful #simulateLabelClick method instead.
- Inherited from:
- Source:
Returns:
The ID of the labelable element
- Type
-
string
|
null
Get an ID of a labelable node which is part of this widget, if any, to be used for <label for>
value.
getTagName() → {string}
#
Get the HTML tag name.
Override this method to base the result on instance information.
- Inherited from:
- Source:
Returns:
HTML tag name
- Type
- string
getValue() → {boolean}
#
Get the value representing the toggle’s state.
- Inherited from:
- Source:
Returns:
The on/off state of the toggle
- Type
- boolean
highlightQuery(text, query, [compare], [combineMarks]) → {jQuery}
#
Highlight the first occurrence of the query in the given text
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
text |
string | Text |
||
query |
string | Query to find |
||
compare |
function |
optional |
Optional string comparator, e.g. Intl.Collator().compare |
|
combineMarks |
boolean |
optional |
false | Pull combining marks into highlighted text |
- Mixes in:
- Source:
Returns:
Text with the first match of the query sub-string wrapped in highlighted span
- Type
- jQuery
isDisabled() → {boolean}
#
Check if the widget is disabled.
- Inherited from:
- Source:
Returns:
Widget is disabled
- Type
- boolean
isElementAttached() → {boolean}
#
Check if the element is attached to the DOM
- Inherited from:
- Source:
Returns:
The element is attached to the DOM
- Type
- boolean
isVisible() → {boolean}
#
Check if element is visible.
- Inherited from:
- Source:
Returns:
element is visible
- Type
- boolean
restorePreInfuseState(state)protected
#
Restore the pre-infusion dynamic state for this widget.
This method is called after #$element has been inserted into DOM. The parameter is the return value of #gatherPreInfuseState.
Parameters:
Name | Type | Description |
---|---|---|
state |
Object |
- Inherited from:
- Source:
scrollElementIntoView([config]) → {jQuery.Promise}
#
Scroll element into view.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
config |
Object |
optional |
Configuration options |
- Inherited from:
- Source:
Returns:
Promise which resolves when the scroll is complete
- Type
- jQuery.Promise
setButtonElement()
#
- Source:
setData(data) → {OO.ui.Element}chainable
#
Set element data.
Parameters:
Name | Type | Description |
---|---|---|
data |
any | Element data |
- Inherited from:
- Source:
Returns:
The element, for chaining
- Type
- OO.ui.Element
setDisabled([disabled]) → {OO.ui.Widget}chainable
#
Set the 'disabled' state of the widget.
When a widget is disabled, it cannot be used and its appearance is updated to reflect this state.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
disabled |
boolean |
optional |
false | Disable widget |
- Inherited from:
- Source:
Returns:
The widget, for chaining
- Type
- OO.ui.Widget
setElementGroup(group) → {OO.ui.Element}chainable
#
Set group element is in.
Parameters:
Name | Type | Description |
---|---|---|
group |
OO.ui.mixin.GroupElement
|
null
|
Group element, null if none |
- Inherited from:
- Source:
Returns:
The element, for chaining
- Type
- OO.ui.Element
setElementId(id) → {OO.ui.Element}chainable
#
Set the element has an 'id' attribute.
Parameters:
Name | Type | Description |
---|---|---|
id |
string |
- Inherited from:
- Source:
Returns:
The element, for chaining
- Type
- OO.ui.Element
setLabelledBy(id)
#
Set the element with the given ID as a label for this widget.
Parameters:
Name | Type | Description |
---|---|---|
id |
string
|
null
|
- Inherited from:
- Source:
setValue([value]) → {OO.ui.Widget}chainable
#
Set the state of the toggle: true
for 'on', false
for 'off'.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
value |
boolean |
optional |
false | The state of the toggle |
- Overrides:
- Source:
Returns:
The widget, for chaining
- Type
- OO.ui.Widget
Fires:
true
for 'on', false
for 'off'.
simulateLabelClick()
#
Simulate the behavior of clicking on a label (a HTML <label>
element) bound to this input.
HTML only allows <label>
to act on specific "labelable" elements; complex widgets might need to
override this method to provide intuitive, accessible behavior.
By default, this does nothing. OO.ui.mixin.TabIndexedElement overrides it for focusable widgets. Individual widgets may override it too.
This method is called by OO.ui.LabelWidget and OO.ui.FieldLayout. It should not be called directly.
- Inherited from:
- Source:
<label>
element) bound to this input.
supports(methods) → {boolean}
#
Check if element supports one or more methods.
Parameters:
Name | Type | Description |
---|---|---|
methods |
string | Array.<string> | Method or list of methods to check |
- Inherited from:
- Source:
Returns:
All methods are supported
- Type
- boolean
toggle([show]) → {OO.ui.Element}chainable
#
Toggle visibility of an element.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
show |
boolean |
optional |
Make element visible, omit to toggle visibility |
- Inherited from:
- Source:
Returns:
The element, for chaining
- Type
- OO.ui.Element
Fires:
updateDisabled() → {OO.ui.Widget}chainable
#
Update the disabled state, in case of changes in parent widget.
- Inherited from:
- Source:
Returns:
The widget, for chaining
- Type
- OO.ui.Widget
updateThemeClasses()
#
Update the theme-provided classes.
This is called in element mixins and widget classes any time state changes. Updating is debounced, minimizing overhead of changing multiple attributes and guaranteeing that theme updates do not occur within an element's constructor
- Inherited from:
- Source:
Events
change(value)
#
A change event is emitted when the on/off state of the toggle changes.
Parameters:
Name | Type | Description |
---|---|---|
value |
boolean | Value representing the new state of the toggle |
- Inherited from:
- Source:
disable(disabled)
#
A 'disable' event is emitted when the disabled state of the widget changes (i.e. on disable and enable).
Parameters:
Name | Type | Description |
---|---|---|
disabled |
boolean | Widget is disabled |
- Inherited from:
- Source:
A 'disable' event is emitted when the disabled state of the widget changes (i.e.
toggle(visible)
#
A 'toggle' event is emitted when the visibility of the widget changes.
Parameters:
Name | Type | Description |
---|---|---|
visible |
boolean | Widget is visible |
- Inherited from:
- Source: