2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 import React from 'react';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
20 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
21 import ListEditorItemViewField from 'nfvo-components/listEditor/ListEditorItemViewField.jsx';
23 const ComponentPropType = React.PropTypes.shape({
24 id: React.PropTypes.string,
25 name: React.PropTypes.string,
26 displayName: React.PropTypes.string,
27 description: React.PropTypes.string
30 class SoftwareProductComponentsListView extends React.Component {
37 isReadOnlyMode: React.PropTypes.bool,
38 componentsList: React.PropTypes.arrayOf(ComponentPropType),
39 onComponentSelect: React.PropTypes.func
43 let {componentsList = [], isManual} = this.props;
47 (componentsList.length > 0 || isManual) && this.renderComponents()
54 const {localFilter} = this.state;
55 const {isManual, onAddComponent, isReadOnlyMode, currentSoftwareProduct: {id: softwareProductId}, componentsList } = this.props;
58 title={i18n('Virtual Function Components')}
59 filterValue={localFilter}
60 placeholder={i18n('Filter Components')}
61 onFilter={value => this.setState({localFilter: value})}
62 isReadOnlyMode={isReadOnlyMode || !!this.filterList().length}
63 plusButtonTitle={i18n('Add Component')}
64 onAdd={isManual && componentsList.length === 0 ? () => onAddComponent(softwareProductId) : false}
66 {this.filterList().map(component => this.renderComponentsListItem(component))}
71 renderComponentsListItem(component) {
72 let {id: componentId, name, displayName, description = ''} = component;
73 let {currentSoftwareProduct: {id, version}, onComponentSelect, isManual, isReadOnlyMode, onDeleteComponent} = this.props;
76 key={name + Math.floor(Math.random() * (100 - 1) + 1).toString()}
77 className='list-editor-item-view'
78 onDelete={isManual && !isReadOnlyMode ? () => onDeleteComponent(component, id, version) : false}
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>
91 let {componentsList = []} = this.props;
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);
101 return componentsList;
106 export default SoftwareProductComponentsListView;