LuCI.ui. ComboButton

The ComboButton class implements a button element which can be expanded into a dropdown to chose from a set of different action choices.

UI widget instances are usually not supposed to be created by view code directly, instead they're implicitly created by LuCI.form when instantiating CBI forms.

This class is automatically instantiated as part of LuCI.ui. To use it in views, use 'require ui' and refer to ui.ComboButton. To import it in external JavaScript, use L.require("ui").then(...) and access the ComboButton property of the class instance value.

Constructor

new ComboButton(valueopt, choices, optionsopt)

Instantiate a combo button widget offering multiple action choices.

Parameters:
NameTypeAttributesDefaultDescription
valuestring | Array.<string><optional>
null

The initial input value(s).

choicesObject.<string, *>

Object containing the selectable choices of the widget. The object keys serve as values for the different choices while the values are used as choice labels.

optionsLuCI.ui.ComboButton.InitOptions<optional>

Object describing the widget specific options to initialize the button.

Extends

Methods

addChoices(values, labels)

Add new choices to the dropdown menu.

This function adds further choices to an existing dropdown menu, ignoring choice values which are already present.

Parameters:
NameTypeDescription
valuesArray.<string>

The choice values to add to the dropdown widget.

labelsObject.<string, *>

The choice label values to use when adding dropdown choices. If no label is found for a particular choice value, the value itself is used as label text. Choice labels may be any valid value accepted by LuCI.dom#content.

clearChoices(reset_valueopt)

Remove all existing choices from the dropdown menu.

This function removes all preexisting dropdown choices from the widget, keeping only choices currently being selected unless reset_values is given, in which case all choices and deselected and removed.

Parameters:
NameTypeAttributesDefaultDescription
reset_valueboolean<optional>
false

If set to true, deselect and remove selected choices as well instead of keeping them.

closeAllDropdowns()

Close all open dropdown widgets in the current document.

getValidationError() → {string}

Returns the current validation error

Returns:

The validation error at this time

Type: 
string

isChanged() → {boolean}

Check whether the input value was altered by the user.

Returns:

Returns true if the input value has been altered by the user or false if it is unchanged. Note that if the user modifies the initial value and changes it back to the original state, it is still reported as changed.

Type: 
boolean

isValid() → {boolean}

Check whether the current input value is valid.

Returns:

Returns true if the current input value is valid or false if it does not meet the validation constraints.

Type: 
boolean

registerEvents(targetNode, synevent, events)

Dispatch a custom (synthetic) event in response to received events.

Sets up event handlers on the given target DOM node for the given event names that dispatch a custom event of the given type to the widget root DOM node.

The primary purpose of this function is to set up a series of custom uniform standard events such as widget-update, validation-success, validation-failure etc. which are triggered by various different widget specific native DOM events.

Parameters:
NameTypeDescription
targetNodeNode

Specifies the DOM node on which the native event listeners should be registered.

syneventstring

The name of the custom event to dispatch to the widget root DOM node.

eventsArray.<string>

The native DOM events for which event handlers should be registered.

setChangeEvents(targetNode, …events)

Set up listeners for native DOM events that may change the widget value.

Sets up event handlers on the given target DOM node for the given event names which may cause the input value to change completely, such as change events in a select menu. In contrast to update events, such change events will not trigger input value validation but they may cause field dependencies to get re-evaluated and will mark the input widget as dirty.

Parameters:
NameTypeAttributesDescription
targetNodeNode

Specifies the DOM node on which the event listeners should be registered.

eventsstring<repeatable>

The DOM events for which event handlers should be registered.

setPlaceholder(value)

Set the current placeholder value of the input widget.

Parameters:
NameTypeDescription
valuestring | Array.<string> | null

The placeholder to set for the input element. Only applicable to text inputs, not to radio buttons, selects or similar.

setUpdateEvents(targetNode, …events)

Set up listeners for native DOM events that may update the widget value.

Sets up event handlers on the given target DOM node for the given event names which may cause the input value to update, such as keyup or onclick events. In contrast to change events, such update events will trigger input value validation.

Parameters:
NameTypeAttributesDescription
targetNodeNode

Specifies the DOM node on which the event listeners should be registered.

eventsstring<repeatable>

The DOM events for which event handlers should be registered.

triggerValidation() → {boolean}

Force validation of the current input value.

Usually input validation is automatically triggered by various DOM events bound to the input widget. In some cases it is required though to manually trigger validation runs, e.g. when programmatically altering values.

Returns:
Type: 
boolean

Type Definitions

InitOptions

ComboButtons support the same properties as Dropdown.InitOptions but enforce specific values for some properties and add additional button specific properties.

Properties
NameTypeAttributesDefaultDescription
multipleboolean<optional>
false

Since ComboButtons never allow selecting multiple actions, this property is forcibly set to false.

createboolean<optional>
false

Since ComboButtons never allow creating custom choices, this property is forcibly set to false.

optionalboolean<optional>
false

Since ComboButtons must always select one action, this property is forcibly set to false.

classesObject.<string, string><optional>

Specifies a mapping of choice values to CSS class names. If an action choice is selected by the user and if a corresponding entry exists in the classes object, the class names corresponding to the selected value are set on the button element.

This is useful to apply different button styles, such as colors, to the combined button depending on the selected action.

clickfunction<optional>

Specifies a handler function to invoke when the user clicks the button. This function will be called with the button DOM node as this context and receive the DOM click event as first as well as the selected action choice value as second argument.