Projekt

Obecné

Profil

Stáhnout (6.21 KB) Statistiky
| Větev: | Tag: | Revize:
1 4e91ed40 Dominik Poch
import { Col, Container, Row, Stack } from 'react-bootstrap';
2 c73aecde Dominik Poch
import { Occurrence, Tag } from '../types/tag';
3
import { ChangeEvent, useContext, useState } from 'react';
4 967f45fc Dominik Poch
import 'antd/dist/antd.css';
5 4e91ed40 Dominik Poch
import { Button, Input } from 'antd';
6 967f45fc Dominik Poch
import {
7
    PlusOutlined,
8
    EyeOutlined,
9
    DownOutlined,
10
    DeleteOutlined,
11
} from '@ant-design/icons';
12 c73aecde Dominik Poch
import { AnnotationContext } from '../../contexts/AnnotationContext';
13 967f45fc Dominik Poch
14 acb8a961 Dominik Poch
/**
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 c73aecde Dominik Poch
export function AnnotationItem(props: { tag: Tag }) {
20 acb8a961 Dominik Poch
    /**
21
     * Should properties of this annotation be visible?
22
     */
23 967f45fc Dominik Poch
    const [visibleProperties, setVisibleProperties] = useState(false);
24 acb8a961 Dominik Poch
25
    /**
26
     * Context that manages annotations.
27
     */
28 c73aecde Dominik Poch
    const {
29
        addOccurrence,
30
        deleteOccurrence,
31
        changeVisibility,
32
        changePosition,
33
        changeLength,
34
    } = useContext(AnnotationContext);
35 967f45fc Dominik Poch
36 acb8a961 Dominik Poch
    /**
37
     * Adds new occurrence of this annotation to the context.
38
     */
39 c73aecde Dominik Poch
    const onAddOccurrence = () => {
40
        addOccurrence(props.tag);
41 4e91ed40 Dominik Poch
    };
42 967f45fc Dominik Poch
43 acb8a961 Dominik Poch
    /**
44
     * Removes an occurrence of this annotation from the context.
45
     * @param occurrence The occurrence that should be removed.
46
     */
47 c73aecde Dominik Poch
    const onDeleteOccurrence = (occurrence: Occurrence) => (e: any) => {
48
        deleteOccurrence(occurrence);
49 4e91ed40 Dominik Poch
    };
50 967f45fc Dominik Poch
51 acb8a961 Dominik Poch
    /**
52
     * Changes visibility of this annotation in the context.
53
     */
54 c73aecde Dominik Poch
    const onChangeVisibility = () => {
55
        changeVisibility(props.tag);
56 967f45fc Dominik Poch
    };
57
58 acb8a961 Dominik Poch
    /**
59
     * Changes a position of an occurrence of this annotation in the context.
60
     * @param occurrence The occurrence that should be changed.
61
     */
62 c73aecde Dominik Poch
    const onChangePosition =
63 4e91ed40 Dominik Poch
        (occurrence: Occurrence) => (e: ChangeEvent<HTMLInputElement>) => {
64 c73aecde Dominik Poch
            changePosition(occurrence, Number(e.currentTarget.value));
65 4e91ed40 Dominik Poch
        };
66
67 acb8a961 Dominik Poch
    /**
68
     * Changes a length of an occurrence of this annotation in the context.
69
     * @param occurrence The occurrence that should be changed.
70
     */
71 c73aecde Dominik Poch
    const onChangeLength =
72 4e91ed40 Dominik Poch
        (occurrence: Occurrence) => (e: ChangeEvent<HTMLInputElement>) => {
73 c73aecde Dominik Poch
            changeLength(occurrence, Number(e.currentTarget.value));
74 4e91ed40 Dominik Poch
        };
75
76 acb8a961 Dominik Poch
    /**
77
     * Changes visibility of properties of this annotation.
78
     */
79 c73aecde Dominik Poch
    const changePropertiesVisibility = () => {
80
        setVisibleProperties(!visibleProperties);
81
    };
82
83 967f45fc Dominik Poch
    return (
84 c73aecde Dominik Poch
        <Container>
85 967f45fc Dominik Poch
            <Row>
86 4e91ed40 Dominik Poch
                <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 c73aecde Dominik Poch
                        onClick={onAddOccurrence}
93 4e91ed40 Dominik Poch
                    />
94
                    <Button
95
                        type="text"
96
                        shape="circle"
97
                        icon={<EyeOutlined />}
98 c73aecde Dominik Poch
                        onClick={onChangeVisibility}
99 4e91ed40 Dominik Poch
                    />
100
                    <Button
101
                        type="text"
102
                        shape="circle"
103
                        icon={<DownOutlined />}
104
                        onClick={changePropertiesVisibility}
105
                    />
106
                </Col>
107 967f45fc Dominik Poch
            </Row>
108
            {visibleProperties && (
109 4e91ed40 Dominik Poch
                <Stack>
110
                    <div>Kategorie: {props.tag.category}</div>
111
                    <div>Výskyty:</div>
112
                    {props.tag.occurrences.map((occurrence, index) => {
113 967f45fc Dominik Poch
                        return (
114 4e91ed40 Dominik Poch
                            <div key={index} id={props.tag.name + index}>
115
                                <Container>
116
                                    <Row>
117
                                        <Col>
118
                                            <Row>
119 c73aecde Dominik Poch
                                                <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 4e91ed40 Dominik Poch
                                            </Row>
131
                                            <Row>
132 c73aecde Dominik Poch
                                                <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 4e91ed40 Dominik Poch
                                            </Row>
144
                                        </Col>
145
                                        <Col
146
                                            sm="auto"
147
                                            className="d-flex align-items-center"
148
                                        >
149
                                            <Button
150
                                                icon={<DeleteOutlined />}
151 c73aecde Dominik Poch
                                                onClick={onDeleteOccurrence(occurrence)}
152 4e91ed40 Dominik Poch
                                            ></Button>
153
                                        </Col>
154
                                    </Row>
155
                                </Container>
156 967f45fc Dominik Poch
                            </div>
157
                        );
158
                    })}
159 4e91ed40 Dominik Poch
                </Stack>
160 967f45fc Dominik Poch
            )}
161 c73aecde Dominik Poch
        </Container>
162 967f45fc Dominik Poch
    );
163
}