diff options
author | Zero~Informatique | 2021-07-02 22:59:56 +0200 |
---|---|---|
committer | Zero~Informatique | 2021-07-03 00:05:22 +0200 |
commit | 92cb34b719b481faf417760f307241e8f6d777a9 (patch) | |
tree | ba33af35b8bb07e6c1c19b84f6c3af37c791584c /viewer/src/services | |
parent | 9165cc1efcf7791f78b61b2c51a9de651b1b09aa (diff) | |
download | ldgallery-92cb34b719b481faf417760f307241e8f6d777a9.tar.gz |
viewer: types normalization - tag.d.ts
GitHub: closes #301
Diffstat (limited to 'viewer/src/services')
-rw-r--r-- | viewer/src/services/indexfactory.ts | 29 | ||||
-rw-r--r-- | viewer/src/services/indexsearch.ts | 13 |
2 files changed, 22 insertions, 20 deletions
diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index 0c5fdc5..691a765 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts | |||
@@ -20,17 +20,18 @@ | |||
20 | import { Item, RawTag } from "@/@types/gallery"; | 20 | import { Item, RawTag } from "@/@types/gallery"; |
21 | import { ItemType } from "@/@types/ItemType"; | 21 | import { ItemType } from "@/@types/ItemType"; |
22 | import { Operation } from "@/@types/Operation"; | 22 | import { Operation } from "@/@types/Operation"; |
23 | import { TagCategory, TagIndex, TagNode, TagSearch } from "@/@types/tag"; | ||
23 | import Navigation from "@/services/navigation"; | 24 | import Navigation from "@/services/navigation"; |
24 | 25 | ||
25 | export default class IndexFactory { | 26 | export default class IndexFactory { |
26 | public static generateTags(root: Item | null): Tag.Index { | 27 | public static generateTags(root: Item | null): TagIndex { |
27 | const tagsIndex: Tag.Index = {}; | 28 | const tagsIndex: TagIndex = {}; |
28 | if (root) IndexFactory.pushTagsForItem(tagsIndex, root); | 29 | if (root) IndexFactory.pushTagsForItem(tagsIndex, root); |
29 | return tagsIndex; | 30 | return tagsIndex; |
30 | } | 31 | } |
31 | 32 | ||
32 | // Pushes all tags for a root item (and its children) to the index | 33 | // Pushes all tags for a root item (and its children) to the index |
33 | private static pushTagsForItem(tagsIndex: Tag.Index, item: Item): void { | 34 | private static pushTagsForItem(tagsIndex: TagIndex, item: Item): void { |
34 | if (item.properties.type === ItemType.DIRECTORY) { | 35 | if (item.properties.type === ItemType.DIRECTORY) { |
35 | item.properties.items.forEach(item => this.pushTagsForItem(tagsIndex, item)); | 36 | item.properties.items.forEach(item => this.pushTagsForItem(tagsIndex, item)); |
36 | return; // Directories are not indexed | 37 | return; // Directories are not indexed |
@@ -50,7 +51,7 @@ export default class IndexFactory { | |||
50 | } | 51 | } |
51 | } | 52 | } |
52 | 53 | ||
53 | private static pushPartToIndex(index: Tag.Node, part: string, item: Item, rootPart: boolean): Tag.Node { | 54 | private static pushPartToIndex(index: TagNode, part: string, item: Item, rootPart: boolean): TagNode { |
54 | if (!index) | 55 | if (!index) |
55 | index = { | 56 | index = { |
56 | tag: part, | 57 | tag: part, |
@@ -69,8 +70,8 @@ export default class IndexFactory { | |||
69 | 70 | ||
70 | // --- | 71 | // --- |
71 | 72 | ||
72 | public static searchTags(tagsIndex: Tag.Index, filter: string, strict: boolean): Tag.Search[] { | 73 | public static searchTags(tagsIndex: TagIndex, filter: string, strict: boolean): TagSearch[] { |
73 | let search: Tag.Search[] = []; | 74 | let search: TagSearch[] = []; |
74 | if (tagsIndex && filter) { | 75 | if (tagsIndex && filter) { |
75 | const operation = IndexFactory.extractOperation(filter); | 76 | const operation = IndexFactory.extractOperation(filter); |
76 | if (operation !== Operation.INTERSECTION) filter = filter.slice(1); | 77 | if (operation !== Operation.INTERSECTION) filter = filter.slice(1); |
@@ -96,12 +97,12 @@ export default class IndexFactory { | |||
96 | } | 97 | } |
97 | 98 | ||
98 | private static searchTagsFromFilterWithCategory( | 99 | private static searchTagsFromFilterWithCategory( |
99 | tagsIndex: Tag.Index, | 100 | tagsIndex: TagIndex, |
100 | operation: Operation, | 101 | operation: Operation, |
101 | category: string, | 102 | category: string, |
102 | disambiguation: string, | 103 | disambiguation: string, |
103 | strict: boolean | 104 | strict: boolean |
104 | ): Tag.Search[] { | 105 | ): TagSearch[] { |
105 | category = Navigation.normalize(category); | 106 | category = Navigation.normalize(category); |
106 | disambiguation = Navigation.normalize(disambiguation); | 107 | disambiguation = Navigation.normalize(disambiguation); |
107 | return Object.values(tagsIndex) | 108 | return Object.values(tagsIndex) |
@@ -114,28 +115,28 @@ export default class IndexFactory { | |||
114 | } | 115 | } |
115 | 116 | ||
116 | private static searchTagsFromFilter( | 117 | private static searchTagsFromFilter( |
117 | tagsIndex: Tag.Index, | 118 | tagsIndex: TagIndex, |
118 | operation: Operation, | 119 | operation: Operation, |
119 | filter: string, | 120 | filter: string, |
120 | strict: boolean | 121 | strict: boolean |
121 | ): Tag.Search[] { | 122 | ): TagSearch[] { |
122 | filter = Navigation.normalize(filter); | 123 | filter = Navigation.normalize(filter); |
123 | return Object.values(tagsIndex) | 124 | return Object.values(tagsIndex) |
124 | .filter(node => IndexFactory.matches(node, filter, strict)) | 125 | .filter(node => IndexFactory.matches(node, filter, strict)) |
125 | .map(node => ({ ...node, operation, display: `${operation}${node.tag}` })); | 126 | .map(node => ({ ...node, operation, display: `${operation}${node.tag}` })); |
126 | } | 127 | } |
127 | 128 | ||
128 | private static matches(node: Tag.Node, filter: string, strict: boolean): boolean { | 129 | private static matches(node: TagNode, filter: string, strict: boolean): boolean { |
129 | if (strict) return node.tagfiltered === filter; | 130 | if (strict) return node.tagfiltered === filter; |
130 | return node.tagfiltered.includes(filter); | 131 | return node.tagfiltered.includes(filter); |
131 | } | 132 | } |
132 | 133 | ||
133 | // --- | 134 | // --- |
134 | 135 | ||
135 | public static generateCategories(tagsIndex: Tag.Index, categoryTags?: RawTag[]): Tag.Category[] { | 136 | public static generateCategories(tagsIndex: TagIndex, categoryTags?: RawTag[]): TagCategory[] { |
136 | if (!categoryTags?.length) return [{ tag: "", index: tagsIndex }]; | 137 | if (!categoryTags?.length) return [{ tag: "", index: tagsIndex }]; |
137 | 138 | ||
138 | const tagsCategories: Tag.Category[] = []; | 139 | const tagsCategories: TagCategory[] = []; |
139 | const tagsRemaining = new Map(Object.entries(tagsIndex)); | 140 | const tagsRemaining = new Map(Object.entries(tagsIndex)); |
140 | categoryTags | 141 | categoryTags |
141 | .map(tag => ({ tag, index: tagsIndex[tag]?.children })) | 142 | .map(tag => ({ tag, index: tagsIndex[tag]?.children })) |
@@ -150,7 +151,7 @@ export default class IndexFactory { | |||
150 | return tagsCategories; | 151 | return tagsCategories; |
151 | } | 152 | } |
152 | 153 | ||
153 | private static isDiscriminantTagOnly(tags: RawTag[], node: Tag.Node): boolean { | 154 | private static isDiscriminantTagOnly(tags: RawTag[], node: TagNode): boolean { |
154 | return !tags.includes(node.tag) || !node.childPart; | 155 | return !tags.includes(node.tag) || !node.childPart; |
155 | } | 156 | } |
156 | } | 157 | } |
diff --git a/viewer/src/services/indexsearch.ts b/viewer/src/services/indexsearch.ts index eda1b27..57bd03c 100644 --- a/viewer/src/services/indexsearch.ts +++ b/viewer/src/services/indexsearch.ts | |||
@@ -19,25 +19,26 @@ | |||
19 | 19 | ||
20 | import { Item } from "@/@types/gallery"; | 20 | import { Item } from "@/@types/gallery"; |
21 | import { Operation } from "@/@types/Operation"; | 21 | import { Operation } from "@/@types/Operation"; |
22 | import { TagSearch, TagSearchByOperation } from "@/@types/tag"; | ||
22 | 23 | ||
23 | export default class IndexSearch { | 24 | export default class IndexSearch { |
24 | // Results of the search (by tags) | 25 | // Results of the search (by tags) |
25 | public static search(searchTags: Tag.Search[]): Item[] { | 26 | public static search(searchTags: TagSearch[]): Item[] { |
26 | const byOperation = this.extractTagsByOperation(searchTags); | 27 | const byOperation = this.extractTagsByOperation(searchTags); |
27 | const intersection = this.extractIntersection(byOperation); | 28 | const intersection = this.extractIntersection(byOperation); |
28 | const substraction = this.extractSubstraction(byOperation); | 29 | const substraction = this.extractSubstraction(byOperation); |
29 | return this.aggregateAll(byOperation, intersection, substraction); | 30 | return this.aggregateAll(byOperation, intersection, substraction); |
30 | } | 31 | } |
31 | 32 | ||
32 | private static extractTagsByOperation(searchTags: Tag.Search[]): Tag.SearchByOperation { | 33 | private static extractTagsByOperation(searchTags: TagSearch[]): TagSearchByOperation { |
33 | const byOperation: Tag.SearchByOperation = {}; | 34 | const byOperation: TagSearchByOperation = {}; |
34 | Object.values(Operation).forEach( | 35 | Object.values(Operation).forEach( |
35 | operation => (byOperation[operation] = searchTags.filter(tag => tag.operation === operation)) | 36 | operation => (byOperation[operation] = searchTags.filter(tag => tag.operation === operation)) |
36 | ); | 37 | ); |
37 | return byOperation; | 38 | return byOperation; |
38 | } | 39 | } |
39 | 40 | ||
40 | private static extractIntersection(byOperation: Tag.SearchByOperation): Set<Item> { | 41 | private static extractIntersection(byOperation: TagSearchByOperation): Set<Item> { |
41 | const intersection = new Set<Item>(); | 42 | const intersection = new Set<Item>(); |
42 | if (byOperation[Operation.INTERSECTION].length > 0) { | 43 | if (byOperation[Operation.INTERSECTION].length > 0) { |
43 | byOperation[Operation.INTERSECTION] | 44 | byOperation[Operation.INTERSECTION] |
@@ -49,7 +50,7 @@ export default class IndexSearch { | |||
49 | return intersection; | 50 | return intersection; |
50 | } | 51 | } |
51 | 52 | ||
52 | private static extractSubstraction(byOperation: Tag.SearchByOperation): Set<Item> { | 53 | private static extractSubstraction(byOperation: TagSearchByOperation): Set<Item> { |
53 | const substraction = new Set<Item>(); | 54 | const substraction = new Set<Item>(); |
54 | if (byOperation[Operation.SUBSTRACTION].length > 0) { | 55 | if (byOperation[Operation.SUBSTRACTION].length > 0) { |
55 | byOperation[Operation.SUBSTRACTION].flatMap(tag => tag.items).forEach(item => substraction.add(item)); | 56 | byOperation[Operation.SUBSTRACTION].flatMap(tag => tag.items).forEach(item => substraction.add(item)); |
@@ -58,7 +59,7 @@ export default class IndexSearch { | |||
58 | } | 59 | } |
59 | 60 | ||
60 | private static aggregateAll( | 61 | private static aggregateAll( |
61 | byOperation: Tag.SearchByOperation, | 62 | byOperation: TagSearchByOperation, |
62 | intersection: Set<Item>, | 63 | intersection: Set<Item>, |
63 | substraction: Set<Item> | 64 | substraction: Set<Item> |
64 | ): Item[] { | 65 | ): Item[] { |