34354390c08c76b3caa12ccdf55a44334f9bed61
[aai/sparky-fe.git] / src / generic-components / panel / SlidePanel.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import React from 'react';
24 import FontAwesome from 'react-fontawesome';
25 import ReactDOM from 'react-dom';
26
27 class SlidePanel extends React.Component {
28                 
29                 static PropTypes = {
30                                 direction: React.PropTypes.string.isRequired,
31                                 className: React.PropTypes.string,
32                                 title: React.PropTypes.string,
33                                 isOpen: React.PropTypes.bool
34                 };
35                 
36                 static defaultProps = {
37                                 title: '',
38                                 className: '',
39                                 isOpen: true
40                 };
41                 
42                 state = {
43                                 isOpen: this.props.isOpen,
44                                 direction: this.props.direction,
45                                 width: 0,
46                                 arrowWidth: 0
47                 };
48                 
49                 componentDidMount() {
50                                 this.setSliderPosition();
51                 }
52                 
53                 componentDidUpdate() {
54                                 this.setSliderPosition();
55                 }
56                 
57                 render() {
58                                 
59                                 let {children, className} = this.props;
60                                 let {isOpen} = this.state;
61                                 
62                                 return (
63                                                 <div className={ `slide-panel ${className}`}>
64                                                                 {this.renderHeader(isOpen)}
65                                                                 <div
66                                                                                 className={'slide-panel-content ' + (isOpen ? 'opened' : 'closed')}>{children}</div>
67                                                 </div>
68                                 );
69                 }
70                 
71                 renderHeader(isOpen) {
72                                 let {direction: initialDirection, title} = this.props;
73                                 let {direction: currentDirection} = this.state;
74                                 
75                                 let iconName = currentDirection ===
76                                                'right'
77                                                 ? 'angle-double-right collapse-double-icon'
78                                                 : 'angle-double-left collapse-double-icon';
79                                 
80                                 let awestyle = {padding: '5px'};
81                                 
82                                 if (!isOpen && initialDirection === 'right') {
83                                                 awestyle.marginLeft = '-1px';
84                                 }
85                                 return (
86                                                 <div className='slide-panel-header'>
87                                                                 { initialDirection === 'left' &&
88                                                                 <span className='slide-panel-header-title'>{title}</span>}
89                                                                 <FontAwesome
90                                                                                 ref='arrowIcon'
91                                                                                 style={awestyle}
92                                                                                 onClick={this.handleClick}
93                                                                                 className='pull-right'
94                                                                                 name={iconName}
95                                                                                 size='2x'/>
96                                                                 { initialDirection === 'right' &&
97                                                                 <span className='slide-panel-header-title'>{title}</span>}
98                                                 </div>
99                                 );
100                 }
101                 
102                 handleClick = () => {
103                                 this.setState({
104                                                 isOpen: !this.state.isOpen,
105                                                 direction: this.state.direction === 'left' ? 'right' : 'left'
106                                 });
107                 }
108                 
109                 setSliderPosition = () => {
110                                 
111                                 let el = ReactDOM.findDOMNode(this);
112                                 let {style} = el;
113                                 
114                                 let {direction: initialDirection} = this.props;
115                                 let arrowIconSize = Math.floor(ReactDOM.findDOMNode(this.refs.arrowIcon)
116                                                                        .getBoundingClientRect().width) * 2;
117                                 if (!this.state.isOpen) {
118                                                 if (this.props.direction === 'left') {
119                                                                 style.left = arrowIconSize - el.getBoundingClientRect().width + 'px';
120                                                 }
121                                                 if (initialDirection === 'right') {
122                                                                 style.right = arrowIconSize - el.getBoundingClientRect().width + 'px';
123                                                 }
124                                 }
125                                 else {
126                                                 if (initialDirection === 'left') {
127                                                                 style.left = '0px';
128                                                 }
129                                                 
130                                                 if (this.props.direction === 'right') {
131                                                                 style.right = '0px';
132                                                 }
133                                 }
134                 }
135                 
136 }
137
138 export default SlidePanel;