Constructor

new Menu()

A basic menu class.

Extends
Blockly.Component

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

blur

package

blur()

Blur the menu element.

canHighlightItem

protected

canHighlightItem(item) returns boolean

Returns whether the given item can be highlighted.

Parameter

item

Blockly.MenuItem

The item to check.

Returns

boolean Whether the item can be highlighted.

clearHighlighted

protected

clearHighlighted()

Clears the currently highlighted item.

createDom

protected

createDom()

Creates the menu DOM.

Inherited from
Blockly.Menu#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.Menu#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.Menu#enterDocument

exitDocument

protected

exitDocument()

Cleans up the container before its DOM is removed from the document, and removes event handlers. Overrides Blockly.Component#exitDocument.

Inherited from
Blockly.Menu#exitDocument

focus

package

focus()

Focus the menu element.

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

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.

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).

getElement

package

getElement() returns Element

Gets the component's element.

Inherited from
Blockly.Component#getElement
Returns

The element for the component.

getHighlighted

protected

getHighlighted() returns Blockly.Component

Returns the currently highlighted item (if any).

Returns

nullable Blockly.Component Highlighted item (null if none).

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.

getMenuItem

protected

getMenuItem(node) returns Blockly.MenuItem

Returns the child menuitem that owns the given DOM node, or null if no such menuitem is found.

Parameter

node

Node

DOM node whose owner is to be returned.

Returns

nullable Blockly.MenuItem menuitem for which the DOM node belongs to.

getParent

protected

getParent() returns Blockly.Component

Returns the component's parent, if any.

Inherited from
Blockly.Component#getParent
Returns

The parent component.

handleKeyEvent

protected

handleKeyEvent(e) returns boolean

Attempts to handle a keyboard event, if the menuitem is enabled, by calling handleKeyEventInternal. Considered protected; should only be used within this package and by subclasses.

Parameter

e

Event

Key event to handle.

Returns

boolean Whether the key event was handled.

handleKeyEventInternal

protected

handleKeyEventInternal(e) returns boolean

Attempts to handle a keyboard event; returns true if the event was handled, false otherwise. If the container is enabled, and a child is highlighted, calls the child menuitem's handleKeyEvent method to give the menuitem a chance to handle the event first.

Parameter

e

Event

Key event to handle.

Returns

boolean Whether the event was handled by the container (or one of its children).

hasChildren

protected

hasChildren() returns boolean

Returns true if the component has children.

Inherited from
Blockly.Component#hasChildren
Returns

True if the component has children.

highlightHelper

protected

highlightHelper(fn, startIndex) returns boolean

Helper function that manages the details of moving the highlight among child menuitems in response to keyboard events.

Parameter

fn

function(number, number)

Function that accepts the current and maximum indices, and returns the next index to check.

startIndex

number

Start index.

Returns

boolean Whether the highlight has changed.

highlightNext

package

highlightNext()

Highlights the next highlightable item (or the first if nothing is currently highlighted).

highlightPrevious

package

highlightPrevious()

Highlights the previous highlightable item (or the last if nothing is currently highlighted).

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.

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.

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

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

setHighlighted

protected

setHighlighted(item)

Highlights the given item if it exists and is a child of the container; otherwise un-highlights the currently highlighted item.

Parameter

item

Blockly.MenuItem

Item to highlight.

setHighlightedIndex

protected

setHighlightedIndex(index)

Highlights the item at the given 0-based index (if any). If another item was previously highlighted, it is un-highlighted.

Parameter

index

number

Index of item to highlight (-1 removes the current highlight).

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.

unhighlightCurrent

protected

unhighlightCurrent()

Unhighlight the current highlighted item.