OMD Documentation

omdPopup

The omdPopup class handles the creation and management of interactive popups for node overlays. These popups can be used for text input or for drawing with a pen tool, often integrated with a transcription service for converting handwriting to text.

Class Definition

export class omdPopup

Constructor

new omdPopup(targetNode, parentElement, [options])

Creates a new omdPopup instance.

Public Properties

Public Methods

show(x, y)

Creates and displays the popup at the specified coordinates with an animation.

hide()

Hides the popup with an animation and performs cleanup.

toggle(x, y)

Toggles the visibility of the popup. If visible, it hides; otherwise, it shows.

setValidationCallback(callback)

Sets the callback function to be invoked when the user submits their input (e.g., by clicking the submit button).

setClearCallback(callback)

Sets the callback function to be invoked when the user clears the input (e.g., by clicking the clear button).

getValue()

Retrieves the current input value from the popup. If in pen mode, it returns the last transcribed text and then clears it.

setValue(value)

Sets the input value of the popup's text field.

switchToMode(mode)

Switches the popup between 'text' and 'pen' input modes. This updates the visible input area and button states.

flashValidation(isValid)

Flashes the popup background (and associated elements) to visually indicate whether the user's input was valid or invalid.

areExpressionsEquivalent(expr1, expr2)

Compares two mathematical expressions for equivalence. It uses math.js to simplify and evaluate expressions with random variable assignments to robustly check for mathematical equality.

destroy()

Completely destroys the popup, removing all associated DOM elements, event listeners, and internal references. This method calls hide() internally to ensure a clean animated exit.

repositionCanvasRelativeToPopup()

Manually triggers an update to the pen canvas's position to ensure it remains correctly aligned with the popup, especially after layout changes.

Internal Methods

Modes

The omdPopup has two primary interaction modes:

Users can switch between modes using the 'T' (Text) and 'P' (Pen) buttons on the popup.

Example Usage

import { omdPopup } from '@teachinglab/omd';
import { omdNode } from '@teachinglab/omd'; // Assuming you have an omdNode instance
import { jsvgGroup } from '@teachinglab/jsvg'; // Assuming a parent jsvgElement

// Create a dummy node and parent element for demonstration
const targetNode = new omdNode({});
const parentElement = new jsvgGroup();

// Create a popup for a node
const popup = new omdPopup(targetNode, parentElement);

// Set a validation callback
popup.setValidationCallback(() => {
    const value = popup.getValue();
    if (popup.areExpressionsEquivalent(value, 'correct')) {
        popup.flashValidation(true);
        popup.hide();
    } else {
        popup.flashValidation(false);
    }
});

// Set a clear callback
popup.setClearCallback(() => {
    console.log('Popup input cleared!');
});

// Show the popup at a specific position
// popup.show(100, 100);

// To add the parentElement to the DOM for actual display:
// document.body.appendChild(parentElement.svgObject);
↑ Top