-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (115 loc) 路 4.37 KB
/
Copy pathscript.js
File metadata and controls
141 lines (115 loc) 路 4.37 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
let dataBarang = [];
let searchInput = document.getElementById("search");
let searchHistoryContainer = document.getElementById("searchHistory");
// Load data.json saat halaman dimuat
document.addEventListener("DOMContentLoaded", function () {
fetch("data.json")
.then((response) => response.json())
.then((data) => {
console.log("Data berhasil dimuat:", data);
dataBarang = data;
})
.catch((error) => console.error("Gagal mengambil data:", error));
showSearchHistory(); // Ganti loadSearchHistory() dengan showSearchHistory()
});
// Event saat input diklik (hanya tampilkan history, tanpa autocomplete)
searchInput.addEventListener("focus", function () {
showSearchHistory();
});
// Event klik di luar search agar history hilang
document.addEventListener("click", function (event) {
if (!searchInput.contains(event.target) && !searchHistoryContainer.contains(event.target)) {
searchHistoryContainer.style.display = "none";
}
});
// Fungsi menampilkan history pencarian
function showSearchHistory() {
searchHistoryContainer.innerHTML = "";
let history = JSON.parse(localStorage.getItem("searchHistory")) || [];
history.forEach((item) => {
let div = document.createElement("div");
div.classList.add("suggestion-item");
div.innerHTML = `馃晵 ${item}`;
div.onclick = () => {
searchInput.value = item;
searchHistoryContainer.style.display = "none";
cariBarang(item);
};
searchHistoryContainer.appendChild(div);
});
searchHistoryContainer.style.display = history.length ? "block" : "none";
}
// Fungsi mencari barang
function cariBarang(keyword) {
let resultContent = document.getElementById("resultContent");
let barcodeSvg = document.getElementById("barcode");
keyword = keyword || searchInput.value.trim();
if (!keyword) return;
resultContent.innerHTML = "";
barcodeSvg.innerHTML = "";
let barang = dataBarang.find((item) => item.PLU == keyword || item.BARCODE == keyword);
if (barang) {
resultContent.innerHTML = `
<p><strong>Nama Barang:</strong> ${barang.DESKRIPSI}</p>
<p><strong>PLU:</strong> ${barang.PLU}</p>
<p><strong>Barcode:</strong> ${barang.BARCODE || "Tidak tersedia"}</p>
`;
if (barang.BARCODE) {
let normalized = normalizeBarcode(barang.BARCODE);
setTimeout(() => {
JsBarcode("#barcode", normalized, { format: "CODE128", displayValue: true });
}, 100);
}
saveSearchHistory(keyword);
showPopup();
} else {
resultContent.innerHTML = "<p>Barang tidak ditemukan!</p>";
showPopup();
}
}
// Fungsi menyimpan history pencarian
function saveSearchHistory(keyword) {
let history = JSON.parse(localStorage.getItem("searchHistory")) || [];
history = history.filter((item) => item !== keyword);
history.unshift(keyword);
if (history.length > 5) history.pop();
localStorage.setItem("searchHistory", JSON.stringify(history));
}
// Fungsi normalisasi barcode (13 digit)
function normalizeBarcode(barcode) {
barcode = barcode.toString();
return barcode.length === 11 ? "00" + barcode : barcode.length === 12 ? "0" + barcode : barcode;
}
// Fungsi menampilkan popup
function showPopup() {
document.getElementById("resultPopup").classList.add("active");
document.getElementById("overlay").style.display = "block";
}
// Fungsi menutup popup
function closePopup() {
document.getElementById("resultPopup").classList.remove("active");
document.getElementById("overlay").style.display = "none";
searchInput.value = "";
searchInput.focus();
}
// Tambahkan fungsi cetakBarcode
function cetakBarcode() {
// Simpan konten halaman saat ini
let originalContents = document.body.innerHTML;
// Buat halaman cetak yang hanya berisi barcode
let barcodeElement = document.getElementById("barcode");
let printContents = '<div style="text-align:center; padding:20px;">' + barcodeElement.outerHTML + '</div>';
// Ganti konten dengan barcode saja
document.body.innerHTML = printContents;
// Cetak halaman
window.print();
// Kembalikan konten asli
document.body.innerHTML = originalContents;
// Refresh event listeners yang mungkin hilang
document.getElementById("search").addEventListener("focus", showSearchHistory);
document.getElementById("printBarcode").onclick = cetakBarcode;
}
// Tutup popup dengan tombol Escape
document.addEventListener("keydown", function (event) {
if (event.key === "Escape") closePopup();
});