Projekt

Obecné

Profil

Stáhnout (6.07 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Col, Container, Row, Stack } from 'react-bootstrap';
2
import { 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
    TagOutlined,
12
} from '@ant-design/icons';
13
import { AnnotationContext } from '../../contexts/AnnotationContext';
14
import { TagInstanceInfo } from '../../api';
15

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

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

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

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

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

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

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

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

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