projects/wms-framework/src/lib/helpers/BrowserDocument.ts
Provides helper functions and properties for HTML document manipulation.
Properties |
|
Methods |
|
Accessors |
Private Static eventTokens |
Type : EventToken[]
|
Default value : []
|
Stores tokens for registered events. |
Static attachEvent | ||||||||||||||||||||
attachEvent(sender: Document, event: string, owner: unknown, handler: (args: any[]) => void)
|
||||||||||||||||||||
Adds an event handler to the given event.
Parameters :
Returns :
void
|
Static detachEvent | ||||||||||||||||||||
detachEvent(sender: Document, event: string, owner: unknown, handler: (args: any[]) => void)
|
||||||||||||||||||||
Removes an event handler from the given event.
Parameters :
Returns :
void
|
Static getSearch |
getSearch()
|
Gets the search part (query string) of the current URL.
Returns :
string
{string} The query string. |
queryString |
getqueryString()
|
Gets a dictionary with the parameters of the current query string.
Returns :
ISimpleDictionary<string, string>
|
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);
}
}
}