Projekt

Obecné

Profil

Stáhnout (6.71 KB) Statistiky
| Větev: | Tag: | Revize:
1 acb8a961 Dominik Poch
import React, { createContext, useEffect, useState } from 'react';
2 7652cf88 Lukáš Vlček
import { AnnotationInfo, TagInstanceInfo } from '../api';
3 9a41c02f Dominik Poch
import { Tag } from '../components/types/tag';
4 7652cf88 Lukáš Vlček
import { annotationController } from '../controllers';
5 c057279b Lukáš Vlček
6 acb8a961 Dominik Poch
/**
7
 * Interface of an annotation context provider.
8
 */
9 c057279b Lukáš Vlček
interface IAnnotationContextProvider {
10 acb8a961 Dominik Poch
    /**
11
     * Tags managed by the context.
12
     */
13 c057279b Lukáš Vlček
    tags: TagInstanceInfo[] | null;
14 acb8a961 Dominik Poch
15
    /**
16
     * Sets new tags.
17
     * @param newTags An array of new tags.
18
     */
19 c057279b Lukáš Vlček
    setTags: (newTags: TagInstanceInfo[] | null) => void;
20 acb8a961 Dominik Poch
21
    /**
22
     * Adds occurrence to an annotation.
23
     * @param tag Tag whose occurrence should be added.
24
     */
25 c73aecde Dominik Poch
    addOccurrence: (tag: Tag) => void;
26 acb8a961 Dominik Poch
27
    /**
28
     * Changes visibility of an annotation.
29
     * @param tag Tag whose visibility should be changed.
30
     */
31 c057279b Lukáš Vlček
    changeVisibility: (tag: Tag) => void;
32 acb8a961 Dominik Poch
33
    /**
34
     * Deletes an occurrence of an annotation.
35
     * @param occurrence Occurrence that should be deleted.
36
     */
37 9a41c02f Dominik Poch
    deleteOccurrence: (occurrence: TagInstanceInfo) => void;
38 acb8a961 Dominik Poch
39
    /**
40
     * Changes a position of an occurrence of an annotation.
41
     * @param occurrence Occurrence whose position should be changed.
42
     * @param newValue New value of the position.
43
     */
44 9a41c02f Dominik Poch
    changePosition: (occurrence: TagInstanceInfo, newValue: number) => void;
45 acb8a961 Dominik Poch
46
    /**
47
     * Changes a length of an occurrence of an annotation.
48
     * @param occurrence Occurrence whose length should be changed.
49
     * @param newValue New value of the length.
50
     */
51 9a41c02f Dominik Poch
    changeLength: (occurrence: TagInstanceInfo, newValue: number) => void;
52 7652cf88 Lukáš Vlček
53
    annotation: AnnotationInfo | null;
54 9a41c02f Dominik Poch
    mappedTags: Tag[] | null;
55 7652cf88 Lukáš Vlček
    refreshAnnotation: () => void;
56 c057279b Lukáš Vlček
}
57
58 acb8a961 Dominik Poch
/**
59
 * The annotation context that manages active annotations.
60
 */
61 c057279b Lukáš Vlček
export const AnnotationContext = createContext<IAnnotationContextProvider>({
62 acb8a961 Dominik Poch
    /**
63
     * Default tags.
64
     */
65 c057279b Lukáš Vlček
    tags: null,
66 acb8a961 Dominik Poch
67
    /**
68
     * Default implementation of setTags method.
69
     * @param v Array of new tags.
70
     */
71 c057279b Lukáš Vlček
    setTags: (v) => {
72
        return;
73
    },
74 acb8a961 Dominik Poch
75
    /**
76
     * Default implementation of addOccurrence method.
77
     * @param tag The tag with new occurrence.
78
     */
79 c73aecde Dominik Poch
    addOccurrence: (tag: Tag) => {
80 c057279b Lukáš Vlček
        return;
81
    },
82 acb8a961 Dominik Poch
83
    /**
84
     * Default implementation of changeVisibility method.
85
     * @param tag The tag whose visibility should be changed.
86
     */
87 c057279b Lukáš Vlček
    changeVisibility: (tag: Tag) => {
88
        return;
89
    },
90 acb8a961 Dominik Poch
91
    /**
92
     * Default implementation of deleteOccurrence method.
93
     * @param occurrence Occurrence that should be deleted.
94
     */
95 9a41c02f Dominik Poch
    deleteOccurrence: (occurrence: TagInstanceInfo) => {
96 c057279b Lukáš Vlček
        return;
97
    },
98 acb8a961 Dominik Poch
99
    /**
100
     * Default implementation of changePosition method.
101
     * @param occurrence Occurrence whose position should be changed.
102
     * @param newValue A new position.
103
     */
104 9a41c02f Dominik Poch
    changePosition: (occurrence: TagInstanceInfo, newValue: number) => {
105 c057279b Lukáš Vlček
        return;
106
    },
107 acb8a961 Dominik Poch
108
    /**
109
     * Default implementation of changeLength method.
110
     * @param occurrence Occurrence whose length should be changed.
111
     * @param newValue A new length.
112
     */
113 9a41c02f Dominik Poch
    changeLength: (occurrence: TagInstanceInfo, newValue: number) => {
114 c057279b Lukáš Vlček
        return;
115
    },
116 7652cf88 Lukáš Vlček
117
    annotation: null,
118 9a41c02f Dominik Poch
    mappedTags: null,
119 7652cf88 Lukáš Vlček
    refreshAnnotation: () => {
120
        return;
121
    },
122 c057279b Lukáš Vlček
});
123
124 acb8a961 Dominik Poch
/**
125
 * Provider of the annotation context.
126
 * @param props Children that should have access to the annotation context.
127
 * @returns Prepared html of the provider.
128
 */
