Bug 278 - Allow updating of NSD when there are instantiated network services
[osm/UI.git] / skyquake / plugins / composer / src / src / components / CatalogItems.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import UID from '../libraries/UniqueId'
22 import React from 'react'
23 import messages from './messages'
24 import ClassNames from 'classnames'
25 import PureRenderMixin from 'react-addons-pure-render-mixin'
26 import CatalogDataStore from '../stores/CatalogDataStore'
27 import CatalogItemsActions from '../actions/CatalogItemsActions'
28 import ComposerAppActions from '../actions/ComposerAppActions'
29 import SelectionManager from '../libraries/SelectionManager'
30
31 import '../styles/CatalogItems.scss'
32 import imgFile from 'file!../images/vendor-riftio.png'
33
34 const CatalogItems = React.createClass({
35 mixins: [PureRenderMixin],
36 getInitialState() {
37 return CatalogDataStore.getState();
38 },
39 getDefaultProps() {
40 return {
41 filterByType: 'nsd'
42 };
43 },
44 componentWillMount() {
45 CatalogDataStore.listen(this.onChange);
46 },
47 componentDidMount() {
48 // async actions creator will dispatch loadCatalogsSuccess and loadCatalogsError messages
49 CatalogDataStore.loadCatalogs().catch(e => console.warn('unable to load catalogs', e));
50 },
51 componentWillUnmount() {
52 CatalogDataStore.unlisten(this.onChange);
53 },
54 onChange(state) {
55 this.setState(state);
56 },
57 renderVersion(version) {
58 if (version) {
59 return (<span className='version'>{version}</span>);
60 } // else return null by default
61 },
62 handleImageError(e) {
63 console.log('Bad logo path, using default');
64 e.target.src = require('style/img/catalog-default.svg');
65 },
66 render() {
67 const self = this;
68 const onDragStart = function(event) {
69 const data = {type: 'catalog-item', item: this};
70 event.dataTransfer.effectAllowed = 'copy';
71 event.dataTransfer.setData('text', JSON.stringify(data));
72 ComposerAppActions.setDragState(data);
73 };
74 const onDblClickCatalogItem = function () {
75 CatalogItemsActions.editCatalogItem(this);
76 };
77 const onClickCatalogItem = function () {
78 // single clicking an item is handled by ComposerApp::onClick handler
79 //CatalogItemsActions.selectCatalogItem(this);
80 };
81 const cleanDataURI = this.cleanDataURI;
82 const items = this.getCatalogItems().map(function (d) {
83 const isNSD = d.uiState.type === 'nsd';
84 const isVNFD = d.uiState.type === 'vnfd';
85 const isDeleted = d.uiState.deleted;
86 const isModified = d.uiState.modified;
87 const isSelected = SelectionManager.isSelected(d);
88 const isOpenForEdit = d.uiState.isOpenForEdit;
89 const spanClassNames = ClassNames({'-is-selected': isSelected, '-is-open-for-edit': isOpenForEdit});
90 const sectionClassNames = ClassNames('catalog-item', {'-is-modified': isModified, '-is-deleted': isDeleted});
91 let type;
92 if(isNSD) {
93 type = 'nsd';
94 }
95 if(isVNFD) {
96 type = 'vnfd';
97 }
98 return (
99 <li key={d.id} data-uid={UID.from(d)} onClick={onClickCatalogItem.bind(d)} onDoubleClick={onDblClickCatalogItem.bind(d)}>
100 <div className={spanClassNames + ' ' + type}>
101 <div className={sectionClassNames} id={d.id} draggable="true" onDragStart={onDragStart.bind(d)}>
102 {isModified ? <div className="-is-modified-indicator" title="This descriptor has changes."></div> : null}
103 <div className="type-header">{type}</div>
104 <dl>
105 <dt className="name">{d.name}</dt>
106 <dd className="logo">
107 <img className="logo" src={cleanDataURI(d['logo'], type, d.id)} draggable="false" onError={self.handleImageError} />
108 </dd>
109 <dd className="short-name" title={d.name}>{d['short-name']}</dd>
110 <dd className="description">{d.description}</dd>
111 <dd className="vendor">{d.vendor || d.provider} {self.renderVersion(d.version)}</dd>
112 </dl>
113 </div>
114 </div>
115 {isOpenForEdit ? <div className="-is-open-for-edit-indicator" title="This descriptor is open in the canvas."></div> : null}
116 </li>
117 );
118 });
119 return (
120 <div className="CatalogItems">
121 <ul data-offset-parent="true">
122 {items.length ? items : messages.catalogWelcome}
123 </ul>
124 </div>
125 );
126 },
127 cleanDataURI(imageString, type, id) {
128 if (/\bbase64\b/g.test(imageString)) {
129 return imageString;
130 } else if (/<\?xml\b/g.test(imageString)) {
131 const imgStr = imageString.substring(imageString.indexOf('<?xml'));
132 return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(imgStr);
133 } else if (/\.(svg|png|gif|jpeg|jpg)$/.test(imageString)) {
134 return 'assets/logos/' + type + '/' + id + '/' + imageString;
135 // return require('../images/logos/' + imageString);
136 }
137 if(type == 'nsd' || type == 'vnfd') {
138 return require('style/img/catalog-'+type+'-default.svg');
139 }
140 return require('style/img/catalog-default.svg');
141 },
142 getCatalogItems() {
143 const catalogFilter = (d) => {return d.type === this.props.filterByType};
144 return this.state.catalogs.filter(catalogFilter).reduce((result, catalog) => {
145 return result.concat(catalog.descriptors);
146 }, []);
147 }
148 });
149
150 export default CatalogItems;