summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-theme-default/src/components/LoadingBar.js
blob: 11e1ddbec4756fa6ef4983f11e73264ce894bb60 (plain)
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
const GitBook = require('gitbook-core');
const { React } = GitBook;

/**
 * Displays a progress bar (YouTube-like) at the top of container
 * Based on https://github.com/lonelyclick/react-loading-bar/blob/master/src/Loading.jsx
 */
const LoadingBar = React.createClass({
    propTypes: {
        show:  React.PropTypes.bool
    },

    getDefaultProps() {
        return {
            show: false
        };
    },

    getInitialState() {
        return {
            size: 0,
            disappearDelayHide: false, // when dispappear, first transition then display none
            percent: 0,
            appearDelayWidth: 0 // when appear, first display block then transition width
        };
    },

    componentWillReceiveProps(nextProps) {
        const { show } = nextProps;

        if (show) {
            this.show();
        } else {
            this.hide();
        }
    },

    shouldComponentUpdate(nextProps, nextState) {
        return true; // !shallowEqual(nextState, this.state)
    },

    show() {
        let { size, percent } = this.state;

        const appearDelayWidth = size === 0;
        percent = calculatePercent(percent);

        this.setState({
            size: ++size,
            appearDelayWidth,
            percent
        });

        if (appearDelayWidth) {
            setTimeout(() => {
                this.setState({
                    appearDelayWidth: false
                });
            });
        }
    },

    hide() {
        let { size } = this.state;

        if (--size < 0) {
            this.setState({ size: 0 });
            return;
        }

        this.setState({
            size: 0,
            disappearDelayHide: true,
            percent: 1
        });

        setTimeout(() => {
            this.setState({
                disappearDelayHide: false,
                percent: 0
            });
        }, 500);
    },

    getBarStyle() {
        const { disappearDelayHide, appearDelayWidth, percent } = this.state;

        return {
            width: appearDelayWidth ? 0 : percent * 100 + '%',
            display: disappearDelayHide || percent > 0 ? 'block' : 'none'
        };
    },

    getShadowStyle() {
        const { percent, disappearDelayHide } = this.state;

        return {
            display: disappearDelayHide || percent > 0 ? 'block' : 'none'
        };
    },

    render() {
        return (
            <div className="LoadingBar">
                <div className="LoadingBar-Bar" style={this.getBarStyle()}>
                    <div className="LoadingBar-Shadow"
                         style={this.getShadowStyle()}>
                    </div>
                </div>
            </div>
        );
    }
});

function calculatePercent(percent) {
    percent = percent || 0;

    // How much of remaining bar we advance
    const progress = 0.1 + Math.random() * 0.3;

    return percent + progress * (1 - percent);
}

module.exports = LoadingBar;