Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
177 lines (151 loc) · 7.64 KB
/
Copy pathapp.js
File metadata and controls
177 lines (151 loc) · 7.64 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
// Ensure DOM is fully loaded
document.addEventListener('DOMContentLoaded', () => {
const wordListContainer = document.getElementById('wordlist-container');
const wordDetailContainer = document.getElementById('word-detail-view');
const searchInput = document.getElementById('search_text');
let allData = [];
// Helper: open a specific tab by ID
window.openTab = (tabId) => {
const tabEl = document.querySelector(`button[data-bs-target="#${tabId}"]`);
if(tabEl) {
const tab = new bootstrap.Tab(tabEl);
tab.show();
}
};
// Load data
fetch('elw.json')
.then(response => response.json())
.then(data => {
allData = data;
renderWordList(allData);
})
.catch(err => console.error('Error loading data:', err));
// Search functionality
if(searchInput) {
searchInput.addEventListener('input', (e) => {
const term = e.target.value.toLowerCase();
filterWordList(term);
});
}
function filterWordList(term) {
const items = document.querySelectorAll('.word-list-item');
let hasVisible = false;
items.forEach(item => {
const searchable = item.dataset.searchable || '';
if (searchable.toLowerCase().includes(term)) {
item.style.display = '';
hasVisible = true;
} else {
item.style.display = 'none';
}
});
// If searching, switch to list tab
if (term.length > 0) {
window.openTab('list');
}
}
function renderWordList(data) {
if (!wordListContainer) return;
const fragment = document.createDocumentFragment();
data.forEach(item => {
const btn = document.createElement('button');
btn.className = 'list-group-item list-group-item-action word-list-item';
// Searchable text logic from original script
const strippedHw = item.hw ? item.hw.replace(/[1-6]\//g, "") : "";
btn.dataset.searchable = `${item.chars} ${item.hw} ${strippedHw}`;
btn.dataset.id = item.wordid;
btn.innerHTML = `
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1 fw-bold text-primary">${item.chars}</h5>
</div>
<small class="text-muted">${item.hw}</small>
`;
btn.addEventListener('click', () => {
showWordDetail(item);
// Highlight active state
document.querySelectorAll('.word-list-item').forEach(el => el.classList.remove('active'));
btn.classList.add('active');
});
fragment.appendChild(btn);
});
wordListContainer.innerHTML = '';
wordListContainer.appendChild(fragment);
}
function showWordDetail(item) {
if (!wordDetailContainer) return;
let html = `
<div class="card shadow-sm h-100">
<div class="card-header bg-light border-bottom">
<h2 class="h4 mb-0 text-primary">${item.chars} <small class="text-muted ms-2 fs-6">/${item.hw}/</small></h2>
</div>
<div class="card-body overflow-auto">
`;
if (item.wordprop && item.wordprop.length > 0) {
item.wordprop.forEach((prop, index) => {
html += `<div class="mb-4 ${index > 0 ? 'border-top pt-3' : ''}">`;
if (prop.ps) html += `<div class="mb-1"><strong class="text-secondary">Part of Speech:</strong> <span class="badge bg-secondary">${prop.ps}</span></div>`;
if (prop.apa) html += `<div class="mb-1"><strong class="text-secondary">APA:</strong> ${prop.apa}</div>`;
if (prop.en) html += `<div class="mb-1"><strong class="text-secondary">English Source:</strong> <span class="fst-italic">${prop.en}</span></div>`;
if (prop.stch) html += `<div class="mb-1"><strong class="text-secondary">Standard Chinese:</strong> ${prop.stch}</div>`;
if (prop.df) html += `<div class="mt-2 p-2 bg-light rounded border-start border-4 border-info"><strong>Definition:</strong> ${prop.df}</div>`;
if (prop.exchar) {
html += `
<div class="mt-2 p-2 bg-light rounded small">
<div class="d-flex align-items-baseline">
<span class="fw-bold me-2 text-secondary text-uppercase" style="font-size: 0.75rem;">Example</span>
<span class="fs-6 text-dark">${prop.exchar}</span>
</div>
<div class="ps-5 ms-3 border-start">
<div class="text-muted fst-italic">${prop.exrom}</div>
<div class="text-secondary">${prop.exeng}</div>
</div>
</div>
`;
}
html += `</div>`;
});
}
// See Also logic
if (item.seealso_ref && item.seealso_ref.length > 0) {
const links = item.seealso_ref.map(refId => {
// refId seems to be mostly 1-based index, but let's be safe and check allData
// Original code assumed data[refId-1]
const refItem = allData[refId-1];
if (refItem) {
// Create a clickable span that calls showWordDetail
// But since we are generating HTML string, we can't attach event listener easily here
// We'll use a data attribute and delegate event listener later or use onclick (simple but discouraged, but okay for generated content if scoped)
// Better: just render span and add listener to container? No.
// Let's use clean approach: button with class
return `<button class="btn btn-sm btn-outline-info me-1 mb-1 see-also-btn" data-id="${refItem.wordid}">${refItem.chars}</button>`;
}
return '';
}).join('');
if (links) html += `<div class="mt-4 pt-3 border-top"><strong>See Also:</strong><br><div class="d-flex flex-wrap mt-2">${links}</div></div>`;
}
// Publications
if (item.publication && item.publication.length > 0) {
html += `<div class="mt-4 pt-3 border-top small text-muted"><strong>Publications:</strong><ul class="list-unstyled mb-0"><li>${item.publication.join('</li><li>')}</li></ul></div>`;
}
html += `</div></div>`; // Close card-body
wordDetailContainer.innerHTML = html;
// Add listeners for "See Also" buttons
wordDetailContainer.querySelectorAll('.see-also-btn').forEach(btn => {
btn.addEventListener('click', () => {
const id = parseInt(btn.dataset.id);
// Find data
const targetItem = allData.find(d => d.wordid === id);
if (targetItem) {
showWordDetail(targetItem);
// Also highlight in list
const listBtn = document.querySelector(`.word-list-item[data-id="${id}"]`);
if (listBtn) {
document.querySelectorAll('.word-list-item').forEach(el => el.classList.remove('active'));
listBtn.classList.add('active');
listBtn.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
});
});
}
});