From ce04802f300ba627a3b9e9612d938b825045e63f Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Sat, 4 Apr 2020 01:36:34 +0200 Subject: viewer: tag categories implementation fixed single tags not appearing in the "Other filters" special category, following code review GitHub: Resolves #29 --- viewer/src/@types/tag.d.ts | 1 + viewer/src/services/indexfactory.ts | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'viewer') diff --git a/viewer/src/@types/tag.d.ts b/viewer/src/@types/tag.d.ts index 425a995..229c418 100644 --- a/viewer/src/@types/tag.d.ts +++ b/viewer/src/@types/tag.d.ts @@ -21,6 +21,7 @@ declare namespace Tag { interface Node { tag: Gallery.RawTag; tagfiltered: Gallery.RawTag; + rootPart: boolean; items: Gallery.Item[]; children: Index; } diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index 466b509..0a84951 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts @@ -38,18 +38,19 @@ export default class IndexFactory { const parts = tag.split(':'); let lastPart: string | null = null; for (const part of parts) { - tagsIndex[part] = IndexFactory.pushPartToIndex(tagsIndex[part], part, item); + tagsIndex[part] = IndexFactory.pushPartToIndex(tagsIndex[part], part, item, !Boolean(lastPart)); if (lastPart) { const children = tagsIndex[lastPart].children; - children[part] = IndexFactory.pushPartToIndex(children[part], part, item); + children[part] = IndexFactory.pushPartToIndex(children[part], part, item, false); } lastPart = part; } } } - private static pushPartToIndex(index: Tag.Node, part: string, item: Gallery.Item): Tag.Node { - if (!index) index = { tag: part, tagfiltered: Navigation.normalize(part), items: [], children: {} }; + private static pushPartToIndex(index: Tag.Node, part: string, item: Gallery.Item, rootPart: boolean): Tag.Node { + if (!index) index = { tag: part, tagfiltered: Navigation.normalize(part), rootPart, items: [], children: {} }; + else if (rootPart) index.rootPart = true; if (!index.items.includes(item)) index.items.push(item); return index; } @@ -125,8 +126,14 @@ export default class IndexFactory { .filter(category => category.index && Object.keys(category.index).length) .forEach(category => { tagsCategories.push(category); - tagsRemaining.delete(category.tag); - Object.values(category.index).map(node => node.tag).forEach(tag => tagsRemaining.delete(tag)); + + if (!tagsIndex[category.tag].rootPart) + tagsRemaining.delete(category.tag); + + Object.values(category.index) + .map(node => node.tag) + .filter(tag => !tagsIndex[tag].rootPart) + .forEach(tag => tagsRemaining.delete(tag)); }); tagsCategories.push({ tag: "", index: Object.fromEntries(tagsRemaining) }); return tagsCategories; -- cgit v1.2.3