All files / triggers load-trigger.ts

100% Statements 12/12
100% Branches 1/1
100% Functions 4/4
100% Lines 11/11

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85              94x   94x       94x                                                                                     94x       188x     3x                     4x 4x   4x 1x     3x      
/**
 * @module Triggers
 *
 * @categoryDescription Load
 * {@link LoadTrigger} allows you to run actions once when the page is loaded.
 */
 
import * as MH from "@lisn/globals/minification-helpers";
 
import { waitForPageReady } from "@lisn/utils/dom-events";
 
import { Action } from "@lisn/actions/action";
 
import {
  registerTrigger,
  Trigger,
  TriggerConfig,
} from "@lisn/triggers/trigger";
 
/**
 * {@link LoadTrigger} allows you to run actions one when the page is loaded.
 *
 * -------
 *
 * To use with auto-widgets (HTML API), see {@link registerTrigger} for the
 * specification.
 *
 * - Arguments: none
 * - Additional trigger options: none
 *
 * @example
 * Scroll to the given element when the page is loaded:
 *
 * ```html
 * <div data-lisn-on-load="@scroll-to"></div>
 * ```
 *
 * @example
 * Scroll to 100px above the given element 500ms after the page is loaded:
 *
 * ```html
 * <div data-lisn-on-load="@scroll-to: offsetY=-100 +delay=500"></div>
 * ```
 *
 * @example
 * Scroll to 100px above the given element 500ms after the page is loaded, and
 * play animations defined on it 500ms later (1000ms after it's loaded):
 *
 * ```html
 * <div data-lisn-on-load="@scroll-to: offsetY=-100 +delay=500 ;
 *                         @animate +delay=1000"
 * ></div>
 * ```
 *
 * @category Load
 */
export class LoadTrigger extends Trigger {
  readonly getConfig: () => TriggerConfig;
 
  static register() {
    registerTrigger(
      "load",
      (element, args, actions, config) =>
        new LoadTrigger(element, actions, config),
    );
  }
 
  /**
   * If no actions are supplied, nothing is done.
   *
   * @throws {@link Errors.LisnUsageError | LisnUsageError}
   *                If the config is invalid.
   */
  constructor(element: Element, actions: Action[], config: TriggerConfig) {
    super(element, actions, config);
    this.getConfig = () => MH.copyObject(config);
 
    if (!MH.lengthOf(actions)) {
      return;
    }
 
    waitForPageReady().then(this.run);
  }
}