4 * Copyright 2016 RIFT.IO Inc
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import _
from 'lodash'
23 import alt
from '../alt'
24 import UID
from '../libraries/UniqueId'
25 import DescriptorModelFactory
from '../libraries/model/DescriptorModelFactory'
26 import PanelResizeAction
from '../actions/PanelResizeAction'
27 import CatalogItemsActions
from '../actions/CatalogItemsActions'
28 import CanvasEditorActions
from '../actions/CanvasEditorActions'
29 import ComposerAppActions
from '../actions/ComposerAppActions'
30 import CatalogFilterActions
from '../actions/CatalogFilterActions'
31 import CanvasPanelTrayActions
from '../actions/CanvasPanelTrayActions'
32 import SelectionManager
from '../libraries/SelectionManager'
33 import CatalogDataStore
from '../stores/CatalogDataStore'
34 import isFullScreen
from '../libraries/isFullScreen'
36 import FileManagerSource
from '../components/filemanager/FileManagerSource';
37 import FileManagerActions
from '../components/filemanager/FileManagerActions';
39 import React
from 'react';
41 //Hack for crouton fix. Should eventually put composer in skyquake alt context
42 import SkyquakeComponent
from 'widgets/skyquake_container/skyquakeComponent.jsx';
43 let NotificationError
= null;
44 class ComponentBridge
extends React
.Component
{
47 NotificationError
= this.props
.flux
.actions
.global
.showNotification
;
53 const getDefault
= (name
, defaultValue
) => {
54 const val
= window
.localStorage
.getItem('defaults-' + name
);
56 if (_
.isNumber(val
)) {
58 return setDefault(name
, 0);
63 setDefault(name
, defaultValue
);
67 const setDefault
= (name
, defaultValue
) => {
68 window
.localStorage
.setItem('defaults-' + name
, defaultValue
);
72 /* the top and bottom positions are managed by css; requires div to be display: absolute*/
74 left
: getDefault('catalog-panel-start-width', 300),
75 right
: getDefault('details-panel-start-width', 365),
76 bottom
: 25 + getDefault('defaults-forwarding-graphs-panel-start-height', 0),
78 zoom
: getDefault('zoom', 100),
79 filterCatalogBy
: 'nsd',
80 defaultPanelTrayOpenZoom
: (() => {
81 let zoom
= parseFloat(getDefault('panel-tray-zoom', 75));
85 zoom
= Math
.min(100, zoom
);
86 zoom
= Math
.max(25, zoom
);
87 setDefault('panel-tray-zoom', zoom
);
92 const autoZoomCanvasScale
= d3
.scale
.linear().domain([0, 300]).range([100, 50]).clamp(true);
94 const uiTransientState
= {};
96 class ComposerAppStore
{
99 //Bridge for crouton fix
100 this.ComponentBridgeElement
= SkyquakeComponent(ComponentBridge
);
102 this.exportAsync(FileManagerSource
)
103 // the catalog item currently being edited in the composer
105 // the left and right sides of the canvas area
108 right
: defaults
.right
,
109 bottom
: defaults
.bottom
111 uiTransientState
.restoreLayout
= this.layout
;
112 this.zoom
= defaults
.zoom
;
113 this.showMore
= defaults
.showMore
;
114 this.filterCatalogByTypeValue
= defaults
.filterCatalogBy
;
115 // transient ui state
118 this.messageType
= '';
119 this.showJSONViewer
= false;
120 this.showClassifiers
= {};
121 this.editPathsMode
= false;
122 this.fullScreenMode
= false;
123 this.panelTabShown
= 'descriptor';
124 //File manager values
126 this.filesState
= {};
127 this.downloadJobs
= {};
128 this.containers
= [];
129 //End File manager values
131 onResize
: PanelResizeAction
.RESIZE
,
132 editCatalogItem
: CatalogItemsActions
.EDIT_CATALOG_ITEM
,
133 catalogItemMetaDataChanged
: CatalogItemsActions
.CATALOG_ITEM_META_DATA_CHANGED
,
134 catalogItemDescriptorChanged
: CatalogItemsActions
.CATALOG_ITEM_DESCRIPTOR_CHANGED
,
135 toggleShowMoreInfo
: CanvasEditorActions
.TOGGLE_SHOW_MORE_INFO
,
136 showMoreInfo
: CanvasEditorActions
.SHOW_MORE_INFO
,
137 showLessInfo
: CanvasEditorActions
.SHOW_LESS_INFO
,
138 applyDefaultLayout
: CanvasEditorActions
.APPLY_DEFAULT_LAYOUT
,
139 addVirtualLinkDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_LINK_DESCRIPTOR
,
140 addForwardingGraphDescriptor
: CanvasEditorActions
.ADD_FORWARDING_GRAPH_DESCRIPTOR
,
141 addVirtualDeploymentDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_DEPLOYMENT_DESCRIPTOR
,
142 selectModel
: ComposerAppActions
.SELECT_MODEL
,
143 outlineModel
: ComposerAppActions
.OUTLINE_MODEL
,
144 showError
: ComposerAppActions
.SHOW_ERROR
,
145 clearError
: ComposerAppActions
.CLEAR_ERROR
,
146 setDragState
: ComposerAppActions
.SET_DRAG_STATE
,
147 filterCatalogByType
: CatalogFilterActions
.FILTER_BY_TYPE
,
148 setCanvasZoom
: CanvasEditorActions
.SET_CANVAS_ZOOM
,
149 showJsonViewer
: ComposerAppActions
.SHOW_JSON_VIEWER
,
150 closeJsonViewer
: ComposerAppActions
.CLOSE_JSON_VIEWER
,
151 toggleCanvasPanelTray
: CanvasPanelTrayActions
.TOGGLE_OPEN_CLOSE
,
152 openCanvasPanelTray
: CanvasPanelTrayActions
.OPEN
,
153 closeCanvasPanelTray
: CanvasPanelTrayActions
.CLOSE
,
154 enterFullScreenMode
: ComposerAppActions
.ENTER_FULL_SCREEN_MODE
,
155 exitFullScreenMode
: ComposerAppActions
.EXIT_FULL_SCREEN_MODE
,
156 showAssets
: ComposerAppActions
.showAssets
,
157 showDescriptor
: ComposerAppActions
.showDescriptor
,
158 getFilelistSuccess
: FileManagerActions
.getFilelistSuccess
,
159 updateFileLocationInput
: FileManagerActions
.updateFileLocationInput
,
160 sendDownloadFileRequst
: FileManagerActions
.sendDownloadFileRequst
,
161 addFileSuccess
: FileManagerActions
.addFileSuccess
,
162 deletePackageFile
: FileManagerActions
.deletePackageFile
,
163 deleteFileSuccess
: FileManagerActions
.deleteFileSuccess
,
164 closeFileManagerSockets
: FileManagerActions
.closeFileManagerSockets
,
165 openFileManagerSockets
: FileManagerActions
.openFileManagerSockets
,
166 openDownloadMonitoringSocketSuccess
: FileManagerActions
.openDownloadMonitoringSocketSuccess
,
167 getFilelistSocketSuccess
: FileManagerActions
.getFilelistSocketSuccess
169 this.exportPublicMethods({
170 closeFileManagerSockets
: this.closeFileManagerSockets
.bind(this)
175 if (e
.type
=== 'resize-manager.resize.catalog-panel') {
176 const layout
= Object
.assign({}, this.layout
);
177 layout
.left
= Math
.max(0, layout
.left
- e
.moved
.x
);
178 if (layout
.left
!== this.layout
.left
) {
179 this.setState({layout
: layout
});
181 } else if (e
.type
=== 'resize-manager.resize.details-panel') {
182 const layout
= Object
.assign({}, this.layout
);
183 layout
.right
= Math
.max(0, layout
.right
+ e
.moved
.x
);
184 if (layout
.right
!== this.layout
.right
) {
185 this.setState({layout
: layout
});
187 } else if (/^resize-manager\.resize\.canvas-panel-tray/.test(e
.type
)) {
188 const layout
= Object
.assign({}, this.layout
);
189 layout
.bottom
= Math
.max(25, layout
.bottom
+ e
.moved
.y
);
190 if (layout
.bottom
!== this.layout
.bottom
) {
191 const zoom
= autoZoomCanvasScale(layout
.bottom
) ;
192 if (this.zoom
!== zoom
) {
193 this.setState({layout
: layout
, zoom
: zoom
});
195 this.setState({layout
: layout
});
198 } else if (e
.type
!== 'resize') {
199 console
.log('no resize handler for ', e
.type
, '. Do you need to add a handler in ComposerAppStore::onResize()?')
201 SelectionManager
.refreshOutline();
208 if(!document
.body
.classList
.contains('resizing')) {
209 containers
= [item
].reduce(DescriptorModelFactory
.buildCatalogItemFactory(CatalogDataStore
.getState().catalogs
), []);
211 containers
.filter(d
=> DescriptorModelFactory
.isConnectionPoint(d
)).forEach(d
=> {
212 d
.cpNumber
= ++cpNumber
;
213 containers
.filter(d
=> DescriptorModelFactory
.isVnfdConnectionPointRef(d
)).filter(ref
=> ref
.key
=== d
.key
).forEach(ref
=> ref
.cpNumber
= d
.cpNumber
);
215 this.setState({containers
: containers
, item
: _
.cloneDeep(item
)});
217 SelectionManager
.refreshOutline();
220 editCatalogItem(item
) {
222 self
.closeFileManagerSockets();
223 if (item
&& item
.uiState
) {
224 item
.uiState
.isOpenForEdit
= true;
225 if (item
.uiState
.type
!== 'nsd') {
226 this.closeCanvasPanelTray();
229 SelectionManager
.select(item
);
230 this.updateItem(item
);
231 this.openFileManagerSockets(item
)
233 catalogItemMetaDataChanged(item
) {
234 this.updateItem(item
);
237 catalogItemDescriptorChanged(itemDescriptor
) {
238 this.catalogItemMetaDataChanged(itemDescriptor
.model
);
242 this.setState({showMore
: true});
246 this.setState({showMore
: false});
250 NotificationError
.defer({msg
: data
.errorMessage
, type
: 'error'})
251 // this.setState({message: data.errorMessage, messageType: 'error'});
255 this.setState({message
: '', messageType
: ''});
258 toggleShowMoreInfo() {
259 this.setState({showMore
: !this.showMore
});
262 applyDefaultLayout() {
263 if (this.item
&& this.item
.uiState
&& this.item
.uiState
.containerPositionMap
) {
264 if (!_
.isEmpty(this.item
.uiState
.containerPositionMap
)) {
265 this.item
.uiState
.containerPositionMap
= {};
266 CatalogItemsActions
.catalogItemMetaDataChanged
.defer(this.item
);
271 addVirtualLinkDescriptor(dropCoordinates
= null) {
274 if (this.item
.uiState
.type
=== 'nsd') {
275 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
276 vld
= nsdc
.createVld();
277 } else if (this.item
.uiState
.type
=== 'vnfd') {
278 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
279 vld
= vnfd
.createVld();
282 vld
.uiState
.dropCoordinates
= dropCoordinates
;
283 SelectionManager
.clearSelectionAndRemoveOutline();
284 SelectionManager
.addSelection(vld
);
285 this.updateItem(vld
.getRoot().model
);
286 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vld
.getRoot());
291 addForwardingGraphDescriptor(dropCoordinates
= null) {
292 if (this.item
&& this.item
.uiState
.type
=== 'nsd') {
293 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
294 const fg
= nsdc
.createVnffgd();
295 fg
.uiState
.dropCoordinates
= dropCoordinates
;
296 SelectionManager
.clearSelectionAndRemoveOutline();
297 SelectionManager
.addSelection(fg
);
298 this.updateItem(nsdc
.model
);
299 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(nsdc
);
303 addVirtualDeploymentDescriptor(dropCoordinates
= null) {
304 if (this.item
.uiState
.type
=== 'vnfd') {
305 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
306 const vdu
= vnfd
.createVdu();
307 vdu
.uiState
.dropCoordinates
= dropCoordinates
;
308 SelectionManager
.clearSelectionAndRemoveOutline();
309 SelectionManager
.addSelection(vdu
);
310 this.updateItem(vdu
.getRoot().model
);
311 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vdu
.getRoot());
315 selectModel(container
) {
316 if (SelectionManager
.select(container
)) {
317 const model
= DescriptorModelFactory
.isContainer(container
) ? container
.getRoot().model
: container
;
318 this.catalogItemMetaDataChanged(model
);
323 const uid
= UID
.from(obj
);
324 requestAnimationFrame(() => {
325 SelectionManager
.outline(Array
.from(document
.querySelectorAll(`[data-uid="${uid}"]`)));
330 SelectionManager
.clearSelectionAndRemoveOutline();
331 this.catalogItemMetaDataChanged(this.item
);
334 setDragState(dragState
) {
335 this.setState({drag
: dragState
});
338 filterCatalogByType(typeValue
) {
339 this.setState({filterCatalogByTypeValue
: typeValue
})
342 setCanvasZoom(zoom
) {
343 this.setState({zoom
: zoom
});
347 this.setState({showJSONViewer
: true});
351 this.setState({showJSONViewer
: false});
354 toggleCanvasPanelTray() {
355 const layout
= this.layout
;
356 if (layout
.bottom
> 25) {
357 this.closeCanvasPanelTray();
359 this.openCanvasPanelTray();
363 openCanvasPanelTray() {
365 left
: this.layout
.left
,
366 right
: this.layout
.right
,
369 const zoom
= defaults
.defaultPanelTrayOpenZoom
;
370 if (this.zoom
!== zoom
) {
371 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: this.zoom
});
373 this.setState({layout
: layout
});
377 closeCanvasPanelTray() {
379 left
: this.layout
.left
,
380 right
: this.layout
.right
,
383 const zoom
= this.restoreZoom
|| autoZoomCanvasScale(layout
.bottom
);
384 if (this.zoom
!== zoom
) {
385 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: null});
387 this.setState({layout
: layout
, restoreZoom
: null});
391 enterFullScreenMode() {
394 * https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
395 * This is an experimental api but works our target browsers and ignored by others
397 const eventNames
= ['fullscreenchange', 'mozfullscreenchange', 'webkitfullscreenchange', 'msfullscreenchange'];
399 const appRoot
= document
.body
;//.getElementById('RIFT_wareLaunchpadComposerAppRoot');
403 function onFullScreenChange() {
405 if (isFullScreen()) {
406 const layout
= comp
.layout
;
407 const restoreLayout
= _
.cloneDeep(layout
);
408 uiTransientState
.restoreLayout
= restoreLayout
;
411 comp
.setState({fullScreenMode
: true, layout
: layout
, restoreLayout
: restoreLayout
});
413 comp
.setState({fullScreenMode
: false, layout
: uiTransientState
.restoreLayout
});
418 if (this.fullScreenMode
=== false) {
420 if (appRoot
.requestFullscreen
) {
421 appRoot
.requestFullscreen();
422 } else if (appRoot
.msRequestFullscreen
) {
423 appRoot
.msRequestFullscreen();
424 } else if (appRoot
.mozRequestFullScreen
) {
425 appRoot
.mozRequestFullScreen();
426 } else if (appRoot
.webkitRequestFullscreen
) {
427 appRoot
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
430 eventNames
.map(name
=> {
431 document
.removeEventListener(name
, onFullScreenChange
);
432 document
.addEventListener(name
, onFullScreenChange
);
439 exitFullScreenMode() {
441 if (document
.exitFullscreen
) {
442 document
.exitFullscreen();
443 } else if (document
.msExitFullscreen
) {
444 document
.msExitFullscreen();
445 } else if (document
.mozCancelFullScreen
) {
446 document
.mozCancelFullScreen();
447 } else if (document
.webkitExitFullscreen
) {
448 document
.webkitExitFullscreen();
451 this.setState({fullScreenMode
: false});
456 panelTabShown
: 'assets'
461 panelTabShown
: 'descriptor'
465 //File Manager methods
466 getFilelistSuccess(data
) {
468 let filesState
= null;
469 if (self
.fileMonitoringSocketID
) {
471 if(data
.hasOwnProperty('contents')) {
472 filesState
= addInputState( _
.cloneDeep(this.filesState
),data
);
473 // filesState = _.merge(self.filesState, addInputState({},data));
474 let normalizedData
= normalizeTree(data
);
477 data
: _
.mergeWith(normalizedData
.data
, self
.files
.data
, function(obj
, src
) {
478 return _
.uniqBy(obj
? obj
.concat(src
) : src
, 'name');
480 id
: self
.files
.id
|| normalizedData
.id
482 filesState
: filesState
490 this.setState(newState
);
492 function normalizeTree(data
) {
497 data
.contents
.map(getContents
);
498 function getContents(d
) {
499 if(d
.hasOwnProperty('contents')) {
501 d
.contents
.map(function(c
,i
) {
502 if (!c
.hasOwnProperty('contents')) {
509 f
.data
[d
.name
] = contents
;
514 function addInputState(obj
, d
) {
516 if(d
.hasOwnProperty('contents')) {
517 d
.contents
.map(addInputState
.bind(null, obj
))
525 sendDownloadFileRequst(data
) {
526 let id
= data
.id
|| this.item
.id
;
527 let type
= data
.type
|| this.item
.uiState
.type
;
528 let path
= data
.path
;
530 this.getInstance().addFile(id
, type
, path
, url
);
532 updateFileLocationInput
= (data
) => {
533 let name
= data
.name
;
534 let value
= data
.value
;
535 var filesState
= _
.cloneDeep(this.filesState
);
536 filesState
[name
] = value
;
538 filesState
: filesState
541 addFileSuccess
= (data
) => {
542 let path
= data
.path
;
543 let fileName
= data
.fileName
;
544 let files
= _
.cloneDeep(this.files
);
545 let loadingIndex
= files
.data
[path
].push({
546 status
: 'DOWNLOADING',
547 name
: path
+ '/' + fileName
549 this.setState({files
: files
});
552 startWatchingJob
= () => {
553 let ws
= window
.multiplexer
.channel(this.jobSocketId
);
558 openDownloadMonitoringSocketSuccess
= (id
) => {
560 let ws
= window
.multiplexer
.channel(id
);
561 let downloadJobs
= _
.cloneDeep(self
.downloadJobs
);
562 let newFiles
= false;
563 ws
.onmessage
= (socket
) => {
564 if (self
.files
&& self
.files
.length
> 0) {
567 jobs
= JSON
.parse(socket
.data
);
569 newFiles
= _
.cloneDeep(self
.files
);
570 jobs
.map(function(j
) {
571 //check if not in completed state
572 let fullPath
= j
['package-path'];
573 let path
= fullPath
.split('/');
574 let fileName
= path
.pop();
575 path
= path
.join('/');
576 let index
= _
.findIndex(self
.files
.data
[path
], function(o
){
577 return fullPath
== o
.name
579 if((index
> -1) && newFiles
.data
[path
][index
]) {
580 newFiles
.data
[path
][index
].status
= j
.status
582 if(j
.status
.toUpperCase() == 'LOADING...' || j
.status
.toUpperCase() == 'IN_PROGRESS') {
583 newFiles
.data
[path
].push({
595 // console.log(JSON.parse(socket.data));
604 getFilelistSocketSuccess
= (id
) => {
606 let ws
= window
.multiplexer
.channel(id
);
607 ws
.onmessage
= (socket
) => {
608 if (self
.fileMonitoringSocketID
) {
611 data
= JSON
.parse(socket
.data
);
613 self
.getFilelistSuccess(data
)
618 fileMonitoringSocketID
: id
,
619 fileMonitoringSocket
: ws
623 closeFileManagerSockets() {
624 this.fileMonitoringSocketID
= null;
627 fileMonitoringSocketID
: null
629 // fileMonitoringSocket : null,
631 this.jobSocket
&& this.jobSocket
.close();
632 this.fileMonitoringSocket
&& this.fileMonitoringSocket
.close();
633 console
.log('closing');
635 openFileManagerSockets(i
) {
637 let item
= i
|| self
.item
;
638 // this.closeFileManagerSockets();
639 this.getInstance().openFileMonitoringSocket(item
.id
, item
.uiState
.type
).then(function() {
640 // // self.getInstance().openDownloadMonitoringSocket(item.id);
642 this.getInstance().openDownloadMonitoringSocket(item
.id
);
647 deletePackageFile(name
) {
648 let id
= this.item
.id
;
649 let type
= this.item
.uiState
.type
;
650 this.getInstance().deleteFile(id
, type
, name
);
652 deleteFileSuccess
= (data
) => {
653 let path
= data
.path
.split('/')
654 let files
= _
.cloneDeep(this.files
);
656 path
= path
.join('/');
657 let pathFiles
= files
.data
[path
]
658 _
.remove(pathFiles
, function(c
) {
659 return c
.name
== data
.path
;
668 export default alt
.createStore(ComposerAppStore
, 'ComposerAppStore');