Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / SoftwareProductComponentsListView.jsx
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import React from 'react';
17 import PropTypes from 'prop-types';
18
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
21 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
22 import ListEditorItemViewField from 'nfvo-components/listEditor/ListEditorItemViewField.jsx';
23
24 const ComponentPropType = PropTypes.shape({
25         id: PropTypes.string,
26         name: PropTypes.string,
27         displayName: PropTypes.string,
28         description: PropTypes.string
29 });
30
31 class SoftwareProductComponentsListView extends React.Component {
32
33         state = {
34                 localFilter: ''
35         };
36
37         static propTypes = {
38                 isReadOnlyMode: PropTypes.bool,
39                 componentsList: PropTypes.arrayOf(ComponentPropType),
40                 onComponentSelect: PropTypes.func
41         };
42
43         render() {
44                 let {componentsList = [], isManual} =  this.props;
45                 return (
46                         <div className=''>
47                                 {
48                                         (componentsList.length > 0 || isManual) && this.renderComponents()
49                                 }
50                         </div>
51                 );
52         }
53
54         renderComponents() {
55                 const {localFilter} = this.state;
56                 const {isManual, onAddComponent, isReadOnlyMode, version, currentSoftwareProduct: {id: softwareProductId}, componentsList } = this.props;
57                 return (
58                         <ListEditorView
59                                 title={i18n('Virtual Function Components')}
60                                 filterValue={localFilter}
61                                 placeholder={i18n('Filter Components')}
62                                 onFilter={value => this.setState({localFilter: value})}
63                                 isReadOnlyMode={isReadOnlyMode || !!this.filterList().length}
64                                 plusButtonTitle={i18n('Add Component')}
65                                 onAdd={isManual && componentsList.length === 0 ? () => onAddComponent(softwareProductId, version) : false}
66                                 twoColumns>
67                                 {this.filterList().map(component => this.renderComponentsListItem(component))}
68                         </ListEditorView>
69                 );
70         }
71
72         renderComponentsListItem(component) {
73                 let {id: componentId, name, displayName, description = ''} = component;
74                 let {currentSoftwareProduct: {id}, onComponentSelect, version} = this.props;
75                 return (
76                         <ListEditorItemView
77                                 key={name + Math.floor(Math.random() * (100 - 1) + 1).toString()}
78                                 className='list-editor-item-view'                               
79                                 onSelect={() => onComponentSelect({id, componentId, version})}>
80                                 <ListEditorItemViewField>
81                                         <div className='name'>{displayName}</div>
82                                 </ListEditorItemViewField>
83                                 <ListEditorItemViewField>
84                                         <div className='description'>{description}</div>
85                                 </ListEditorItemViewField>
86                         </ListEditorItemView>
87                 );
88         }
89
90         filterList() {
91                 let {componentsList = []} = this.props;
92
93                 let {localFilter} = this.state;
94                 if (localFilter.trim()) {
95                         const filter = new RegExp(escape(localFilter), 'i');
96                         return componentsList.filter(({displayName = '', description = ''}) => {
97                                 return escape(displayName).match(filter) || escape(description).match(filter);
98                         });
99                 }
100                 else {
101                         return componentsList;
102                 }
103         }
104 }
105
106 export default SoftwareProductComponentsListView;