stat2025 commited on
Commit
735085e
·
verified ·
1 Parent(s): 0fb67ec

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +154 -44
script.js CHANGED
@@ -1,55 +1,165 @@
1
- const tableBody = document.getElementById("tableBody");
2
- const searchBox = document.getElementById("searchBox");
3
- const counter = document.getElementById("counter");
4
- const loading = document.getElementById("loading");
5
-
6
- // تحميل ملف Excel وتحويله لبيانات
7
- async function loadData() {
8
- const response = await fetch("isic4.json"); // ملف محول من Excel إلى JSON
9
- const data = await response.json();
10
- loading.style.display = "none";
11
- return data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
 
14
- // تحديث الجدول حسب البحث
15
- function updateTable(data, searchTerm = "") {
16
- tableBody.innerHTML = "";
 
 
 
17
 
18
- let filtered = data.filter(row =>
19
- row.code.includes(searchTerm) ||
20
- row.name.includes(searchTerm)
21
- );
22
-
23
- if (filtered.length === 0) {
24
- tableBody.innerHTML = `<tr><td colspan="3" class="no-results">🚫 لا توجد نتائج مطابقة</td></tr>`;
25
- } else {
26
- filtered.forEach(row => {
27
- let tr = document.createElement("tr");
28
- tr.innerHTML = `
29
- <td>${row.code}</td>
30
- <td>${row.name}</td>
31
- <td></td>
32
- `;
33
- tableBody.appendChild(tr);
34
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  }
36
 
37
- // تحديث العد بشكل مباشر
38
- if (searchTerm === "") {
39
- counter.textContent = `إجمالي الأنشطة الاقتصادية: ${data.length}`;
40
- } else {
41
- counter.textContent = filtered.length === 0
42
- ? "لا توجد نتائج"
43
- : `عدد النتائج المطابقة: ${filtered.length}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  }
 
 
 
45
  }
46
 
47
- // تشغيل
48
- loadData().then(data => {
49
- updateTable(data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- searchBox.addEventListener("input", () => {
52
- const searchTerm = searchBox.value.trim();
53
- updateTable(data, searchTerm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  });
55
- });
 
1
+ // عناصر الواجهة
2
+ const searchBox = document.getElementById("searchBox");
3
+ const clearSearch = document.getElementById("clearSearch");
4
+ const pageSizeEl = document.getElementById("pageSize");
5
+ const themeBtn = document.getElementById("themeToggle");
6
+ const tableBody = document.getElementById("tableBody");
7
+ const pagination = document.getElementById("pagination");
8
+ const counter = document.getElementById("counter");
9
+ const loading = document.getElementById("loading");
10
+
11
+ // حالة البيانات
12
+ let data = [];
13
+ let filtered = [];
14
+ let currentPage = 1;
15
+ let rowsPerPage = pageSizeEl ? parseInt(pageSizeEl.value, 10) : 50;
16
+
17
+ // ألوان صغيرة للعمود الثالث
18
+ const colors = ["#00bcd4","#4caf50","#f44336","#ff9800","#9c27b0","#e91e63","#009688","#3f51b5","#607d8b","#795548"];
19
+
20
+ // أدوات مساعدة
21
+ function normalizeArabic(str=""){
22
+ return str
23
+ .replace(/[\u064B-\u0652]/g, "")
24
+ .replace(/[أإآا]/g,"ا").replace(/ى/g,"ي").replace(/ؤ/g,"و")
25
+ .replace(/ئ/g,"ي").replace(/ة/g,"ه").toLowerCase();
26
+ }
27
+ function matches(text, q){ if(!q) return true; return normalizeArabic(String(text??"")).includes(normalizeArabic(q)); }
28
+ function highlight(text, q){
29
+ if(!q) return String(text ?? "");
30
+ const esc = q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
31
+ return String(text ?? "").replace(new RegExp(esc, 'gi'), m => `<mark class="hl">${m}</mark>`);
32
  }
33
 
34
+ // العرض
35
+ function renderPagination(){
36
+ const pages = Math.ceil(filtered.length / rowsPerPage) || 1;
37
+ if (!pagination) return;
38
+ pagination.innerHTML = "";
39
+ if (pages <= 1) return;
40
 
41
+ const makeBtn = (label, page, active=false, disabled=false)=>{
42
+ const b = document.createElement("button");
43
+ b.textContent = label;
44
+ b.className = "page-btn" + (active ? " active" : "");
45
+ b.disabled = disabled;
46
+ b.addEventListener("click", ()=>{
47
+ if(page === currentPage) return;
48
+ currentPage = page; updateView();
49
+ window.scrollTo({top:0, behavior:"smooth"});
 
 
 
 
 
 
 
50
  });
51
+ return b;
52
+ };
53
+
54
+ pagination.appendChild(makeBtn("‹ السابق", Math.max(1, currentPage-1), false, currentPage===1));
55
+
56
+ const win = 5;
57
+ const total = pages;
58
+ let start = Math.max(1, currentPage - Math.floor(win/2));
59
+ let end = Math.min(total, start + win - 1);
60
+ if (end - start + 1 < win) start = Math.max(1, end - win + 1);
61
+
62
+ if (start > 1) pagination.appendChild(makeBtn("1", 1, currentPage===1));
63
+ if (start > 2) pagination.appendChild(Object.assign(document.createElement("span"), {className:"dots", textContent:"..."}));
64
+ for (let p=start; p<=end; p++) pagination.appendChild(makeBtn(String(p), p, p===currentPage));
65
+ if (end < total - 1) pagination.appendChild(Object.assign(document.createElement("span"), {className:"dots", textContent:"..."}));
66
+ if (end < total) pagination.appendChild(makeBtn(String(total), total, currentPage===total));
67
+
68
+ pagination.appendChild(makeBtn("التالي ›", Math.min(total, currentPage+1), false, currentPage===total));
69
+ }
70
+
71
+ function renderTable(){
72
+ if (!tableBody) return;
73
+ const q = searchBox ? searchBox.value.trim() : "";
74
+ tableBody.innerHTML = "";
75
+
76
+ const start = (currentPage - 1) * rowsPerPage;
77
+ const pageItems = filtered.slice(start, start + rowsPerPage);
78
+
79
+ if (pageItems.length === 0) {
80
+ const tr = document.createElement("tr");
81
+ const td = document.createElement("td");
82
+ td.colSpan = 3;
83
+ td.className = "no-results";
84
+ td.textContent = "🚫 لا توجد نتائج مطابقة";
85
+ tr.appendChild(td);
86
+ tableBody.appendChild(tr);
87
+ return;
88
  }
89
 
90
+ pageItems.forEach((it, i)=>{
91
+ // يدعم مفاتيح مختلفة: activity أو name
92
+ const code = it.code ?? "";
93
+ const activity = (it.activity ?? it.name ?? "");
94
+
95
+ const tr = document.createElement("tr");
96
+
97
+ const codeTd = document.createElement("td");
98
+ codeTd.innerHTML = highlight(code, q);
99
+
100
+ const actTd = document.createElement("td");
101
+ actTd.innerHTML = highlight(activity, q);
102
+
103
+ const colorTd = document.createElement("td");
104
+ const colorBar = document.createElement("div");
105
+ colorBar.className = "color-bar";
106
+ colorBar.style.backgroundColor = colors[i % colors.length];
107
+ colorTd.appendChild(colorBar);
108
+
109
+ tr.appendChild(codeTd); tr.appendChild(actTd); tr.appendChild(colorTd);
110
+ tableBody.appendChild(tr);
111
+ });
112
+ }
113
+
114
+ function updateView(){
115
+ const q = searchBox ? searchBox.value.trim() : "";
116
+ filtered = data.filter(it => matches(it.code, q) || matches(it.activity ?? it.name, q));
117
+ if (counter) {
118
+ counter.textContent = filtered.length
119
+ ? `عدد الأنشطة الاقتصادية: ${filtered.length.toLocaleString()}`
120
+ : "لا توجد نتائج";
121
  }
122
+ if (loading) loading.style.display = "none";
123
+ renderTable();
124
+ renderPagination();
125
  }
126
 
127
+ // أحداث
128
+ function debounce(fn, delay=200){ let t; return (...a)=>{ clearTimeout(t); t=setTimeout(()=>fn(...a), delay); }; }
129
+ searchBox?.addEventListener("input", debounce(()=>{ currentPage = 1; updateView(); }, 180));
130
+ clearSearch?.addEventListener("click", ()=>{ if(!searchBox) return; searchBox.value=""; searchBox.focus(); currentPage=1; updateView(); });
131
+ pageSizeEl?.addEventListener("change", ()=>{ rowsPerPage = parseInt(pageSizeEl.value, 10) || 50; currentPage=1; updateView(); });
132
+
133
+ // وضع ليلي اختياري
134
+ (function initTheme(){
135
+ if(!themeBtn) return;
136
+ const saved = localStorage.getItem("theme") || "light";
137
+ document.documentElement.classList.toggle("dark", saved === "dark");
138
+ themeBtn.textContent = saved === "dark" ? "☀️ الوضع الفاتح" : "🌙 الوضع الليلي";
139
+ themeBtn.addEventListener("click", ()=>{
140
+ const isDark = document.documentElement.classList.toggle("dark");
141
+ localStorage.setItem("theme", isDark ? "dark" : "light");
142
+ themeBtn.textContent = isDark ? "☀️ الوضع الفاتح" : "🌙 الوضع الليلي";
143
+ });
144
+ })();
145
+
146
+ document.addEventListener("keydown", (e)=>{ if(e.key==="/"){ e.preventDefault(); searchBox?.focus(); } });
147
 
148
+ // تحميل البيانات من data.json (الافتراضي في مشروعك)
149
+ fetch("data.json", { cache: "no-cache" })
150
+ .then(r=>{
151
+ if(!r.ok) throw new Error("HTTP "+r.status);
152
+ return r.json();
153
+ })
154
+ .then(json=>{
155
+ // توحيد الحقول لو جاءت بصيغ مختلفة
156
+ data = Array.isArray(json) ? json.map(row => ({
157
+ code: row.code ?? row.Code ?? row["رمز"] ?? "",
158
+ activity: row.activity ?? row.Activity ?? row.name ?? row["النشاط"] ?? ""
159
+ })) : [];
160
+ updateView();
161
+ })
162
+ .catch(err=>{
163
+ console.error(err);
164
+ if (loading) loading.textContent = "تعذر تحميل البيانات (تأكدي من وجود data.json وصيغة الحقول).";
165
  });