All files / actions animate-play.ts

100% Statements 36/36
100% Branches 7/7
100% Functions 12/12
100% Lines 34/34

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                      94x   94x       94x           94x                                                 94x                                 188x       2x     2x   2x 2x 2x                                                     94x                                 188x       2x     2x   2x 2x 2x               94x 94x 94x   94x 4x 6x 4x 4x       94x         16x                       16x 2x     16x 16x 8x   8x            
/**
 * @module Actions
 *
 * @categoryDescription Animation
 * {@link AnimatePlay} and {@link AnimatePause} resume or pause all animations
 * on the given element. They work with CSS or Web Animations.
 *
 * {@link Actions.Animate | Animate} plays or reverses all animations on the
 * given element. It works with CSS or Web Animations.
 */
 
import * as MC from "@lisn/globals/minification-constants";
 
import {
  iterateAnimations,
  resetCssAnimationsNow,
} from "@lisn/utils/animations";
import {
  hasClass,
  addClassesNow,
  removeClassesNow,
} from "@lisn/utils/css-alter";
 
import { Action, registerAction } from "@lisn/actions/action";
 
/**
 * Resumes or pauses all animations on the given element.
 *
 * It works with CSS or Web Animations.
 *
 * **IMPORTANT:** When constructed, it resets and pauses the animations as a
 * form of initialization.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "animate-play".
 * - Arguments: none
 * - Options: none
 *
 * @example
 * ```html
 * <button id="btn">Play/pause</button>
 * <div data-lisn-on-click="@animate-play +target=#btn"></div>
 * ```
 *
 * @category Animation
 */
export class AnimatePlay implements Action {
  /**
   * Resumes the animations without resetting them.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Pauses the animations without resetting them.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Resumes the animations if paused, otherwise pauses them.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction("animate-play", (element) => new AnimatePlay(element));
  }
 
  constructor(element: Element) {
    const { _play, _pause, _toggle } = getMethods(element);
 
    // initial state is 0% and paused
    animate(element, PAUSE, true);
 
    this.do = _play;
    this.undo = _pause;
    this[MC.S_TOGGLE] = _toggle;
  }
}
 
/**
 * Pauses or resumes all animations on the given element.
 *
 * It works with CSS or Web Animations.
 *
 * **IMPORTANT:** When constructed, it plays the animations as a form of
 * initialization.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "animate-pause".
 * - Arguments: none
 * - Options: none
 *
 * @example
 * ```html
 * <button id="btn">Play/pause</button>
 * <div data-lisn-on-click="@animate-pause +target=#btn"></div>
 * ```
 *
 * @category Animation
 */
export class AnimatePause implements Action {
  /**
   * Pauses the animations without resetting them.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Resumes the animations without resetting them.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Resumes the animations if paused, otherwise pauses them.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction("animate-pause", (element) => new AnimatePause(element));
  }
 
  constructor(element: Element) {
    const { _play, _pause, _toggle } = getMethods(element);
 
    // Initial state is playing
    _play();
 
    this.do = _pause;
    this.undo = _play;
    this[MC.S_TOGGLE] = _toggle;
  }
}
 
// --------------------
 
type AnimateAction = typeof PLAY | typeof PAUSE | typeof TOGGLE;
 
const PLAY = 0;
const PAUSE = 1;
const TOGGLE = 2;
 
const getMethods = (element: Element) => {
  return {
    _play: () => animate(element, PLAY),
    _pause: () => animate(element, PAUSE),
    _toggle: () => animate(element, TOGGLE),
  };
};
 
const animate = (
  element: Element,
  action: AnimateAction,
  isInitial = false,
) => {
  return iterateAnimations(
    element,
    /* istanbul ignore next */ // jsdom doesn't support Web Animations
    (animation) => {
      const isPaused = animation.playState === "paused";
      if (action === PLAY || (isPaused && action === TOGGLE)) {
        animation.play();
      } else {
        animation.pause();
      }
    },
    (element) => {
      if (isInitial) {
        resetCssAnimationsNow(element);
      }
 
      const isPaused = hasClass(element, MC.PREFIX_ANIMATE_PAUSE);
      if (action === PLAY || (isPaused && action === TOGGLE)) {
        removeClassesNow(element, MC.PREFIX_ANIMATE_PAUSE);
      } else {
        addClassesNow(element, MC.PREFIX_ANIMATE_PAUSE);
      }
    },
    isInitial,
  );
};