mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
29.06.2018 ffb8a2b791d277c1918f70d0fb8563c201809a8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React from 'react';
import './EditableTextField.css';
import {Button, Input, InputGroup, InputGroupAddon} from 'reactstrap';
import {IconCheck, IconCancel} from '../IconComponents'
 
class EditableTextField extends React.Component {
 
    state = {
        editing: false
    };
 
    constructor(props) {
        super(props);
 
        this.startEditing = this.startEditing.bind(this);
        this.stopEditing = this.stopEditing.bind(this);
    }
 
    startEditing = () => {
        this.setState({
            editing: true
        });
    };
 
    stopEditing = value => {
        this.setState({
            editing: false
        });
 
        if (value && this.props.onChange) {
            this.props.onChange(value, this.props.name, this.props.index);
        }
    };
 
    render = () => <div className={'editable-text-field'}>
        <div className={'editable-text-div editable-text-field-label'}>{this.props.title}</div>
        <div className={'editable-text-div editable-text-field-value-container'}>
            {this.state.editing ?
                <EditableTextFieldInput
                    type={this.props.type}
                    value={this.props.value}
                    name={this.props.name}
                    stopEditing={this.stopEditing}
                /> :
                <div className={'text-truncate editable-text-field-value'} onClick={this.startEditing}
                     title={this.props.value}>
                    {this.props.value}
                </div>
            }
        </div>
    </div>;
}
 
class EditableTextFieldInput extends React.Component {
 
    state = {
        value: this.props.value
    };
 
    constructor(props) {
        super(props);
 
        this.setReference = this.setReference.bind(this);
        this.handleInputChange = this.handleInputChange.bind(this);
        this.stopEditing = this.stopEditing.bind(this);
        this.handleMouseDown = this.handleMouseDown.bind(this);
        this.handleKeyPress = this.handleKeyPress.bind(this);
    }
 
    componentDidMount = () => {
        document.addEventListener('mousedown', this.handleMouseDown);
        document.addEventListener('keypress', this.handleKeyPress);
    };
 
    componentWillUnmount = () => {
        document.removeEventListener('mousedown', this.handleMouseDown);
        document.removeEventListener('keypress', this.handleKeyPress);
    };
 
    setReference = reference => this.reference = reference;
 
    handleInputChange = event => this.setState({
        value: event.target.value
    });
 
    handleMouseDown = event =>
        this.reference && !this.reference.contains(event.target) ?
            this.stopEditing(false)() : undefined;
 
    handleKeyPress = event => {
        switch (event.key) {
            case 'Escape':
                this.stopEditing(false)();
                return;
            case 'Enter':
                if (this.props.type !== 'textarea') {
                    this.stopEditing(true)();
                    return;
                }
                break;
            default:
        }
    };
 
    stopEditing = (save = false) => () =>
        this.props.stopEditing(save ? this.state.value : undefined);
 
    render = () => <div ref={this.setReference}>
        <InputGroup>
            <Input
                autoFocus
                type={this.props.type}
                value={this.state.value}
                onChange={this.handleInputChange}/>
            <InputGroupAddon addonType={'append'}>
                <Button
                className={'btn-outline-primary'}
                    onClick={this.stopEditing(true)}
                    color={'success'}
                >
                    <IconCheck/>
                </Button>
                <Button
                className={'btn-outline-primary'}
                    onClick={this.stopEditing(false)}
                    color={'danger'}
                >
                    <IconCancel/>
                </Button>
            </InputGroupAddon>
        </InputGroup>
    </div>;
}
 
 
export default EditableTextField;