Ultronprime commited on
Commit
5676853
·
verified ·
1 Parent(s): 8ca5fda

Update events.js

Browse files
Files changed (1) hide show
  1. events.js +91 -11
events.js CHANGED
@@ -1,6 +1,6 @@
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
 
@@ -11,7 +11,76 @@ export function attachAllListeners() {
11
  attachModalListeners();
12
  }
13
 
14
- // ... attachProductInputListeners, attachMaxStockEditListeners, attachRestockListeners functions are the same ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  function attachModalListeners() {
17
  const resetModal = document.getElementById('reset-modal');
@@ -19,20 +88,31 @@ function attachModalListeners() {
19
 
20
  // Reset Modal
21
  const showResetModalBtn = document.getElementById('show-reset-modal-btn');
22
- if (!showResetModalBtn.dataset.listenerAttached) {
23
  showResetModalBtn.addEventListener('click', () => resetModal.classList.remove('hidden'));
24
- document.getElementById('cancel-reset-btn').addEventListener('click', () => resetModal.classList.add('hidden'));
25
- document.getElementById('confirm-reset-btn').addEventListener('click', () => { window.location.reload(); });
26
- showResetModalBtn.dataset.listenerAttached = true;
 
 
 
 
 
 
 
27
  }
28
 
29
  // Reports Modal
30
  const showReportsModalBtn = document.getElementById('show-reports-modal-btn');
31
- if (!showReportsModalBtn.dataset.listenerAttached) {
32
  showReportsModalBtn.addEventListener('click', () => reportsModal.classList.remove('hidden'));
33
- document.getElementById('close-reports-modal-btn').addEventListener('click', () => reportsModal.classList.add('hidden'));
34
- document.getElementById('report-prod-summary').addEventListener('click', () => renderReport('production'));
35
- document.getElementById('report-mat-usage').addEventListener('click', () => renderReport('material'));
36
- showReportsModalBtn.dataset.listenerAttached = true;
 
 
 
 
37
  }
38
  }
 
1
  // events.js - Manages all event listeners
2
 
3
+ import { appState, saveState, loadState } from './state.js';
4
  import { handleUpdateStock, handleRestock } from './services.js';
5
  import { refreshUI, showToast, renderReport } from './ui.js';
6
 
 
11
  attachModalListeners();
12
  }
13
 
