zhongrj
2025-11-25 b89962006164a462404b79a738bee8cbb6d7fe7e
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
import React from 'react';
import PropTypes from 'prop-types';
import './SLCheckbox.scss';
import ErrorMessage from 'webodm/components/ErrorMessage';
import { _, interpolate } from 'webodm/classes/gettext';
import $ from 'jquery';
 
export default class SLCheckbox extends React.Component{
    static defaultProps = {
        sharePopup: null
    };
 
    static propTypes = {
        sharePopup: PropTypes.object.isRequired
    };
 
    constructor(props){
        super(props);
 
        this.state = {
            error: '',
            loading: false,
            useShortLink: false,
        };
    }
 
    toggleShortLinks = (e) => {
        e.stopPropagation();
        if (!this.state.useShortLink && !this.state.loading){
            this.setState({loading: true});
 
            const task = this.props.sharePopup.props.task;
            const linksTarget = this.props.sharePopup.props.linksTarget;
 
            $.ajax({
                type: 'POST',
                url: `/api/plugins/shortlinks/task/${task.id}/shortlink`,
                contentType: 'application/json'
            }).done(res => {
                const shortId = res.shortId;
                const linksTargetChar = linksTarget === '3d' ? '3' : 'm';
 
                const relShareLink = `s${linksTargetChar}${shortId}`;
 
                if (shortId) this.props.sharePopup.setState({relShareLink});
                else this.setState({error: interpolate(_('Invalid response from server: %(error)s'), { error: JSON.stringify(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});
        }
    }
 
    render(){
        const { error, loading, useShortLink } = this.state;
 
        if (error) return (<ErrorMessage bind={[this, "error"]} />);
 
        return (<label className="slcheckbox" >
            <input 
              type="checkbox"
              disabled={loading}
              checked={useShortLink}
              onChange={this.toggleShortLinks}
               /> {_("Short Link")}
          </label>);
    }
}