-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarcodes.js
96 lines (84 loc) · 3.02 KB
/
barcodes.js
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
String.format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
function createBarcodeGrid(entries) {
let html = [];
html.push('<div class="grid-container">');
for (let i = 0; i < entries.length; i++) {
const id = `barcode_image_${i}`;
html.push(`<div class="barcode-container"><img id="${id}" class="barcode-image"></div>`);
}
html.push('</div>');
return html.join('');
}
function generateBarcodes() {
try {
let str = document.getElementById("barcodesEntry").value;
if (str == '') {
document.getElementById("barcodesContainer").innerText = '';
return
}
localStorage.setItem('lastRequest', str);
var separator;
if (str.includes('\n')) {
separator = '\n'
}
else if (str.includes(' ')) {
separator = ' '
}
else if (str.includes(';')) {
separator = ';'
}
else if (str.includes(',')) {
separator = ','
}
let entries = str.split(separator)
const entriesCount = entries.length
const columnsPerRow = 4
var rows = 1
if (entriesCount > columnsPerRow) {
let mod = entriesCount % columnsPerRow
if (mod > 0) {
rows = entriesCount / columnsPerRow
}
else {
rows = (entriesCount / columnsPerRow) + 1
}
}
let gridHtml = createBarcodeGrid(entries);
document.getElementById("barcodesContainer").innerHTML = gridHtml;
const symbology = document.getElementById("symbologySelector").value
localStorage.setItem('lastSymbology', symbology);
const addText = document.getElementById("addTextCheck").checked
let canvas = document.createElement('canvas');
for (let i = 0; i < entries.length; i++) {
const barcodeValue = entries[i].trim();
const id = `barcode_image_${i}`;//this is the value used when creating the grid
var options = {
bcid: symbology, // Barcode type
text: barcodeValue, // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: addText, // Show human-readable text
textxalign: 'center', // Always good to set this
};
try {
bwipjs.toCanvas(canvas, options);
var img = document.getElementById(id);
if (img != null) {
img.src = canvas.toDataURL('image/png');
}
} catch (e) {
console.error(e);
}
}
}
catch (error) {
document.getElementById("barcodesContainer").innerText = error;
}
}