69 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/**
 | 
						|
 * Confirmation Modal Manager
 | 
						|
 * Handles showing and hiding a Bootstrap modal for confirmations and prompts.
 | 
						|
 */
 | 
						|
class Confirmation {
 | 
						|
    constructor(modalId) {
 | 
						|
        this.modalElement = document.getElementById(modalId);
 | 
						|
        this.modal = new bootstrap.Modal(this.modalElement);
 | 
						|
        this.messageElement = this.modalElement.querySelector('#confirmationMessage');
 | 
						|
        this.inputElement = this.modalElement.querySelector('#confirmationInput');
 | 
						|
        this.confirmButton = this.modalElement.querySelector('#confirmButton');
 | 
						|
        this.titleElement = this.modalElement.querySelector('.modal-title');
 | 
						|
        this.currentResolver = null;
 | 
						|
    }
 | 
						|
 | 
						|
    _show(message, title, showInput = false, defaultValue = '') {
 | 
						|
        return new Promise((resolve) => {
 | 
						|
            this.currentResolver = resolve;
 | 
						|
            this.titleElement.textContent = title;
 | 
						|
            this.messageElement.textContent = message;
 | 
						|
 | 
						|
            if (showInput) {
 | 
						|
                this.inputElement.style.display = 'block';
 | 
						|
                this.inputElement.value = defaultValue;
 | 
						|
                this.inputElement.focus();
 | 
						|
            } else {
 | 
						|
                this.inputElement.style.display = 'none';
 | 
						|
            }
 | 
						|
 | 
						|
            this.confirmButton.onclick = () => this._handleConfirm(showInput);
 | 
						|
            this.modalElement.addEventListener('hidden.bs.modal', () => this._handleCancel(), { once: true });
 | 
						|
            
 | 
						|
            this.modal.show();
 | 
						|
        });
 | 
						|
    }
 | 
						|
 | 
						|
    _handleConfirm(isPrompt) {
 | 
						|
        if (this.currentResolver) {
 | 
						|
            const value = isPrompt ? this.inputElement.value : true;
 | 
						|
            this.currentResolver(value);
 | 
						|
            this._cleanup();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    _handleCancel() {
 | 
						|
        if (this.currentResolver) {
 | 
						|
            this.currentResolver(null); // Resolve with null for cancellation
 | 
						|
            this._cleanup();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    _cleanup() {
 | 
						|
        this.confirmButton.onclick = null;
 | 
						|
        this.modal.hide();
 | 
						|
        this.currentResolver = null;
 | 
						|
    }
 | 
						|
 | 
						|
    confirm(message, title = 'Confirmation') {
 | 
						|
        return this._show(message, title, false);
 | 
						|
    }
 | 
						|
 | 
						|
    prompt(message, defaultValue = '', title = 'Prompt') {
 | 
						|
        return this._show(message, title, true, defaultValue);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// Make it globally available
 | 
						|
window.ConfirmationManager = new Confirmation('confirmationModal');
 |