mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Adds client side dialogs
This commit is contained in:
1 parent
b2acd9653b
commit
891aeb3281
46 files changed
+1013
-21
No files matched your search
@@ -0,0 +1,121 @@
|
||||
.dialog-modal-background {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: #000000;
|
||||
opacity: 0.6;
|
||||
z-index: 1998;
|
||||
}
|
||||
|
||||
.dialog-container {
|
||||
display: none;
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
z-index: 999;
|
||||
width: 400px;
|
||||
max-width: 90%;
|
||||
}
|
||||
|
||||
.dialog {
|
||||
background: linear-gradient(135deg, #404040, #232323);
|
||||
border-radius: 4px;
|
||||
border-top: 1px solid #505050;
|
||||
border-left: 1px solid #505050;
|
||||
border-right: 1px solid #000;
|
||||
border-bottom: 1px solid #000;
|
||||
box-shadow: 5px 5px 5px var(--shadow-color);
|
||||
overflow: hidden;
|
||||
animation: fadeIn 0.3s ease-out;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.dialog-content {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.dialog-header {
|
||||
padding-bottom: 15px;
|
||||
border-bottom: 1px solid #000;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.dialog-header h2 {
|
||||
margin: 0;
|
||||
color: var(--text-foreground);
|
||||
font-family: monospace;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.dialog-shine-effect {
|
||||
background: linear-gradient(to right, transparent, #fff, transparent);
|
||||
width: 100%;
|
||||
top: -1px;
|
||||
height: 1px;
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.dialog-body {
|
||||
padding: 20px;
|
||||
color: var(--text-foreground);
|
||||
font-family: monospace;
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.dialog-icon {
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.dialog-icon.info {
|
||||
background-image: url('path-to-info-icon.svg');
|
||||
}
|
||||
|
||||
.dialog-icon.warning {
|
||||
background-image: url('path-to-warning-icon.svg');
|
||||
}
|
||||
|
||||
.dialog-icon.error {
|
||||
background-image: url('path-to-error-icon.svg');
|
||||
}
|
||||
|
||||
.dialog-icon.question {
|
||||
background-image: url('path-to-question-icon.svg');
|
||||
}
|
||||
|
||||
.dialog-message {
|
||||
flex-grow: 1;
|
||||
word-wrap: break-word;
|
||||
overflow-wrap: break-word;
|
||||
word-break: break-word;
|
||||
hyphens: auto;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.dialog-buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding-top: 15px;
|
||||
gap: 10px;
|
||||
border-top: 1px solid #000;
|
||||
}
|
||||
|
||||
.dialog-button {
|
||||
padding: 2px 16px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
@keyframes dialogAppear {
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* Enum-like object for dialog buttons.
|
||||
* @readonly
|
||||
* @enum {string}
|
||||
*/
|
||||
const DialogButton = {
|
||||
OK: 'ok',
|
||||
CANCEL: 'cancel',
|
||||
YES: 'yes',
|
||||
NO: 'no',
|
||||
RETRY: 'retry',
|
||||
ABORT: 'abort',
|
||||
IGNORE: 'ignore'
|
||||
};
|
||||
|
||||
/**
|
||||
* Enum-like object for dialog icons.
|
||||
* @readonly
|
||||
* @enum {string}
|
||||
*/
|
||||
const DialogIcon = {
|
||||
NONE: 'none',
|
||||
INFO: 'info',
|
||||
WARNING: 'warning',
|
||||
ERROR: 'error',
|
||||
QUESTION: 'question'
|
||||
};
|
||||
|
||||
/**
|
||||
* Enum-like object for predefined button combinations.
|
||||
* @readonly
|
||||
* @enum {Array<DialogButton>}
|
||||
*/
|
||||
const DialogButtons = {
|
||||
OK: [DialogButton.OK],
|
||||
OK_CANCEL: [DialogButton.OK, DialogButton.CANCEL],
|
||||
YES_NO: [DialogButton.YES, DialogButton.NO],
|
||||
YES_NO_CANCEL: [DialogButton.YES, DialogButton.NO, DialogButton.CANCEL],
|
||||
RETRY_CANCEL: [DialogButton.RETRY, DialogButton.CANCEL],
|
||||
ABORT_RETRY_IGNORE: [DialogButton.ABORT, DialogButton.RETRY, DialogButton.IGNORE]
|
||||
};
|
||||
|
||||
/**
|
||||
* Manages the creation and display of modal dialogs.
|
||||
*/
|
||||
class DialogService {
|
||||
constructor() {
|
||||
this.dialogQueue = [];
|
||||
this.isDialogOpen = false;
|
||||
|
||||
// create dialog container inside the article element
|
||||
const articleElement = document.querySelector('article');
|
||||
this.container = document.createElement('div');
|
||||
this.container.className = 'dialog-container';
|
||||
articleElement.appendChild(this.container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a modal dialog and returns a Promise that resolves with the clicked button.
|
||||
* @param {string} message - The message to display in the dialog.
|
||||
* @param {string} title - The title of the dialog.
|
||||
* @param {Array<DialogButton>} buttons - Array of buttons to display.
|
||||
* @param {DialogIcon} icon - The icon to display.
|
||||
* @returns {Promise<DialogButton>} A promise that resolves with the clicked button.
|
||||
*/
|
||||
show(message, title = jsLocalizedDialogs.confirmation, buttons = DialogButtons.OK, icon = DialogIcon.NONE) {
|
||||
const dialogPromise = new Promise((resolve) => {
|
||||
const dialog = {
|
||||
message,
|
||||
title,
|
||||
buttons,
|
||||
icon,
|
||||
resolve
|
||||
};
|
||||
|
||||
this.dialogQueue.push(dialog);
|
||||
if (!this.isDialogOpen)
|
||||
this.showNextDialog();
|
||||
});
|
||||
|
||||
return dialogPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the next dialog in the queue.
|
||||
* @private
|
||||
*/
|
||||
showNextDialog() {
|
||||
if (this.dialogQueue.length === 0) {
|
||||
this.isDialogOpen = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this.isDialogOpen = true;
|
||||
const dialog = this.dialogQueue[0];
|
||||
this.displayDialog(dialog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and displays a dialog element in the DOM.
|
||||
* @private
|
||||
* @param {Object} dialog - The dialog configuration object.
|
||||
*/
|
||||
displayDialog(dialog) {
|
||||
const modalBackground = document.getElementById('modal-background-article');
|
||||
modalBackground.style.display = 'block';
|
||||
this.container.style.display = 'block';
|
||||
const dialogElement = document.createElement('div');
|
||||
dialogElement.className = 'dialog';
|
||||
|
||||
dialogElement.innerHTML = `
|
||||
<div class="dialog-content">
|
||||
<div class="dialog-header">
|
||||
<h2>${dialog.title}</h2>
|
||||
</div>
|
||||
<div class="dialog-body">
|
||||
${dialog.icon !== DialogIcon.NONE ? `<div class="dialog-icon ${dialog.icon}"></div>` : ''}
|
||||
<div class="dialog-message">${dialog.message}</div>
|
||||
</div>
|
||||
<div class="dialog-buttons">
|
||||
${dialog.buttons.map(button =>
|
||||
`<button class="dialog-button ${button === 'yes' || button === 'ok' ? 'confirm-button' : 'abort-button'}" data-button="${button}">
|
||||
${this.getLocalizedButtonText(button)}
|
||||
</button>`
|
||||
).join('')}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// add click handlers for buttons
|
||||
const buttons = dialogElement.querySelectorAll('.dialog-button');
|
||||
buttons.forEach(button => {
|
||||
button.addEventListener('click', () => this.handleButtonClick(button.dataset.button));
|
||||
});
|
||||
|
||||
// add keyboard support
|
||||
const keyHandler = (e) => {
|
||||
if (e.key === 'Escape' && dialog.buttons.includes(DialogButton.CANCEL))
|
||||
this.handleButtonClick(DialogButton.CANCEL);
|
||||
if (e.key === 'Enter' && dialog.buttons.includes(DialogButton.OK))
|
||||
this.handleButtonClick(DialogButton.OK);
|
||||
};
|
||||
document.addEventListener('keydown', keyHandler);
|
||||
|
||||
this.container.appendChild(dialogElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles button clicks and resolves the dialog promise.
|
||||
* @private
|
||||
* @param {DialogButton} buttonType - The type of button clicked.
|
||||
*/
|
||||
handleButtonClick(buttonType) {
|
||||
const currentDialog = this.dialogQueue.shift();
|
||||
const dialogElement = this.container.querySelector('.dialog');
|
||||
|
||||
if (dialogElement)
|
||||
dialogElement.remove();
|
||||
|
||||
if (this.dialogQueue.length === 0) {
|
||||
const modalBackground = document.getElementById('modal-background-article');
|
||||
modalBackground.style.display = 'none';
|
||||
this.container.style.display = 'none';
|
||||
}
|
||||
|
||||
currentDialog.resolve(buttonType);
|
||||
this.showNextDialog();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the localized text for a button type.
|
||||
* @private
|
||||
* @param {DialogButton} buttonType - The button type.
|
||||
* @returns {string} The localized button text.
|
||||
*/
|
||||
getLocalizedButtonText(buttonType) {
|
||||
return jsLocalizedDialogs[buttonType.toLowerCase()] || buttonType;
|
||||
}
|
||||
}
|
||||
|
||||
// create a global instance of the DialogService
|
||||
const dialogService = new DialogService();
|
||||
Reference in new issue
Block a user