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
import React from 'react';
import PropTypes from 'prop-types';
import './SLControls.scss';
import ErrorMessage from 'webodm/components/ErrorMessage';
import { _, interpolate } from 'webodm/classes/gettext';
import $ from 'jquery';
 
export default class SLControls extends React.Component{
    static defaultProps = {
        sharePopup: null
    };
 
    static propTypes = {
        sharePopup: PropTypes.object.isRequired
    };
 
    constructor(props){
        super(props);
 
        this.state = {
            error: '',
            loading: false,
            useShortLink: false,
            shortId: '',
            editingShortId: false,
            showEditSuccess: false,
            savingShortId: false
        };
    }
 
    updateRelShareLink = (res, onSuccess = () => {}) => {
        if (res.shortId){
            const linksTarget = this.props.sharePopup.props.linksTarget;
    
            const {username, shortId} = res;
            const linksTargetChar = linksTarget === '3d' ? '3' : 'm';
    
            const relShareLink = `s${linksTargetChar}/${username}/${shortId}`;
            this.props.sharePopup.setState({relShareLink});
            this.setState({shortId});
            onSuccess();
        }else if (res.error){
            this.setState({error: res.error});
        }else this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error: JSON.stringify(res)})});
    }
 
    toggleShortLinks = (e) => {
        e.stopPropagation();
        if (!this.state.useShortLink && !this.state.loading){
            this.setState({loading: true});
 
            const task = this.props.sharePopup.props.task;
            
            $.ajax({
                type: 'POST',
                url: `/api/plugins/editshortlinks/task/${task.id}/shortlink`,
                contentType: 'application/json'
            }).done(res => {
                this.updateRelShareLink(res);
                this.setState({loading: false, useShortLink: !this.state.useShortLink});
            }).fail(error => {
                this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error }), loading: false});
            });
        }else{
            this.props.sharePopup.setState({relShareLink: this.props.sharePopup.getRelShareLink()});
            this.setState({useShortLink: !this.state.useShortLink});
        }
    }
 
    handleEdit = () => {
        this.setState({editingShortId: true});
        setTimeout(() => {
            this.editTextbox.focus();
        }, 0);
    }
 
 
    handleSave = () => {
        if (!this.state.savingShortId){
            this.setState({savingShortId: true});
            
            const task = this.props.sharePopup.props.task;
 
            $.ajax({
                type: 'POST',
                url: `/api/plugins/editshortlinks/task/${task.id}/edit`,
                contentType: 'application/json',
                data: JSON.stringify({
                    shortId: this.state.shortId
                }),
                dataType: 'json',
            }).done(res => {
                this.updateRelShareLink(res, () => {
                    this.setState({editingShortId: false, showEditSuccess: true});
                    setTimeout(() => {
                        this.setState({showEditSuccess: false});
                    }, 2000);
                });
            }).fail(error => {
                this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error }), loading: false});
            }).always(() => {
                this.setState({savingShortId: false});
            });
        }
    }
 
    handleEditShortId = e => {
        this.setState({shortId: e.target.value});
    }
 
    handleDelete = e => {
        if (window.confirm(_("Are you sure?"))){
            this.setState({loading: true});
            const task = this.props.sharePopup.props.task;
 
            $.ajax({
                type: 'POST',
                url: `/api/plugins/editshortlinks/task/${task.id}/delete`,
                contentType: 'application/json',
                dataType: 'json',
            }).done(res => {
                if (res.success){
                    this.setState({useShortLink: false, shortId: ""});
                    this.props.sharePopup.setState({relShareLink: this.props.sharePopup.getRelShareLink()});
                }else if (res.error){
                    this.setState({error: res.error});
                }else{
                    this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error: JSON.stringify(res) })});
                }
            }).fail(error => {
                this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error }), loading: false});
            }).always(() => {
                this.setState({loading: false});
            });
        }
    }
 
    handleShortIdKeyPress = e => {
        if (e.key === 'Enter'){
            this.handleSave();
        }
    }
 
    render(){
        const { loading, useShortLink, savingShortId,
                shortId, editingShortId, showEditSuccess } = this.state;
 
        const controls = [<label className="slcheckbox" >
            <input 
              type="checkbox"
              disabled={loading}
              checked={useShortLink}
              onChange={this.toggleShortLinks}
               /> {_("Short Link")}
          </label>];
 
        if (useShortLink){
            let editIcon = "fa-edit";
            if (showEditSuccess) editIcon = "fa-check";
 
            const editButton = (<button className="btn btn-secondary" type="button" 
                                        title={_("Edit")} 
                                        onClick={this.handleEdit}><i className={"fa " + editIcon}></i></button>);
            
            let saveIcon = "fa-save";
            if (savingShortId) saveIcon = "fa-circle-notch fa-spin";
 
            const saveButton = (<button className="btn btn-secondary" type="button"
                                        disabled={shortId.length === 0 || savingShortId}
                                        title={_("Save")} onClick={this.handleSave}><i className={"fa " + saveIcon}></i></button>);
 
            controls.push(<div className="input-group">
                    <input
                        className="form-control"
                        readOnly={!editingShortId}
                        value={shortId}
                        type="text"
                        placeholder={_("Suffix")}
                        disabled={loading}
                        onChange={this.handleEditShortId}
                        onKeyPress={this.handleShortIdKeyPress}
                        ref={(ref) => { this.editTextbox = ref; }}
                    ></input>
                    
                    <span className="input-group-btn">
                        {editingShortId ? saveButton : editButton}
                    </span>
                </div>);
 
            controls.push(<div>
                <a className="sldelete" onClick={this.handleDelete}><i className="fa fa-trash"></i> {_("Delete Short Link")}</a>
            </div>);
        }
 
        return (<div className="slcontrols">
            {controls}
            <ErrorMessage bind={[this, "error"]} />
        </div>);
    }
}