Projekt

Obecné

Profil

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

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

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

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

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

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

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

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

    
53
/**
54
 * The annotation context that manages active annotations.
55
 */
56
export const AnnotationContext = createContext<IAnnotationContextProvider>({
57
    /**
58
     * Default tags.
59
     */
60
    tags: null,
61

    
62
    /**
63
     * Default implementation of setTags method.
64
     * @param v Array of new tags.
65
     */
66
    setTags: (v) => {
67
        return;
68
    },
69

    
70
    /**
71
     * Default implementation of addOccurrence method.
72
     * @param tag The tag with new occurrence.
73
     */
74
    addOccurrence: (tag: Tag) => {
75
        return;
76
    },
77

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

    
86
    /**
87
     * Default implementation of deleteOccurrence method.
88
     * @param occurrence Occurrence that should be deleted.
89
     */
90
    deleteOccurrence: (occurrence: Occurrence) => {
91
        return;
92
    },
93

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

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

    
113
/**
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
const AnnotationProvider = (props: { children: React.ReactNode }) => {
119
    /**
120
     * Tags managed by the context.
121
     */
122
    const [tags, setTags] = useState<TagInstanceInfo[] | null>(null);
123

    
124
    /**
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
    useEffect(() => {
170
        //TODO: Implement initialization with values from the server
171
    }, []);
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;
(1-1/2)