Mediawiki:scmcchemistry.js
| Строка 17: | Строка 17: | ||
{ id: 'medicine', label: 'Медицина' }, | { id: 'medicine', label: 'Медицина' }, | ||
{ id: 'drinks', label: 'Напитки' }, | { id: 'drinks', label: 'Напитки' }, | ||
{ id: ' | { id: 'elements', label: 'Элементы' }, | ||
{ id: 'other', label: 'Другие вещества' } | |||
]; | ]; | ||
var REQUIRED_CATALOG_SECTIONS = TAB_DEFINITIONS.map(function (tab) { | |||
return tab.id; | |||
}); | |||
var DAMAGE_LABELS = { | var DAMAGE_LABELS = { | ||
| Строка 237: | Строка 242: | ||
function entriesForTab(tabId) { | function entriesForTab(tabId) { | ||
return state.data.catalogSections[tabId] || []; | |||
} | } | ||
| Строка 1021: | Строка 1012: | ||
} | } | ||
function renderSection(group) { | function sectionId(group, index) { | ||
return 'scmc-chem-section-' + state.activeTab + '-' + index; | |||
} | |||
function renderSectionNavigation(root, groups) { | |||
var navigation = root.querySelector('.scmc-chem__section-nav'); | |||
navigation.textContent = ''; | |||
navigation.hidden = groups.length < 2; | |||
groups.forEach(function (group, index) { | |||
var title = capitalizeFirst(group.path[group.path.length - 1] || 'Без раздела'); | |||
var link = element('a', 'scmc-chem__section-link', title); | |||
link.href = '#' + sectionId(group, index); | |||
link.appendChild(element('span', 'scmc-chem__section-link-count', group.entries.length)); | |||
navigation.appendChild(link); | |||
}); | |||
} | |||
function renderSection(group, index) { | |||
var section = element('section', 'scmc-chem__section'); | var section = element('section', 'scmc-chem__section'); | ||
var title = capitalizeFirst(group.path[group.path.length - 1] || 'Без раздела'); | var title = capitalizeFirst(group.path[group.path.length - 1] || 'Без раздела'); | ||
var heading = element('div', 'scmc-chem__section-heading'); | var heading = element('div', 'scmc-chem__section-heading'); | ||
section.id = sectionId(group, index); | |||
heading.appendChild(element('h2', 'scmc-chem__section-title', title)); | heading.appendChild(element('h2', 'scmc-chem__section-title', title)); | ||
| Строка 1054: | Строка 1064: | ||
if (!entries.length) { | if (!entries.length) { | ||
renderSectionNavigation(root, []); | |||
var empty = element('div', 'scmc-chem__no-results'); | var empty = element('div', 'scmc-chem__no-results'); | ||
append( | append( | ||
| Строка 1064: | Строка 1075: | ||
} | } | ||
groupEntries(entries).forEach(function (group) { | var groups = groupEntries(entries); | ||
results.appendChild(renderSection(group)); | renderSectionNavigation(root, groups); | ||
groups.forEach(function (group, index) { | |||
results.appendChild(renderSection(group, index)); | |||
}); | }); | ||
} | } | ||
| Строка 1134: | Строка 1148: | ||
tabs.setAttribute('role', 'tablist'); | tabs.setAttribute('role', 'tablist'); | ||
root.appendChild(tabs); | root.appendChild(tabs); | ||
var sectionNavigation = element('nav', 'scmc-chem__section-nav'); | |||
sectionNavigation.setAttribute('aria-label', 'Подразделы каталога'); | |||
sectionNavigation.hidden = true; | |||
root.appendChild(sectionNavigation); | |||
root.appendChild(element('main', 'scmc-chem__results')); | root.appendChild(element('main', 'scmc-chem__results')); | ||
| Строка 1179: | Строка 1197: | ||
function validateData(data) { | function validateData(data) { | ||
var hasSections = data && data.catalogSections && REQUIRED_CATALOG_SECTIONS.every(function (sectionId) { | |||
return Array.isArray(data.catalogSections[sectionId]); | |||
}); | |||
var classificationGuide = data && data.classification && data.classification.guideFile; | |||
return data && | return data && | ||
data.schemaVersion === 1 && | data.schemaVersion === 1 && | ||
| Строка 1185: | Строка 1208: | ||
data.reactions && | data.reactions && | ||
data.guides && | data.guides && | ||
data.counts; | data.counts && | ||
hasSections && | |||
/\/RMCChemicals\.xml$/.test(classificationGuide || ''); | |||
} | } | ||