zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React from 'react';
import ErrorMessage from './ErrorMessage';
import '../css/FormDialog.scss';
import PropTypes from 'prop-types';
import $ from 'jquery';
import { _ } from '../classes/gettext';
 
class FormDialog extends React.Component {
    static defaultProps = {
        title: _("Title"),
        saveLabel: _("Save"),
        savingLabel: _("Saving…"),
        saveIcon: "glyphicon glyphicon-plus",
        deleteWarning: _("Are you sure?"),
        show: false
    };
 
    static propTypes = {
        getFormData: PropTypes.func.isRequired,
        reset: PropTypes.func,
        saveAction: PropTypes.func.isRequired,
        handleSaveFunction: PropTypes.func,
        onShow: PropTypes.func,
        onHide: PropTypes.func,
        deleteAction: PropTypes.func,
        title: PropTypes.string,
        saveLabel: PropTypes.string,
        savingLabel: PropTypes.string,
        saveIcon: PropTypes.string,
        deleteWarning: PropTypes.oneOfType([
            PropTypes.string,
            PropTypes.bool
        ]),
        show: PropTypes.bool
    };
 
    constructor(props){
        super(props);
 
        this.state = {
            showModal: props.show,
            saving: false,
            deleting: false,
            error: ""
        };
 
        this.setModal = this.setModal.bind(this);
        this.show = this.show.bind(this);
        this.hide = this.hide.bind(this);
        this.handleSave = this.handleSave.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
    }
 
    setModal(domNode){
        this.modal = domNode;
    }
 
    componentDidMount(){
        this._mounted = true;
 
        $(this.modal)
            // Ensure state is kept up to date when
            // the user presses the escape key
            .on('hidden.bs.modal', (e) => {
                this.hide();
            })
 
            // Autofocus
            .on('shown.bs.modal', (e) => {
                if (this.props.onShow) this.props.onShow();
            });
 
        this.componentDidUpdate();
    }
 
    componentWillUnmount(){
        this._mounted = false;
 
        $(this.modal).off('hidden.bs.modal hidden.bs.modal')
                     .modal('hide');
    }
 
    componentDidUpdate(){
        if (this.state.showModal){
            $(this.modal).modal('show');
        }else{
            $(this.modal).modal('hide');
        }
    }
 
    show(){
        if (this.props.reset) this.props.reset();
        this.setState({showModal: true, saving: false, error: ""});
    }
 
    hide(){
        this.setState({showModal: false});
        if (this.props.onHide) this.props.onHide();
        if (this.serverRequest){
            this.serverRequest.abort();
            this.serverRequest = null;
        }
    }
 
    handleEnter = e => {
        if (e.key === 'Enter' || e.keyCode === 13){
          this.handleSave(e);
        }
    }
 
    handleSave(e){
        e.preventDefault();
 
        this.setState({saving: true, error: ""});
 
        if (this.props.handleSaveFunction){
            this.props.handleSaveFunction(err => {
                if (!err) this.hide();
                else{
                    this.setState({saving: false, error: err.message});
                }
            });
        }else{
            let formData = {};
            if (this.props.getFormData) formData = this.props.getFormData();
    
            this.serverRequest = this.props.saveAction(formData);
            if (this.serverRequest){
                this.serverRequest.fail(e => {
                    this.setState({error: e.message || (e.responseJSON || {}).detail || (e.responseJSON || {}).error || e.responseText || _("Could not apply changes")});
                }).always(() => {
                    this.setState({saving: false});
                    this.serverRequest = null;
                }).done(() => {
                    this.hide();
                });
            }else{
                this.setState({saving: false});
                this.hide();
            }
        }
 
    }
 
    handleDelete(){
        if (this.props.deleteAction){
            if (!this.props.deleteWarning || window.confirm(this.props.deleteWarning)){
                this.setState({deleting: true});
                this.props.deleteAction()
                    .fail(e => {
                        if (this._mounted) this.setState({error: e.message || (e.responseJSON || {}).detail || e.responseText || _("Could not delete item")});
                    }).always(() => {
                        if (this._mounted) this.setState({deleting: false});
                    });
            }
        }
    }
 
    render(){
        let leftButtons = [];
        if (this.props.deleteAction){
            leftButtons.push(<button 
                disabled={this.state.deleting}
                className="btn btn-danger"
                key="delete" 
                onClick={this.handleDelete}>
                {this.state.deleting ? 
                    <span>
                        <i className="fa fa-circle-notch fa-spin"></i><span className="hidden-xs"> {_("Deleting...")}</span>
                    </span>
                :   <span>
                        <i className="fa fa-trash"></i><span className="hidden-xs"> {_("Delete")}</span>
                    </span>}
            </button>);
        }
        if (this.props.leftButtons){
            leftButtons = leftButtons.concat(this.props.leftButtons);
        } 
 
        return (
            <div ref={this.setModal}
                className="modal form-dialog" tabIndex="-1"
                data-backdrop="static"
            >
              <div className="modal-dialog">
                <div className="modal-content">
                  <div className="modal-header">
                    <button type="button" className="close" onClick={this.hide}><span>&times;</span></button>
                    <h4 className="modal-title">{this.props.title}</h4>
                  </div>
                  <div className="modal-body">
                    <ErrorMessage bind={[this, "error"]} />
                    <div className="form-horizontal" onSubmit={this.handleSave}>
                      {this.props.children}
                    </div>
                  </div>
                  <div className="modal-footer">
                    <div className="pull-right">
                        <button type="button" className="btn btn-default" onClick={this.hide} disabled={this.state.saving}>{_("Cancel")}</button>
                        <button type="button" className="btn btn-primary save" onClick={this.handleSave} disabled={this.state.saving}>
                            {this.state.saving ? 
                                <span>
                                    <i className="fa fa-circle-notch fa-spin"></i> {this.props.savingLabel}
                                </span>
                            :   <span>
                                    <i className={this.props.saveIcon}></i> {this.props.saveLabel}
                                </span>}
                        </button>
                    </div>
                    
                    {leftButtons.length > 0 ?
                        <div className="text-left">
                            {leftButtons}
                        </div>
                    : ""}
                  </div>
                </div>
              </div>
            </div>
        );
    }
}
 
export default FormDialog;