2019-06-02 04:48:48 +02:00
|
|
|
import { KEY } from "./helpers/keyCodes";
|
2021-03-16 10:42:12 +01:00
|
|
|
import { DialogBox } from "./ui/DialogBox";
|
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
2019-06-02 04:48:48 +02:00
|
|
|
|
2019-04-13 09:26:49 +02:00
|
|
|
/**
|
|
|
|
* Create and display a pop-up dialog box.
|
|
|
|
* This dialog box does not allow for any interaction and should close when clicking
|
|
|
|
* outside of it
|
|
|
|
*/
|
2017-08-30 19:44:29 +02:00
|
|
|
let dialogBoxes = [];
|
2017-05-24 15:49:52 +02:00
|
|
|
|
2019-04-13 09:26:49 +02:00
|
|
|
// Close dialog box when clicking outside
|
2017-06-07 02:28:20 +02:00
|
|
|
$(document).click(function(event) {
|
|
|
|
if (dialogBoxOpened && dialogBoxes.length >= 1) {
|
|
|
|
if (!$(event.target).closest(dialogBoxes[0]).length){
|
2019-06-02 04:48:48 +02:00
|
|
|
closeTopmostDialogBox();
|
2017-05-24 15:49:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-02 04:48:48 +02:00
|
|
|
function closeTopmostDialogBox() {
|
|
|
|
if (!dialogBoxOpened || dialogBoxes.length === 0) return;
|
|
|
|
dialogBoxes[0].remove();
|
|
|
|
dialogBoxes.shift();
|
|
|
|
if (dialogBoxes.length == 0) {
|
|
|
|
dialogBoxOpened = false;
|
|
|
|
} else {
|
|
|
|
dialogBoxes[0].style.visibility = "visible";
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 02:28:20 +02:00
|
|
|
|
2019-04-13 09:26:49 +02:00
|
|
|
// Dialog box close buttons
|
2021-05-01 09:17:31 +02:00
|
|
|
$(document).on('click', '.dialog-box-close-button', function() {
|
2019-06-02 04:48:48 +02:00
|
|
|
closeTopmostDialogBox();
|
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener("keydown", function (event) {
|
|
|
|
if (event.keyCode == KEY.ESC && dialogBoxOpened) {
|
|
|
|
closeTopmostDialogBox();
|
|
|
|
event.preventDefault();
|
2017-06-07 02:28:20 +02:00
|
|
|
}
|
|
|
|
});
|
2017-02-03 23:05:59 +01:00
|
|
|
|
2019-04-13 09:26:49 +02:00
|
|
|
let dialogBoxOpened = false;
|
2017-02-03 23:05:59 +01:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
|
|
|
|
|
2021-03-16 10:42:12 +01:00
|
|
|
function dialogBoxCreate(txt, preformatted=false) {
|
|
|
|
const container = document.createElement("div");
|
|
|
|
container.setAttribute("class", "dialog-box-container");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-03-16 10:42:12 +01:00
|
|
|
let elem = txt;
|
|
|
|
if (typeof txt === 'string') {
|
|
|
|
if (preformatted) {
|
|
|
|
// For text files as they are often computed data that
|
|
|
|
// shouldn't be wrapped and should retain tabstops.
|
|
|
|
elem = <pre dangerouslySetInnerHTML={{ __html: txt }} />
|
|
|
|
} else {
|
|
|
|
elem = <p dangerouslySetInnerHTML={{ __html: txt.replace(/(?:\r\n|\r|\n)/g, '<br />') }} />
|
|
|
|
}
|
2018-02-18 04:08:54 +01:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-03-16 10:42:12 +01:00
|
|
|
ReactDOM.render(DialogBox(elem), container);
|
2017-06-01 06:17:50 +02:00
|
|
|
document.body.appendChild(container);
|
2017-06-07 02:28:20 +02:00
|
|
|
if (dialogBoxes.length >= 1) {
|
|
|
|
container.style.visibility = "hidden";
|
|
|
|
}
|
|
|
|
dialogBoxes.push(container);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-05-24 15:49:52 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
dialogBoxOpened = true;
|
2017-06-07 02:28:20 +02:00
|
|
|
}, 400);
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 20:38:03 +02:00
|
|
|
export {dialogBoxCreate, dialogBoxOpened};
|