-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
337 lines (293 loc) · 11.7 KB
/
script.js
File metadata and controls
337 lines (293 loc) · 11.7 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Global variables
let currentUser = null;
let labelTemplates = {};
// Initialize application
document.addEventListener('DOMContentLoaded', function() {
// Load saved templates from localStorage
loadSavedTemplates();
// Add event listeners for real-time preview
const inputs = document.querySelectorAll('.field-inputs input, .layout-controls select, .layout-controls input');
inputs.forEach(input => {
input.addEventListener('input', debounce(generatePreview, 300));
});
});
// Simple authentication (can be replaced with real authentication)
function login() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (username && password) {
// Simple validation - replace with real authentication
if (username.length >= 3 && password.length >= 3) {
currentUser = username;
document.getElementById('loginSection').classList.add('hidden');
document.getElementById('appSection').classList.remove('hidden');
// Load sample data for demo
loadSampleData();
} else {
alert('Please enter valid credentials (minimum 3 characters each)');
}
} else {
alert('Please enter both username and password');
}
}
// Load sample data for demonstration
function loadSampleData() {
document.getElementById('productCode').value = 'PRD001';
document.getElementById('productName').value = 'Sample Product';
document.getElementById('price').value = '$25.99';
document.getElementById('batchNumber').value = 'B2024001';
document.getElementById('expiryDate').value = '2024-12-31';
// Generate initial preview
setTimeout(generatePreview, 100);
}
// Generate barcode and label preview
function generatePreview() {
const productCode = document.getElementById('productCode').value;
const productName = document.getElementById('productName').value;
const price = document.getElementById('price').value;
const batchNumber = document.getElementById('batchNumber').value;
const expiryDate = document.getElementById('expiryDate').value;
const customField1 = document.getElementById('customField1').value;
const customField2 = document.getElementById('customField2').value;
const fontSize = document.getElementById('fontSize').value;
const barcodeHeight = document.getElementById('barcodeHeight').value;
const showBarcodeText = document.getElementById('showBarcodeText').checked;
if (!productCode) {
document.getElementById('labelPreview').innerHTML =
'<div class="preview-placeholder">Please enter a product code</div>';
return;
}
// Create label content
let labelHTML = '<div class="label-content" style="font-size: ' + fontSize + 'px;">';
// Add product name if provided
if (productName) {
labelHTML += '<div class="label-field product-name">' + escapeHtml(productName) + '</div>';
}
// Add barcode
labelHTML += '<div class="barcode-container">';
labelHTML += '<svg id="barcode"></svg>';
labelHTML += '</div>';
// Add other fields
const fields = [
{ value: price, label: 'Price' },
{ value: batchNumber, label: 'Batch' },
{ value: expiryDate, label: 'Exp' },
{ value: customField1, label: 'CF1' },
{ value: customField2, label: 'CF2' }
];
fields.forEach(field => {
if (field.value) {
labelHTML += '<div class="label-field">' + escapeHtml(field.value) + '</div>';
}
});
labelHTML += '</div>';
// Update preview
document.getElementById('labelPreview').innerHTML = labelHTML;
// Generate barcode
try {
JsBarcode("#barcode", productCode, {
format: "CODE128",
width: 1,
height: parseInt(barcodeHeight),
displayValue: showBarcodeText,
fontSize: Math.max(8, parseInt(fontSize) - 2),
margin: 0,
background: "transparent"
});
} catch (error) {
console.error('Barcode generation error:', error);
document.getElementById('barcode').innerHTML = '<text x="50%" y="50%" text-anchor="middle">Invalid Code</text>';
}
// Generate ZPL code
generateZPL();
}
// Generate ZPL code for Zebra printer
function generateZPL() {
const productCode = document.getElementById('productCode').value;
const productName = document.getElementById('productName').value;
const price = document.getElementById('price').value;
const batchNumber = document.getElementById('batchNumber').value;
const expiryDate = document.getElementById('expiryDate').value;
const customField1 = document.getElementById('customField1').value;
const customField2 = document.getElementById('customField2').value;
const copies = document.getElementById('copies').value;
if (!productCode) {
document.getElementById('zplCode').value = '';
return;
}
// ZPL template for 3.5cm x 2.5cm label (approximately 132x94 dots at 203 DPI)
let zpl = '^XA\n'; // Start format
zpl += '^MMT\n'; // Set media type to thermal transfer
zpl += '^PW132\n'; // Set print width to 132 dots
zpl += '^LL94\n'; // Set label length to 94 dots
zpl += '^LS0\n'; // Label shift
let yPos = 10; // Starting Y position
// Product name (if provided)
if (productName) {
zpl += '^FO5,' + yPos + '^A0N,12,12^FD' + productName.substring(0, 20) + '^FS\n';
yPos += 15;
}
// Barcode
zpl += '^FO5,' + yPos + '^BCN,' + (productName ? '25' : '35') + ',Y,N,N^FD' + productCode + '^FS\n';
yPos += (productName ? 30 : 40);
// Other fields
const fields = [price, batchNumber, expiryDate, customField1, customField2];
fields.forEach(field => {
if (field && yPos < 85) {
zpl += '^FO5,' + yPos + '^A0N,8,8^FD' + field.substring(0, 25) + '^FS\n';
yPos += 10;
}
});
zpl += '^PQ' + copies + '\n'; // Print quantity
zpl += '^XZ'; // End format
document.getElementById('zplCode').value = zpl;
}
// Print label function
function printLabel() {
const productCode = document.getElementById('productCode').value;
const thermalMode = document.getElementById('thermalMode').checked;
if (!productCode) {
alert('Please enter a product code before printing');
return;
}
if (thermalMode) {
// For thermal printer, show ZPL code and instructions
const zplCode = document.getElementById('zplCode').value;
if (zplCode) {
const printWindow = window.open('', '_blank');
printWindow.document.write(`
<html>
<head>
<title>ZPL Code for Thermal Printer</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.zpl-code { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; white-space: pre-wrap; }
.instructions { background: #e7f3ff; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
</style>
</head>
<body>
<h2>ZPL Code for Zebra Thermal Printer</h2>
<div class="instructions">
<strong>Instructions:</strong><br>
1. Copy the ZPL code below<br>
2. Send it directly to your Zebra ZD220 printer via USB, Serial, or Network<br>
3. You can use Zebra Setup Utilities or send via command line/application
</div>
<div class="zpl-code">${zplCode}</div>
<button onclick="navigator.clipboard.writeText('${zplCode.replace(/'/g, "\\'")}')">Copy ZPL Code</button>
</body>
</html>
`);
}
} else {
// Regular browser print
window.print();
}
}
// Save current template
function saveTemplate() {
const templateName = prompt('Enter template name:');
if (!templateName) return;
const template = {
productCode: document.getElementById('productCode').value,
productName: document.getElementById('productName').value,
price: document.getElementById('price').value,
batchNumber: document.getElementById('batchNumber').value,
expiryDate: document.getElementById('expiryDate').value,
customField1: document.getElementById('customField1').value,
customField2: document.getElementById('customField2').value,
fontSize: document.getElementById('fontSize').value,
barcodeHeight: document.getElementById('barcodeHeight').value,
showBarcodeText: document.getElementById('showBarcodeText').checked
};
labelTemplates[templateName] = template;
localStorage.setItem('barcodeTemplates', JSON.stringify(labelTemplates));
alert('Template saved successfully!');
}
// Load template
function loadTemplate() {
const templateNames = Object.keys(labelTemplates);
if (templateNames.length === 0) {
alert('No saved templates found');
return;
}
const templateName = prompt('Select template:\n' + templateNames.join('\n'));
if (!templateName || !labelTemplates[templateName]) return;
const template = labelTemplates[templateName];
// Load template values
Object.keys(template).forEach(key => {
const element = document.getElementById(key);
if (element) {
if (element.type === 'checkbox') {
element.checked = template[key];
} else {
element.value = template[key];
}
}
});
generatePreview();
}
// Load saved templates from localStorage
function loadSavedTemplates() {
const saved = localStorage.getItem('barcodeTemplates');
if (saved) {
labelTemplates = JSON.parse(saved);
}
}
// Copy ZPL code to clipboard
function copyZPL() {
const zplCode = document.getElementById('zplCode').value;
if (zplCode) {
navigator.clipboard.writeText(zplCode).then(() => {
alert('ZPL code copied to clipboard!');
}).catch(err => {
console.error('Failed to copy: ', err);
// Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = zplCode;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
alert('ZPL code copied to clipboard!');
});
}
}
// Utility functions
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Keyboard shortcuts
document.addEventListener('keydown', function(e) {
if (e.ctrlKey || e.metaKey) {
switch(e.key) {
case 'p':
e.preventDefault();
printLabel();
break;
case 's':
e.preventDefault();
saveTemplate();
break;
case 'Enter':
if (document.getElementById('loginSection').style.display !== 'none') {
e.preventDefault();
login();
}
break;
}
}
});