/**
* @fileoverview Chahua Database Manager - UI Helper Module
* @description ฟังก์ชันช่วยเหลือสำหรับการจัดการ User Interface และ UX interactions
* คุณสมบัติ: Modal management, Loading states, Toast notifications, UI utilities
* @author ทีมพัฒนา Chahua
* @version 1.0.0
* @since 2024-01-01
* @calledBy app.js, database.js, query-editor.js, renderer components
* @dependencies
* - DOM elements: Modal containers, loading overlay, toast container
* - CSS classes: Modal states, loading animations, toast styles
* @features
* - Modal dialog management (show/hide)
* - Loading overlay with custom messages
* - Toast notification system (success, error, warning, info)
* - UI animation and transition helpers
* - Responsive design utilities
* @security
* - XSS protection in dynamic content
* - Safe HTML content insertion
* - DOM manipulation validation
* @example
* // Show loading state
* UIHelpers.showLoading('กำลังเชื่อมต่อ...');
*
* // Show success toast
* UIHelpers.showToast('success', 'สำเร็จ', 'บันทึกข้อมูลแล้ว');
*
* // Show modal
* UIHelpers.showModal('connectionModal');
*/
// UI Helper functions
/**
* UI Helpers - Static utility class for user interface management
* @class UIHelpers
* @description Provides static methods for common UI operations and user feedback
* @features
* - Modal dialog control
* - Loading state management
* - Toast notification system
* - Animation and transition helpers
*/
class UIHelpers {
/**
* Show modal dialog
* @static
* @function showModal
* @param {string} modalId - ID of the modal element to show
* @description Displays a modal dialog by adding active class
*/
static showModal(modalId) {
document.getElementById(modalId).classList.add('active');
}
/**
* Hide modal dialog
* @static
* @function hideModal
* @param {string} modalId - ID of the modal element to hide
* @description Hides a modal dialog by removing active class
*/
static hideModal(modalId) {
document.getElementById(modalId).classList.remove('active');
}
/**
* Show loading overlay
* @static
* @function showLoading
* @param {string} [message='Loading...'] - Loading message to display
* @description Shows loading overlay with custom message
*/
static showLoading(message = 'Loading...') {
const overlay = document.getElementById('loadingOverlay');
if (overlay) {
overlay.querySelector('.loading-text').textContent = message;
overlay.classList.add('active');
}
}
/**
* Hide loading overlay
* @static
* @function hideLoading
* @description Hides the loading overlay
*/
static hideLoading() {
const overlay = document.getElementById('loadingOverlay');
if (overlay) {
overlay.classList.remove('active');
}
}
/**
* Show toast notification
* @static
* @function showToast
* @param {string} type - Toast type (success, error, warning, info)
* @param {string} title - Toast title
* @param {string} message - Toast message content
* @description Creates and displays a toast notification with auto-dismiss
* @features
* - Multiple toast types with different styling
* - Auto-dismiss after 5 seconds
* - Manual close button
* - Stacking support for multiple toasts
*/
static showToast(type, title, message) {
const container = document.getElementById('toastContainer');
if (!container) return;
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
toast.innerHTML = `
`;
const closeBtn = toast.querySelector('.toast-close');
closeBtn.addEventListener('click', () => {
UIHelpers.removeToast(toast);
});
container.appendChild(toast);
// Auto remove after 5 seconds
setTimeout(() => {
if (toast.parentNode) {
UIHelpers.removeToast(toast);
}
}, 5000);
}
static removeToast(toast) {
toast.classList.add('removing');
setTimeout(() => {
if (toast.parentNode) {
toast.parentNode.removeChild(toast);
}
}, 300);
}
static createTable(columns, rows) {
const table = document.createElement('table');
table.className = 'data-table';
// Create header
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
columns.forEach(column => {
const th = document.createElement('th');
th.textContent = column;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// Create body
const tbody = document.createElement('tbody');
rows.forEach(row => {
const tr = document.createElement('tr');
row.forEach(cell => {
const td = document.createElement('td');
if (cell === 'NULL' || cell === null) {
td.className = 'null-value';
td.textContent = 'NULL';
} else {
td.textContent = cell;
td.title = cell; // Tooltip for long content
}
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
return table;
}
static updateConnectionStatus(status, message) {
const indicator = document.getElementById('statusIndicator');
if (!indicator) return;
const dot = indicator.querySelector('.status-dot');
const text = indicator.querySelector('.status-text');
if (dot) dot.className = `status-dot ${status}`;
if (text) text.textContent = message;
}
static populateConnectionSelector(connections) {
const select = document.getElementById('connectionSelect');
if (!select) return;
select.innerHTML = '';
connections.forEach(conn => {
const option = document.createElement('option');
option.value = conn.id;
option.textContent = `${conn.name} (${conn.type})`;
select.appendChild(option);
});
}
static renderDatabaseTree(structure) {
const treeContainer = document.getElementById('databaseTree');
if (!treeContainer) return;
treeContainer.innerHTML = '';
if (!structure.tables || structure.tables.length === 0) {
treeContainer.innerHTML = '';
return;
}
// Create tables section
const tablesSection = document.createElement('div');
tablesSection.className = 'tree-section';
const tablesHeader = document.createElement('div');
tablesHeader.className = 'tree-header';
tablesHeader.innerHTML = `Tables (${structure.tables.length})`;
tablesSection.appendChild(tablesHeader);
structure.tables.forEach(table => {
const tableItem = document.createElement('div');
tableItem.className = 'tree-item';
tableItem.innerHTML = `
[TBL]
${table.name}
${table.rowCount || 0} rows
`;
tableItem.addEventListener('click', () => {
UIHelpers.showToast('info', 'Table Selected', `Selected table: ${table.name}`);
});
tablesSection.appendChild(tableItem);
});
treeContainer.appendChild(tablesSection);
// Add views if available
if (structure.views && structure.views.length > 0) {
const viewsSection = document.createElement('div');
viewsSection.className = 'tree-section';
const viewsHeader = document.createElement('div');
viewsHeader.className = 'tree-header';
viewsHeader.innerHTML = `Views (${structure.views.length})`;
viewsSection.appendChild(viewsHeader);
structure.views.forEach(view => {
const viewItem = document.createElement('div');
viewItem.className = 'tree-item';
viewItem.innerHTML = `
[SVC]
${view.name}
`;
viewItem.addEventListener('click', () => {
UIHelpers.showToast('info', 'View Selected', `Selected view: ${view.name}`);
});
viewsSection.appendChild(viewItem);
});
treeContainer.appendChild(viewsSection);
}
}
}
// Make it available globally
if (typeof window !== 'undefined') {
window.UIHelpers = UIHelpers;
}
// Add horizontal splitter functionality
function initializeHorizontalSplitter() {
const splitter = document.querySelector('.splitter');
const queryPanel = document.querySelector('.query-panel');
const resultsPanel = document.querySelector('.results-panel');
const container = document.querySelector('.horizontal-split-container');
if (!splitter || !queryPanel || !resultsPanel || !container) {
return;
}
let isResizing = false;
splitter.addEventListener('mousedown', (e) => {
isResizing = true;
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
e.preventDefault();
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
const containerRect = container.getBoundingClientRect();
const mouseX = e.clientX - containerRect.left;
const containerWidth = containerRect.width;
// Calculate percentage, constrain between 20% and 80%
let percentage = (mouseX / containerWidth) * 100;
percentage = Math.max(20, Math.min(80, percentage));
queryPanel.style.width = `${percentage}%`;
resultsPanel.style.width = `${100 - percentage}%`;
e.preventDefault();
});
document.addEventListener('mouseup', () => {
if (isResizing) {
isResizing = false;
document.body.style.cursor = '';
document.body.style.userSelect = '';
}
});
// Double-click to reset to 50/50 split
splitter.addEventListener('dblclick', () => {
queryPanel.style.width = '50%';
resultsPanel.style.width = '50%';
});
}
// Initialize UI components
document.addEventListener('DOMContentLoaded', function() {
console.log('UI initialized');
initializeHorizontalSplitter();
});