Projekt

Obecné

Profil

Stáhnout (5.8 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 c73aecde Dominik Poch
import { Occurrence, 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 c057279b Lukáš Vlček
    deleteOccurrence: (occurrence: Occurrence) => 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 c057279b Lukáš Vlček
    changePosition: (occurrence: Occurrence, 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 c057279b Lukáš Vlček
    changeLength: (occurrence: Occurrence, newValue: number) => void;
52 7652cf88 Lukáš Vlček
53
    annotation: AnnotationInfo | null;
54
    refreshAnnotation: () => void;
55 c057279b Lukáš Vlček
}
56
57 acb8a961 Dominik Poch
/**
58
 * The annotation context that manages active annotations.
59
 */
60 c057279b Lukáš Vlček
export const AnnotationContext = createContext<IAnnotationContextProvider>({
61 acb8a961 Dominik Poch
    /**
62
     * Default tags.
63
     */
64 c057279b Lukáš Vlček
    tags: null,
65 acb8a961 Dominik Poch
66
    /**
67
     * Default implementation of setTags method.
68
     * @param v Array of new tags.
69
     */
70 c057279b Lukáš Vlček
    setTags: (v) => {
71
        return;
72
    },
73 acb8a961 Dominik Poch
74
    /**
75
     * Default implementation of addOccurrence method.
76
     * @param tag The tag with new occurrence.
77
     */
78 c73aecde Dominik Poch
    addOccurrence: (tag: Tag) => {
79 c057279b Lukáš Vlček
        return;
80
    },
81 acb8a961 Dominik Poch
82
    /**
83
     * Default implementation of changeVisibility method.
84
     * @param tag The tag whose visibility should be changed.
85
     */
86 c057279b Lukáš Vlček
    changeVisibility: (tag: Tag) => {
87
        return;
88
    },
89 acb8a961 Dominik Poch
90
    /**
91
     * Default implementation of deleteOccurrence method.
92
     * @param occurrence Occurrence that should be deleted.
93
     */
94 c057279b Lukáš Vlček
    deleteOccurrence: (occurrence: Occurrence) => {
95
        return;
96
    },
97 acb8a961 Dominik Poch
98
    /**
99
     * Default implementation of changePosition method.
100
     * @param occurrence Occurrence whose position should be changed.
101
     * @param newValue A new position.
102
     */
103 c057279b Lukáš Vlček
    changePosition: (occurrence: Occurrence, newValue: number) => {
104
        return;
105
    },
106 acb8a961 Dominik Poch
107
    /**
108
     * Default implementation of changeLength method.
109
     * @param occurrence Occurrence whose length should be changed.
110
     * @param newValue A new length.
111
     */
112 c057279b Lukáš Vlček
    changeLength: (occurrence: Occurrence, newValue: number) => {
113
        return;
114
    },
115 7652cf88 Lukáš Vlček
116
    annotation: null,
117
    refreshAnnotation: () => {
118
        return;
119
    },
120 c057279b Lukáš Vlček
});
121
122 acb8a961 Dominik Poch
/**
123
 * Provider of the annotation context.
124
 * @param props Children that should have access to the annotation context.
125
 * @returns Prepared html of the provider.
126
 */
127 7595035c Lukáš Vlček
const AnnotationProvider = (props: {
128
    children: React.ReactNode;
129
    annotationId: string;
130
}) => {
131 7652cf88 Lukáš Vlček
    const [annotation, setAnnotation] = useState<AnnotationInfo | null>(null);
132
133 acb8a961 Dominik Poch
    /**
134
     * Tags managed by the context.
135
     */
136 c057279b Lukáš Vlček
    const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
137
138 acb8a961 Dominik Poch
    /**
139
     * Default implementation of addOccurrence method.
140
     * @param tag The tag with new occurrence.
141
     */
142
    const addOccurrence = (tag: Tag) => {
143
        //TODO: Implement method (should use objects from server API)
144
    };
145
146
    /**
147
     * Changes visibility of an annotation.
148
     * @param tag Tag whose visibility should be changed.
149
     */
150
    const changeVisibility = (tag: Tag) => {
151
        //TODO: Implement method (should use objects from server API)
152
    };
153
154
    /**
155
     * Deletes an occurrence of an annotation.
156
     * @param occurrence Occurrence that should be deleted.
157
     */
158
    const deleteOccurrence = (occurrence: Occurrence) => {
159
        //TODO: Implement method (should use objects from server API)
160
    };
161
162
    /**
163
     * Changes a position of an occurrence of an annotation.
164
     * @param occurrence Occurrence whose position should be changed.
165
     * @param newValue New value of the position.
166
     */
167
    const changePosition = (occurrence: Occurrence, newValue: number) => {
168
        //TODO: Implement method (should use objects from server API)
169
    };
170
171
    /**
172
     * Changes a length of an occurrence of an annotation.
173
     * @param occurrence Occurrence whose length should be changed.
174
     * @param newValue New value of the length.
175
     */
176
    const changeLength = (occurrence: Occurrence, newValue: number) => {
177
        //TODO: Implement method (should use objects from server API)
178
    };
179
180 7652cf88 Lukáš Vlček
    async function refreshAnnotation() {
181
        const data = await annotationController.annotationAnnotationIdGet(
182
            props.annotationId
183
        );
184
185
        setAnnotation(data.data ?? null);
186
    }
187
188 acb8a961 Dominik Poch
    /**
189
     * Initializes the context.
190
     */
191 c057279b Lukáš Vlček
    useEffect(() => {
192 7652cf88 Lukáš Vlček
        refreshAnnotation();
193 7595035c Lukáš Vlček
    }, [props.annotationId]);
194 c057279b Lukáš Vlček
195
    return (
196
        <AnnotationContext.Provider
197
            value={{
198
                tags,
199
                setTags,
200
                addOccurrence,
201
                changeVisibility,
202
                deleteOccurrence,
203
                changeLength,
204
                changePosition,
205 7652cf88 Lukáš Vlček
                refreshAnnotation,
206
                annotation,
207 c057279b Lukáš Vlček
            }}
208
        >
209
            {props.children}
210
        </AnnotationContext.Provider>
211
    );
212
};
213
214
export default AnnotationProvider;