maybe good
This commit is contained in:
489
static/js/keyboard-shortcuts.js
Normal file
489
static/js/keyboard-shortcuts.js
Normal file
@@ -0,0 +1,489 @@
|
||||
/**
|
||||
* Keyboard Shortcuts for Delphi Consulting Group Database System
|
||||
* Replicates legacy Pascal system shortcuts for user familiarity
|
||||
*/
|
||||
|
||||
let keyboardShortcutsEnabled = true;
|
||||
|
||||
function initializeKeyboardShortcuts() {
|
||||
document.addEventListener('keydown', handleKeyboardShortcuts);
|
||||
console.log('Keyboard shortcuts initialized');
|
||||
}
|
||||
|
||||
function handleKeyboardShortcuts(event) {
|
||||
if (!keyboardShortcutsEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't process shortcuts if user is typing in input fields
|
||||
const activeElement = document.activeElement;
|
||||
const isInputField = ['INPUT', 'TEXTAREA', 'SELECT'].includes(activeElement.tagName) ||
|
||||
activeElement.contentEditable === 'true';
|
||||
|
||||
// Allow specific shortcuts even in input fields
|
||||
const allowedInInputs = ['F1', 'Escape'];
|
||||
const keyName = getKeyName(event);
|
||||
|
||||
if (isInputField && !allowedInInputs.includes(keyName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle shortcuts based on key combination
|
||||
const shortcut = getShortcutKey(event);
|
||||
|
||||
switch (shortcut) {
|
||||
// Help
|
||||
case 'F1':
|
||||
event.preventDefault();
|
||||
showHelp();
|
||||
break;
|
||||
|
||||
// Navigation shortcuts
|
||||
case 'Alt+C':
|
||||
event.preventDefault();
|
||||
navigateTo('/customers');
|
||||
break;
|
||||
case 'Alt+F':
|
||||
event.preventDefault();
|
||||
navigateTo('/files');
|
||||
break;
|
||||
case 'Alt+L':
|
||||
event.preventDefault();
|
||||
navigateTo('/financial');
|
||||
break;
|
||||
case 'Alt+D':
|
||||
event.preventDefault();
|
||||
navigateTo('/documents');
|
||||
break;
|
||||
case 'Alt+A':
|
||||
event.preventDefault();
|
||||
navigateTo('/admin');
|
||||
break;
|
||||
|
||||
// Global search
|
||||
case 'Ctrl+F':
|
||||
event.preventDefault();
|
||||
focusGlobalSearch();
|
||||
break;
|
||||
|
||||
// Form shortcuts
|
||||
case 'Ctrl+N':
|
||||
event.preventDefault();
|
||||
newRecord();
|
||||
break;
|
||||
case 'Ctrl+S':
|
||||
event.preventDefault();
|
||||
saveRecord();
|
||||
break;
|
||||
case 'F9':
|
||||
event.preventDefault();
|
||||
editMode();
|
||||
break;
|
||||
case 'F2':
|
||||
event.preventDefault();
|
||||
completeAction();
|
||||
break;
|
||||
case 'F8':
|
||||
event.preventDefault();
|
||||
clearForm();
|
||||
break;
|
||||
case 'Delete':
|
||||
if (!isInputField) {
|
||||
event.preventDefault();
|
||||
deleteRecord();
|
||||
}
|
||||
break;
|
||||
case 'Escape':
|
||||
event.preventDefault();
|
||||
cancelAction();
|
||||
break;
|
||||
|
||||
// Legacy system shortcuts
|
||||
case 'F10':
|
||||
event.preventDefault();
|
||||
showMenu();
|
||||
break;
|
||||
case 'Alt+M':
|
||||
event.preventDefault();
|
||||
showMemo();
|
||||
break;
|
||||
case 'Alt+T':
|
||||
event.preventDefault();
|
||||
toggleTimer();
|
||||
break;
|
||||
case 'Alt+B':
|
||||
event.preventDefault();
|
||||
showBalanceSummary();
|
||||
break;
|
||||
|
||||
// Quick creation shortcuts
|
||||
case 'Ctrl+Shift+C':
|
||||
event.preventDefault();
|
||||
newCustomer();
|
||||
break;
|
||||
case 'Ctrl+Shift+F':
|
||||
event.preventDefault();
|
||||
newFile();
|
||||
break;
|
||||
case 'Ctrl+Shift+T':
|
||||
event.preventDefault();
|
||||
newTransaction();
|
||||
break;
|
||||
|
||||
// Date navigation (legacy system feature)
|
||||
case '+':
|
||||
if (!isInputField && isDateField(activeElement)) {
|
||||
event.preventDefault();
|
||||
changeDateBy(1);
|
||||
}
|
||||
break;
|
||||
case '-':
|
||||
if (!isInputField && isDateField(activeElement)) {
|
||||
event.preventDefault();
|
||||
changeDateBy(-1);
|
||||
}
|
||||
break;
|
||||
|
||||
// Table navigation
|
||||
case 'ArrowUp':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('up');
|
||||
}
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('down');
|
||||
}
|
||||
break;
|
||||
case 'PageUp':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('pageup');
|
||||
}
|
||||
break;
|
||||
case 'PageDown':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('pagedown');
|
||||
}
|
||||
break;
|
||||
case 'Home':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('home');
|
||||
}
|
||||
break;
|
||||
case 'End':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
navigateTable('end');
|
||||
}
|
||||
break;
|
||||
case 'Enter':
|
||||
if (!isInputField && isInTable()) {
|
||||
event.preventDefault();
|
||||
openRecord();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function getShortcutKey(event) {
|
||||
const parts = [];
|
||||
|
||||
if (event.ctrlKey) parts.push('Ctrl');
|
||||
if (event.altKey) parts.push('Alt');
|
||||
if (event.shiftKey) parts.push('Shift');
|
||||
|
||||
let key = event.key;
|
||||
|
||||
// Handle special keys
|
||||
switch (event.keyCode) {
|
||||
case 112: key = 'F1'; break;
|
||||
case 113: key = 'F2'; break;
|
||||
case 114: key = 'F3'; break;
|
||||
case 115: key = 'F4'; break;
|
||||
case 116: key = 'F5'; break;
|
||||
case 117: key = 'F6'; break;
|
||||
case 118: key = 'F7'; break;
|
||||
case 119: key = 'F8'; break;
|
||||
case 120: key = 'F9'; break;
|
||||
case 121: key = 'F10'; break;
|
||||
case 122: key = 'F11'; break;
|
||||
case 123: key = 'F12'; break;
|
||||
case 46: key = 'Delete'; break;
|
||||
case 27: key = 'Escape'; break;
|
||||
case 33: key = 'PageUp'; break;
|
||||
case 34: key = 'PageDown'; break;
|
||||
case 35: key = 'End'; break;
|
||||
case 36: key = 'Home'; break;
|
||||
case 37: key = 'ArrowLeft'; break;
|
||||
case 38: key = 'ArrowUp'; break;
|
||||
case 39: key = 'ArrowRight'; break;
|
||||
case 40: key = 'ArrowDown'; break;
|
||||
case 13: key = 'Enter'; break;
|
||||
case 187: key = '+'; break; // Plus key
|
||||
case 189: key = '-'; break; // Minus key
|
||||
}
|
||||
|
||||
parts.push(key);
|
||||
return parts.join('+');
|
||||
}
|
||||
|
||||
function getKeyName(event) {
|
||||
switch (event.keyCode) {
|
||||
case 112: return 'F1';
|
||||
case 27: return 'Escape';
|
||||
default: return event.key;
|
||||
}
|
||||
}
|
||||
|
||||
// Navigation functions
|
||||
function navigateTo(url) {
|
||||
window.location.href = url;
|
||||
}
|
||||
|
||||
function focusGlobalSearch() {
|
||||
const searchInput = document.querySelector('#global-search, .search-input, [name="search"]');
|
||||
if (searchInput) {
|
||||
searchInput.focus();
|
||||
searchInput.select();
|
||||
} else {
|
||||
navigateTo('/search');
|
||||
}
|
||||
}
|
||||
|
||||
// Form action functions
|
||||
function newRecord() {
|
||||
const newBtn = document.querySelector('.btn-new, [data-action="new"], .btn-primary[href*="new"]');
|
||||
if (newBtn) {
|
||||
newBtn.click();
|
||||
} else {
|
||||
showToast('New record shortcut not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function saveRecord() {
|
||||
const saveBtn = document.querySelector('.btn-save, [data-action="save"], .btn-success[type="submit"]');
|
||||
if (saveBtn) {
|
||||
saveBtn.click();
|
||||
} else {
|
||||
// Try to submit the main form
|
||||
const form = document.querySelector('form.main-form, form');
|
||||
if (form) {
|
||||
form.submit();
|
||||
} else {
|
||||
showToast('Save shortcut not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function editMode() {
|
||||
const editBtn = document.querySelector('.btn-edit, [data-action="edit"]');
|
||||
if (editBtn) {
|
||||
editBtn.click();
|
||||
} else {
|
||||
showToast('Edit mode shortcut not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function completeAction() {
|
||||
const completeBtn = document.querySelector('.btn-complete, [data-action="complete"], .btn-primary');
|
||||
if (completeBtn) {
|
||||
completeBtn.click();
|
||||
} else {
|
||||
saveRecord(); // Fallback to save
|
||||
}
|
||||
}
|
||||
|
||||
function clearForm() {
|
||||
const clearBtn = document.querySelector('.btn-clear, [data-action="clear"]');
|
||||
if (clearBtn) {
|
||||
clearBtn.click();
|
||||
} else {
|
||||
// Clear all form inputs
|
||||
const form = document.querySelector('form');
|
||||
if (form) {
|
||||
form.reset();
|
||||
showToast('Form cleared', 'info');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function deleteRecord() {
|
||||
const deleteBtn = document.querySelector('.btn-delete, [data-action="delete"], .btn-danger');
|
||||
if (deleteBtn) {
|
||||
deleteBtn.click();
|
||||
} else {
|
||||
showToast('Delete shortcut not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function cancelAction() {
|
||||
// Close modals first
|
||||
const modal = document.querySelector('.modal.show');
|
||||
if (modal) {
|
||||
const bsModal = bootstrap.Modal.getInstance(modal);
|
||||
if (bsModal) {
|
||||
bsModal.hide();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Then try cancel buttons
|
||||
const cancelBtn = document.querySelector('.btn-cancel, [data-action="cancel"], .btn-secondary');
|
||||
if (cancelBtn) {
|
||||
cancelBtn.click();
|
||||
} else {
|
||||
window.history.back();
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy system specific functions
|
||||
function showHelp() {
|
||||
const helpModal = document.querySelector('#shortcutsModal');
|
||||
if (helpModal) {
|
||||
const modal = new bootstrap.Modal(helpModal);
|
||||
modal.show();
|
||||
} else {
|
||||
showToast('Press F1 to see keyboard shortcuts', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function showMenu() {
|
||||
// Toggle main navigation menu on mobile or show dropdown
|
||||
const navbarToggler = document.querySelector('.navbar-toggler');
|
||||
if (navbarToggler && !navbarToggler.classList.contains('collapsed')) {
|
||||
navbarToggler.click();
|
||||
} else {
|
||||
showToast('Menu (F10) - Use Alt+C, Alt+F, Alt+L, Alt+D for navigation', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function showMemo() {
|
||||
const memoBtn = document.querySelector('[data-action="memo"], .btn-memo');
|
||||
if (memoBtn) {
|
||||
memoBtn.click();
|
||||
} else {
|
||||
showToast('Memo function not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function toggleTimer() {
|
||||
const timerBtn = document.querySelector('[data-action="timer"], .btn-timer');
|
||||
if (timerBtn) {
|
||||
timerBtn.click();
|
||||
} else {
|
||||
showToast('Timer function not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
function showBalanceSummary() {
|
||||
const balanceBtn = document.querySelector('[data-action="balance"], .btn-balance');
|
||||
if (balanceBtn) {
|
||||
balanceBtn.click();
|
||||
} else {
|
||||
showToast('Balance summary not available on this page', 'info');
|
||||
}
|
||||
}
|
||||
|
||||
// Quick creation functions
|
||||
function newCustomer() {
|
||||
navigateTo('/customers/new');
|
||||
}
|
||||
|
||||
function newFile() {
|
||||
navigateTo('/files/new');
|
||||
}
|
||||
|
||||
function newTransaction() {
|
||||
navigateTo('/financial/new');
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
function isDateField(element) {
|
||||
if (!element) return false;
|
||||
return element.type === 'date' ||
|
||||
element.classList.contains('date-field') ||
|
||||
element.getAttribute('data-type') === 'date';
|
||||
}
|
||||
|
||||
function isInTable() {
|
||||
const activeElement = document.activeElement;
|
||||
return activeElement && (
|
||||
activeElement.closest('table') ||
|
||||
activeElement.classList.contains('table-row') ||
|
||||
activeElement.getAttribute('role') === 'gridcell'
|
||||
);
|
||||
}
|
||||
|
||||
function changeDateBy(days) {
|
||||
const activeElement = document.activeElement;
|
||||
if (isDateField(activeElement)) {
|
||||
const currentDate = new Date(activeElement.value || Date.now());
|
||||
currentDate.setDate(currentDate.getDate() + days);
|
||||
activeElement.value = currentDate.toISOString().split('T')[0];
|
||||
activeElement.dispatchEvent(new Event('change', { bubbles: true }));
|
||||
}
|
||||
}
|
||||
|
||||
function navigateTable(direction) {
|
||||
// Table navigation implementation would depend on the specific table structure
|
||||
showToast(`Table navigation: ${direction}`, 'info');
|
||||
}
|
||||
|
||||
function openRecord() {
|
||||
const activeElement = document.activeElement;
|
||||
const row = activeElement.closest('tr, .table-row');
|
||||
if (row) {
|
||||
const link = row.querySelector('a, [data-action="open"]');
|
||||
if (link) {
|
||||
link.click();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function showToast(message, type = 'info') {
|
||||
// Create toast element
|
||||
const toastHtml = `
|
||||
<div class="toast align-items-center text-white bg-${type}" role="alert" aria-live="assertive" aria-atomic="true">
|
||||
<div class="d-flex">
|
||||
<div class="toast-body">${message}</div>
|
||||
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Get or create toast container
|
||||
let toastContainer = document.querySelector('.toast-container');
|
||||
if (!toastContainer) {
|
||||
toastContainer = document.createElement('div');
|
||||
toastContainer.className = 'toast-container position-fixed top-0 end-0 p-3';
|
||||
document.body.appendChild(toastContainer);
|
||||
}
|
||||
|
||||
// Add toast
|
||||
const toastWrapper = document.createElement('div');
|
||||
toastWrapper.innerHTML = toastHtml;
|
||||
const toastElement = toastWrapper.firstElementChild;
|
||||
toastContainer.appendChild(toastElement);
|
||||
|
||||
// Show toast
|
||||
const toast = new bootstrap.Toast(toastElement, { delay: 3000 });
|
||||
toast.show();
|
||||
|
||||
// Remove toast element after it's hidden
|
||||
toastElement.addEventListener('hidden.bs.toast', () => {
|
||||
toastElement.remove();
|
||||
});
|
||||
}
|
||||
|
||||
// Export for use in other scripts
|
||||
window.keyboardShortcuts = {
|
||||
initialize: initializeKeyboardShortcuts,
|
||||
enable: () => { keyboardShortcutsEnabled = true; },
|
||||
disable: () => { keyboardShortcutsEnabled = false; },
|
||||
showHelp: showHelp
|
||||
};
|
||||
409
static/js/main.js
Normal file
409
static/js/main.js
Normal file
@@ -0,0 +1,409 @@
|
||||
/**
|
||||
* Main JavaScript for Delphi Consulting Group Database System
|
||||
*/
|
||||
|
||||
// Global application state
|
||||
const app = {
|
||||
token: localStorage.getItem('auth_token'),
|
||||
user: null,
|
||||
initialized: false
|
||||
};
|
||||
|
||||
// Initialize application
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initializeApp();
|
||||
});
|
||||
|
||||
async function initializeApp() {
|
||||
// Initialize keyboard shortcuts
|
||||
if (window.keyboardShortcuts) {
|
||||
window.keyboardShortcuts.initialize();
|
||||
}
|
||||
|
||||
// Initialize tooltips
|
||||
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
|
||||
tooltipTriggerList.map(function (tooltipTriggerEl) {
|
||||
return new bootstrap.Tooltip(tooltipTriggerEl);
|
||||
});
|
||||
|
||||
// Initialize popovers
|
||||
const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
|
||||
popoverTriggerList.map(function (popoverTriggerEl) {
|
||||
return new bootstrap.Popover(popoverTriggerEl);
|
||||
});
|
||||
|
||||
// Add form validation classes
|
||||
initializeFormValidation();
|
||||
|
||||
// Initialize API helpers
|
||||
setupAPIHelpers();
|
||||
|
||||
app.initialized = true;
|
||||
console.log('Delphi Database System initialized');
|
||||
}
|
||||
|
||||
// Form validation
|
||||
function initializeFormValidation() {
|
||||
// Add Bootstrap validation styles
|
||||
const forms = document.querySelectorAll('form.needs-validation');
|
||||
forms.forEach(form => {
|
||||
form.addEventListener('submit', function(event) {
|
||||
if (!form.checkValidity()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
form.classList.add('was-validated');
|
||||
});
|
||||
});
|
||||
|
||||
// Real-time validation for specific fields
|
||||
const requiredFields = document.querySelectorAll('input[required], select[required], textarea[required]');
|
||||
requiredFields.forEach(field => {
|
||||
field.addEventListener('blur', function() {
|
||||
validateField(field);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function validateField(field) {
|
||||
const isValid = field.checkValidity();
|
||||
field.classList.remove('is-valid', 'is-invalid');
|
||||
field.classList.add(isValid ? 'is-valid' : 'is-invalid');
|
||||
|
||||
// Show/hide custom feedback
|
||||
const feedback = field.parentNode.querySelector('.invalid-feedback');
|
||||
if (feedback) {
|
||||
feedback.classList.toggle('hidden', isValid);
|
||||
feedback.classList.toggle('visible', !isValid);
|
||||
}
|
||||
}
|
||||
|
||||
// API helpers
|
||||
function setupAPIHelpers() {
|
||||
// Set up default headers for all API calls
|
||||
window.apiHeaders = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
};
|
||||
|
||||
if (app.token) {
|
||||
window.apiHeaders['Authorization'] = `Bearer ${app.token}`;
|
||||
}
|
||||
}
|
||||
|
||||
// API utility functions
|
||||
async function apiCall(url, options = {}) {
|
||||
const config = {
|
||||
headers: { ...window.apiHeaders, ...options.headers },
|
||||
...options
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url, config);
|
||||
|
||||
if (response.status === 401) {
|
||||
// Token expired or invalid
|
||||
logout();
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({ detail: 'Request failed' }));
|
||||
throw new Error(errorData.detail || `HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('API call failed:', error);
|
||||
showNotification(`Error: ${error.message}`, 'error');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function apiGet(url) {
|
||||
return apiCall(url, { method: 'GET' });
|
||||
}
|
||||
|
||||
async function apiPost(url, data) {
|
||||
return apiCall(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
|
||||
async function apiPut(url, data) {
|
||||
return apiCall(url, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
|
||||
async function apiDelete(url) {
|
||||
return apiCall(url, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// Authentication functions
|
||||
function setAuthToken(token) {
|
||||
app.token = token;
|
||||
localStorage.setItem('auth_token', token);
|
||||
window.apiHeaders['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
function logout() {
|
||||
app.token = null;
|
||||
app.user = null;
|
||||
localStorage.removeItem('auth_token');
|
||||
delete window.apiHeaders['Authorization'];
|
||||
window.location.href = '/login';
|
||||
}
|
||||
|
||||
// Notification system
|
||||
function showNotification(message, type = 'info', duration = 5000) {
|
||||
const notificationContainer = getOrCreateNotificationContainer();
|
||||
|
||||
const notification = document.createElement('div');
|
||||
notification.className = `alert alert-${type} alert-dismissible fade show`;
|
||||
notification.setAttribute('role', 'alert');
|
||||
notification.innerHTML = `
|
||||
${message}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
`;
|
||||
|
||||
notificationContainer.appendChild(notification);
|
||||
|
||||
// Auto-dismiss after duration
|
||||
if (duration > 0) {
|
||||
setTimeout(() => {
|
||||
notification.remove();
|
||||
}, duration);
|
||||
}
|
||||
|
||||
return notification;
|
||||
}
|
||||
|
||||
function getOrCreateNotificationContainer() {
|
||||
let container = document.querySelector('#notification-container');
|
||||
if (!container) {
|
||||
container = document.createElement('div');
|
||||
container.id = 'notification-container';
|
||||
container.className = 'position-fixed top-0 end-0 p-3';
|
||||
container.classList.add('notification-container');
|
||||
document.body.appendChild(container);
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
// Loading states
|
||||
function showLoading(element, text = 'Loading...') {
|
||||
const spinner = `<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>`;
|
||||
const originalContent = element.innerHTML;
|
||||
element.innerHTML = `${spinner}${text}`;
|
||||
element.disabled = true;
|
||||
element.dataset.originalContent = originalContent;
|
||||
}
|
||||
|
||||
function hideLoading(element) {
|
||||
if (element.dataset.originalContent) {
|
||||
element.innerHTML = element.dataset.originalContent;
|
||||
delete element.dataset.originalContent;
|
||||
}
|
||||
element.disabled = false;
|
||||
}
|
||||
|
||||
// Table helpers
|
||||
function initializeDataTable(tableId, options = {}) {
|
||||
const table = document.getElementById(tableId);
|
||||
if (!table) return null;
|
||||
|
||||
// Add sorting capability
|
||||
const headers = table.querySelectorAll('th[data-sort]');
|
||||
headers.forEach(header => {
|
||||
header.classList.add('sortable-header');
|
||||
header.addEventListener('click', () => sortTable(table, header));
|
||||
});
|
||||
|
||||
// Add row selection if enabled
|
||||
if (options.selectable) {
|
||||
addRowSelection(table);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
function sortTable(table, header) {
|
||||
const columnIndex = Array.from(header.parentNode.children).indexOf(header);
|
||||
const sortType = header.dataset.sort;
|
||||
const tbody = table.querySelector('tbody');
|
||||
const rows = Array.from(tbody.querySelectorAll('tr'));
|
||||
|
||||
const isAscending = !header.classList.contains('sort-asc');
|
||||
|
||||
// Remove sort classes from all headers
|
||||
table.querySelectorAll('th').forEach(th => {
|
||||
th.classList.remove('sort-asc', 'sort-desc');
|
||||
});
|
||||
|
||||
// Add sort class to current header
|
||||
header.classList.add(isAscending ? 'sort-asc' : 'sort-desc');
|
||||
|
||||
rows.sort((a, b) => {
|
||||
const aValue = a.children[columnIndex].textContent.trim();
|
||||
const bValue = b.children[columnIndex].textContent.trim();
|
||||
|
||||
let comparison = 0;
|
||||
if (sortType === 'number') {
|
||||
comparison = parseFloat(aValue) - parseFloat(bValue);
|
||||
} else if (sortType === 'date') {
|
||||
comparison = new Date(aValue) - new Date(bValue);
|
||||
} else {
|
||||
comparison = aValue.localeCompare(bValue);
|
||||
}
|
||||
|
||||
return isAscending ? comparison : -comparison;
|
||||
});
|
||||
|
||||
// Re-append sorted rows
|
||||
rows.forEach(row => tbody.appendChild(row));
|
||||
}
|
||||
|
||||
function addRowSelection(table) {
|
||||
const tbody = table.querySelector('tbody');
|
||||
tbody.addEventListener('click', function(e) {
|
||||
const row = e.target.closest('tr');
|
||||
if (row && e.target.type !== 'checkbox') {
|
||||
row.classList.toggle('table-active');
|
||||
|
||||
// Trigger custom event
|
||||
const event = new CustomEvent('rowSelect', {
|
||||
detail: { row, selected: row.classList.contains('table-active') }
|
||||
});
|
||||
table.dispatchEvent(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Form helpers
|
||||
function serializeForm(form) {
|
||||
const formData = new FormData(form);
|
||||
const data = {};
|
||||
|
||||
for (let [key, value] of formData.entries()) {
|
||||
// Handle multiple values (checkboxes, multi-select)
|
||||
if (data.hasOwnProperty(key)) {
|
||||
if (!Array.isArray(data[key])) {
|
||||
data[key] = [data[key]];
|
||||
}
|
||||
data[key].push(value);
|
||||
} else {
|
||||
data[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function populateForm(form, data) {
|
||||
Object.keys(data).forEach(key => {
|
||||
const field = form.querySelector(`[name="${key}"]`);
|
||||
if (field) {
|
||||
if (field.type === 'checkbox' || field.type === 'radio') {
|
||||
field.checked = data[key];
|
||||
} else {
|
||||
field.value = data[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Search functionality
|
||||
function initializeSearch(searchInput, resultsContainer, searchFunction) {
|
||||
let searchTimeout;
|
||||
|
||||
searchInput.addEventListener('input', function() {
|
||||
clearTimeout(searchTimeout);
|
||||
const query = this.value.trim();
|
||||
|
||||
if (query.length < 2) {
|
||||
resultsContainer.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
searchTimeout = setTimeout(async () => {
|
||||
try {
|
||||
showLoading(resultsContainer, 'Searching...');
|
||||
const results = await searchFunction(query);
|
||||
displaySearchResults(resultsContainer, results);
|
||||
} catch (error) {
|
||||
resultsContainer.innerHTML = '<p class="text-danger">Search failed</p>';
|
||||
}
|
||||
}, 300);
|
||||
});
|
||||
}
|
||||
|
||||
function displaySearchResults(container, results) {
|
||||
if (!results || results.length === 0) {
|
||||
container.innerHTML = '<p class="text-muted">No results found</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
const resultsHtml = results.map(result => `
|
||||
<div class="search-result p-2 border-bottom">
|
||||
<div class="d-flex justify-content-between">
|
||||
<div>
|
||||
<strong>${result.title}</strong>
|
||||
<small class="text-muted d-block">${result.description}</small>
|
||||
</div>
|
||||
<span class="badge bg-secondary">${result.type}</span>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
container.innerHTML = resultsHtml;
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
function formatCurrency(amount) {
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD'
|
||||
}).format(amount);
|
||||
}
|
||||
|
||||
function formatDate(date) {
|
||||
return new Intl.DateTimeFormat('en-US').format(new Date(date));
|
||||
}
|
||||
|
||||
function debounce(func, wait) {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
}
|
||||
|
||||
function throttle(func, limit) {
|
||||
let inThrottle;
|
||||
return function() {
|
||||
const args = arguments;
|
||||
const context = this;
|
||||
if (!inThrottle) {
|
||||
func.apply(context, args);
|
||||
inThrottle = true;
|
||||
setTimeout(() => inThrottle = false, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export global functions
|
||||
window.app = app;
|
||||
window.showNotification = showNotification;
|
||||
window.apiGet = apiGet;
|
||||
window.apiPost = apiPost;
|
||||
window.apiPut = apiPut;
|
||||
window.apiDelete = apiDelete;
|
||||
window.formatCurrency = formatCurrency;
|
||||
window.formatDate = formatDate;
|
||||
Reference in New Issue
Block a user