bitburner-src/utils/DialogBox.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-06-02 04:48:48 +02:00
import { KEY } from "./helpers/keyCodes";
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
*/
let dialogBoxes = [];
2019-04-13 09:26:49 +02:00
// Close dialog box when clicking outside
$(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();
}
}
});
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";
}
}
2019-04-13 09:26:49 +02:00
// Dialog box close buttons
$(document).on('click', '.dialog-box-close-button', function( event ) {
2019-06-02 04:48:48 +02:00
closeTopmostDialogBox();
});
document.addEventListener("keydown", function (event) {
if (event.keyCode == KEY.ESC && dialogBoxOpened) {
closeTopmostDialogBox();
event.preventDefault();
}
});
2019-04-13 09:26:49 +02:00
let dialogBoxOpened = false;
2018-02-18 04:08:54 +01:00
function dialogBoxCreate(txt, preformatted=false) {
var container = document.createElement("div");
container.setAttribute("class", "dialog-box-container");
var content = document.createElement("div");
content.setAttribute("class", "dialog-box-content");
var closeButton = document.createElement("span");
closeButton.setAttribute("class", "dialog-box-close-button");
closeButton.innerHTML = "×"
2018-02-18 04:08:54 +01:00
var textE;
if (preformatted) {
// For text files as they are often computed data that
// shouldn't be wrapped and should retain tabstops.
textE = document.createElement("pre");
textE.innerHTML = txt;
} else {
textE = document.createElement("p");
textE.innerHTML = txt.replace(/(?:\r\n|\r|\n)/g, '<br>');
}
content.appendChild(closeButton);
content.appendChild(textE);
container.appendChild(content);
document.body.appendChild(container);
if (dialogBoxes.length >= 1) {
container.style.visibility = "hidden";
}
dialogBoxes.push(container);
setTimeout(function() {
dialogBoxOpened = true;
}, 400);
}
2017-09-19 20:38:03 +02:00
export {dialogBoxCreate, dialogBoxOpened};