Spaces:
Running
Running
Update events.js
Browse files
events.js
CHANGED
|
@@ -1,22 +1,31 @@
|
|
| 1 |
// events.js - Manages all event listeners
|
| 2 |
|
| 3 |
-
import { appState, saveState
|
| 4 |
import { handleUpdateStock, handleRestock } from './services.js';
|
| 5 |
import { refreshUI, showToast, renderReport } from './ui.js';
|
| 6 |
import { generatePurchaseOrder } from './purchaseOrderService.js';
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
export function attachAllListeners() {
|
| 9 |
attachProductInputListeners();
|
| 10 |
attachMaxStockEditListeners();
|
| 11 |
attachRestockListeners();
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
attachModalListeners();
|
| 14 |
}
|
| 15 |
|
| 16 |
function attachProductInputListeners() {
|
| 17 |
document.querySelectorAll('.update-btn').forEach(btn => {
|
| 18 |
if (btn.dataset.listenerAttached) return;
|
| 19 |
-
btn.dataset.listenerAttached = true;
|
| 20 |
btn.addEventListener('click', (e) => {
|
| 21 |
const card = e.target.closest('.dashboard-card');
|
| 22 |
const productName = e.target.dataset.productName;
|
|
@@ -29,7 +38,7 @@ function attachProductInputListeners() {
|
|
| 29 |
function attachMaxStockEditListeners() {
|
| 30 |
document.querySelectorAll('.edit-max-stock').forEach(icon => {
|
| 31 |
if (icon.dataset.listenerAttached) return;
|
| 32 |
-
icon.dataset.listenerAttached = true;
|
| 33 |
icon.addEventListener('click', (e) => {
|
| 34 |
const valueSpan = e.target.closest('.flex').querySelector('.max-stock-value');
|
| 35 |
const materialName = e.target.closest('.dashboard-card').dataset.materialName;
|
|
@@ -61,7 +70,7 @@ function attachMaxStockEditListeners() {
|
|
| 61 |
function attachRestockListeners() {
|
| 62 |
document.querySelectorAll('.restock-icon').forEach(btn => {
|
| 63 |
if (btn.dataset.listenerAttached) return;
|
| 64 |
-
btn.dataset.listenerAttached = true;
|
| 65 |
btn.addEventListener('click', (e) => {
|
| 66 |
const parentEl = e.target.closest('[data-material-name]');
|
| 67 |
const materialName = parentEl.dataset.materialName;
|
|
@@ -83,19 +92,20 @@ function attachRestockListeners() {
|
|
| 83 |
});
|
| 84 |
}
|
| 85 |
|
| 86 |
-
function
|
| 87 |
-
document.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
const
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
}
|
| 97 |
});
|
| 98 |
-
}
|
| 99 |
}
|
| 100 |
|
| 101 |
function attachModalListeners() {
|
|
@@ -103,34 +113,26 @@ function attachModalListeners() {
|
|
| 103 |
const reportsModal = document.getElementById('reports-modal');
|
| 104 |
|
| 105 |
// Reset Modal
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
}
|
| 118 |
-
});
|
| 119 |
-
showResetModalBtn.dataset.listenerAttachedForReset = true;
|
| 120 |
-
}
|
| 121 |
|
| 122 |
// Reports Modal
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if (target && target.id === 'report-mat-usage') renderReport('material');
|
| 133 |
-
});
|
| 134 |
-
showReportsModalBtn.dataset.listenerAttachedForReports = true;
|
| 135 |
-
}
|
| 136 |
}
|
|
|
|
| 1 |
// events.js - Manages all event listeners
|
| 2 |
|
| 3 |
+
import { appState, saveState } from './state.js';
|
| 4 |
import { handleUpdateStock, handleRestock } from './services.js';
|
| 5 |
import { refreshUI, showToast, renderReport } from './ui.js';
|
| 6 |
import { generatePurchaseOrder } from './purchaseOrderService.js';
|
| 7 |
|
| 8 |
+
/**
|
| 9 |
+
* Attaches listeners to elements that are frequently re-rendered.
|
| 10 |
+
*/
|
| 11 |
export function attachAllListeners() {
|
| 12 |
attachProductInputListeners();
|
| 13 |
attachMaxStockEditListeners();
|
| 14 |
attachRestockListeners();
|
| 15 |
+
attachPurchaseOrderListener(); // Note: singular now
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
/**
|
| 19 |
+
* Attaches listeners to static elements, like modals, only once on startup.
|
| 20 |
+
*/
|
| 21 |
+
export function attachOneTimeListeners() {
|
| 22 |
attachModalListeners();
|
| 23 |
}
|
| 24 |
|
| 25 |
function attachProductInputListeners() {
|
| 26 |
document.querySelectorAll('.update-btn').forEach(btn => {
|
| 27 |
if (btn.dataset.listenerAttached) return;
|
| 28 |
+
btn.dataset.listenerAttached = 'true';
|
| 29 |
btn.addEventListener('click', (e) => {
|
| 30 |
const card = e.target.closest('.dashboard-card');
|
| 31 |
const productName = e.target.dataset.productName;
|
|
|
|
| 38 |
function attachMaxStockEditListeners() {
|
| 39 |
document.querySelectorAll('.edit-max-stock').forEach(icon => {
|
| 40 |
if (icon.dataset.listenerAttached) return;
|
| 41 |
+
icon.dataset.listenerAttached = 'true';
|
| 42 |
icon.addEventListener('click', (e) => {
|
| 43 |
const valueSpan = e.target.closest('.flex').querySelector('.max-stock-value');
|
| 44 |
const materialName = e.target.closest('.dashboard-card').dataset.materialName;
|
|
|
|
| 70 |
function attachRestockListeners() {
|
| 71 |
document.querySelectorAll('.restock-icon').forEach(btn => {
|
| 72 |
if (btn.dataset.listenerAttached) return;
|
| 73 |
+
btn.dataset.listenerAttached = 'true';
|
| 74 |
btn.addEventListener('click', (e) => {
|
| 75 |
const parentEl = e.target.closest('[data-material-name]');
|
| 76 |
const materialName = parentEl.dataset.materialName;
|
|
|
|
| 92 |
});
|
| 93 |
}
|
| 94 |
|
| 95 |
+
function attachPurchaseOrderListener() {
|
| 96 |
+
const poButton = document.getElementById('generate-full-po-btn');
|
| 97 |
+
if (poButton && !poButton.dataset.listenerAttached) {
|
| 98 |
+
poButton.dataset.listenerAttached = 'true';
|
| 99 |
+
poButton.addEventListener('click', () => {
|
| 100 |
+
const materialsToOrder = appState.materials.filter(m => m.currentStock <= m.reorderPoint);
|
| 101 |
+
if (materialsToOrder.length > 0) {
|
| 102 |
+
generatePurchaseOrder(materialsToOrder);
|
| 103 |
+
showToast(`Generated consolidated PO for ${materialsToOrder.length} items.`, 'success');
|
| 104 |
+
} else {
|
| 105 |
+
showToast('No items to reorder.', 'info');
|
| 106 |
}
|
| 107 |
});
|
| 108 |
+
}
|
| 109 |
}
|
| 110 |
|
| 111 |
function attachModalListeners() {
|
|
|
|
| 113 |
const reportsModal = document.getElementById('reports-modal');
|
| 114 |
|
| 115 |
// Reset Modal
|
| 116 |
+
document.getElementById('show-reset-modal-btn').addEventListener('click', () => resetModal.classList.remove('hidden'));
|
| 117 |
+
resetModal.addEventListener('click', (e) => {
|
| 118 |
+
const target = e.target.closest('button');
|
| 119 |
+
if ((target && target.id === 'cancel-reset-btn') || e.target === resetModal) {
|
| 120 |
+
resetModal.classList.add('hidden');
|
| 121 |
+
}
|
| 122 |
+
if (target && target.id === 'confirm-reset-btn') {
|
| 123 |
+
localStorage.removeItem('antennaTrackerState');
|
| 124 |
+
window.location.reload();
|
| 125 |
+
}
|
| 126 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
// Reports Modal
|
| 129 |
+
document.getElementById('show-reports-modal-btn').addEventListener('click', () => reportsModal.classList.remove('hidden'));
|
| 130 |
+
reportsModal.addEventListener('click', (e) => {
|
| 131 |
+
const target = e.target.closest('button');
|
| 132 |
+
if ((target && target.id === 'close-reports-modal-btn') || e.target === reportsModal) {
|
| 133 |
+
reportsModal.classList.add('hidden');
|
| 134 |
+
}
|
| 135 |
+
if (target && target.id === 'report-prod-summary') renderReport('production');
|
| 136 |
+
if (target && target.id === 'report-mat-usage') renderReport('material');
|
| 137 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|