Projekt

Obecné

Profil

Stáhnout (6.71 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { createContext, useEffect, useState } from 'react';
2
import { AnnotationInfo, TagInstanceInfo } from '../api';
3
import { Tag } from '../components/types/tag';
4
import { annotationController } from '../controllers';
5

    
6
/**
7
 * Interface of an annotation context provider.
8
 */
9
interface IAnnotationContextProvider {
10
    /**
11
     * Tags managed by the context.
12
     */
13
    tags: TagInstanceInfo[] | null;
14

    
15
    /**
16
     * Sets new tags.
17
     * @param newTags An array of new tags.
18
     */
19
    setTags: (newTags: TagInstanceInfo[] | null) => void;
20

    
21
    /**
22
     * Adds occurrence to an annotation.
23
     * @param tag Tag whose occurrence should be added.
24
     */
25
    addOccurrence: (tag: Tag) => void;
26

    
27
    /**
28
     * Changes visibility of an annotation.
29
     * @param tag Tag whose visibility should be changed.
30
     */
31
    changeVisibility: (tag: Tag) => void;
32

    
33
    /**
34
     * Deletes an occurrence of an annotation.
35
     * @param occurrence Occurrence that should be deleted.
36
     */
37
    deleteOccurrence: (occurrence: TagInstanceInfo) => void;
38

    
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
    changePosition: (occurrence: TagInstanceInfo, newValue: number) => void;
45

    
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
    changeLength: (occurrence: TagInstanceInfo, newValue: number) => void;
52

    
53
    annotation: AnnotationInfo | null;
54
    mappedTags: Tag[] | null;
55
    refreshAnnotation: () => void;
56
}
57

    
58
/**
59
 * The annotation context that manages active annotations.
60
 */
61
export const AnnotationContext = createContext<IAnnotationContextProvider>({
62
    /**
63
     * Default tags.
64
     */
65
    tags: null,
66

    
67
    /**
68
     * Default implementation of setTags method.
69
     * @param v Array of new tags.
70
     */
71
    setTags: (v) => {
72
        return;
73
    },
74

    
75
    /**
76
     * Default implementation of addOccurrence method.
77
     * @param tag The tag with new occurrence.
78
     */
79
    addOccurrence: (tag: Tag) => {
80
        return;
81
    },
82

    
83
    /**
84
     * Default implementation of changeVisibility method.
85
     * @param tag The tag whose visibility should be changed.
86
     */
87
    changeVisibility: (tag: Tag) => {
88
        return;
89
    },
90

    
91
    /**
92
     * Default implementation of deleteOccurrence method.
93
     * @param occurrence Occurrence that should be deleted.
94
     */
95
    deleteOccurrence: (occurrence: TagInstanceInfo) => {
96
        return;
97
    },
98

    
99
    /**
100
     * Default implementation of changePosition method.
101
     * @param occurrence Occurrence whose position should be changed.
102
     * @param newValue A new position.
103
     */
104
    changePosition: (occurrence: TagInstanceInfo, newValue: number) => {
105
        return;
106
    },
107

    
108
    /**
109
     * Default implementation of changeLength method.
110
     * @param occurrence Occurrence whose length should be changed.
111
     * @param newValue A new length.
112
     */
113
    changeLength: (occurrence: TagInstanceInfo, newValue: number) => {
114
        return;
115
    },
116

    
117
    annotation: null,
118
    mappedTags: null,
119
    refreshAnnotation: () => {
120
        return;
121
    },
122
});
123

    
124
/**
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
const AnnotationProvider = (props: {
130
    children: React.ReactNode;
131
    annotationId: string;
132
}) => {
133
    const [annotation, setAnnotation] = useState<AnnotationInfo | null>(null);
134

    
135
    /**
136
     * Tags managed by the context.
137
     */
138
    const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
139

    
140
    const [mappedTags, setMappedTags] = useState<Tag[] | null>(null);
141

    
142
    /**
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
    const deleteOccurrence = (occurrence: TagInstanceInfo) => {
163
        //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
    const changePosition = (occurrence: TagInstanceInfo, newValue: number) => {
172
        //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
    const changeLength = (occurrence: TagInstanceInfo, newValue: number) => {
181
        //TODO: Implement method (should use objects from server API)
182
    };
183

    
184
    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
    async function refreshAnnotation() {
204
        const data = await annotationController.annotationAnnotationIdGet(
205
            props.annotationId
206
        );
207

    
208
        remapAnnotations(data.data);
209
        setAnnotation(data.data ?? null);
210
    }
211

    
212
    /**
213
     * Initializes the context.
214
     */
215
    useEffect(() => {
216
        refreshAnnotation();
217
    }, [props.annotationId]);
218

    
219
    return (
220
        <AnnotationContext.Provider
221
            value={{
222
                tags,
223
                setTags,
224
                addOccurrence,
225
                changeVisibility,
226
                deleteOccurrence,
227
                changeLength,
228
                changePosition,
229
                refreshAnnotation,
230
                annotation,
231
                mappedTags,
232
            }}
233
        >
234
            {props.children}
235
        </AnnotationContext.Provider>
236
    );
237
};
238

    
239
export default AnnotationProvider;
(1-1/2)