File

projects/wms-framework/src/lib/helpers/BrowserDocument.ts

Description

Stores information associated with an event handler.

Index

Properties
Methods

Constructor

constructor(sender: Document, event: string, owner: unknown, handler: (args: any[]) => void)

Creates an instance of EventToken.

Parameters :
Name Type Optional Description
sender Document No

The document from which the event originates.

event string No

The name of the event.

owner unknown No

The owner of the handler.

handler function No

The function to handle the event.

Properties

Public Readonly event
Type : string
The name of the event.
Public Readonly handler
Type : function
The function to handle the event.
Public Readonly listener
Type : function

The listener function which is directly added to the document event.

Public Readonly owner
Type : unknown
The owner of the handler.
Public Readonly sender
Type : Document
The document from which the event originates.

Methods

equals
equals(other: EventToken)

Checks if this event token is functionally the same as other event token.

Parameters :
Name Type Optional Description
other EventToken No

The other event token.

Returns : boolean

{boolean} true if both event tokens are equal, false otherwise.

import { ISimpleDictionary } from '../baseframework/ISimpleDictionary';
import { SimpleDictionary } from '../baseframework/SimpleDictionary';

/**
 * Stores information associated with an event handler.
 *
 * @class EventToken
 */
class EventToken {
  /**
   * The listener function which is directly added to the document event.
   *
   * @memberof EventToken
   */
  public readonly listener: (args: any) => void;

  /**
   * Creates an instance of EventToken.
   *
   * @param {Document} sender The document from which the event originates.
   * @param {string} event The name of the event.
   * @param {unknown} owner The owner of the handler.
   * @param {(...args: any[]) => void} handler The function to handle the event.
   * @memberof EventToken
   */
  constructor(
    public readonly sender: Document,
    public readonly event: string,
    public readonly owner: unknown,
    public readonly handler: (...args: any[]) => void
  ) {
    this.listener = (arg) => handler.call(owner, sender, arg);
  }

  /**
   * Checks if this event token is functionally the same as other event token.
   *
   * @param {EventToken} other The other event token.
   * @return {*}  {boolean} `true` if both event tokens are equal, `false` otherwise.
   * @memberof EventToken
   */
  equals(other: EventToken): boolean {
    return (
      this.sender === other.sender &&
      this.event === other.event &&
      this.owner === other.owner &&
      this.handler === other.handler
    );
  }
}

/**
 * Provides helper functions and properties for HTML document manipulation.
 *
 * @export
 * @class BrowserDocument
 */
// @dynamic()
export class BrowserDocument {
  /**
   * Stores tokens for registered events.
   *
   * @private
   * @static
   * @type {EventToken[]}
   * @memberof BrowserDocument
   */
  private static eventTokens: EventToken[] = [];

  /**
   * Gets a dictionary with the parameters of the current query string.
   *
   * @readonly
   * @static
   * @type {ISimpleDictionary<string, string>}
   * @memberof BrowserDocument
   */
  public static get queryString(): ISimpleDictionary<string, string> {
    const urlParams = new URLSearchParams(BrowserDocument.getSearch());
    const dict = new SimpleDictionary<string, string>();
    urlParams.forEach((value: string, key: string) =>
      dict.addEntry(key, value)
    );
    return dict;
  }

  /**
   * Gets the search part (query string) of the current URL.
   *
   * @private
   * @static
   * @return {*}  {string} The query string.
   * @memberof BrowserDocument
   */
  public static getSearch(): string {
    return window.location.search;
  }

  /**
   * Adds an event handler to the given event.
   *
   * @static
   * @param {Document} sender The document from which the event originates.
   * @param {string} event The name of the event.
   * @param {unknown} owner The owner of the handler.
   * @param {(...args: any[]) => void} handler The function to handle the event.
   * @memberof BrowserDocument
   */
  public static attachEvent(
    sender: Document,
    event: string,
    owner: unknown,
    handler: (...args: any[]) => void
  ): void {
    const token = new EventToken(sender, event, owner, handler);
    BrowserDocument.eventTokens.push(token);
    sender.addEventListener(event, token.listener);
  }

  /**
   * Removes an event handler from the given event.
   *
   * @static
   * @param {Document} sender The document from which the event originates.
   * @param {string} event The name of the event.
   * @param {unknown} owner The owner of the handler.
   * @param {(...args: any[]) => void} handler The function which used to handle the event.
   * @memberof BrowserDocument
   */
  public static detachEvent(
    sender: Document,
    event: string,
    owner: unknown,
    handler: (...args: any[]) => void
  ): void {
    const searchToken = new EventToken(sender, event, owner, handler);
    const idx = BrowserDocument.eventTokens.findIndex((x) =>
      x.equals(searchToken)
    );
    if (idx !== -1) {
      const token = BrowserDocument.eventTokens[idx];
      sender.removeEventListener(event, token.listener);
      BrowserDocument.eventTokens.splice(idx, 1);
    }
  }
}

result-matching ""

    No results matching ""