Blockly. Component
Constructor
Component
new Component()
Default implementation of a UI component. Similar to Closure's goog.ui.Component.
Enumeration
Error
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
boolean
The default right to left value.
Error
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
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 boolean If true, the child component will be rendered into the parent. |
- See also
- Blockly.Component#addChildAt
addChildAt
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 |
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
createDom()
Creates the initial DOM representation for the component. The default implementation is to set this.element_ = div.
dispose
dispose()
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal.
disposeInternal
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
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
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
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
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
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
getChildCount() returns number
Returns the number of children of this component.
- Returns
-
number
The number of children.
getContentElement
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
getElement() returns Element
Gets the component's element.
- Returns
-
Element
The element for the component.
getId
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
getParent() returns Blockly.Component
Returns the component's parent, if any.
- Returns
-
nullable Blockly.Component
The parent component.
hasChildren
hasChildren() returns boolean
Returns true if the component has children.
- Returns
-
boolean
True if the component has children.
indexOfChild
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. |
- Returns
-
number
0-based index of the child component; -1 if not found.
isInDocument
isInDocument() returns boolean
Determines whether the component has been added to the document.
- Returns
-
boolean
TRUE if rendered. Otherwise, FALSE.
isRightToLeft
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
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
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
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
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. |
setRightToLeft
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. |