Skip to main content

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.

tip

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.

caution

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​

INamespace


🏷️ 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​

NameTypeOptionalDescription
identifierstringNoSpecifies the identifier of the namespace to activate.

Return value​

Returns a reference to the active INamespace object.


🔧 activateDefault​

Activates the default (built-in) namespace.

info

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

danger

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.

caution

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​

NameTypeOptionalDescription
identifierstringNoSpecifies the namespace identifier.
context{}NoReference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded).
namestringNoSpecifies the (to the UMD exposed) symbol name of the supplied context.
umdstringNoSpecifies the UMD code to load.
alwaysLoadbooleanYesSpecifies if the bundle should always load, even when there is already a namespace available with the specified identifier (default is true).
unloadbooleanYesSpecifies 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).

info

The UMD bundle is loaded using trusted-types with a policy name defined by the policy parameter.

caution

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​

NameTypeOptionalDescription
identifierstringNoSpecifies the namespace identifier.
context{}NoReference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded).
namestringNoSpecifies the (to the UMD exposed) symbol name of the supplied context.
policystringNoSpecifies the trusted-types policy name to use.
urlstringNoSpecifies the URL of the UMD block bundle to load.
done(namespace: INamespace) => voidYesCallback function that is invoked when the loading is completed.
alwaysLoadbooleanYesSpecifies if the bundle should always load, even when there is already a namespace available with the specified identifier (default is true).
unloadbooleanYesSpecifies 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​

NameTypeOptionalDescription
blocksstring[]NoSpecifies 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.

tip

Flags can be verified using the flag method of a NodeBlock or ConditionBlock.

Signature​

flag(flag: string, ...types: string[]): void

Parameters​

NameTypeOptionalDescription
flagstringNoSpecifies the flag identifier to set.
typesstring[]NoSpecifies 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).

info

A new namespace is created when there is no namespace found for the supplied identifier.

Signature​

get(identifier?: string): INamespace

Parameters​

NameTypeOptionalDescription
identifierstringNoSpecifies 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​

NameTypeOptionalDescription
idstringNoSpecifies the flag identifier to verify.
typestringNoSpecifies 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​

NameTypeOptionalDescription
blocksstring[]NoSpecifies 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​

NameTypeOptionalDescription
identifierstringNoSpecifies 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​

NameTypeOptionalDescription
identifierstringNoSpecifies 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.

danger

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.

caution

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​

NameTypeOptionalDescription
identifierstringNoSpecifies the namespace identifier.
context{}NoReference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded).
namestringNoSpecifies the (to the UMD exposed) symbol name of the supplied context.
umdstringNoSpecifies the UMD code to load.
unloadbooleanYesSpecifies 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.

info

The UMD bundle is loaded using trusted-types with a policy name defined by the policy parameter.

caution

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​

NameTypeOptionalDescription
identifierstringNoSpecifies the namespace identifier.
context{}NoReference to a context to use (this context is made available under the supplied name to the UMD bundle that is loaded).
namestringNoSpecifies the (to the UMD exposed) symbol name of the supplied context.
policystringNoSpecifies the trusted-types policy name to use.
urlstringNoSpecifies the URL of the UMD block bundle to load.
done(namespace: INamespace) => voidYesCallback function that is invoked when the loading is completed.
unloadbooleanYesSpecifies 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.

tip

You can also use the shorthand mountNamespace function.

Signature​

mount(identifier?: string): void

Parameters​

NameTypeOptionalDescription
identifierstringNoSpecifies the identifier of the namespace to mount.

🔧 unload​

Unloads the specified namespace.

Signature​

unload(identifier?: string): boolean

Parameters​

NameTypeOptionalDescription
identifierstringNoSpecifies the identifier of the namespace to unload.

Return value​

Returns true when the namespace is unloaded.


🔧 unmount​

Unmounts (deactivates) the active namespace context.

tip

You can also use the shorthand unmountNamespace function.

Signature​

unmount(): void