Namespaces module
📖 Description
The Namespaces
module contains functions to load block bundles for the builder and functions to include or exclude certain blocks. Tripetto allows the use of multiple namespaces for loading block bundles. This is useful when the builder is used to create forms that run in different runners, where each runner has its own set of blocks.
To learn more about using namespaces, read the Loading blocks guide. And have a look at this guide to see how namespaces are used to create a live preview setup with multiple runners.
The preferred way to load block bundles is by using the namespace
property of the builder constructor or the useNamespace
method of a builder instance.
🗃️ Properties
🏷️ active
Retrieves the active builder block namespace.
Type
🏷️ erroneousBlocks
Retrieves the total number of erroneous blocks due to missing block types.
Type
number
🏷️ missingBlockTypes
Retrieves the array of missing block types.
Type
string[]
▶️ Functions
🔧 activate
Activates the namespace with the specified identifier.
Signature
activate(identifier?: string): INamespace
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the identifier of the namespace to activate. |
Return value
Returns a reference to the active INamespace
object.
🔧 activateDefault
Activates the default (built-in) namespace.
This default namespace is used when blocks are registered without mounting a specific namespace for them.
Signature
activateDefault(): INamespace
Return value
Returns a reference to the active INamespace
object.
🔧 activateOrLoadUMD
Activate a namespace for the specified identifier (or tries to load it if it is not available).
Loading UMD code from a string is unsafe! Only use when the source of the UMD code is trusted. When you are using a Content Security Policy (CSP) configuration, make sure script-src 'unsafe-eval'
is allowed.
The preferred way to load block bundles is by using the namespace
property of the builder constructor or the useNamespace
method of a builder instance.
Signature
activateOrLoadUMD(
identifier: string,
context: {},
name: string,
umd: string,
alwaysLoad?: boolean,
unload?: boolean
): INamespace | undefined
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the namespace identifier. |
context | {} | No | Reference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded). |
name | string | No | Specifies the (to the UMD exposed) symbol name of the supplied context. |
umd | string | No | Specifies the UMD code to load. |
alwaysLoad | boolean | Yes | Specifies if the bundle should always load, even when there is already a namespace available with the specified identifier (default is true ). |
unload | boolean | Yes | Specifies if the namespace should unload if it is already available (default is false ). |
Return value
Returns a reference to the INamespace
object or undefined
if the namespace was not loaded.
🔧 activateOrLoadURL
Activate a namespace for the specified identifier (or tries to load it if it is not available).
The UMD bundle is loaded using trusted-types with a policy name defined by the policy
parameter.
The preferred way to load block bundles is by using the namespace
property of the builder constructor or the useNamespace
method of a builder instance.
Signature
activateOrLoadURL(
identifier: string,
context: {},
name: string,
policy: string,
url: string,
done?: (namespace: INamespace) => void,
alwaysLoad?: boolean,
unload?: boolean
): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the namespace identifier. |
context | {} | No | Reference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded). |
name | string | No | Specifies the (to the UMD exposed) symbol name of the supplied context. |
policy | string | No | Specifies the trusted-types policy name to use. |
url | string | No | Specifies the URL of the UMD block bundle to load. |
done | (namespace: INamespace ) => void | Yes | Callback function that is invoked when the loading is completed. |
alwaysLoad | boolean | Yes | Specifies if the bundle should always load, even when there is already a namespace available with the specified identifier (default is true ). |
unload | boolean | Yes | Specifies if the namespace should unload if it is already available (default is false ). |
🔧 exclude
Specifies the blocks to exclude from the builder.
Signature
exclude(...blocks: string[]): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
blocks | string[] | No | Specifies the block identifiers of the blocks to exclude. |
Example
import { Namespaces } from "@tripetto/builder";
// Let's exclude the mailer block and the file upload block
Namespaces.exclude("@tripetto/block-mailer", "@tripetto/block-file-upload");
🔧 flag
Applies a certain flag to the specified blocks.
Flags can be verified using the flag
method of a NodeBlock
or ConditionBlock
.
Signature
flag(flag: string, ...types: string[]): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
flag | string | No | Specifies the flag identifier to set. |
types | string[] | No | Specifies the type identifiers of the blocks where the flag should be applied. |
🔧 get
Retrieves the namespace for the specified identifier (or the active namespace if the identifier is omitted).
A new namespace is created when there is no namespace found for the supplied identifier.
Signature
get(identifier?: string): INamespace
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the identifier of the namespace to retrieve. |
Return value
Returns the INamespace
object that holds the blocks for the namespace.
🔧 hasFlag
Verifies if the block has the specified flag enabled.
Signature
flag(flag: string, type: string): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
id | string | No | Specifies the flag identifier to verify. |
type | string | No | Specifies the type identifier of the block to verify. |
Return value
Returns true
if the specified flag was enabled.
🔧 include
Specifies the blocks to use in the builder. Only the blocks specified are usable in the builder. All other blocks are unavailable.
Signature
include(...blocks: string[]): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
blocks | string[] | No | Specifies the block identifiers of the blocks to include. |
Example
import { Namespaces } from "@tripetto/builder";
// Only allow the text input blocks in the builder
Namespaces.include("@tripetto/block-text", "@tripetto/block-textarea");
🔧 isAllowed
Retrieves if a certain block is available in the builder.
Signature
isAllowed(identifier: string): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the block identifier to test. |
Return value
Returns true
if the block is available in the builder.
Example
import { Namespaces } from "@tripetto/builder";
// Let's check if the mailer block is loaded and available
if (Namespaces.isAllowed("@tripetto/block-mailer")) {
// Yes, it is!
}
🔧 isAvailable
Check is the specified namespace is available.
Signature
isAvailable(identifier: string): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the namespace identifier to check. |
Return value
Returns true
if the namespace is available in the builder.
Example
import { Namespaces } from "@tripetto/builder";
// Load the block bundle of the Autoscroll stock runner
mountNamespace("autoscroll");
import "@tripetto/runner-autoscroll/builder";
unmountNamespace();
// Let's check if the block namespace for the autoscroll stock runner is loaded
if (Namespaces.isAvailable("autoscroll")) {
// Yes, it is!
}
🔧 loadUMD
Loads a block bundle using UMD code.
Loading UMD code from a string is unsafe! Only use when the source of the UMD code is trusted. When you are using a Content Security Policy (CSP) configuration, make sure script-src 'unsafe-eval'
is allowed.
The preferred way to load block bundles is by using the namespace
property of the builder constructor or the useNamespace
method of a builder instance.
Signature
loadUMD(
identifier: string,
context: {},
name: string,
umd: string,
unload?: boolean
): INamespace | undefined
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the namespace identifier. |
context | {} | No | Reference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded). |
name | string | No | Specifies the (to the UMD exposed) symbol name of the supplied context. |
umd | string | No | Specifies the UMD code to load. |
unload | boolean | Yes | Specifies if the namespace should unload if it is already available (default is true ). |
Return value
Returns a reference to the INamespace
object or undefined
if the namespace was not loaded.
🔧 loadURL
Loads a block bundle from an URL.
The UMD bundle is loaded using trusted-types with a policy name defined by the policy
parameter.
The preferred way to load block bundles is by using the namespace
property of the builder constructor or the useNamespace
method of a builder instance.
Signature
loadURL(
identifier: string,
context: {},
name: string,
policy: string,
url: string,
done?: (namespace: INamespace) => void,
unload?: boolean
): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the namespace identifier. |
context | {} | No | Reference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded). |
name | string | No | Specifies the (to the UMD exposed) symbol name of the supplied context. |
policy | string | No | Specifies the trusted-types policy name to use. |
url | string | No | Specifies the URL of the UMD block bundle to load. |
done | (namespace: INamespace ) => void | Yes | Callback function that is invoked when the loading is completed. |
unload | boolean | Yes | Specifies if the namespace should unload if it is already available (default is true ). |
🔧 mount
Mounts (activates) a namespace context. All blocks that self-register when this namespace is mounted will become part of the namespace.
You can also use the shorthand mountNamespace
function.
Signature
mount(identifier?: string): void
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the identifier of the namespace to mount. |
🔧 unload
Unloads the specified namespace.
Signature
unload(identifier?: string): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
identifier | string | No | Specifies the identifier of the namespace to unload. |
Return value
Returns true
when the namespace is unloaded.
🔧 unmount
Unmounts (deactivates) the active namespace context.
You can also use the shorthand unmountNamespace
function.
Signature
unmount(): void