Class: Component

Constructor

Component

new Component()

Default implementation of a UI component. Similar to Closure's goog.ui.Component.

Enumeration

Error

static

string

Errors thrown by the component.

Value

ALREADY_RENDERED

Error when the component is already rendered and another render attempt is made.

PARENT_UNABLE_TO_BE_SET

Error when an attempt is made to set the parent of a component in a way that would result in an inconsistent object graph.

CHILD_INDEX_OUT_OF_BOUNDS

Error when an attempt is made to add a child component at an out-of-bounds index. We don't support sparse child arrays.

Properties

defaultRightToLeft

package   static

boolean

The default right to left value.

Error

static

string

Errors thrown by the component.

Value

ALREADY_RENDERED

Error when the component is already rendered and another render attempt is made.

PARENT_UNABLE_TO_BE_SET

Error when an attempt is made to set the parent of a component in a way that would result in an inconsistent object graph.

CHILD_INDEX_OUT_OF_BOUNDS

Error when an attempt is made to add a child component at an out-of-bounds index. We don't support sparse child arrays.

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

Blockly.Component

The new child component.

opt_render

Optional

boolean

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

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

Blockly.Component

The new child component.

index

number

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

boolean

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

See also
Blockly.Component#addChild

createDom

protected

createDom()

Creates the initial DOM representation for the component. The default implementation is to set this.element_ = div.

dispose

protected

dispose()

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

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.

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.

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.

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

function(unknown, number)

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

opt_obj

Optional

T

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

getChild

protected

getChild(id) returns Blockly.Component

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

Parameter

id

string

Child component ID.

Returns

nullable Blockly.Component 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

number

0-based index.

Returns

nullable Blockly.Component The child at the given index; null if none.

getChildCount

protected

getChildCount() returns number

Returns the number of children of this component.

Returns

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

Returns

Element Element to contain child elements (null if none).

getElement

package

getElement() returns Element

Gets the component's element.

Returns

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

Returns

string Unique component ID.

getParent

protected

getParent() returns Blockly.Component

Returns the component's parent, if any.

Returns

nullable Blockly.Component The parent component.

hasChildren

protected

hasChildren() returns boolean

Returns true if the component has children.

Returns

boolean 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

Blockly.Component

The child component.

Value may be null.

Returns

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

Returns

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

Returns

boolean 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

Element

Optional parent element to render the component into.

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

Node to render the component before.

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

Element

Root element for 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

Blockly.Component

The parent component.

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

boolean

Whether the component is rendered right-to-left.