129 7595035c Lukáš Vlček
const AnnotationProvider = (props: {
130
    children: React.ReactNode;
131
    annotationId: string;
132
}) => {
133 7652cf88 Lukáš Vlček
    const [annotation, setAnnotation] = useState<AnnotationInfo | null>(null);
134
135 acb8a961 Dominik Poch
    /**
136
     * Tags managed by the context.
137
     */
138 c057279b Lukáš Vlček
    const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
139
140 9a41c02f Dominik Poch
    const [mappedTags, setMappedTags] = useState<Tag[] | null>(null);
141
142 acb8a961 Dominik Poch
    /**
143
     * Default implementation of addOccurrence method.
144
     * @param tag The tag with new occurrence.
145
     */
146
    const addOccurrence = (tag: Tag) => {
147
        //TODO: Implement method (should use objects from server API)
148
    };
149
150
    /**
151
     * Changes visibility of an annotation.
152
     * @param tag Tag whose visibility should be changed.
153
     */
154
    const changeVisibility = (tag: Tag) => {
155
        //TODO: Implement method (should use objects from server API)
156
    };
157
158
    /**
159
     * Deletes an occurrence of an annotation.
160
     * @param occurrence Occurrence that should be deleted.
161
     */
162 9a41c02f Dominik Poch
    const deleteOccurrence = (occurrence: TagInstanceInfo) => {
163 acb8a961 Dominik Poch
        //TODO: Implement method (should use objects from server API)
164
    };
165
166
    /**
167
     * Changes a position of an occurrence of an annotation.
168
     * @param occurrence Occurrence whose position should be changed.
169
     * @param newValue New value of the position.
170
     */
171 9a41c02f Dominik Poch
    const changePosition = (occurrence: TagInstanceInfo, newValue: number) => {
172 acb8a961 Dominik Poch
        //TODO: Implement method (should use objects from server API)
173
    };
174
175
    /**
176
     * Changes a length of an occurrence of an annotation.
177
     * @param occurrence Occurrence whose length should be changed.
178
     * @param newValue New value of the length.
179
     */
180 9a41c02f Dominik Poch
    const changeLength = (occurrence: TagInstanceInfo, newValue: number) => {
181 acb8a961 Dominik Poch
        //TODO: Implement method (should use objects from server API)
182
    };
183
184 9a41c02f Dominik Poch
    const remapAnnotations = (data: AnnotationInfo) => {
185
        let map = new Map<number, Tag>();
186
        data.tagInstances?.forEach((tagInstance) => {
187
            if (map.has(tagInstance.instance ?? 0)) {
188
                let tag = map.get(tagInstance.instance ?? 0);
189
                tag!.occurrences = [...tag!.occurrences, tagInstance];
190
            } else {
191
                map.set(tagInstance.position ?? 0, {
192
                    name: tagInstance.tagName ?? '',
193
                    category: tagInstance.tagCategoryName ?? '',
194
                    visible: true,
195
                    occurrences: [tagInstance],
196
                });
197
            }
198
        });
199
200
        setMappedTags(Array.from(map.values()));
201
    };
202
203 7652cf88 Lukáš Vlček
    async function refreshAnnotation() {
204
        const data = await annotationController.annotationAnnotationIdGet(
205
            props.annotationId
206
        );
207
208 9a41c02f Dominik Poch
        remapAnnotations(data.data);
209 7652cf88 Lukáš Vlček
        setAnnotation(data.data ?? null);
210
    }
211
212 acb8a961 Dominik Poch
    /**
213
     * Initializes the context.
214
     */
215 c057279b Lukáš Vlček
    useEffect(() => {
216 7652cf88 Lukáš Vlček
        refreshAnnotation();
217 7595035c Lukáš Vlček
    }, [props.annotationId]);
218 c057279b Lukáš Vlček
219
    return (
220
        <AnnotationContext.Provider
221
            value={{
222
                tags,
223
                setTags,
224
                addOccurrence,
225
                changeVisibility,
226
                deleteOccurrence,
227
                changeLength,
228
                changePosition,
229 7652cf88 Lukáš Vlček
                refreshAnnotation,
230
                annotation,
231 9a41c02f Dominik Poch
                mappedTags,
232 c057279b Lukáš Vlček
            }}
233
        >
234
            {props.children}
235
        </AnnotationContext.Provider>
236
    );
237
};
238
239
export default AnnotationProvider;