aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/indexfactory.ts
diff options
context:
space:
mode:
authorzeroinformatique2021-07-03 00:48:53 +0200
committerGitHub2021-07-03 00:48:53 +0200
commitb6605e2c4ee73ac8b994624098344db5e44ac07d (patch)
tree5ed06cc5ecdabe070f6fdb9bc4f9a8a3b435cbe6 /viewer/src/services/indexfactory.ts
parent08ac32103fb5f8cca1861267dfd07a7c0d2faf62 (diff)
parent1f0377c1b4c2959c73fe4e368673f057ef369917 (diff)
downloadldgallery-b6605e2c4ee73ac8b994624098344db5e44ac07d.tar.gz
Merge pull request #302 from ldgallery/oz-types-normalization
viewer: types normalization
Diffstat (limited to 'viewer/src/services/indexfactory.ts')
-rw-r--r--viewer/src/services/indexfactory.ts32
1 files changed, 17 insertions, 15 deletions
diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts
index 4b28a60..691a765 100644
--- a/viewer/src/services/indexfactory.ts
+++ b/viewer/src/services/indexfactory.ts
@@ -17,19 +17,21 @@
17-- along with this program. If not, see <https://www.gnu.org/licenses/>. 17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/ 18*/
19 19
20import { Operation } from "@/@types/Operation"; 20import { Item, RawTag } from "@/@types/gallery";
21import { ItemType } from "@/@types/ItemType"; 21import { ItemType } from "@/@types/ItemType";
22import { Operation } from "@/@types/Operation";
23import { TagCategory, TagIndex, TagNode, TagSearch } from "@/@types/tag";
22import Navigation from "@/services/navigation"; 24import Navigation from "@/services/navigation";
23 25
24export default class IndexFactory { 26export default class IndexFactory {
25 public static generateTags(root: Gallery.Item | null): Tag.Index { 27 public static generateTags(root: Item | null): TagIndex {
26 const tagsIndex: Tag.Index = {}; 28 const tagsIndex: TagIndex = {};
27 if (root) IndexFactory.pushTagsForItem(tagsIndex, root); 29 if (root) IndexFactory.pushTagsForItem(tagsIndex, root);
28 return tagsIndex; 30 return tagsIndex;
29 } 31 }
30 32
31 // 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
32 private static pushTagsForItem(tagsIndex: Tag.Index, item: Gallery.Item): void { 34 private static pushTagsForItem(tagsIndex: TagIndex, item: Item): void {
33 if (item.properties.type === ItemType.DIRECTORY) { 35 if (item.properties.type === ItemType.DIRECTORY) {
34 item.properties.items.forEach(item => this.pushTagsForItem(tagsIndex, item)); 36 item.properties.items.forEach(item => this.pushTagsForItem(tagsIndex, item));
35 return; // Directories are not indexed 37 return; // Directories are not indexed
@@ -49,7 +51,7 @@ export default class IndexFactory {
49 } 51 }
50 } 52 }
51 53
52 private static pushPartToIndex(index: Tag.Node, part: string, item: Gallery.Item, rootPart: boolean): Tag.Node { 54 private static pushPartToIndex(index: TagNode, part: string, item: Item, rootPart: boolean): TagNode {
53 if (!index) 55 if (!index)
54 index = { 56 index = {
55 tag: part, 57 tag: part,
@@ -68,8 +70,8 @@ export default class IndexFactory {
68 70
69 // --- 71 // ---
70 72
71 public static searchTags(tagsIndex: Tag.Index, filter: string, strict: boolean): Tag.Search[] { 73 public static searchTags(tagsIndex: TagIndex, filter: string, strict: boolean): TagSearch[] {
72 let search: Tag.Search[] = []; 74 let search: TagSearch[] = [];
73 if (tagsIndex && filter) { 75 if (tagsIndex && filter) {
74 const operation = IndexFactory.extractOperation(filter); 76 const operation = IndexFactory.extractOperation(filter);
75 if (operation !== Operation.INTERSECTION) filter = filter.slice(1); 77 if (operation !== Operation.INTERSECTION) filter = filter.slice(1);
@@ -95,12 +97,12 @@ export default class IndexFactory {
95 } 97 }
96 98
97 private static searchTagsFromFilterWithCategory( 99 private static searchTagsFromFilterWithCategory(
98 tagsIndex: Tag.Index, 100 tagsIndex: TagIndex,
99 operation: Operation, 101 operation: Operation,
100 category: string, 102 category: string,
101 disambiguation: string, 103 disambiguation: string,
102 strict: boolean 104 strict: boolean
103 ): Tag.Search[] { 105 ): TagSearch[] {
104 category = Navigation.normalize(category); 106 category = Navigation.normalize(category);
105 disambiguation = Navigation.normalize(disambiguation); 107 disambiguation = Navigation.normalize(disambiguation);
106 return Object.values(tagsIndex) 108 return Object.values(tagsIndex)
@@ -113,28 +115,28 @@ export default class IndexFactory {
113 } 115 }
114 116
115 private static searchTagsFromFilter( 117 private static searchTagsFromFilter(
116 tagsIndex: Tag.Index, 118 tagsIndex: TagIndex,
117 operation: Operation, 119 operation: Operation,
118 filter: string, 120 filter: string,
119 strict: boolean 121 strict: boolean
120 ): Tag.Search[] { 122 ): TagSearch[] {
121 filter = Navigation.normalize(filter); 123 filter = Navigation.normalize(filter);
122 return Object.values(tagsIndex) 124 return Object.values(tagsIndex)
123 .filter(node => IndexFactory.matches(node, filter, strict)) 125 .filter(node => IndexFactory.matches(node, filter, strict))
124 .map(node => ({ ...node, operation, display: `${operation}${node.tag}` })); 126 .map(node => ({ ...node, operation, display: `${operation}${node.tag}` }));
125 } 127 }
126 128
127 private static matches(node: Tag.Node, filter: string, strict: boolean): boolean { 129 private static matches(node: TagNode, filter: string, strict: boolean): boolean {
128 if (strict) return node.tagfiltered === filter; 130 if (strict) return node.tagfiltered === filter;
129 return node.tagfiltered.includes(filter); 131 return node.tagfiltered.includes(filter);
130 } 132 }
131 133
132 // --- 134 // ---
133 135
134 public static generateCategories(tagsIndex: Tag.Index, categoryTags?: Gallery.RawTag[]): Tag.Category[] { 136 public static generateCategories(tagsIndex: TagIndex, categoryTags?: RawTag[]): TagCategory[] {
135 if (!categoryTags?.length) return [{ tag: "", index: tagsIndex }]; 137 if (!categoryTags?.length) return [{ tag: "", index: tagsIndex }];
136 138
137 const tagsCategories: Tag.Category[] = []; 139 const tagsCategories: TagCategory[] = [];
138 const tagsRemaining = new Map(Object.entries(tagsIndex)); 140 const tagsRemaining = new Map(Object.entries(tagsIndex));
139 categoryTags 141 categoryTags
140 .map(tag => ({ tag, index: tagsIndex[tag]?.children })) 142 .map(tag => ({ tag, index: tagsIndex[tag]?.children }))
@@ -149,7 +151,7 @@ export default class IndexFactory {
149 return tagsCategories; 151 return tagsCategories;
150 } 152 }
151 153
152 private static isDiscriminantTagOnly(tags: Gallery.RawTag[], node: Tag.Node): boolean { 154 private static isDiscriminantTagOnly(tags: RawTag[], node: TagNode): boolean {
153 return !tags.includes(node.tag) || !node.childPart; 155 return !tags.includes(node.tag) || !node.childPart;
154 } 156 }
155} 157}