2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options specific to creating an anchor ("<a>") element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementAnchorOptions {
|
|
|
|
href?: string;
|
|
|
|
target?: string;
|
|
|
|
text?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options specific to creating an input ("<input>") element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementInputOptions {
|
|
|
|
checked?: boolean;
|
|
|
|
maxLength?: number;
|
|
|
|
name?: string;
|
|
|
|
pattern?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
type?: string;
|
|
|
|
value?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options specific to creating a label ("<label>") element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementLabelOptions {
|
|
|
|
for?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options for setting up event listeners on the element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementListenerOptions {
|
|
|
|
changeListener?(this: HTMLElement, ev: Event): any;
|
|
|
|
clickListener?(this: HTMLElement, ev: MouseEvent): any;
|
|
|
|
inputListener?(this: HTMLElement, ev: Event): any;
|
|
|
|
onfocus?(this: HTMLElement, ev: FocusEvent): any;
|
|
|
|
onkeyup?(this: HTMLElement, ev: KeyboardEvent): any;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options for setting up the inline-styling of element.
|
|
|
|
* NOTE: Relying on CSS styling should be preferred over forcing the higher specificity via inline styles.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementStyleOptions {
|
|
|
|
backgroundColor?: string;
|
|
|
|
border?: string;
|
|
|
|
color?: string;
|
|
|
|
display?: string;
|
|
|
|
float?: string;
|
|
|
|
fontSize?: string;
|
|
|
|
margin?: string;
|
|
|
|
marginLeft?: string;
|
|
|
|
marginTop?: string;
|
|
|
|
padding?: string;
|
|
|
|
position?: string;
|
|
|
|
visibility?: string;
|
|
|
|
whiteSpace?: string;
|
|
|
|
width?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Options for adding an in-game tooltip to the element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementTooltipOptions {
|
|
|
|
tooltip?: string;
|
|
|
|
tooltipleft?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* All possible configuration options when creating an element.
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
interface ICreateElementOptions extends
|
|
|
|
ICreateElementStyleOptions,
|
|
|
|
ICreateElementListenerOptions,
|
|
|
|
ICreateElementInputOptions,
|
|
|
|
ICreateElementAnchorOptions,
|
|
|
|
ICreateElementLabelOptions,
|
|
|
|
ICreateElementTooltipOptions {
|
|
|
|
/**
|
|
|
|
* CSS Class(es) to initially set.
|
|
|
|
*/
|
|
|
|
class?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A (hopefully) unique identifier for the element.
|
|
|
|
*/
|
|
|
|
id?: string;
|
|
|
|
|
|
|
|
innerHTML?: string;
|
|
|
|
innerText?: string;
|
|
|
|
tabIndex?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setElementAnchor(el: HTMLAnchorElement, params: ICreateElementAnchorOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.text !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.text = params.text;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.href !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.href = params.href;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.target !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.target = params.target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setElementInput(el: HTMLInputElement, params: ICreateElementInputOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.name !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.name = params.name;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.value !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.value = params.value;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.type !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.type = params.type;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.checked !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.checked = params.checked;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.pattern !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.pattern = params.pattern;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.maxLength !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.maxLength = params.maxLength;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.placeholder !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.placeholder = params.placeholder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setElementLabel(el: HTMLLabelElement, params: ICreateElementLabelOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.for !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.htmlFor = params.for;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setElementListeners(el: HTMLElement, params: ICreateElementListenerOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
// tslint:disable:no-unbound-method
|
|
|
|
if (params.clickListener !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.addEventListener("click", params.clickListener);
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.inputListener !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.addEventListener("input", params.inputListener);
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.changeListener !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.addEventListener("change", params.changeListener);
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.onkeyup !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.addEventListener("keyup", params.onkeyup);
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.onfocus !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.addEventListener("focus", params.onfocus);
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
// tslint:enable:no-unbound-method
|
2018-07-05 06:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setElementStyle(el: HTMLElement, params: ICreateElementStyleOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.display !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.display = params.display;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.visibility !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.visibility = params.visibility;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.margin !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.margin = params.margin;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.marginLeft !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.marginLeft = params.marginLeft;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.marginTop !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.marginTop = params.marginTop;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.padding !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.padding = params.padding;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.color !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.color = params.color;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.border !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.border = params.border;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.float !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.cssFloat = params.float;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.fontSize !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.fontSize = params.fontSize;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.whiteSpace !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.whiteSpace = params.whiteSpace;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.width !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.width = params.width;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.backgroundColor !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.backgroundColor = params.backgroundColor;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.position !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.style.position = params.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setElementTooltip(el: HTMLElement, params: ICreateElementTooltipOptions) {
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.tooltip !== undefined && params.tooltip !== "") {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.className += " tooltip";
|
|
|
|
el.appendChild(createElement("span", {
|
|
|
|
class: "tooltiptext",
|
|
|
|
innerHTML: params.tooltip,
|
|
|
|
}));
|
2018-07-08 07:11:34 +02:00
|
|
|
} else if (params.tooltipleft !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.className += " tooltip";
|
|
|
|
el.appendChild(createElement("span", {
|
|
|
|
class: "tooltiptextleft",
|
|
|
|
innerHTML: params.tooltipleft,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* An all-in-one-call way of creating an element to be added to the DOM at some point.
|
|
|
|
* @param tagName The HTML tag/element name
|
|
|
|
* @param params Additional parameters to set on the element
|
|
|
|
*/
|
2018-07-05 06:38:00 +02:00
|
|
|
export function createElement(tagName: string, params: ICreateElementOptions = {}) {
|
|
|
|
const el: HTMLElement = document.createElement(tagName);
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.id !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.id = params.id;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.class !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.className = params.class;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.innerHTML !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.innerHTML = params.innerHTML;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.innerText !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.innerText = params.innerText;
|
|
|
|
}
|
2018-07-08 07:11:34 +02:00
|
|
|
if (params.tabIndex !== undefined) {
|
2018-07-05 06:38:00 +02:00
|
|
|
el.tabIndex = params.tabIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
setElementAnchor(el as HTMLAnchorElement, params);
|
|
|
|
setElementInput(el as HTMLInputElement, params);
|
|
|
|
setElementLabel(el as HTMLLabelElement, params);
|
|
|
|
setElementListeners(el, params);
|
|
|
|
setElementStyle(el, params);
|
|
|
|
setElementTooltip(el, params);
|
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|