2019-03-26 05:38:57 +01:00
|
|
|
/**
|
|
|
|
* Creates a Close/Cancel button that is used for removing popups
|
|
|
|
*/
|
2018-12-24 14:02:10 +01:00
|
|
|
|
|
|
|
import { createElement } from "./createElement";
|
|
|
|
import { removeElement } from "./removeElement";
|
|
|
|
|
|
|
|
interface ICreatePopupCloseButtonOptions {
|
2018-12-25 08:55:54 +01:00
|
|
|
class?: string;
|
|
|
|
display?: string;
|
2018-12-24 14:02:10 +01:00
|
|
|
innerText?: string;
|
2018-12-25 08:55:54 +01:00
|
|
|
type?: string;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createPopupCloseButton(popup: Element | string, options: ICreatePopupCloseButtonOptions) {
|
|
|
|
let button: HTMLButtonElement;
|
|
|
|
|
|
|
|
function closePopupWithEscFn(e: any): void {
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
button.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
button = createElement("button", {
|
2018-12-25 08:55:54 +01:00
|
|
|
class: options.class ? options.class : "popup-box-button",
|
|
|
|
display: options.display ? options.display : "inline-block",
|
2018-12-24 14:02:10 +01:00
|
|
|
innerText: options.innerText == null ? "Cancel" : options.innerText,
|
|
|
|
clickListener: () => {
|
|
|
|
if (popup instanceof Element) {
|
|
|
|
removeElement(popup);
|
|
|
|
} else {
|
|
|
|
try {
|
2018-12-25 08:55:54 +01:00
|
|
|
const popupEl = document.getElementById(popup);
|
|
|
|
if (popupEl instanceof Element) {
|
|
|
|
removeElement(popupEl);
|
|
|
|
}
|
2018-12-24 14:02:10 +01:00
|
|
|
} catch(e) {
|
|
|
|
console.error(`createPopupCloseButton() threw: ${e}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.removeEventListener("keydown", closePopupWithEscFn);
|
2018-12-25 08:55:54 +01:00
|
|
|
return false;
|
2018-12-24 14:02:10 +01:00
|
|
|
},
|
|
|
|
}) as HTMLButtonElement;
|
|
|
|
|
|
|
|
document.addEventListener("keydown", closePopupWithEscFn);
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|