Interface IH5PFieldInstance<TParams, TField>

Any class that implements this interface can be used as a widget in H5P. It defines the methods that are required for a widget to work: appendTo, validate and remove.

appendTo is called when the widget is added to the DOM. It provides the container element as a jQuery object. It is crucial to add the widget to the container element.

validate is called in certain cases by the H5P framework to check if the widget is valid, for instance when the user collapses the dropdown that the widget belongs in. If the widget is not valid, the dropdown will not collapse.

remove is called when the widget is removed from the DOM. Here you should remove any global event listeners that you have added to avoid memory leaks.

It is recommended to use the H5PWidget class from h5p-utils as a base class for your widget. It sets up the widget and adds the constructor parameters as properties on the class. It also extends the H5P.EventDispatcher class, which allows you to use the trigger and on methods to trigger and listen to events.

⚠️ If you decide not to extend the H5PWidget class, you will have to extend H5P.EventDispatcher yourself.

Example

import { H5PWidget } from "h5p-utils";
import type { IH5PWidget } from "h5p-types";

class MyWidget extends H5PWidget implements IH5PWidget {
appendTo($container: JQuery<HTMLElement>) {
const containerElement = $container.get(0);

if (!containerElement) {
throw new Error("Could not find container element for `MyWidget`");
}

const input = document.createElement("input");
input.addEventListener("change", () =>
this.setValue(this.field, input.value),
);

containerElement.appendChild(input);
}

validate() {
return true;
}

remove() {}
}
interface IH5PFieldInstance<TParams, TField> {
    $common: null | JQuery<HTMLElement>;
    $commonButton: null | JQuery<HTMLElement>;
    $form: null | JQuery<HTMLElement>;
    addLanguages: ((libraryName, languageCodes) => void);
    children: H5PGroup[];
    commonFields: Record<`H5P.${string} ${number}.${number}`, {
        l10n: {
            instance: H5PGroup;
            params: unknown;
            parents: H5PForm<unknown>;
            setValues: H5PSetValue<unknown>;
        };
    }>;
    currentLibrary?: `H5P.${string} ${number}.${number}`;
    field: TField;
    metadata: H5PMetadata;
    metadataForm: null | H5PMetadataForm;
    off: (<TData>(type, listener?) => void);
    offset: {
        left: number;
        top: number;
    };
    on: (<TData, TType>(type, listener, thisArg?) => void);
    once: (<TData, TType>(type, listener, thisArg?) => void);
    params: TParams;
    parent: H5PForm | IH5PFieldInstance<unknown, H5PField>;
    passReadies: boolean;
    readies: unknown[];
    ready: ((callback) => void);
    removeLanguages: ((libraryName, languageCodes) => void);
    trigger: (<TData>(event, eventData?, extras?) => void);
    triggerXAPI: ((verb, extra) => void);
    zebra: "odd" | "even";
    appendTo($container): void;
    createXAPIEventTemplate<TVerb>(verb, extra): XAPIEvent;
    remove(): void;
    validate(): boolean;
}

Type Parameters

Hierarchy-Diagram

UML class diagram of IH5PFieldInstance
Legend
icon for a class in the UML class diagram class
icon for an interface in the UML class diagram interface
icon for a public property in the UML class diagram public property
icon for a public method in the UML class diagram public method

Hierarchy (view full)

Properties

$common: null | JQuery<HTMLElement>
$commonButton: null | JQuery<HTMLElement>
$form: null | JQuery<HTMLElement>
addLanguages: ((libraryName, languageCodes) => void)

Add new languages for content type.

Type declaration

    • (libraryName, languageCodes): void
    • Parameters

      • libraryName: string
      • languageCodes: (undefined | string)[]

      Returns void

children: H5PGroup[]
commonFields: Record<`H5P.${string} ${number}.${number}`, {
    l10n: {
        instance: H5PGroup;
        params: unknown;
        parents: H5PForm<unknown>;
        setValues: H5PSetValue<unknown>;
    };
}>

Type declaration

currentLibrary?: `H5P.${string} ${number}.${number}`
field: TField
metadata: H5PMetadata
metadataForm: null | H5PMetadataForm
off: (<TData>(type, listener?) => void)

Remove event listener If no listener is specified, all listeners will be removed

Type declaration

    • <TData>(type, listener?): void
    • Type Parameters

      • TData = unknown

      Parameters

      • type: string

        Event type

      • Optional listener: ((event) => void)

        Event listener

          • (event): void
          • Parameters

            Returns void

      Returns void

Throws

listener must be a function

offset: {
    left: number;
    top: number;
}

Type declaration

  • left: number
  • top: number
on: (<TData, TType>(type, listener, thisArg?) => void)

Add new event listener

Type declaration

    • <TData, TType>(type, listener, thisArg?): void
    • Type Parameters

      • TData = unknown
      • TType extends string = string

      Parameters

      • type: TType

        Event type. If the event type is xAPI, the listener's event parameter will be of type XAPIEvent. Any dispatched XAPIEvent will trigger the xAPI event.

      • listener: EventListener<TData, TType>

        Event listener

      • Optional thisArg: ThisType<unknown>

        Optionally specify the listener's this value

      Returns void

Throws

listener must be a function

once: (<TData, TType>(type, listener, thisArg?) => void)

Add new event listener that will be fired only once

Type declaration

    • <TData, TType>(type, listener, thisArg?): void
    • Type Parameters

      • TData = unknown
      • TType extends string = string

      Parameters

      • type: TType

        Event type. If the event type is xAPI, the listener's event parameter will be of type XAPIEvent. Any dispatched XAPIEvent will trigger the xAPI event.

      • listener: EventListener<TData, TType>

        Event listener

      • Optional thisArg: ThisType<unknown>

        Optionally specify the listener's this value

      Returns void

Throws

listener must be a function

params: TParams
parent: H5PForm | IH5PFieldInstance<unknown, H5PField>
passReadies: boolean
readies: unknown[]
ready: ((callback) => void)

Type declaration

    • (callback): void
    • Parameters

      • callback: (() => void)
          • (): void
          • Returns void

      Returns void

removeLanguages: ((libraryName, languageCodes) => void)

Type declaration

    • (libraryName, languageCodes): void
    • Parameters

      • libraryName: string
      • languageCodes: (undefined | string)[]

      Returns void

trigger: (<TData>(event, eventData?, extras?) => void)

Dispatch event

Type declaration

    • <TData>(event, eventData?, extras?): void
    • Type Parameters

      • TData = unknown

      Parameters

      • event: unknown

        Event object or event type as string

      • Optional eventData: TData

        Custom event data (used when event type as string is used as first argument)

      • Optional extras: {
            bubbles?: boolean;
            external?: boolean;
        }
        • Optional bubbles?: boolean
        • Optional external?: boolean

      Returns void

triggerXAPI: ((verb, extra) => void)

Type declaration

    • (verb, extra): void
    • Parameters

      Returns void

zebra: "odd" | "even"

Methods

  • Parameters

    Returns void

  • Returns void

  • Returns boolean