import { Option, Category } from '../../classes/classes'
export { Option, Category } from '../../classes/classes'
import { slugify } from '../../utils/string-helpers'
import { App } from '../../gogocarto'
export class TaxonomyModule {
categories: Category[] = []
options: Option[] = []
// the full hierachic taxonomy
taxonomy: Category
rootCategories: Category[]
categoriesCreatedCount = 1
optionsCreatedCount = 1
constructor() {
this.options = []
this.categories = []
}
createTaxonomyFromJson(taxonomyJson): void {
if (App.config.data.transformTaxonomy) taxonomyJson = App.config.data.transformTaxonomy(taxonomyJson)
const isSkosTaxonomy = taxonomyJson['@graph']
if (isSkosTaxonomy) {
taxonomyJson = App.taxonomySkosModule.convertSkosIntoGoGoTaxonomy(taxonomyJson)
}
if (Array.isArray(taxonomyJson) && taxonomyJson.length == 1) {
taxonomyJson = taxonomyJson[0]
}
// If multiple root categories, we encapsulate them into a single fake category & root option
if (Array.isArray(taxonomyJson) && taxonomyJson.length > 1) {
for (const json of taxonomyJson) {
json.isRootCategory = true
}
taxonomyJson = {
name: 'RootFakeCategory',
id: -1,
options: [
{
name: 'RootFakeOption',
id: -1,
displayInInfoBar: false,
displayInMenu: false,
showExpanded: true,
subcategories: taxonomyJson,
},
],
}
} else if (!isSkosTaxonomy) {
taxonomyJson.isRootCategory = true
}
this.taxonomy = this.recursivelyCreateCategoryAndOptions(taxonomyJson)
const parentOption = this.taxonomy.name == 'RootFakeCategory' ? this.taxonomy.options[0] : null
this.rootCategories = parentOption ? parentOption.subcategories : [this.taxonomy]
for (const option of this.mainCategory.children) {
option.isMainOption = true
}
this.recursivelyCalculateParentIds(this.rootCategories, parentOption)
}
private recursivelyCreateCategoryAndOptions(categoryJson: any): Category {
return this.recursivelyCreateCategory(categoryJson)
}
private recursivelyCreateCategory(categoryJson: any): Category {
if (!categoryJson.id) {
categoryJson.id = this.categoriesCreatedCount++
}
const category = new Category(categoryJson)
if (categoryJson.options) {
for (const optionJson of categoryJson.options) {
this.recursivelyCreateOption(optionJson, category)
}
} else if (categoryJson.subcategories) {
this.recursivelyCreateOption(
{
subcategories: categoryJson.subcategories,
showExpanded: true,
displayInMenu: false,
displayInInfoBar: false,
},
category,
)
}
this.categories.push(category)
return category
}
private recursivelyCreateOption(optionJson: any, parentCategory: Category): void {
optionJson.intId = this.optionsCreatedCount++
const option = new Option(optionJson)
option.ownerId = parentCategory.id
if (optionJson.subcategories) {
for (const subcategoryJson of optionJson.subcategories) {
const subcategory = this.recursivelyCreateCategoryAndOptions(subcategoryJson)
subcategory.ownerId = option.id
option.addCategory(subcategory)
}
} else if (optionJson.suboptions) {
const subcategory = this.recursivelyCreateCategoryAndOptions({
options: optionJson.suboptions,
showExpanded: optionJson.showExpanded,
})
subcategory.ownerId = option.id
option.addCategory(subcategory)
}
parentCategory.addOption(option)
this.options.push(option)
}
// Every option knows all its parent ids (option and category)
private recursivelyCalculateParentIds(categories: Category[], parentOption: Option = null): void {
const depth = parentOption ? parentOption.depth + 1 : 0
for (const category of categories) {
for (const option of category.children) {
if (option.isMainOption || parentOption === null) {
option.mainOwnerId = 'all'
} else if (parentOption.isMainOption || (!parentOption.mainOwnerId && parentOption.id != 'Racine')) {
option.mainOwnerId = parentOption.id
} else {
option.mainOwnerId = parentOption.mainOwnerId
}
;(