From 577f49ab6e1fd9cd8007804a13dea1471ee2fb1f Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Fri, 3 Apr 2020 03:42:35 +0200 Subject: viewer: tag categories implementation GitHub: Resolves #29 --- viewer/src/services/indexfactory.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'viewer/src/services/indexfactory.ts') diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index a31f3ef..466b509 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts @@ -112,4 +112,23 @@ export default class IndexFactory { if (strict) return node.tagfiltered === filter; return node.tagfiltered.includes(filter) } + + // --- + + public static generateCategories(tagsIndex: Tag.Index, tags?: Gallery.RawTag[]): Tag.Category[] { + if (!tags?.length) return [{ tag: "", index: tagsIndex }]; + + const tagsCategories: Tag.Category[] = []; + const tagsRemaining = new Map(Object.entries(tagsIndex)); + tags + .map(tag => ({ tag, index: tagsIndex[tag]?.children })) + .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)); + }); + tagsCategories.push({ tag: "", index: Object.fromEntries(tagsRemaining) }); + return tagsCategories; + } } -- cgit v1.2.3 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/services/indexfactory.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'viewer/src/services/indexfactory.ts') 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 From 84090e0f534cfa8bf601ae6df21e5df695fd149a Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Sat, 4 Apr 2020 02:24:10 +0200 Subject: viewer: tag categories implementation code cleaning GitHub: Resolves #29 --- viewer/src/services/indexfactory.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'viewer/src/services/indexfactory.ts') diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index 0a84951..25027f3 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts @@ -127,11 +127,7 @@ export default class IndexFactory { .forEach(category => { tagsCategories.push(category); - if (!tagsIndex[category.tag].rootPart) - tagsRemaining.delete(category.tag); - - Object.values(category.index) - .map(node => node.tag) + [category.tag, ...Object.values(category.index).map(node => node.tag)] .filter(tag => !tagsIndex[tag].rootPart) .forEach(tag => tagsRemaining.delete(tag)); }); -- cgit v1.2.3