Constructor

new MenuItem(content, opt_value)

Class representing an item in a menu.

Parameter

content

string

Text caption to display as the content of the item.

opt_value

Optional

string

Data/model associated with the menu item.

Extends
Blockly.Component

Property

actionHandlerObj_

unknown

Methods

addChild

package

addChild(child, opt_render)

Adds the specified component as the last child of this component. See Blockly.Component#addChildAt for detailed semantics.

Parameter

child

The new child component.

opt_render

Optional

If true, the child component will be rendered into the parent.

Inherited from
Blockly.Component#addChild
See also
Blockly.Component#addChildAt

addChildAt

protected

addChildAt(child, index, opt_render)

Adds the specified component as a child of this component at the given 0-based index.

Both addChild and addChildAt assume the following contract between parent and child components:

  • the child component's element must be a descendant of the parent component's element, and
  • the DOM state of the child component must be consistent with the DOM state of the parent component (see `isInDocument`) in the steady state -- the exception is to addChildAt(child, i, false) and then immediately decorate/render the child.

In particular, parent.addChild(child) will throw an error if the child component is already in the document, but the parent isn't.

Clients of this API may call addChild and addChildAt with opt_render set to true. If opt_render is true, calling these methods will automatically render the child component's element into the parent component's element. If the parent does not yet have an element, then createDom will automatically be invoked on the parent before rendering the child.

Invoking {@code parent.addChild(child, true)} will throw an error if the child component is already in the document, regardless of the parent's DOM state.

If opt_render is true and the parent component is not already in the document, enterDocument will not be called on this component at this point.

Finally, this method also throws an error if the new child already has a different parent, or the given index is out of bounds.

Parameter

child

The new child component.

index

0-based index at which the new child component is to be added; must be between 0 and the current child count (inclusive).

opt_render

Optional

If true, the child component will be rendered into the parent.

Inherited from
Blockly.Component#addChildAt
See also
Blockly.Component#addChild

createDom

protected

createDom()

Creates the menuitem's DOM.

Inherited from
Blockly.MenuItem#createDom

dispose

protected

dispose()

Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal.

Inherited from
Blockly.Component#dispose

disposeInternal

protected

disposeInternal()

Disposes of the component. Calls exitDocument, which is expected to remove event handlers and clean up the component. Propagates the call to the component's children, if any. Removes the component's DOM from the document.

Inherited from
Blockly.Component#disposeInternal

enterDocument

protected

enterDocument()

Called when the component's element is known to be in the document. Anything using document.getElementById etc. should be done at this stage.

If the component contains child components, this call is propagated to its children.

Inherited from
Blockly.Component#enterDocument

exitDocument

protected

exitDocument()

Called by dispose to clean up the elements and listeners created by a component, or by a parent component/application who has removed the component from the document but wants to reuse it later.

If the component contains child components, this call is propagated to its children.

It should be possible for the component to be rendered again once this method has been called.

Inherited from
Blockly.Component#exitDocument

forEachChild

protected

forEachChild(f, opt_obj)

Calls the given function on each of this component's children in order. If opt_obj is provided, it will be used as the 'this' object in the function when called. The function should take two arguments: the child component and its 0-based index. The return value is ignored.

Parameter

f

The function to call for every child component; should take 2 arguments (the child and its index).

opt_obj

Optional

Used as the 'this' object in f when called.

Inherited from
Blockly.Component#forEachChild

getCheckboxDom

protected

getCheckboxDom() returns Element

Returns

Element The HTML element for the checkbox.

getChild

protected

getChild(id) returns Blockly.Component

Returns the child with the given ID, or null if no such child exists.

Parameter

id

Child component ID.

Inherited from
Blockly.Component#getChild
Returns

The child with the given ID; null if none.

getChildAt

protected

getChildAt(index) returns Blockly.Component

Returns the child at the given index, or null if the index is out of bounds.

Parameter

index

0-based index.

Inherited from
Blockly.Component#getChildAt
Returns

The child at the given index; null if none.

getChildCount

protected

getChildCount() returns number

Returns the number of children of this component.

Inherited from
Blockly.Component#getChildCount
Returns

The number of children.

getContentDom

protected

getContentDom() returns Element

Returns

non-null Element The HTML for the content.

getContentElement

protected

getContentElement() returns Element

Returns the DOM element into which child components are to be rendered, or null if the component itself hasn't been rendered yet. This default implementation returns the component's root element. Subclasses with complex DOM structures must override this method.

Inherited from
Blockly.Component#getContentElement
Returns

Element to contain child elements (null if none).

getContentWrapperDom

protected

getContentWrapperDom() returns Element

Returns

non-null Element The HTML for the content wrapper.

getElement

package

getElement() returns Element

Gets the component's element.

Inherited from
Blockly.Component#getElement
Returns

The element for the component.

getId

package

