aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/indexfactory.ts
diff options
context:
space:
mode:
authorZero~Informatique2021-07-02 22:59:56 +0200
committerZero~Informatique2021-07-03 00:05:22 +0200
commit92cb34b719b481faf417760f307241e8f6d777a9 (patch)
treeba33af35b8bb07e6c1c19b84f6c3af37c791584c /viewer/src/services/indexfactory.ts
parent9165cc1efcf7791f78b61b2c51a9de651b1b09aa (diff)
downloadldgallery-92cb34b719b481faf417760f307241e8f6d777a9.tar.gz
viewer: types normalization - tag.d.ts
GitHub: closes #301
Diffstat (limited to 'viewer/src/services/indexfactory.ts')
-rw-r--r--viewer/src/services/indexfactory.ts29
1 files changed, 15 insertions, 14 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 @@
20import { Item, RawTag } from "@/@types/gallery"; 20import { Item, RawTag } from "@/@types/gallery";
21import { ItemType } from "@/@types/ItemType"; 21import { ItemType } from "@/@types/ItemType";
22import { Operation } from "@/@types/Operation"; 22import { Operation } from "@/@types/Operation";
23import { TagCategory, TagIndex, TagNode, TagSearch } from "@/@types/tag";
23import Navigation from "@/services/navigation"; 24import Navigation from "@/services/navigation";
24 25
25export default class IndexFactory { 26export 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}