14
+ function attachProductInputListeners() {
15
+ document.querySelectorAll('.product-card .update-btn').forEach(btn => {
16
+ if (btn.dataset.listenerAttached) return;
17
+ btn.dataset.listenerAttached = true;
18
+ btn.addEventListener('click', (e) => {
19
+ const card = e.target.closest('.product-card');
20
+ const productName = card.dataset.productName;
21
+ const quantity = parseInt(card.querySelector('input').value, 10);
22
+ handleUpdateStock(productName, quantity);
23
+ });
24
+ });
25
+ }
26
+
27
+ function attachMaxStockEditListeners() {
28
+ document.querySelectorAll('.edit-max-stock').forEach(icon => {
29
+ if (icon.dataset.listenerAttached) return;
30
+ icon.dataset.listenerAttached = true;
31
+ icon.addEventListener('click', (e) => {
32
+ const valueSpan = e.target.closest('.flex').querySelector('.max-stock-value');
33
+ const materialName = e.target.closest('.material-card').dataset.materialName;
34
+ const material = appState.materials.find(m => m.name === materialName);
35
+ const input = document.createElement('input');
36
+ input.type = 'number';
37
+ input.value = material.maxStock;
38
+ input.className = 'w-20 text-right font-semibold border rounded px-1';
39
+ valueSpan.replaceWith(input);
40
+ input.focus();
41
+ input.select();
42
+ const saveChange = () => {
43
+ const newValue = parseInt(input.value, 10);
44
+ if (!isNaN(newValue) && newValue >= material.currentStock) {
45
+ material.maxStock = newValue;
46
+ saveState();
47
+ refreshUI();
48
+ } else {
49
+ showToast('Max stock must be a number >= current stock.', 'error');
50
+ refreshUI();
51
+ }
52
+ };
53
+ input.addEventListener('blur', saveChange);
54
+ input.addEventListener('keydown', (event) => { if (event.key === 'Enter') input.blur(); });
55
+ });
56
+ });
57
+ }
58
+
59
+ function attachRestockListeners() {
60
+ const showRestockForm = (e) => {
61
+ const parentEl = e.target.closest('[data-material-name]');
62
+ const materialName = parentEl.dataset.materialName;
63
+ const formContainer = parentEl.querySelector('.restock-form');
64
+
65
+ if (formContainer.classList.contains('hidden')) {
66
+ document.querySelectorAll('.restock-form').forEach(f => { f.classList.add('hidden'); f.innerHTML = ''; });
67
+ formContainer.classList.remove('hidden');
68
+ formContainer.innerHTML = `<div class="flex items-center gap-2"><input type="number" placeholder="Qty" class="input-number w-20 px-2 py-1 border border-gray-300 rounded"><button class="confirm-restock-btn px-3 py-1 bg-green-500 text-white text-xs rounded hover:bg-green-600">Add</button></div>`;
69
+ const input = formContainer.querySelector('input');
70
+ input.focus();
71
+ formContainer.querySelector('.confirm-restock-btn').addEventListener('click', () => { handleRestock(materialName, parseInt(input.value, 10)); });
72
+ input.addEventListener('keydown', (event) => { if (event.key === 'Enter') handleRestock(materialName, parseInt(input.value, 10)); if (event.key === 'Escape') formContainer.classList.add('hidden'); });
73
+ } else {
74
+ formContainer.classList.add('hidden');
75
+ formContainer.innerHTML = '';
76
+ }
77
+ };
78
+ document.querySelectorAll('.restock-icon, .restock-btn-reorder').forEach(btn => {
79
+ if (btn.dataset.listenerAttached) return;
80
+ btn.dataset.listenerAttached = true;
81
+ btn.addEventListener('click', showRestockForm);
82
+ });
83
+ }
84
 
85
  function attachModalListeners() {
86
  const resetModal = document.getElementById('reset-modal');
 
88
 
89
  // Reset Modal
90
  const showResetModalBtn = document.getElementById('show-reset-modal-btn');
91
+ if (!showResetModalBtn.dataset.listenerAttachedForReset) {
92
  showResetModalBtn.addEventListener('click', () => resetModal.classList.remove('hidden'));
93
+ resetModal.addEventListener('click', (e) => {
94
+ if (e.target.id === 'cancel-reset-btn' || e.target.id === 'reset-modal') {
95
+ resetModal.classList.add('hidden');
96
+ }
97
+ if (e.target.id === 'confirm-reset-btn') {
98
+ localStorage.removeItem('antennaTrackerState');
99
+ window.location.reload();
100
+ }
101
+ });
102
+ showResetModalBtn.dataset.listenerAttachedForReset = true;
103
  }
104
 
105
  // Reports Modal
106
  const showReportsModalBtn = document.getElementById('show-reports-modal-btn');
107
+ if (!showReportsModalBtn.dataset.listenerAttachedForReports) {
108
  showReportsModalBtn.addEventListener('click', () => reportsModal.classList.remove('hidden'));
109
+ reportsModal.addEventListener('click', (e) => {
110
+ if (e.target.id === 'close-reports-modal-btn' || e.target.id === 'reports-modal') {
111
+ reportsModal.classList.add('hidden');
112
+ }
113
+ if (e.target.id === 'report-prod-summary') renderReport('production');
114
+ if (e.target.id === 'report-mat-usage') renderReport('material');
115
+ });
116
+ showReportsModalBtn.dataset.listenerAttachedForReports = true;
117
  }
118
  }