Projekt

Obecné

Profil

Stáhnout (5.23 KB) Statistiky
| Větev: | Tag: | Revize:
1 acb8a961 Dominik Poch
import React, { createContext, useEffect, useState } from 'react';
2 c057279b Lukáš Vlček
import { TagInstanceInfo } from '../api';
3 c73aecde Dominik Poch
import { Occurrence, Tag } from '../components/types/tag';
4 c057279b Lukáš Vlček
5 acb8a961 Dominik Poch
/**
6
 * Interface of an annotation context provider.
7
 */
8 c057279b Lukáš Vlček
interface IAnnotationContextProvider {
9 acb8a961 Dominik Poch
    /**
10
     * Tags managed by the context.
11
     */
12 c057279b Lukáš Vlček
    tags: TagInstanceInfo[] | null;
13 acb8a961 Dominik Poch
14
    /**
15
     * Sets new tags.
16
     * @param newTags An array of new tags.
17
     */
18 c057279b Lukáš Vlček
    setTags: (newTags: TagInstanceInfo[] | null) => void;
19 acb8a961 Dominik Poch
20
    /**
21
     * Adds occurrence to an annotation.
22
     * @param tag Tag whose occurrence should be added.
23
     */
24 c73aecde Dominik Poch
    addOccurrence: (tag: Tag) => void;
25 acb8a961 Dominik Poch
26
    /**
27
     * Changes visibility of an annotation.
28
     * @param tag Tag whose visibility should be changed.
29
     */
30 c057279b Lukáš Vlček
    changeVisibility: (tag: Tag) => void;
31 acb8a961 Dominik Poch
32
    /**
33
     * Deletes an occurrence of an annotation.
34
     * @param occurrence Occurrence that should be deleted.
35
     */
36 c057279b Lukáš Vlček
    deleteOccurrence: (occurrence: Occurrence) => void;
37 acb8a961 Dominik Poch
38
    /**
39
     * Changes a position of an occurrence of an annotation.
40
     * @param occurrence Occurrence whose position should be changed.
41
     * @param newValue New value of the position.
42
     */
43 c057279b Lukáš Vlček
    changePosition: (occurrence: Occurrence, newValue: number) => void;
44 acb8a961 Dominik Poch
45
    /**
46
     * Changes a length of an occurrence of an annotation.
47
     * @param occurrence Occurrence whose length should be changed.
48
     * @param newValue New value of the length.
49
     */
50 c057279b Lukáš Vlček
    changeLength: (occurrence: Occurrence, newValue: number) => void;
51
}
52
53 acb8a961 Dominik Poch
/**
54
 * The annotation context that manages active annotations.
55
 */
56 c057279b Lukáš Vlček
export const AnnotationContext = createContext<IAnnotationContextProvider>({
57 acb8a961 Dominik Poch
    /**
58
     * Default tags.
59
     */
60 c057279b Lukáš Vlček
    tags: null,
61 acb8a961 Dominik Poch
62
    /**
63
     * Default implementation of setTags method.
64
     * @param v Array of new tags.
65
     */
66 c057279b Lukáš Vlček
    setTags: (v) => {
67
        return;
68
    },
69 acb8a961 Dominik Poch
70
    /**
71
     * Default implementation of addOccurrence method.
72
     * @param tag The tag with new occurrence.
73
     */
74 c73aecde Dominik Poch
    addOccurrence: (tag: Tag) => {
75 c057279b Lukáš Vlček
        return;
76
    },
77 acb8a961 Dominik Poch
78
    /**
79
     * Default implementation of changeVisibility method.
80
     * @param tag The tag whose visibility should be changed.
81
     */
82 c057279b Lukáš Vlček
    changeVisibility: (tag: Tag) => {
83
        return;
84
    },
85 acb8a961 Dominik Poch
86
    /**
87
     * Default implementation of deleteOccurrence method.
88
     * @param occurrence Occurrence that should be deleted.
89
     */
90 c057279b Lukáš Vlček
    deleteOccurrence: (occurrence: Occurrence) => {
91
        return;
92
    },
93 acb8a961 Dominik Poch
94
    /**
95
     * Default implementation of changePosition method.
96
     * @param occurrence Occurrence whose position should be changed.
97
     * @param newValue A new position.
98
     */
99 c057279b Lukáš Vlček
    changePosition: (occurrence: Occurrence, newValue: number) => {
100
        return;
101
    },
102 acb8a961 Dominik Poch
103
    /**
104
     * Default implementation of changeLength method.
105
     * @param occurrence Occurrence whose length should be changed.
106
     * @param newValue A new length.
107
     */
108 c057279b Lukáš Vlček
    changeLength: (occurrence: Occurrence, newValue: number) => {
109
        return;
110
    },
111
});
112
113 acb8a961 Dominik Poch
/**
114
 * Provider of the annotation context.
115
 * @param props Children that should have access to the annotation context.
116
 * @returns Prepared html of the provider.
117
 */
118 c057279b Lukáš Vlček
const AnnotationProvider = (props: { children: React.ReactNode }) => {
119 acb8a961 Dominik Poch
    /**
120
     * Tags managed by the context.
121
     */
122 c057279b Lukáš Vlček
    const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
123
124 acb8a961 Dominik Poch
    /**
125
     * Default implementation of addOccurrence method.
126
     * @param tag The tag with new occurrence.
127
     */
128
    const addOccurrence = (tag: Tag) => {
129
        //TODO: Implement method (should use objects from server API)
130
    };
131
132
    /**
133
     * Changes visibility of an annotation.
134
     * @param tag Tag whose visibility should be changed.
135
     */
136
    const changeVisibility = (tag: Tag) => {
137
        //TODO: Implement method (should use objects from server API)
138
    };
139
140
    /**
141
     * Deletes an occurrence of an annotation.
142
     * @param occurrence Occurrence that should be deleted.
143
     */
144
    const deleteOccurrence = (occurrence: Occurrence) => {
145
        //TODO: Implement method (should use objects from server API)
146
    };
147
148
    /**
149
     * Changes a position of an occurrence of an annotation.
150
     * @param occurrence Occurrence whose position should be changed.
151
     * @param newValue New value of the position.
152
     */
153
    const changePosition = (occurrence: Occurrence, newValue: number) => {
154
        //TODO: Implement method (should use objects from server API)
155
    };
156
157
    /**
158
     * Changes a length of an occurrence of an annotation.
159
     * @param occurrence Occurrence whose length should be changed.
160
     * @param newValue New value of the length.
161
     */
162
    const changeLength = (occurrence: Occurrence, newValue: number) => {
163
        //TODO: Implement method (should use objects from server API)
164
    };
165
166
    /**
167
     * Initializes the context.
168
     */
169 c057279b Lukáš Vlček
    useEffect(() => {
170 acb8a961 Dominik Poch
        //TODO: Implement initialization with values from the server
171 c057279b Lukáš Vlček
    }, []);
172
173
    return (
174
        <AnnotationContext.Provider
175
            value={{
176
                tags,
177
                setTags,
178
                addOccurrence,
179
                changeVisibility,
180
                deleteOccurrence,
181
                changeLength,
182
                changePosition,
183
            }}
184
        >
185
            {props.children}
186
        </AnnotationContext.Provider>
187
    );
188
};
189
190
export default AnnotationProvider;