-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
211 lines (203 loc) · 7.05 KB
/
script.js
File metadata and controls
211 lines (203 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// OOP
class BarcodeGenerator {
constructor() {
this.currentBarcode = null;
this.initializeEventListeners();
}
initializeEventListeners() {
const nameInput = document.getElementById("barcode-name-input");
const generateBtn = document.getElementById("generate-random-btn");
if (nameInput && generateBtn) {
nameInput.addEventListener("input", () => {
generateBtn.disabled = nameInput.value.trim() === "";
});
}
if (generateBtn) {
generateBtn.addEventListener("click", async () => {
generateBtn.disabled = true;
generateBtn.innerHTML = `<span class='inline-block w-5 h-5 mr-2 align-middle rounded-full border-2 border-primary-500 border-t-transparent animate-spin-custom'></span> Generating...`;
// Custom spinner animation via CSS
if (!document.getElementById("custom-spinner-style")) {
const style = document.createElement("style");
style.id = "custom-spinner-style";
style.innerHTML = `
@keyframes spin-custom { to { transform: rotate(360deg); } }
.animate-spin-custom { animation: spin-custom 0.8s linear infinite; border-width: 2px; border-style: solid; border-radius: 9999px; }
`;
document.head.appendChild(style);
}
await new Promise((r) => setTimeout(r, 2000));
const upc = this.generateUPC();
const name = nameInput.value.trim();
const labelSize =
document.getElementById("label-size")?.value || "30mm";
const copies =
parseInt(document.getElementById("copies-input")?.value) || 1;
this.currentBarcode = {
value: upc,
type: "UPC",
labelSize,
copies,
name,
};
this.generateBarcodeImage();
// Show name and details in preview
const nameDisplay = document.getElementById("barcode-name-display");
if (nameDisplay) nameDisplay.textContent = name;
const details = document.getElementById("preview-details");
if (details)
details.textContent = `Label Size: ${labelSize} • Copies: ${copies}`;
this.showPreview();
// Save to DB
const userId = localStorage.getItem("userId");
if (userId) {
try {
const res = await fetch("/api/history", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
userId,
value: upc,
name,
labelSize,
copies,
}),
});
if (res.ok) {
const feedback = document.getElementById("save-feedback");
if (feedback) {
feedback.classList.remove("hidden");
setTimeout(() => feedback.classList.add("hidden"), 2000);
}
}
} catch {}
}
generateBtn.disabled = false;
generateBtn.innerHTML = `Generate Random Barcode`;
});
}
// Label size change in preview
const labelSizeSelect = document.getElementById("label-size");
if (labelSizeSelect) {
labelSizeSelect.addEventListener("change", () => {
if (this.currentBarcode) {
this.currentBarcode.labelSize = labelSizeSelect.value;
const details = document.getElementById("preview-details");
if (details)
details.textContent = `Label Size: ${labelSizeSelect.value} • Copies: ${this.currentBarcode.copies}`;
}
});
}
const saveBtn = document.getElementById("save-btn");
if (saveBtn) {
saveBtn.addEventListener("click", () => {
this.saveBarcode();
});
}
const printBtn = document.getElementById("print-btn");
if (printBtn) {
printBtn.addEventListener("click", () => {
this.printBarcode();
});
}
const backBtn = document.getElementById("back-to-generator-btn");
if (backBtn) {
backBtn.addEventListener("click", () => {
this.showGenerator();
});
}
const showHistoryBtn = document.getElementById("show-history-btn");
if (showHistoryBtn) {
showHistoryBtn.addEventListener("click", () => {
window.location.href = "history.html";
});
}
}
generateUPC() {
let digits = "";
for (let i = 0; i < 11; i++) {
digits += Math.floor(Math.random() * 10);
}
let sum = 0;
for (let i = 0; i < 11; i++) {
sum += parseInt(digits[i]) * (i % 2 === 0 ? 3 : 1);
}
const checkDigit = (10 - (sum % 10)) % 10;
return digits + checkDigit;
}
generateBarcodeImage() {
const canvas = document.getElementById("barcode-canvas");
const valueDisplay = document.getElementById("barcode-value-display");
try {
JsBarcode(canvas, this.currentBarcode.value, {
format: "UPC",
width: 2,
height: 100,
displayValue: true,
fontSize: 14,
margin: 10,
});
valueDisplay.textContent = this.currentBarcode.value;
} catch (error) {
console.error("Error generating barcode:", error);
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
valueDisplay.textContent = "Error generating barcode";
}
}
showPreview() {
document.getElementById("step-1")?.classList.add("hidden");
document.getElementById("step-2")?.classList.remove("hidden");
}
showGenerator() {
document.getElementById("step-2")?.classList.add("hidden");
document.getElementById("step-1")?.classList.remove("hidden");
}
printBarcode() {
if (!this.currentBarcode) return;
const printWindow = window.open("", "_blank");
const canvas = document.getElementById("barcode-canvas");
printWindow.document.write(`
<html>
<head>
<title>Print Barcode</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; text-align: center; }
.barcode-container { margin: 20px 0; page-break-inside: avoid; }
.barcode-value { font-family: monospace; margin-top: 10px; font-size: 14px; }
@media print { body { margin: 0; } }
</style>
</head>
<body>
${Array(this.currentBarcode.copies)
.fill()
.map(
() => `
<div class="barcode-container">
<img src="${canvas.toDataURL()}" alt="Barcode">
<div class="barcode-value">${this.currentBarcode.value}</div>
</div>
`
)
.join("")}
</body>
</html>
`);
printWindow.document.close();
printWindow.focus();
setTimeout(() => {
printWindow.print();
printWindow.close();
}, 500);
}
saveBarcode() {
if (!this.currentBarcode) return;
const canvas = document.getElementById("barcode-canvas");
const link = document.createElement("a");
link.download = `barcode-${this.currentBarcode.value}.png`;
link.href = canvas.toDataURL();
link.click();
}
}
window.addEventListener("DOMContentLoaded", () => {
window.app = new BarcodeGenerator();
});