All files / actions pager.ts

97.19% Statements 104/107
76.92% Branches 10/13
100% Functions 47/47
96.66% Lines 87/90

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378                            94x 94x   94x   94x 94x   94x                                       94x                                   94x       7x 7x   7x 6x 6x     7x 5x 5x     7x 13x 13x 13x                                             94x                                   94x       7x 7x   7x 6x 6x     7x 6x 6x     7x 13x 13x 13x                                               94x                                     94x   1x         7x       7x   7x 7x 13x                                                   94x                                   94x   1x         3x       3x 3x   3x 3x 3x                                                   94x                                   94x   1x         3x       3x 3x   3x 3x 3x           94x 27x   27x 27x   27x         25x   25x 25x 11x     25x 21x   18x     21x       27x 9x 27x 9x 27x 6x   27x   27x 25x 24x     25x     9x   9x   6x      
/**
 * @module Actions
 *
 * @categoryDescription Controlling pagers
 * {@link NextPage} and {@link PrevPage} advance or reverse the {@link Pager}
 * widget setup for the given element.
 *
 * {@link GoToPage} sets the {@link Pager} to the given page or toggles to the
 * last saved one.
 *
 * {@link EnablePage} and {@link DisablePage} enable or disable the given page
 * of the {@link Pager} widget setup for the given element.
 */
 
import * as MC from "@lisn/globals/minification-constants";
import * as MH from "@lisn/globals/minification-helpers";
 
import { toInt } from "@lisn/utils/math";
 
import { Pager } from "@lisn/widgets/pager";
import { fetchUniqueWidget } from "@lisn/widgets/widget";
 
import { Action, registerAction } from "@lisn/actions/action";
 
/**
 * Advances or reverses the {@link Pager} widget setup for the given element.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "next-page".
 * - Arguments: none
 * - Options: none
 *
 * @example
 * ```html
 * <div class="lisn-pager" data-lisn-on-click="@next-page +target=#myNextButton"></div>
 * <button id="myNextButton"></button>
 * ```
 *
 * @category Controlling pagers
 */
export class NextPage implements Action {
  /**
   * Advances the pager by one page.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Reverses the pager by one page.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Undoes the last action: reverses the pager if {@link do} was last called
   * or advances it otherwise.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction("next-page", (element) => new NextPage(element));
  }
 
  constructor(element: Element) {
    let toggleState = false;
    const { _nextPage, _prevPage } = getMethods(element);
 
    this.do = () => {
      toggleState = true;
      return _nextPage();
    };
 
    this.undo = () => {
      toggleState = false;
      return _prevPage();
    };
 
    this[MC.S_TOGGLE] = () => {
      const method = toggleState ? _prevPage : _nextPage;
      toggleState = !toggleState;
      return method();
    };
  }
}
 
/**
 * Reverses or advances the {@link Pager} widget setup for the given element.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "prev-page".
 * - Arguments: none
 * - Options: none
 *
 * @example
 * ```html
 * <div class="lisn-pager" data-lisn-on-click="@prev-page +target=#myPrevButton"></div>
 * <button id="myPrevButton"></button>
 * ```
 *
 * @category Controlling pagers
 */
export class PrevPage implements Action {
  /**
   * Reverses the pager by one page.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Advances the pager by one page.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Undoes the last action: advances the pager if {@link do} was last called
   * or reverses it otherwise.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction("prev-page", (element) => new PrevPage(element));
  }
 
  constructor(element: Element) {
    let toggleState = false;
    const { _nextPage, _prevPage } = getMethods(element);
 
    this.do = () => {
      toggleState = true;
      return _prevPage();
    };
 
    this.undo = () => {
      toggleState = false;
      return _nextPage();
    };
 
    this[MC.S_TOGGLE] = () => {
      const method = toggleState ? _nextPage : _prevPage;
      toggleState = !toggleState;
      return method();
    };
  }
}
 
/**
 * Goes to a given page, or to the last one beforehand, of the {@link Pager}
 * widget setup for the given element.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "go-to-page".
 * - Arguments (required): the number of the target page
 * - Options: none
 *
 * @example
 * ```html
 * <div class="lisn-pager" data-lisn-on-click="@go-to-page:2 +target=#myGoToButton"></div>
 * <button id="myGoToButton"></button>
 * ```
 *
 * @category Controlling pagers
 */
export class GoToPage implements Action {
  /**
   * Goes to the page number given to the constructor. Numbers start at 1.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Goes to the last saved page number before the action was {@link do}ne. If
   * the action had never been done, does nothing.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Goes to the page number given to the constructor, or if already at this
   * number, goes to the last saved page if any. Numbers start at 1.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction(
      "go-to-page",
      (element, args) => new GoToPage(element, toInt(args[0])),
    );
  }
 
  constructor(element: Element, pageNum: number) {
    Iif (!pageNum) {
      throw MH.usageError("Target page is required");
    }
 
    const { _goToPage } = getMethods(element);
 
    this.do = () => _goToPage(pageNum);
    this.undo = () => _goToPage(-1);
    this[MC.S_TOGGLE] = () => _goToPage(pageNum, -1);
  }
}
 
/**
 * Enables or disables the given page of the {@link Pager} widget setup for the
 * given element.
 *
 * **IMPORTANT:** When constructed, it disables the given page as a form of
 * initialization.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "enable-page".
 * - Arguments (required): the number of the target page
 * - Options: none
 *
 * @example
 * ```html
 * <div class="lisn-pager" data-lisn-on-click="@enable-page:2 +target=#myEnableButton"></div>
 * <button id="myEnableButton"></button>
 * ```
 *
 * @category Controlling pagers
 */
export class EnablePage implements Action {
  /**
   * Enables the page number given to the constructor. Numbers start at 1.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Disables the page number given to the constructor. Numbers start at 1.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Enables the page number given to the constructor, if it is disabled,
   * otherwise disables it. Numbers start at 1.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction(
      "enable-page",
      (element, args) => new EnablePage(element, toInt(args[0])),
    );
  }
 
  constructor(element: Element, pageNum: number) {
    Iif (!pageNum) {
      throw MH.usageError("Target page number is required");
    }
 
    const { _enablePage, _disablePage, _togglePage } = getMethods(element);
    _disablePage(pageNum); // initial state
 
    this.do = () => _enablePage(pageNum);
    this.undo = () => _disablePage(pageNum);
    this[MC.S_TOGGLE] = () => _togglePage(pageNum);
  }
}
 
/**
 * Disables or enables the given page of the {@link Pager} widget setup for the
 * given element.
 *
 * **IMPORTANT:** When constructed, it enables the given page as a form of
 * initialization.
 *
 * -------
 *
 * To use with auto-widgets (HTML API) as part of a trigger specification:
 * - Action name: "disable-page".
 * - Arguments (required): the number of the target page
 * - Options: none
 *
 * @example
 * ```html
 * <button id="myDisableButton"></button>
 * <div class="lisn-pager" data-lisn-on-click="@disable-page:2 +target=#myDisableButton"></div>
 * ```
 *
 * @category Controlling pagers
 */
export class DisablePage implements Action {
  /**
   * Disables the page number given to the constructor. Numbers start at 1.
   */
  readonly do: () => Promise<void>;
 
