diff options
Diffstat (limited to 'viewer/src/services')
-rw-r--r-- | viewer/src/services/indexfactory.ts | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index a31f3ef..25027f3 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts | |||
@@ -38,18 +38,19 @@ export default class IndexFactory { | |||
38 | const parts = tag.split(':'); | 38 | const parts = tag.split(':'); |
39 | let lastPart: string | null = null; | 39 | let lastPart: string | null = null; |
40 | for (const part of parts) { | 40 | for (const part of parts) { |
41 | tagsIndex[part] = IndexFactory.pushPartToIndex(tagsIndex[part], part, item); | 41 | tagsIndex[part] = IndexFactory.pushPartToIndex(tagsIndex[part], part, item, !Boolean(lastPart)); |
42 | if (lastPart) { | 42 | if (lastPart) { |
43 | const children = tagsIndex[lastPart].children; | 43 | const children = tagsIndex[lastPart].children; |
44 | children[part] = IndexFactory.pushPartToIndex(children[part], part, item); | 44 | children[part] = IndexFactory.pushPartToIndex(children[part], part, item, false); |
45 | } | 45 | } |
46 | lastPart = part; | 46 | lastPart = part; |
47 | } | 47 | } |
48 | } | 48 | } |
49 | } | 49 | } |
50 | 50 | ||
51 | private static pushPartToIndex(index: Tag.Node, part: string, item: Gallery.Item): Tag.Node { | 51 | private static pushPartToIndex(index: Tag.Node, part: string, item: Gallery.Item, rootPart: boolean): Tag.Node { |
52 | if (!index) index = { tag: part, tagfiltered: Navigation.normalize(part), items: [], children: {} }; | 52 | if (!index) index = { tag: part, tagfiltered: Navigation.normalize(part), rootPart, items: [], children: {} }; |
53 | else if (rootPart) index.rootPart = true; | ||
53 | if (!index.items.includes(item)) index.items.push(item); | 54 | if (!index.items.includes(item)) index.items.push(item); |
54 | return index; | 55 | return index; |
55 | } | 56 | } |
@@ -112,4 +113,25 @@ export default class IndexFactory { | |||
112 | if (strict) return node.tagfiltered === filter; | 113 | if (strict) return node.tagfiltered === filter; |
113 | return node.tagfiltered.includes(filter) | 114 | return node.tagfiltered.includes(filter) |
114 | } | 115 | } |
116 | |||
117 | // --- | ||
118 | |||
119 | public static generateCategories(tagsIndex: Tag.Index, tags?: Gallery.RawTag[]): Tag.Category[] { | ||
120 | if (!tags?.length) return [{ tag: "", index: tagsIndex }]; | ||
121 | |||
122 | const tagsCategories: Tag.Category[] = []; | ||
123 | const tagsRemaining = new Map(Object.entries(tagsIndex)); | ||
124 | tags | ||
125 | .map(tag => ({ tag, index: tagsIndex[tag]?.children })) | ||
126 | .filter(category => category.index && Object.keys(category.index).length) | ||
127 | .forEach(category => { | ||
128 | tagsCategories.push(category); | ||
129 | |||
130 | [category.tag, ...Object.values(category.index).map(node => node.tag)] | ||
131 | .filter(tag => !tagsIndex[tag].rootPart) | ||
132 | .forEach(tag => tagsRemaining.delete(tag)); | ||
133 | }); | ||
134 | tagsCategories.push({ tag: "", index: Object.fromEntries(tagsRemaining) }); | ||
135 | return tagsCategories; | ||
136 | } | ||
115 | } | 137 | } |