getId() returns string

Gets the unique ID for the instance of this component. If the instance doesn't already have an ID, generates one on the fly.

Inherited from
Blockly.Component#getId
Returns

Unique component ID.

getParent

protected

getParent() returns Blockly.Component

Returns the component's parent, if any.

Inherited from
Blockly.Component#getParent
Returns

The parent component.

getValue

package

getValue() returns any type

Gets the value associated with the menu item.

Returns

any type value Value associated with the menu item.

handleClick

package

handleClick(_e)

Handles click events. If the component is enabled, trigger the action associated with this menu item.

Parameter

_e

Event

Mouse event to handle.

hasChildren

protected

hasChildren() returns boolean

Returns true if the component has children.

Inherited from
Blockly.Component#hasChildren
Returns

True if the component has children.

indexOfChild

protected

indexOfChild(child) returns number

Returns the 0-based index of the given child component, or -1 if no such child is found.

Parameter

child

The child component.

Value may be null.

Inherited from
Blockly.Component#indexOfChild
Returns

0-based index of the child component; -1 if not found.

isEnabled

package

isEnabled() returns boolean

Returns true if the menu item is enabled, false otherwise.

Returns

boolean Whether the menu item is enabled.

isInDocument

protected

isInDocument() returns boolean

Determines whether the component has been added to the document.

Inherited from
Blockly.Component#isInDocument
Returns

TRUE if rendered. Otherwise, FALSE.

isRightToLeft

protected

isRightToLeft() returns boolean

Returns true if the component is rendered right-to-left, false otherwise. The first time this function is invoked, the right-to-left rendering property is set if it has not been already.

Inherited from
Blockly.Component#isRightToLeft
Returns

Whether the control is rendered right-to-left.

onAction

package

onAction(fn, opt_obj)

Set the handler that's triggered when the menu item is activated by the user. If opt_obj is provided, it will be used as the 'this' object in the function when called.

Parameter

fn

function(non-null Blockly.MenuItem)

The handler.

opt_obj

Optional

T

Used as the 'this' object in f when called.

performActionInternal

protected

performActionInternal()

Performs the appropriate action when the menu item is activated by the user.

render

package

render(opt_parentElement)

Renders the component. If a parent element is supplied, the component's element will be appended to it. If there is no optional parent element and the element doesn't have a parentNode then it will be appended to the document body.

If this component has a parent component, and the parent component is not in the document already, then this will not call enterDocument on this component.

Throws an Error if the component is already rendered.

Parameter

opt_parentElement

Optional

Optional parent element to render the component into.

Inherited from
Blockly.Component#render

renderBefore

protected

renderBefore(sibling)

Renders the component before another element. The other element should be in the document already.

Throws an Error if the component is already rendered.

Parameter

sibling

Node to render the component before.

Inherited from
Blockly.Component#renderBefore

setCheckable

package

setCheckable(checkable)

Sets the menu item to be checkable or not. Set to true for menu items that represent checkable options.

Parameter

checkable

boolean

Whether the menu item is checkable.

setChecked

package

setChecked(checked)

Checks or unchecks the component.

Parameter

checked

boolean

Whether to check or uncheck the component.

setContentInternal

protected

setContentInternal(content)

Sets the content associated with the menu item.

Parameter

content

string

Text caption to set as the menuitem's contents.

setElementInternal

protected

setElementInternal(element)

Sets the component's root element to the given element. Considered protected and final.

This should generally only be called during createDom. Setting the element does not actually change which element is rendered, only the element that is associated with this UI component.

This should only be used by subclasses and its associated renderers.

Parameter

element

Root element for the component.

Inherited from
Blockly.Component#setElementInternal

setEnabled

package

setEnabled(enabled)

Enables or disables the menu item.

Parameter

enabled

boolean

Whether to enable or disable the menu item.

setHighlighted

package

setHighlighted(highlight)

Highlights or unhighlights the component.

Parameter

highlight

boolean

Whether to highlight or unhighlight the component.

setParent

protected

setParent(parent)

Sets the parent of this component to use for event bubbling. Throws an error if the component already has a parent or if an attempt is made to add a component to itself as a child.

Parameter

parent

The parent component.

Inherited from
Blockly.Component#setParent

setRightToLeft

package

setRightToLeft(rightToLeft)

Set is right-to-left. This function should be used if the component needs to know the rendering direction during DOM creation (i.e. before #enterDocument is called and is right-to-left is set).

Parameter

rightToLeft

Whether the component is rendered right-to-left.

Inherited from
Blockly.Component#setRightToLeft

setRole

package

setRole(roleName)

Set the menu accessibility role.

Parameter

roleName

(non-null Blockly.utils.aria.Role or string)

role name.

setValue

package

setValue(value)

Sets the value associated with the menu item.

Parameter

value

any type

Value to be associated with the menu item.