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 //End File manager values
130 onResize
: PanelResizeAction
.RESIZE
,
131 editCatalogItem
: CatalogItemsActions
.EDIT_CATALOG_ITEM
,
132 catalogItemMetaDataChanged
: CatalogItemsActions
.CATALOG_ITEM_META_DATA_CHANGED
,
133 catalogItemDescriptorChanged
: CatalogItemsActions
.CATALOG_ITEM_DESCRIPTOR_CHANGED
,
134 toggleShowMoreInfo
: CanvasEditorActions
.TOGGLE_SHOW_MORE_INFO
,
135 showMoreInfo
: CanvasEditorActions
.SHOW_MORE_INFO
,
136 showLessInfo
: CanvasEditorActions
.SHOW_LESS_INFO
,
137 applyDefaultLayout
: CanvasEditorActions
.APPLY_DEFAULT_LAYOUT
,
138 addVirtualLinkDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_LINK_DESCRIPTOR
,
139 addForwardingGraphDescriptor
: CanvasEditorActions
.ADD_FORWARDING_GRAPH_DESCRIPTOR
,
140 addVirtualDeploymentDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_DEPLOYMENT_DESCRIPTOR
,
141 selectModel
: ComposerAppActions
.SELECT_MODEL
,
142 outlineModel
: ComposerAppActions
.OUTLINE_MODEL
,
143 showError
: ComposerAppActions
.SHOW_ERROR
,
144 clearError
: ComposerAppActions
.CLEAR_ERROR
,
145 setDragState
: ComposerAppActions
.SET_DRAG_STATE
,
146 filterCatalogByType
: CatalogFilterActions
.FILTER_BY_TYPE
,
147 setCanvasZoom
: CanvasEditorActions
.SET_CANVAS_ZOOM
,
148 showJsonViewer
: ComposerAppActions
.SHOW_JSON_VIEWER
,
149 closeJsonViewer
: ComposerAppActions
.CLOSE_JSON_VIEWER
,
150 toggleCanvasPanelTray
: CanvasPanelTrayActions
.TOGGLE_OPEN_CLOSE
,
151 openCanvasPanelTray
: CanvasPanelTrayActions
.OPEN
,
152 closeCanvasPanelTray
: CanvasPanelTrayActions
.CLOSE
,
153 enterFullScreenMode
: ComposerAppActions
.ENTER_FULL_SCREEN_MODE
,
154 exitFullScreenMode
: ComposerAppActions
.EXIT_FULL_SCREEN_MODE
,
155 showAssets
: ComposerAppActions
.showAssets
,
156 showDescriptor
: ComposerAppActions
.showDescriptor
,
157 getFilelistSuccess
: FileManagerActions
.getFilelistSuccess
,
158 updateFileLocationInput
: FileManagerActions
.updateFileLocationInput
,
159 sendDownloadFileRequst
: FileManagerActions
.sendDownloadFileRequst
,
160 addFileSuccess
: FileManagerActions
.addFileSuccess
,
161 deletePackageFile
: FileManagerActions
.deletePackageFile
,
162 deleteFileSuccess
: FileManagerActions
.deleteFileSuccess
,
163 closeFileManagerSockets
: FileManagerActions
.closeFileManagerSockets
,
164 openFileManagerSockets
: FileManagerActions
.openFileManagerSockets
,
165 openDownloadMonitoringSocketSuccess
: FileManagerActions
.openDownloadMonitoringSocketSuccess
,
166 getFilelistSocketSuccess
: FileManagerActions
.getFilelistSocketSuccess
168 this.exportPublicMethods({
169 closeFileManagerSockets
: this.closeFileManagerSockets
.bind(this)
174 if (e
.type
=== 'resize-manager.resize.catalog-panel') {
175 const layout
= Object
.assign({}, this.layout
);
176 layout
.left
= Math
.max(0, layout
.left
- e
.moved
.x
);
177 if (layout
.left
!== this.layout
.left
) {
178 this.setState({layout
: layout
});
180 } else if (e
.type
=== 'resize-manager.resize.details-panel') {
181 const layout
= Object
.assign({}, this.layout
);
182 layout
.right
= Math
.max(0, layout
.right
+ e
.moved
.x
);
183 if (layout
.right
!== this.layout
.right
) {
184 this.setState({layout
: layout
});
186 } else if (/^resize-manager\.resize\.canvas-panel-tray/.test(e
.type
)) {
187 const layout
= Object
.assign({}, this.layout
);
188 layout
.bottom
= Math
.max(25, layout
.bottom
+ e
.moved
.y
);
189 if (layout
.bottom
!== this.layout
.bottom
) {
190 const zoom
= autoZoomCanvasScale(layout
.bottom
) ;
191 if (this.zoom
!== zoom
) {
192 this.setState({layout
: layout
, zoom
: zoom
});
194 this.setState({layout
: layout
});
197 } else if (e
.type
!== 'resize') {
198 console
.log('no resize handler for ', e
.type
, '. Do you need to add a handler in ComposerAppStore::onResize()?')
200 SelectionManager
.refreshOutline();
204 if(!document
.body
.classList
.contains('resizing')) {
205 this.setState({item
: _
.cloneDeep(item
)});
207 SelectionManager
.refreshOutline();
210 editCatalogItem(item
) {
212 self
.closeFileManagerSockets();
213 if (item
&& item
.uiState
) {
214 item
.uiState
.isOpenForEdit
= true;
215 if (item
.uiState
.type
!== 'nsd') {
216 this.closeCanvasPanelTray();
219 SelectionManager
.select(item
);
220 this.updateItem(item
);
221 this.openFileManagerSockets(item
)
223 catalogItemMetaDataChanged(item
) {
224 this.updateItem(item
);
227 catalogItemDescriptorChanged(itemDescriptor
) {
228 this.catalogItemMetaDataChanged(itemDescriptor
.model
);
232 this.setState({showMore
: true});
236 this.setState({showMore
: false});
240 NotificationError
.defer({msg
: data
.errorMessage
, type
: 'error'})
241 // this.setState({message: data.errorMessage, messageType: 'error'});
245 this.setState({message
: '', messageType
: ''});
248 toggleShowMoreInfo() {
249 this.setState({showMore
: !this.showMore
});
252 applyDefaultLayout() {
253 if (this.item
&& this.item
.uiState
&& this.item
.uiState
.containerPositionMap
) {
254 if (!_
.isEmpty(this.item
.uiState
.containerPositionMap
)) {
255 this.item
.uiState
.containerPositionMap
= {};
256 CatalogItemsActions
.catalogItemMetaDataChanged
.defer(this.item
);
261 addVirtualLinkDescriptor(dropCoordinates
= null) {
264 if (this.item
.uiState
.type
=== 'nsd') {
265 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
266 vld
= nsdc
.createVld();
267 } else if (this.item
.uiState
.type
=== 'vnfd') {
268 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
269 vld
= vnfd
.createVld();
272 vld
.uiState
.dropCoordinates
= dropCoordinates
;
273 SelectionManager
.clearSelectionAndRemoveOutline();
274 SelectionManager
.addSelection(vld
);
275 this.updateItem(vld
.getRoot().model
);
276 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vld
.getRoot());
281 addForwardingGraphDescriptor(dropCoordinates
= null) {
282 if (this.item
&& this.item
.uiState
.type
=== 'nsd') {
283 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
284 const fg
= nsdc
.createVnffgd();
285 fg
.uiState
.dropCoordinates
= dropCoordinates
;
286 SelectionManager
.clearSelectionAndRemoveOutline();
287 SelectionManager
.addSelection(fg
);
288 this.updateItem(nsdc
.model
);
289 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(nsdc
);
293 addVirtualDeploymentDescriptor(dropCoordinates
= null) {
294 if (this.item
.uiState
.type
=== 'vnfd') {
295 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
296 const vdu
= vnfd
.createVdu();
297 vdu
.uiState
.dropCoordinates
= dropCoordinates
;
298 SelectionManager
.clearSelectionAndRemoveOutline();
299 SelectionManager
.addSelection(vdu
);
300 this.updateItem(vdu
.getRoot().model
);
301 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vdu
.getRoot());
305 selectModel(container
) {
306 if (SelectionManager
.select(container
)) {
307 const model
= DescriptorModelFactory
.isContainer(container
) ? container
.getRoot().model
: container
;
308 this.catalogItemMetaDataChanged(model
);
313 const uid
= UID
.from(obj
);
314 requestAnimationFrame(() => {
315 SelectionManager
.outline(Array
.from(document
.querySelectorAll(`[data-uid="${uid}"]`)));
320 SelectionManager
.clearSelectionAndRemoveOutline();
321 this.catalogItemMetaDataChanged(this.item
);
324 setDragState(dragState
) {
325 this.setState({drag
: dragState
});
328 filterCatalogByType(typeValue
) {
329 this.setState({filterCatalogByTypeValue
: typeValue
})
332 setCanvasZoom(zoom
) {
333 this.setState({zoom
: zoom
});
337 this.setState({showJSONViewer
: true});
341 this.setState({showJSONViewer
: false});
344 toggleCanvasPanelTray(event
) {
345 const layout
= this.layout
;
346 const attrMap
= event
.target
.attributes
;
347 let panelEvent
= null;
348 for(let k
in attrMap
) {
349 if(attrMap
[k
].name
== 'data-event') {
350 panelEvent
= attrMap
[k
].nodeValue
;
353 if ((layout
.bottom
> 25) && ((panelEvent
== this.displayedPanel
) || panelEvent
== 'arrow')) {
354 this.closeCanvasPanelTray();
356 this.openCanvasPanelTray();
358 this.setState({displayedPanel
: panelEvent
})
361 openCanvasPanelTray() {
363 left
: this.layout
.left
,
364 right
: this.layout
.right
,
367 const zoom
= defaults
.defaultPanelTrayOpenZoom
;
368 if (this.zoom
!== zoom
) {
369 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: this.zoom
});
371 this.setState({layout
: layout
});
375 closeCanvasPanelTray() {
377 left
: this.layout
.left
,
378 right
: this.layout
.right
,
381 const zoom
= this.restoreZoom
|| autoZoomCanvasScale(layout
.bottom
);
382 if (this.zoom
!== zoom
) {
383 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: null});
385 this.setState({layout
: layout
, restoreZoom
: null});
389 enterFullScreenMode() {
392 * https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
393 * This is an experimental api but works our target browsers and ignored by others
395 const eventNames
= ['fullscreenchange', 'mozfullscreenchange', 'webkitfullscreenchange', 'msfullscreenchange'];
397 const appRoot
= document
.body
;//.getElementById('RIFT_wareLaunchpadComposerAppRoot');
401 function onFullScreenChange() {
403 if (isFullScreen()) {
404 const layout
= comp
.layout
;
405 const restoreLayout
= _
.cloneDeep(layout
);
406 uiTransientState
.restoreLayout
= restoreLayout
;
409 comp
.setState({fullScreenMode
: true, layout
: layout
, restoreLayout
: restoreLayout
});
411 comp
.setState({fullScreenMode
: false, layout
: uiTransientState
.restoreLayout
});
416 if (this.fullScreenMode
=== false) {
418 if (appRoot
.requestFullscreen
) {
419 appRoot
.requestFullscreen();
420 } else if (appRoot
.msRequestFullscreen
) {
421 appRoot
.msRequestFullscreen();
422 } else if (appRoot
.mozRequestFullScreen
) {
423 appRoot
.mozRequestFullScreen();
424 } else if (appRoot
.webkitRequestFullscreen
) {
425 appRoot
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
428 eventNames
.map(name
=> {
429 document
.removeEventListener(name
, onFullScreenChange
);
430 document
.addEventListener(name
, onFullScreenChange
);
437 exitFullScreenMode() {
439 if (document
.exitFullscreen
) {
440 document
.exitFullscreen();
441 } else if (document
.msExitFullscreen
) {
442 document
.msExitFullscreen();
443 } else if (document
.mozCancelFullScreen
) {
444 document
.mozCancelFullScreen();
445 } else if (document
.webkitExitFullscreen
) {
446 document
.webkitExitFullscreen();
449 this.setState({fullScreenMode
: false});
454 panelTabShown
: 'assets'
459 panelTabShown
: 'descriptor'
463 //File Manager methods
464 getFilelistSuccess(data
) {
466 let filesState
= null;
467 if (self
.fileMonitoringSocketID
) {
468 filesState
= addInputState( _
.cloneDeep(this.filesState
),data
);
469 // filesState = _.merge(self.filesState, addInputState({},data));
470 let normalizedData
= normalizeTree(data
);
473 data
: _
.mergeWith(normalizedData
.data
, self
.files
.data
, function(obj
, src
) {
474 return _
.uniqBy(obj
? obj
.concat(src
) : src
, 'name');
476 id
: self
.files
.id
|| normalizedData
.id
478 filesState
: filesState
481 function normalizeTree(data
) {
486 data
.contents
.map(getContents
);
487 function getContents(d
) {
488 if(d
.hasOwnProperty('contents')) {
490 d
.contents
.map(function(c
,i
) {
491 if (!c
.hasOwnProperty('contents')) {
498 f
.data
[d
.name
] = contents
;
503 function addInputState(obj
, d
) {
505 if(d
.hasOwnProperty('contents')) {
506 d
.contents
.map(addInputState
.bind(null, obj
))
514 sendDownloadFileRequst(data
) {
515 let id
= data
.id
|| this.item
.id
;
516 let type
= data
.type
|| this.item
.uiState
.type
;
517 let path
= data
.path
;
519 this.getInstance().addFile(id
, type
, path
, url
);
521 updateFileLocationInput
= (data
) => {
522 let name
= data
.name
;
523 let value
= data
.value
;
524 var filesState
= _
.cloneDeep(this.filesState
);
525 filesState
[name
] = value
;
527 filesState
: filesState
530 addFileSuccess
= (data
) => {
531 let path
= data
.path
;
532 let fileName
= data
.fileName
;
533 let files
= _
.cloneDeep(this.files
);
534 let loadingIndex
= files
.data
[path
].push({
535 status
: 'DOWNLOADING',
536 name
: path
+ '/' + fileName
538 this.setState({files
: files
});
541 startWatchingJob
= () => {
542 let ws
= window
.multiplexer
.channel(this.jobSocketId
);
547 openDownloadMonitoringSocketSuccess
= (id
) => {
549 let ws
= window
.multiplexer
.channel(id
);
550 let downloadJobs
= _
.cloneDeep(self
.downloadJobs
);
552 ws
.onmessage
= (socket
) => {
553 if (self
.files
&& self
.files
.length
> 0) {
556 jobs
= JSON
.parse(socket
.data
);
558 newFiles
= _
.cloneDeep(self
.files
);
559 jobs
.map(function(j
) {
560 //check if not in completed state
561 let fullPath
= j
['package-path'];
562 let path
= fullPath
.split('/');
563 let fileName
= path
.pop();
564 path
= path
.join('/');
565 let index
= _
.findIndex(self
.files
.data
[path
], function(o
){
566 return fullPath
== o
.name
568 if((index
> -1) && newFiles
.data
[path
][index
]) {
569 newFiles
.data
[path
][index
].status
= j
.status
571 if(j
.status
.toUpperCase() == 'LOADING...' || j
.status
.toUpperCase() == 'IN_PROGRESS') {
572 newFiles
.data
[path
].push({
584 // console.log(JSON.parse(socket.data));
593 getFilelistSocketSuccess
= (id
) => {
595 let ws
= window
.multiplexer
.channel(id
);
596 ws
.onmessage
= (socket
) => {
597 if (self
.fileMonitoringSocketID
) {
600 data
= JSON
.parse(socket
.data
);
602 self
.getFilelistSuccess(data
)
607 fileMonitoringSocketID
: id
,
608 fileMonitoringSocket
: ws
612 closeFileManagerSockets() {
613 this.fileMonitoringSocketID
= null;
616 fileMonitoringSocketID
: null
618 // fileMonitoringSocket : null,
620 this.jobSocket
&& this.jobSocket
.close();
621 this.fileMonitoringSocket
&& this.fileMonitoringSocket
.close();
622 console
.log('closing');
624 openFileManagerSockets(i
) {
626 let item
= i
|| self
.item
;
627 this.files
= {data
:[]};
628 // this.closeFileManagerSockets();
629 this.getInstance().openFileMonitoringSocket(item
.id
, item
.uiState
.type
).then(function() {
630 // // self.getInstance().openDownloadMonitoringSocket(item.id);
632 this.getInstance().openDownloadMonitoringSocket(item
.id
);
637 deletePackageFile(name
) {
638 let id
= this.item
.id
;
639 let type
= this.item
.uiState
.type
;
640 this.getInstance().deleteFile(id
, type
, name
);
642 deleteFileSuccess
= (data
) => {
643 let path
= data
.path
.split('/')
644 let files
= _
.cloneDeep(this.files
);
646 path
= path
.join('/');
647 let pathFiles
= files
.data
[path
]
648 _
.remove(pathFiles
, function(c
) {
649 return c
.name
== data
.path
;
658 export default alt
.createStore(ComposerAppStore
, 'ComposerAppStore');