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