Projekt

Obecné

Profil

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