Update search.js
This commit is contained in:
+15
-42
@@ -1,19 +1,18 @@
|
||||
|
||||
let documents = [];
|
||||
|
||||
const searchInput = document.getElementById("searchInput");
|
||||
const results = document.getElementById("results");
|
||||
const status = document.getElementById("status");
|
||||
|
||||
// لینک مستقیم فایل CSV در مخزن Git
|
||||
// آدرس فایل CSV
|
||||
|
||||
const CSV_URL = "https://git.sadentel.net/f.taghipour/Document/raw/branch/main/SadenTel_Documents.csv";
|
||||
|
||||
|
||||
// یکسانسازی حروف و اعداد
|
||||
// یکسانسازی متن و اعداد
|
||||
function normalize(value) {
|
||||
return String(value ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[۰-۹]/g, digit =>
|
||||
"۰۱۲۳۴۵۶۷۸۹".indexOf(digit)
|
||||
)
|
||||
@@ -24,8 +23,7 @@ function normalize(value) {
|
||||
.replace(/ك/g, "ک");
|
||||
}
|
||||
|
||||
|
||||
// پردازش فایل CSV
|
||||
// پردازش CSV
|
||||
function parseCSV(text) {
|
||||
const rows = [];
|
||||
let row = [];
|
||||
@@ -42,14 +40,10 @@ function parseCSV(text) {
|
||||
} else {
|
||||
insideQuotes = !insideQuotes;
|
||||
}
|
||||
}
|
||||
|
||||
else if (char === "," && !insideQuotes) {
|
||||
} else if (char === "," && !insideQuotes) {
|
||||
row.push(field);
|
||||
field = "";
|
||||
}
|
||||
|
||||
else if (
|
||||
} else if (
|
||||
(char === "\n" || char === "\r") &&
|
||||
!insideQuotes
|
||||
) {
|
||||
@@ -65,9 +59,7 @@ function parseCSV(text) {
|
||||
|
||||
row = [];
|
||||
field = "";
|
||||
}
|
||||
|
||||
else {
|
||||
} else {
|
||||
field += char;
|
||||
}
|
||||
}
|
||||
@@ -81,12 +73,10 @@ function parseCSV(text) {
|
||||
return rows;
|
||||
}
|
||||
|
||||
|
||||
// دریافت اطلاعات از لینک آنلاین CSV
|
||||
// دریافت اطلاعات CSV
|
||||
async function loadDocuments() {
|
||||
try {
|
||||
searchInput.disabled = true;
|
||||
|
||||
status.textContent = "در حال دریافت اسناد...";
|
||||
|
||||
const response = await fetch(CSV_URL, {
|
||||
@@ -94,11 +84,10 @@ async function loadDocuments() {
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("CSV file could not be loaded");
|
||||
throw new Error(`HTTP Error: ${response.status}`);
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
|
||||
const rows = parseCSV(text);
|
||||
|
||||
documents = rows.slice(1).map(columns => ({
|
||||
@@ -113,18 +102,15 @@ async function loadDocuments() {
|
||||
searchInput.disabled = false;
|
||||
|
||||
} catch (error) {
|
||||
|
||||
status.textContent =
|
||||
"خطا در دریافت اطلاعات. دسترسی به فایل CSV را بررسی کنید.";
|
||||
"خطا در دریافت اطلاعات. مسیر فایل CSV را بررسی کنید.";
|
||||
|
||||
console.error("Loading error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// جستوجوی اسناد
|
||||
function searchDocuments() {
|
||||
|
||||
const query = normalize(searchInput.value);
|
||||
|
||||
results.innerHTML = "";
|
||||
@@ -136,35 +122,25 @@ function searchDocuments() {
|
||||
}
|
||||
|
||||
const matches = documents.filter(doc => {
|
||||
const titleWords =
|
||||
normalize(doc.title).match(/[\p{L}\p{N}]+/gu) || [];
|
||||
|
||||
const title = normalize(doc.title);
|
||||
const id = normalize(doc.id);
|
||||
|
||||
// استخراج کلمات کامل عنوان
|
||||
const titleWords =
|
||||
title.match(/[\p{L}\p{N}]+/gu) || [];
|
||||
|
||||
// تطابق کامل کلمه یا شناسه
|
||||
// فقط تطابق کامل کلمه یا شناسه
|
||||
return titleWords.includes(query) || id === query;
|
||||
});
|
||||
|
||||
console.log("Search query:", query);
|
||||
console.table(matches);
|
||||
|
||||
status.textContent =
|
||||
`${matches.length} نتیجه پیدا شد.`;
|
||||
|
||||
if (matches.length === 0) {
|
||||
|
||||
results.innerHTML =
|
||||
'<div class="empty">سندی با این مشخصات پیدا نشد.</div>';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// نمایش نتایج
|
||||
matches.forEach(doc => {
|
||||
|
||||
const item = document.createElement("div");
|
||||
item.className = "result";
|
||||
|
||||
@@ -194,15 +170,12 @@ function searchDocuments() {
|
||||
idInfo.append(idLabel, idValue);
|
||||
|
||||
item.append(title, rowInfo, idInfo);
|
||||
|
||||
results.appendChild(item);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// جستوجوی لحظهای
|
||||
searchInput.addEventListener("input", searchDocuments);
|
||||
|
||||
|
||||
// بارگذاری اولیه اطلاعات
|
||||
// بارگذاری اولیه
|
||||
loadDocuments();
|
||||
|
||||
مرجع در شماره جدید
مسدود کردن کاربر