-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (196 loc) · 7.12 KB
/
script.js
File metadata and controls
219 lines (196 loc) · 7.12 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
// 🔹 Replace with your real Google Apps Script API URL
const API_URL = 'https://script.googleusercontent.com/macros/echo?user_content_key=AehSKLjy6ww6NT42xWSS78jYeGP9_9LWyEwmaV0JipWnTqyvVRKq4uXYtVSGCU0fndAFgkAUlh34xzKrvmPTBuRMWjZwJ_s1DfVuhV6I5bybGyHVKjRWsfL13_5aH2kBYu4vqeyDPyQ9VNve8gMYNi7FOqWopb6Mdj5OJ6WFYtErDUX9fowqhyxVb9Ep-G5ZzrajGVTz2JywhDtxpCPnfh79z-NbKBYu4LddhzyXz81QpYRRhvA5gSuTvVgVM_0hqP1sx9cjizCcSqS2SsWxyiCBNtlm7mVUGw&lib=MGomoR7OA82DnxJG-b41jitkh30YExdAc';
let currentLang = 'en';
let allData = [];
const translations = {
en: {
title: "Company Directory",
searchPlaceholder: "🔍 Search by name or focus area...",
footer: 'Made with ❤️ by <a href="https://github.com/demanejar" target="_blank">Demanejar</a>',
loading: "Loading...",
fields: {
employees: "Employees",
hourlyRate: "Hourly rate",
founded: "Founded year",
location: "Location",
focus: "Focus areas",
website: "🌐 Website"
},
langButton: "🇺🇸 English",
langSwitch: "🇻🇳 Vietnamese"
},
vi: {
title: "Danh sách công ty",
searchPlaceholder: "🔍 Tìm kiếm theo tên hoặc lĩnh vực...",
footer: 'Tạo với ❤️ bởi <a href="https://github.com/demanejar" target="_blank">Demanejar</a>',
loading: "Đang tải...",
fields: {
employees: "Nhân viên",
hourlyRate: "Giá/giờ",
founded: "Thành lập",
location: "Vị trí",
focus: "Lĩnh vực",
website: "🌐 Trang web"
},
langButton: "🇻🇳 Tiếng Việt",
langSwitch: "🇺🇸 English"
}
};
// ------------------- UTILITY FUNCTIONS -------------------
function truncateText(text, maxLength = 150) {
if (!text) return '';
if (text.length <= maxLength) return text;
return text.substring(0, maxLength) + '...';
}
// ------------------- LOADING UTILITIES -------------------
function showLoading() {
const loadingEl = document.getElementById('loading');
const loadingText = loadingEl.querySelector('.loading-text');
const t = translations[currentLang];
loadingText.textContent = t.loading;
loadingEl.style.display = 'flex';
document.getElementById('company-list').style.display = 'none';
}
function hideLoading() {
document.getElementById('loading').style.display = 'none';
document.getElementById('company-list').style.display = 'grid';
}
// ------------------- FETCH DATA -------------------
async function fetchCompanies() {
showLoading();
try {
const res = await fetch(API_URL);
allData = await res.json();
renderCompanies(allData);
setupSearch();
} catch (err) {
document.getElementById('company-list').innerHTML =
`<p style="color:red;text-align:center;">❌ Error loading data: ${err}</p>`;
} finally {
hideLoading();
}
}
// ------------------- RENDER UI -------------------
function renderCompanies(data) {
const t = translations[currentLang];
const container = document.getElementById('company-list');
container.innerHTML = '';
if (!data || data.length === 0) {
container.innerHTML = `<p style="text-align:center;">${
currentLang === 'vi' ? 'Không có dữ liệu.' : 'No data found.'
}</p>`;
return;
}
data.forEach((c, index) => {
const div = document.createElement('div');
div.className = 'card';
div.innerHTML = `
<h2>${c.Name || '(No name)'}</h2>
<p><strong>${t.fields.employees}:</strong> ${c.Employees || 'N/A'}</p>
<p><strong>${t.fields.hourlyRate}:</strong> ${c.HourlyRate ? c.HourlyRate : 'N/A'}</p>
<p><strong>${t.fields.founded}:</strong> ${c.FoundedYear || 'N/A'}</p>
<p><strong>${t.fields.location}:</strong> ${c.Location || 'N/A'}</p>
<p><strong>${t.fields.focus}:</strong> ${c.FocusAreas || 'N/A'}</p>
${c.Description ? `<p class="description">${c.Description}</p>` : ''}
<div class="card-footer">
${c.Website ? `<a href="${c.Website}" target="_blank" onclick="event.stopPropagation();">${t.fields.website}</a>` : '<span style="opacity: 0.5;">No website</span>'}
</div>
`;
div.addEventListener('click', () => showModal(c));
container.appendChild(div);
});
}
// ------------------- SEARCH -------------------
function setupSearch() {
const searchBox = document.getElementById('searchBox');
searchBox.addEventListener('input', e => {
const q = e.target.value.toLowerCase();
const filtered = allData.filter(c =>
(c.Name || '').toLowerCase().includes(q) ||
(c.FocusAreas || '').toLowerCase().includes(q)
);
renderCompanies(filtered);
});
}
// ------------------- LANGUAGE TOGGLE -------------------
function setupLanguage() {
const langBtn = document.getElementById('langToggle');
const title = document.getElementById('page-title');
const footer = document.getElementById('footer-text');
const searchBox = document.getElementById('searchBox');
function applyLang() {
const t = translations[currentLang];
title.textContent = t.title;
footer.innerHTML = t.footer;
searchBox.placeholder = t.searchPlaceholder;
langBtn.textContent = t.langSwitch;
}
langBtn.addEventListener('click', () => {
currentLang = currentLang === 'en' ? 'vi' : 'en';
applyLang();
renderCompanies(allData);
});
applyLang();
}
// ------------------- MODAL POPUP -------------------
function showModal(company) {
const t = translations[currentLang];
const modal = document.getElementById('modal');
const modalBody = document.getElementById('modal-body');
modalBody.innerHTML = `
<h2>${company.Name || '(No name)'}</h2>
<div class="modal-field">
<strong>${t.fields.employees}:</strong> ${company.Employees || 'N/A'}
</div>
<div class="modal-field">
<strong>${t.fields.hourlyRate}:</strong> ${company.HourlyRate ? company.HourlyRate : 'N/A'}
</div>
<div class="modal-field">
<strong>${t.fields.founded}:</strong> ${company.FoundedYear || 'N/A'}
</div>
<div class="modal-field">
<strong>${t.fields.location}:</strong> ${company.Location || 'N/A'}
</div>
<div class="modal-field">
<strong>${t.fields.focus}:</strong> ${company.FocusAreas || 'N/A'}
</div>
${company.Description ? `
<div class="modal-description">
<strong>Description:</strong>
<p>${company.Description}</p>
</div>
` : ''}
${company.Website ? `
<div class="modal-website">
<a href="${company.Website}" target="_blank">${t.fields.website}</a>
</div>
` : ''}
`;
modal.style.display = 'block';
}
function closeModal() {
document.getElementById('modal').style.display = 'none';
}
// Setup modal close handlers
function setupModal() {
const modal = document.getElementById('modal');
const closeBtn = document.querySelector('.close');
if (closeBtn) {
closeBtn.addEventListener('click', closeModal);
}
window.addEventListener('click', (e) => {
if (e.target === modal) {
closeModal();
}
});
// Close on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && modal && modal.style.display === 'block') {
closeModal();
}
});
}
// ------------------- INIT -------------------
setupLanguage();
setupModal();
fetchCompanies();