81 lines
1.7 KiB
JavaScript
81 lines
1.7 KiB
JavaScript
|
|
export function paginateModels(models) {
|
|
for (let model of models) {
|
|
// ensure sizes
|
|
model.size = modelSize(model)
|
|
}
|
|
|
|
let pages = []
|
|
let currentSize = 0
|
|
let currentPage = []
|
|
for (let model of models) {
|
|
if (model.size > 0) { // skip empty models
|
|
currentSize += model.size
|
|
if (currentSize > 3) {
|
|
pages.push(currentPage)
|
|
currentPage = [model]
|
|
currentSize = model.size
|
|
} else {
|
|
currentPage.push(model)
|
|
}
|
|
}
|
|
}
|
|
if (currentPage.length > 0) {
|
|
pages.push(currentPage)
|
|
}
|
|
|
|
return pages
|
|
}
|
|
|
|
export function modelSize(model) {
|
|
if (!model || !model.ids) return 0
|
|
if (model.ids.length <= 4) return 1
|
|
if (model.ids.length <= 8) return 2
|
|
return 3
|
|
}
|
|
|
|
export function sectionModels(section) {
|
|
// remove pages, return models (blocks) in order
|
|
let models = []
|
|
if (section && section.pages) {
|
|
for (let page of section.pages) {
|
|
models.push(...page)
|
|
}
|
|
}
|
|
|
|
return models
|
|
}
|
|
|
|
export function sectionTitle(material) {
|
|
// "Trailhead (Men)"
|
|
if (material) {
|
|
return `${material.category} (${material.gender})`
|
|
} else {
|
|
return '(unknown)'
|
|
}
|
|
}
|
|
|
|
// add properties from the material to the model,
|
|
// creating it if necessary.
|
|
export function extendModelFromMaterial(material, model = {}) {
|
|
if (material) {
|
|
if (!model['name']) {
|
|
// don't overwrite possibly overridden value
|
|
model['name'] = material.name
|
|
}
|
|
model['model'] = material.model
|
|
model['family'] = material.family
|
|
model['gender'] = material.gender
|
|
model['category'] = material.category
|
|
|
|
// make sure the material id is in the list
|
|
let ids = model['ids'] || []
|
|
if (!ids.includes(material.id)) {
|
|
ids.push(material.id)
|
|
model['ids'] = ids
|
|
}
|
|
}
|
|
|
|
return model
|
|
}
|