  /**
   * Enables the page number given to the constructor. Numbers start at 1.
   */
  readonly undo: () => Promise<void>;
 
  /**
   * Disables the page number given to the constructor, if it is enabled,
   * otherwise enables it. Numbers start at 1.
   */
  readonly toggle: () => Promise<void>;
 
  static register() {
    registerAction(
      "disable-page",
      (element, args) => new DisablePage(element, toInt(args[0])),
    );
  }
 
  constructor(element: Element, pageNum: number) {
    Iif (!pageNum) {
      throw MH.usageError("Target page number is required");
    }
 
    const { _enablePage, _disablePage, _togglePage } = getMethods(element);
    _enablePage(pageNum); // initial state
 
    this.do = () => _disablePage(pageNum);
    this.undo = () => _enablePage(pageNum);
    this[MC.S_TOGGLE] = () => _togglePage(pageNum);
  }
}
 
// --------------------
 
const getMethods = (element: Element) => {
  let lastPageNum: number | null | undefined = null;
 
  const nextPage = (widget: Pager | null) => widget?.nextPage();
  const prevPage = (widget: Pager | null) => widget?.prevPage();
 
  const goToPage = async (
    widget: Pager | null,
    pageNum: number,
    altPageNum: number | null,
  ) => {
    const currentNum = widget?.getCurrentPageNum();
    let targetNum: number | null | undefined =
      currentNum === pageNum ? altPageNum : pageNum;
    if (targetNum === -1) {
      targetNum = lastPageNum;
    }
 
    if (targetNum) {
      if (pageNum !== -1) {
        // save the current number unless this is "undo"
        lastPageNum = currentNum;
      }
 
      await widget?.goToPage(targetNum);
    }
  };
 
  const enablePage = (widget: Pager | null, pageNum: number) =>
    widget?.enablePage(pageNum);
  const disablePage = (widget: Pager | null, pageNum: number) =>
    widget?.disablePage(pageNum);
  const togglePage = (widget: Pager | null, pageNum: number) =>
    widget?.togglePage(pageNum);
 
  const widgetPromise = fetchUniqueWidget("pager", element, Pager);
 
  return {
    _nextPage: () => widgetPromise.then(nextPage),
    _prevPage: () => widgetPromise.then(prevPage),
 
    _goToPage: (pageNum: number, altPageNum: number | null = null) =>
      widgetPromise.then((w) => goToPage(w, pageNum, altPageNum)),
 
    _enablePage: (pageNum: number) =>
      widgetPromise.then((w) => enablePage(w, pageNum)),
    _disablePage: (pageNum: number) =>
      widgetPromise.then((w) => disablePage(w, pageNum)),
    _togglePage: (pageNum: number) =>
      widgetPromise.then((w) => togglePage(w, pageNum)),
  };
};