Package Mananger
[osm/UI.git] / skyquake / plugins / composer / src / src / components / CanvasPanel.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 _ from 'lodash'
22 import cc from 'change-case'
23 import React from 'react'
24 import PureRenderMixin from 'react-addons-pure-render-mixin'
25 import utils from '../libraries/utils'
26 import messages from './messages'
27 import DescriptorModelFactory from '../libraries/model/DescriptorModelFactory'
28 import CatalogItemCanvasEditor from './CatalogItemCanvasEditor'
29 import CatalogItemsActions from '../actions/CatalogItemsActions'
30 import CanvasEditorActions from '../actions/CanvasEditorActions'
31 import ComposerAppActions from '../actions/ComposerAppActions'
32 import CanvasZoom from './CanvasZoom'
33 import CanvasPanelTray from './CanvasPanelTray'
34 import EditForwardingGraphPaths from './EditorForwardingGraph/EditForwardingGraphPaths'
35 import SelectionManager from '../libraries/SelectionManager'
36 import DescriptorModelIconFactory from '../libraries/model/IconFactory'
37
38 import FileManager from './filemanager/FileManager.jsx';
39
40 import '../styles/CanvasPanel.scss'
41
42 const CanvasPanel = React.createClass({
43 mixins: [PureRenderMixin],
44 getInitialState() {
45 return {};
46 },
47 getDefaultProps() {
48 return {
49 title: '',
50 layout: {
51 left: 300,
52 right: 300
53 },
54 showMore: false,
55 containers: []
56 };
57 },
58 componentWillMount() {
59 },
60 componentDidMount() {
61 },
62 componentDidUpdate() {
63 SelectionManager.refreshOutline();
64 },
65 componentWillUnmount() {
66 },
67 render() {
68 var style = {
69 left: this.props.layout.left
70 };
71 var req = require.context("../", true, /^\.\/.*\.svg$/);
72 const hasItem = this.props.containers.length !== 0;
73 const isEditingNSD = DescriptorModelFactory.isNetworkService(this.props.containers[0]);
74 const isDescriptorView = (this.props.panelTabShown == 'descriptor');
75 const hasNoCatalogs = this.props.hasNoCatalogs;
76 const bodyComponent = hasItem ? <CatalogItemCanvasEditor zoom={this.props.zoom} isShowingMoreInfo={this.props.showMore} containers={this.props.containers}/> : messages.canvasWelcome();
77 const viewFiles = this.props.panelTabShown == 'assets';
78 const viewButtonTabs = !hasItem ? null : (
79 <div className="CanvasPanelTabs">
80 <div className="CatalogFilter">
81 <button className={isDescriptorView ? '-selected' : ''} onClick={ComposerAppActions.showDescriptor}>
82 Descriptor
83 </button>
84 <button className={!isDescriptorView ? '-selected' : ''} onClick={ComposerAppActions.showAssets}>
85 Assets
86 </button>
87 </div>
88 </div>
89 )
90 return (
91 <div id="canvasPanelDiv" className="CanvasPanel" style={style} onDragOver={this.onDragOver} onDrop={this.onDrop}>
92 <div onDoubleClick={this.onDblClickOpenFullScreen} className="CanvasPanelHeader panel-header" data-resizable="limit_bottom">
93 <h1>
94 {hasItem ? <img src={req('./' + DescriptorModelIconFactory.getUrlForType(this.props.containers[0].type, 'black'))} width="20px" /> : null}
95 <span className="model-name">{this.props.title}</span>
96 </h1>
97 </div>
98 {viewButtonTabs}
99 <div className="CanvasPanelBody panel-body" style={{marginRight: this.props.layout.right, bottom: this.props.layout.bottom}} >
100 {hasNoCatalogs ? null : viewFiles ? <FileManager files={this.props.files} type={this.props.type} item={this.props.item} filesState={this.props.filesState} /> : bodyComponent}
101 </div>
102 {
103 isDescriptorView ?
104 <CanvasZoom zoom={this.props.zoom} style={{bottom: this.props.layout.bottom + 20}}/>
105 : null
106 }
107 <CanvasPanelTray layout={this.props.layout} show={isEditingNSD && isDescriptorView}>
108 <EditForwardingGraphPaths containers={this.props.containers} />
109 </CanvasPanelTray>
110 </div>
111 );
112 },
113 onDragOver(event) {
114 const isDraggingFiles = _.includes(event.dataTransfer.types, 'Files');
115 if (!isDraggingFiles) {
116 event.preventDefault();
117 event.dataTransfer.dropEffect = 'copy';
118 }
119 },
120 onDrop(event) {
121 // given a drop event determine which action to take in the canvas:
122 // open item or add item to an existing, already opened nsd
123 // note: nsd is the only editable container
124 const data = utils.parseJSONIgnoreErrors(event.dataTransfer.getData('text'));
125 if (data.type === 'catalog-item') {
126 this.handleDropCatalogItem(event, data);
127 } else if (data.type === 'action') {
128 this.handleDropCanvasAction(event, data);
129 }
130 },
131 handleDropCanvasAction(event, data) {
132 const action = cc.camel('on-' + data.action);
133 if (typeof this[action] === 'function') {
134 if (this[action]({clientX: event.clientX, clientY: event.clientY})) {
135 event.preventDefault();
136 }
137 } else {
138 console.warn(`no action defined for drop event ${data.action}. Did you forget to add CanvasPanel.${action}() event handler?`);
139 }
140 },
141 handleDropCatalogItem(event, data) {
142 let openItem = null;
143 const currentItem = this.props.containers[0];
144 if (data.item.uiState.type === 'nsd') {
145 // if item is an nsd then open the descriptor in the canvas
146 openItem = data.item;
147 // if item is a vnfd or pnfd then check if the current item is an nsd
148 } else if (DescriptorModelFactory.isNetworkService(currentItem)) {
149 // so add the item to the nsd and re-render the canvas
150 switch (data.item.uiState.type) {
151 case 'vnfd':
152 this.onAddVnfd(data.item, {clientX: event.clientX, clientY: event.clientY});
153 break;
154 case 'pnfd':
155 this.onAddPnfd(data.item, {clientX: event.clientX, clientY: event.clientY});
156 break;
157 default:
158 console.warn(`Unknown catalog-item type. Expect type "nsd", "vnfd" or "pnfd" but got ${data.item.uiState.type}.`);
159 }
160 } else {
161 // otherwise the default action is to open the item
162 openItem = data.item;
163 }
164 if (openItem) {
165 event.preventDefault();
166 CatalogItemsActions.editCatalogItem(openItem);
167 }
168 },
169 onAddVdu(dropCoordinates) {
170 const currentItem = this.props.containers[0];
171 if (DescriptorModelFactory.isVirtualNetworkFunction(currentItem)) {
172 const vdu = currentItem.createVdu();
173 vdu.uiState.dropCoordinates = dropCoordinates;
174 CatalogItemsActions.catalogItemDescriptorChanged(currentItem);
175 }
176 },
177 onAddVld(dropCoordinates) {
178 const currentItem = this.props.containers[0];
179 if (DescriptorModelFactory.isNetworkService(currentItem) || DescriptorModelFactory.isVirtualNetworkFunction(currentItem)) {
180 const vld = currentItem.createVld();
181 vld.uiState.dropCoordinates = dropCoordinates;
182 CatalogItemsActions.catalogItemDescriptorChanged(currentItem);
183 }
184 },
185 onAddVnffgd(dropCoordinates) {
186 const currentItem = this.props.containers[0];
187 if (DescriptorModelFactory.isNetworkService(currentItem)) {
188 const vld = currentItem.createVnffgd();
189 vld.uiState.dropCoordinates = dropCoordinates;
190 CatalogItemsActions.catalogItemDescriptorChanged(currentItem);
191 }
192 },
193 onAddVnfd(model, dropCoordinates) {
194 const currentItem = this.props.containers[0];
195 if (DescriptorModelFactory.isNetworkService(currentItem) || DescriptorModelFactory.isVirtualNetworkFunction(currentItem)) {
196 const vnfd = DescriptorModelFactory.newVirtualNetworkFunction(model);
197 const cvnfd = currentItem.createConstituentVnfdForVnfd(vnfd);
198 cvnfd.uiState.dropCoordinates = dropCoordinates;
199 CatalogItemsActions.catalogItemDescriptorChanged(currentItem);
200 }
201 },
202 onAddPnfd(model, dropCoordinates) {
203 const currentItem = this.props.containers[0];
204 if (DescriptorModelFactory.isNetworkService(currentItem)) {
205 const pnfd = DescriptorModelFactory.newPhysicalNetworkFunction(model);
206 pnfd.uiState.dropCoordinates = dropCoordinates;
207 currentItem.createPnfd(pnfd);
208 CatalogItemsActions.catalogItemDescriptorChanged(currentItem);
209 }
210 },
211 onDblClickOpenFullScreen(event) {
212 event.stopPropagation();
213 ComposerAppActions.enterFullScreenMode();
214 }
215 });
216
217 export default CanvasPanel;