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;
|
2019-01-27 23:08:45 +01:00
|
|
|
max?: string;
|
2018-07-05 06:38:00 +02:00
|
|
|
maxLength?: number;
|
2019-01-27 23:08:45 +01:00
|
|
|
min?: string;
|
2018-07-05 06:38:00 +02:00
|
|
|
name?: string;
|
|
|
|
pattern?: string;
|
|
|
|
placeholder?: string;
|
2019-01-27 23:08:45 +01:00
|
|
|
step?: string;
|
2018-07-05 06:38:00 +02:00
|
|
|
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;
|
2018-09-23 02:25:48 +02:00
|
|
|
onkeydown?(this: HTMLElement, ev: KeyboardEvent): any;
|
2018-07-05 06:38:00 +02:00
|
|
|
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;
|
2018-12-16 00:31:21 +01:00
|
|
|
overflow?: string;
|
2018-07-05 06:38:00 +02:00
|
|
|
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-10-23 20:55:42 +02:00
|
|
|
tooltipsmall?: string;
|
2018-12-22 11:27:04 +01:00
|
|
|
tooltiplow?: string;
|
2018-07-05 06:38:00 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2019-01-27 23:08:45 +01:00
|
|
|
if (params.max !== undefined) {
|
|
|
|
el.max = params.max;
|
|
|
|
}
|
|
|
|
if (params.min !== undefined) {
|
|
|
|
el.min = params.min;
|
|
|
|
}
|
|
|
|
if (params.step !== undefined) {
|
|
|
|
el.step = params.step;
|
|
|
|
}
|
2018-07-05 06:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-23 02:25:48 +02:00
|
|
|
if (params.onkeydown !== undefined) {
|
|
|
|
el.addEventListener("keydown", params.onkeydown);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2018-12-16 00:31:21 +01:00
|
|
|
if (params.overflow !== undefined) {
|
|
|
|
el.style.overflow = params.overflow;
|
|
|
|
}
|
2018-07-05 06:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-23 20:55:42 +02:00
|
|
|
} else if (params.tooltipsmall !== undefined) {
|
|
|
|
el.className += " tooltip";
|
|
|
|
el.appendChild(createElement("span", {
|
|
|
|
class: "tooltiptext smallfont",
|
|
|
|
innerHTML: params.tooltipsmall,
|
2018-12-22 11:27:04 +01:00
|
|
|
}));
|
|
|
|
} else if (params.tooltiplow !== undefined) {
|
|
|
|
el.className += "tooltip";
|
|
|
|
el.appendChild(createElement("span", {
|
|
|
|
class: "tooltiptextlow",
|
|
|
|
innerHTML: params.tooltiplow,
|
|
|
|
}));
|
2018-07-05 06:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|