All files / actions open.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 9/9
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                94x   94x 94x   94x                                     94x                                 94x       3x 3x 3x   3x   3x 3x 3x      
/**
 * @module Actions
 *
 * @categoryDescription Controlling openables
 * {@link Open} opens or closes the {@link Openable} widget setup for the given
 * element.
 */
 
import * as MC from "@lisn/globals/minification-constants";
 
import { Openable } from "@lisn/widgets/openable";
import { fetchUniqueWidget } from "@lisn/widgets/widget";
 
import { Action, registerAction } from "@lisn/actions/action";
 
/**
 * Opens or closes the {@link Openable} widget setup for the given element.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "open".
 * - Arguments: none
 * - Options: none
 *
 * @example
 * ```html
 * <div class="lisn-modal" data-lisn-on-view="@open +reference=top:50%"></div>
 * ```
 *
 * @category Controlling openables
 */
export class Open implements Action {
  /**
   * Opens the Openable widget setup for the element.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Closes the Openable widget setup for the element.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Toggles the Openable widget setup for the element.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction("open", (element) => new Open(element));
  }
 
  constructor(element: Element) {
    const open = (widget: Openable | null) => widget?.open();
    const close = (widget: Openable | null) => widget?.close();
    const toggle = (widget: Openable | null) => widget?.toggle();
 
    const widgetPromise = fetchUniqueWidget("openable", element, Openable);
 
    this.do = () => widgetPromise.then(open);
    this.undo = () => widgetPromise.then(close);
    this[MC.S_TOGGLE] = () => widgetPromise.then(toggle);
  }
}