mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:38:36 +00:00
Merge pull request #3094 from MoonEyes/carry/2377
studio: show category names in the chosen language (carry of #2377)
This commit is contained in:
@ -290,6 +290,46 @@ test('switching to EU relabels the stats panel, the buttons, the texture gallery
|
||||
assert.equal(studio.elements.get('tex-count').innerText, 'Could not load the textures');
|
||||
});
|
||||
|
||||
test('category chips and headers show the catNames of the chosen language and keep the server name as the filter', () => {
|
||||
const studio = loadStudio();
|
||||
studio.context.models = [
|
||||
{ name: 'car.glb', folder: 'veh', category: 'Транспорт (Vehicles)', size: 4, url: '/models/car.glb' },
|
||||
{ name: 'odd.glb', folder: 'x', category: 'Custom', size: 4, url: '/models/odd.glb' },
|
||||
];
|
||||
studio.run(`allModels = models; renderCategoryChips({'Транспорт (Vehicles)': 1, Custom: 1}); renderAccordionList();`);
|
||||
const chips = () => studio.elements.get('category-chips').children.slice(1).map(chip => chip.innerText);
|
||||
const headers = () => studio.elements.get('model-list').children.map(group => visibleText(group.children[0].innerHTML).trim());
|
||||
assert.deepEqual(chips(), ['Транспорт (1)', 'Custom (1)']);
|
||||
assert.deepEqual(headers().map(text => text.split(/\s+/)[0]), ['Транспорт', 'Custom']);
|
||||
|
||||
studio.run(`setLanguage('eu')`);
|
||||
assert.deepEqual(chips(), ['Vehicles (1)', 'Custom (1)']);
|
||||
assert.deepEqual(headers().map(text => text.split(/\s+/)[0]), ['Vehicles', 'Custom']);
|
||||
// The chip still filters by the server's name.
|
||||
studio.run(`selectCategory('Транспорт (Vehicles)', document.getElementById('category-chips').children[1])`);
|
||||
assert.equal(studio.run('activeCategory'), 'Транспорт (Vehicles)');
|
||||
assert.deepEqual(headers().map(text => text.split(/\s+/)[0]), ['Vehicles']);
|
||||
});
|
||||
|
||||
test('model search matches the category name shown in the chosen language, not only the server name', () => {
|
||||
const studio = loadStudio();
|
||||
studio.context.models = [
|
||||
{ name: 'tree.glb', folder: 'x', category: 'Растительность (Nature)', size: 4, url: '/models/tree.glb' },
|
||||
{ name: 'car.glb', folder: 'y', category: 'Транспорт (Vehicles)', size: 4, url: '/models/car.glb' },
|
||||
];
|
||||
studio.run(`currentLang = 'eu'; allModels = models; renderAccordionList();`);
|
||||
const search = query => {
|
||||
studio.context.query = query;
|
||||
studio.run(`document.getElementById('model-search').value = query; onModelSearch();`);
|
||||
return studio.elements.get('model-list').children.map(group => visibleText(group.children[0].innerHTML).trim().split(/\s+/)[0]);
|
||||
};
|
||||
// "Vegetation" is only in the EU label "Vegetation & Nature", not in the server's name.
|
||||
assert.deepEqual(search('Vegetation'), ['Vegetation']);
|
||||
// The server's name still matches, in either language.
|
||||
assert.deepEqual(search('Транспорт'), ['Vehicles']);
|
||||
assert.deepEqual(search('Nature'), ['Vegetation']);
|
||||
});
|
||||
|
||||
test('each model category pages independently, including a partial last page', () => {
|
||||
const studio = loadStudio();
|
||||
studio.context.models = ['Props', 'Vehicles'].flatMap(category => Array.from({ length: 101 }, (_, i) => ({
|
||||
|
||||
@ -590,6 +590,7 @@ function applyTranslations() {
|
||||
document.getElementById('tex-heading').innerText = t.texHeading;
|
||||
const allChip = document.getElementById('chip-all');
|
||||
if (allChip) allChip.innerText = `${t.allCategories} (${allModels.length})`;
|
||||
for (const { chip, cat, count } of categoryChips) chip.innerText = `${categoryLabel(cat)} (${count})`;
|
||||
if (texturesLoaded) document.getElementById('tex-count').innerText = `${allTextures.length} ${t.texCountSuffix}`;
|
||||
// Each stats label is the text node in front of its value (<div>Модель: <b id="stat-name">).
|
||||
for (const [id, key] of [['stat-name', 'statModel'], ['stat-category', 'statCategory'],
|
||||
@ -609,6 +610,7 @@ let shownModel = null;
|
||||
const modelPages = new Map();
|
||||
let allTextures = [];
|
||||
let texturesLoaded = false;
|
||||
const categoryChips = []; // { chip, cat, count } of the category filter chips, relabelled on a language switch
|
||||
let activeCategory = 'ALL';
|
||||
let modelSearchQuery = '';
|
||||
let texSearchQuery = '';
|
||||
@ -938,6 +940,7 @@ async function loadModels() {
|
||||
function renderCategoryChips(categories) {
|
||||
const container = document.getElementById('category-chips');
|
||||
container.innerHTML = '';
|
||||
categoryChips.length = 0;
|
||||
|
||||
const allChip = document.createElement('div');
|
||||
allChip.className = 'filter-chip active';
|
||||
@ -949,12 +952,19 @@ function renderCategoryChips(categories) {
|
||||
Object.entries(categories).forEach(([cat, count]) => {
|
||||
const chip = document.createElement('div');
|
||||
chip.className = 'filter-chip';
|
||||
chip.innerText = `${cat} (${count})`;
|
||||
chip.innerText = `${categoryLabel(cat)} (${count})`;
|
||||
chip.onclick = () => selectCategory(cat, chip);
|
||||
container.appendChild(chip);
|
||||
categoryChips.push({ chip, cat, count });
|
||||
});
|
||||
}
|
||||
|
||||
// The server names each category in both languages ("Транспорт (Vehicles)"); catNames gives the name to show, and
|
||||
// the server's name stays the filter key. A name catNames does not list is shown as it is.
|
||||
function categoryLabel(cat) {
|
||||
return I18N[currentLang].catNames[cat] || cat;
|
||||
}
|
||||
|
||||
function selectCategory(cat, chipElem) {
|
||||
if (activeCategory !== cat) modelPages.clear();
|
||||
activeCategory = cat;
|
||||
@ -980,7 +990,9 @@ function renderAccordionList() {
|
||||
const matchesCat = (activeCategory === 'ALL' || m.category === activeCategory);
|
||||
const matchesSearch = !modelSearchQuery ||
|
||||
m.name.toLowerCase().includes(modelSearchQuery) ||
|
||||
(m.category && m.category.toLowerCase().includes(modelSearchQuery)) ||
|
||||
// The category as shown (catNames of the chosen language) and the server's name both match.
|
||||
(m.category && (m.category.toLowerCase().includes(modelSearchQuery) ||
|
||||
categoryLabel(m.category).toLowerCase().includes(modelSearchQuery))) ||
|
||||
(m.folder && m.folder.toLowerCase().includes(modelSearchQuery));
|
||||
return matchesCat && matchesSearch;
|
||||
});
|
||||
@ -1006,7 +1018,7 @@ function renderAccordionList() {
|
||||
const header = document.createElement('div');
|
||||
header.className = 'cat-header';
|
||||
header.innerHTML = `
|
||||
<span>${escapeHtml(cat)} <span class="badge">${list.length}</span></span>
|
||||
<span>${escapeHtml(categoryLabel(cat))} <span class="badge">${list.length}</span></span>
|
||||
<span class="arrow">▼</span>
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user