Projekt

Obecné

Profil

Stáhnout (5.8 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { createContext, useEffect, useState } from 'react';
2
import { AnnotationInfo, TagInstanceInfo } from '../api';
3
import { Occurrence, 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: Occurrence) => 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: Occurrence, 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: Occurrence, newValue: number) => void;
52

    
53
    annotation: AnnotationInfo | null;
54
    refreshAnnotation: () => void;
55
}
56

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

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

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

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

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

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

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

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

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

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

    
138
    /**
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
    async function refreshAnnotation() {
181
        const data = await annotationController.annotationAnnotationIdGet(
182
            props.annotationId
183
        );
184

    
185
        setAnnotation(data.data ?? null);
186
    }
187

    
188
    /**
189
     * Initializes the context.
190
     */
191
    useEffect(() => {
192
        refreshAnnotation();
193
    }, [props.annotationId]);
194

    
195
    return (
196
        <AnnotationContext.Provider
197
            value={{
198
                tags,
199
                setTags,
200
                addOccurrence,
201
                changeVisibility,
202
                deleteOccurrence,
203
                changeLength,
204
                changePosition,
205
                refreshAnnotation,
206
                annotation,
207
            }}
208
        >
209
            {props.children}
210
        </AnnotationContext.Provider>
211
    );
212
};
213

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