Projekt

Obecné

Profil

Stáhnout (6.21 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Col, Container, Row, Stack } from 'react-bootstrap';
2
import { Occurrence, Tag } from '../types/tag';
3
import { ChangeEvent, useContext, useState } from 'react';
4
import 'antd/dist/antd.css';
5
import { Button, Input } from 'antd';
6
import {
7
    PlusOutlined,
8
    EyeOutlined,
9
    DownOutlined,
10
    DeleteOutlined,
11
} from '@ant-design/icons';
12
import { AnnotationContext } from '../../contexts/AnnotationContext';
13

    
14
/**
15
 * Creates a single item in an annotation panel.
16
 * @param props Properties should contain a tag that will be shown in this annotation.
17
 * @returns The item that represents an annotation.
18
 */
19
export function AnnotationItem(props: { tag: Tag }) {
20
    /**
21
     * Should properties of this annotation be visible?
22
     */
23
    const [visibleProperties, setVisibleProperties] = useState(false);
24

    
25
    /**
26
     * Context that manages annotations.
27
     */
28
    const {
29
        addOccurrence,
30
        deleteOccurrence,
31
        changeVisibility,
32
        changePosition,
33
        changeLength,
34
    } = useContext(AnnotationContext);
35

    
36
    /**
37
     * Adds new occurrence of this annotation to the context.
38
     */
39
    const onAddOccurrence = () => {
40
        addOccurrence(props.tag);
41
    };
42

    
43
    /**
44
     * Removes an occurrence of this annotation from the context.
45
     * @param occurrence The occurrence that should be removed.
46
     */
47
    const onDeleteOccurrence = (occurrence: Occurrence) => (e: any) => {
48
        deleteOccurrence(occurrence);
49
    };
50

    
51
    /**
52
     * Changes visibility of this annotation in the context.
53
     */
54
    const onChangeVisibility = () => {
55
        changeVisibility(props.tag);
56
    };
57

    
58
    /**
59
     * Changes a position of an occurrence of this annotation in the context.
60
     * @param occurrence The occurrence that should be changed.
61
     */
62
    const onChangePosition =
63
        (occurrence: Occurrence) => (e: ChangeEvent<HTMLInputElement>) => {
64
            changePosition(occurrence, Number(e.currentTarget.value));
65
        };
66

    
67
    /**
68
     * Changes a length of an occurrence of this annotation in the context.
69
     * @param occurrence The occurrence that should be changed.
70
     */
71
    const onChangeLength =
72
        (occurrence: Occurrence) => (e: ChangeEvent<HTMLInputElement>) => {
73
            changeLength(occurrence, Number(e.currentTarget.value));
74
        };
75

    
76
    /**
77
     * Changes visibility of properties of this annotation.
78
     */
79
    const changePropertiesVisibility = () => {
80
        setVisibleProperties(!visibleProperties);
81
    };
82

    
83
    return (
84
        <Container>
85
            <Row>
86
                <Col className="d-flex align-items-center">{props.tag.name}</Col>
87
                <Col sm="auto">
88
                    <Button
89
                        type="text"
90
                        shape="circle"
91
                        icon={<PlusOutlined />}
92
                        onClick={onAddOccurrence}
93
                    />
94
                    <Button
95
                        type="text"
96
                        shape="circle"
97
                        icon={<EyeOutlined />}
98
                        onClick={onChangeVisibility}
99
                    />
100
                    <Button
101
                        type="text"
102
                        shape="circle"
103
                        icon={<DownOutlined />}
104
                        onClick={changePropertiesVisibility}
105
                    />
106
                </Col>
107
            </Row>
108
            {visibleProperties && (
109
                <Stack>
110
                    <div>Kategorie: {props.tag.category}</div>
111
                    <div>Výskyty:</div>
112
                    {props.tag.occurrences.map((occurrence, index) => {
113
                        return (
114
                            <div key={index} id={props.tag.name + index}>
115
                                <Container>
116
                                    <Row>
117
                                        <Col>
118
                                            <Row>
119
                                                <Col className="d-flex align-items-center">
120
                                                    Pozice:
121
                                                </Col>
122
                                                <Col sm="auto">
123
                                                    <Input
124
                                                        value={occurrence.position}
125
                                                        onChange={onChangePosition(
126
                                                            occurrence
127
                                                        )}
128
                                                    />
129
                                                </Col>
130
                                            </Row>
131
                                            <Row>
132
                                                <Col className="d-flex align-items-center">
133
                                                    Délka:
134
                                                </Col>
135
                                                <Col sm="auto">
136
                                                    <Input
137
                                                        value={occurrence.length}
138
                                                        onChange={onChangeLength(
139
                                                            occurrence
140
                                                        )}
141
                                                    />
142
                                                </Col>
143
                                            </Row>
144
                                        </Col>
145
                                        <Col
146
                                            sm="auto"
147
                                            className="d-flex align-items-center"
148
                                        >
149
                                            <Button
150
                                                icon={<DeleteOutlined />}
151
                                                onClick={onDeleteOccurrence(occurrence)}
152
                                            ></Button>
153
                                        </Col>
154
                                    </Row>
155
                                </Container>
156
                            </div>
157
                        );
158
                    })}
159
                </Stack>
160
            )}
161
        </Container>
162
    );
163
}
(1-1/4)