summaryrefslogtreecommitdiffstats
path: root/webapp/components/backstage/backstage_section.jsx
blob: 41ce766bdca1003fc8db2e3308441a0753862573 (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
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import {Link} from 'react-router';

export default class BackstageSection extends React.Component {
    static get propTypes() {
        return {
            name: React.PropTypes.string.isRequired,
            title: React.PropTypes.node.isRequired,
            parentLink: React.PropTypes.string,
            subsection: React.PropTypes.bool,
            children: React.PropTypes.arrayOf(React.PropTypes.element)
        };
    }

    static get defaultProps() {
        return {
            parentLink: '',
            subsection: false,
            children: []
        };
    }

    static get contextTypes() {
        return {
            router: React.PropTypes.object.isRequired
        };
    }

    constructor(props) {
        super(props);

        this.handleClick = this.handleClick.bind(this);

        this.state = {
            expanded: true
        };
    }

    getLink() {
        return this.props.parentLink + '/' + this.props.name;
    }

    isActive() {
        const link = this.getLink();

        return this.context.router.isActive(link);
    }

    handleClick(e) {
        if (this.isActive()) {
            // we're already on this page so just toggle the link
            e.preventDefault();

            this.setState({
                expanded: !this.state.expanded
            });
        }

        // otherwise, just follow the link
    }

    render() {
        const {title, subsection, children} = this.props;

        const link = this.getLink();
        const active = this.isActive();

        // act like docs.mattermost.com and only expand if this link is active
        const expanded = active && this.state.expanded;

        let toggle = null;
        if (children.length > 0) {
            if (expanded) {
                toggle = <i className='fa fa-minus-square-o'/>;
            } else {
                toggle = <i className='fa fa-plus-square-o'/>;
            }
        }

        let clonedChildren = null;
        if (children.length > 0 && expanded) {
            clonedChildren = (
                <ul className='subsections'>
                    {
                        React.Children.map(children, (child) => {
                            return React.cloneElement(child, {
                                parentLink: link,
                                subsection: true
                            });
                        })
                    }
                </ul>
            );
        }

        let className = 'section';
        if (subsection) {
            className = 'subsection';
        }

        return (
            <li className={className}>
                <Link
                    className={`${className}-title`}
                    activeClassName={`${className}-title--active`}
                    onClick={this.handleClick}
                    to={link}
                >
                    {toggle}
                    <span className={`${className}-title__text`}>
                        {title}
                    </span>
                </Link>
                {clonedChildren}
            </li>
        );
    }
}