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 _isNumber
from 'lodash/isNumber'
22 import _cloneDeep
from 'lodash/cloneDeep'
23 import _isEmpty
from 'lodash/isEmpty'
24 import _mergeWith
from 'lodash/mergeWith'
25 import _uniqBy
from 'lodash/uniqBy'
26 import _isEqual
from 'lodash/isEqual'
27 import _findIndex
from 'lodash/findIndex'
28 import _remove
from 'lodash/remove'
30 import alt
from '../alt'
31 import UID
from '../libraries/UniqueId'
32 import DescriptorModelFactory
from '../libraries/model/DescriptorModelFactory'
33 import PanelResizeAction
from '../actions/PanelResizeAction'
34 import CatalogItemsActions
from '../actions/CatalogItemsActions'
35 import CanvasEditorActions
from '../actions/CanvasEditorActions'
36 import ComposerAppActions
from '../actions/ComposerAppActions'
37 import CatalogFilterActions
from '../actions/CatalogFilterActions'
38 import CanvasPanelTrayActions
from '../actions/CanvasPanelTrayActions'
39 import SelectionManager
from '../libraries/SelectionManager'
40 import CatalogDataStore
from '../stores/CatalogDataStore'
41 import isFullScreen
from '../libraries/isFullScreen'
43 import FileManagerSource
from '../components/filemanager/FileManagerSource';
44 import FileManagerActions
from '../components/filemanager/FileManagerActions';
46 import React
from 'react';
48 //Hack for crouton fix. Should eventually put composer in skyquake alt context
49 import SkyquakeComponent
from 'widgets/skyquake_container/skyquakeComponent.jsx';
50 let NotificationError
= null;
52 import utils
from '../libraries/utils';
54 class ComponentBridge
extends React
.Component
{
57 NotificationError
= this.props
.flux
.actions
.global
.showNotification
;
63 const getDefault
= (name
, defaultValue
) => {
64 const val
= window
.localStorage
.getItem('defaults-' + name
);
68 return setDefault(name
, 0);
73 setDefault(name
, defaultValue
);
77 const setDefault
= (name
, defaultValue
) => {
78 window
.localStorage
.setItem('defaults-' + name
, defaultValue
);
82 /* the top and bottom positions are managed by css; requires div to be display: absolute*/
84 left
: getDefault('catalog-panel-start-width', 300),
85 right
: getDefault('details-panel-start-width', 365),
86 bottom
: 25 + getDefault('defaults-forwarding-graphs-panel-start-height', 0),
88 zoom
: getDefault('zoom', 100),
89 filterCatalogBy
: 'nsd',
90 defaultPanelTrayOpenZoom
: (() => {
91 let zoom
= parseFloat(getDefault('panel-tray-zoom', 75));
95 zoom
= Math
.min(100, zoom
);
96 zoom
= Math
.max(25, zoom
);
97 setDefault('panel-tray-zoom', zoom
);
102 const autoZoomCanvasScale
= d3
.scale
.linear().domain([0, 300]).range([100, 50]).clamp(true);
104 const uiTransientState
= {};
106 class ComposerAppStore
{
109 //Bridge for crouton fix
110 this.ComponentBridgeElement
= SkyquakeComponent(ComponentBridge
);
112 this.exportAsync(FileManagerSource
)
113 // the catalog item currently being edited in the composer
115 // the left and right sides of the canvas area
118 right
: defaults
.right
,
119 bottom
: defaults
.bottom
121 uiTransientState
.restoreLayout
= this.layout
;
122 this.zoom
= defaults
.zoom
;
123 this.showMore
= defaults
.showMore
;
124 this.filterCatalogByTypeValue
= defaults
.filterCatalogBy
;
125 // transient ui state
128 this.messageType
= '';
129 this.showJSONViewer
= false;
130 this.showClassifiers
= {};
131 this.editPathsMode
= false;
132 this.fullScreenMode
= false;
133 this.panelTabShown
= 'descriptor';
134 //File manager values
136 this.filesState
= {};
137 this.downloadJobs
= {};
138 this.containers
= [];
139 this.newPathName
= '';
140 //End File manager values
142 onResize
: PanelResizeAction
.RESIZE
,
143 editCatalogItem
: CatalogItemsActions
.EDIT_CATALOG_ITEM
,
144 catalogItemMetaDataChanged
: CatalogItemsActions
.CATALOG_ITEM_META_DATA_CHANGED
,
145 catalogItemDescriptorChanged
: CatalogItemsActions
.CATALOG_ITEM_DESCRIPTOR_CHANGED
,
146 toggleShowMoreInfo
: CanvasEditorActions
.TOGGLE_SHOW_MORE_INFO
,
147 showMoreInfo
: CanvasEditorActions
.SHOW_MORE_INFO
,
148 showLessInfo
: CanvasEditorActions
.SHOW_LESS_INFO
,
149 applyDefaultLayout
: CanvasEditorActions
.APPLY_DEFAULT_LAYOUT
,
150 addVirtualLinkDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_LINK_DESCRIPTOR
,
151 addForwardingGraphDescriptor
: CanvasEditorActions
.ADD_FORWARDING_GRAPH_DESCRIPTOR
,
152 addVirtualDeploymentDescriptor
: CanvasEditorActions
.ADD_VIRTUAL_DEPLOYMENT_DESCRIPTOR
,
153 selectModel
: ComposerAppActions
.SELECT_MODEL
,
154 outlineModel
: ComposerAppActions
.OUTLINE_MODEL
,
155 showError
: ComposerAppActions
.SHOW_ERROR
,
156 clearError
: ComposerAppActions
.CLEAR_ERROR
,
157 setDragState
: ComposerAppActions
.SET_DRAG_STATE
,
158 filterCatalogByType
: CatalogFilterActions
.FILTER_BY_TYPE
,
159 setCanvasZoom
: CanvasEditorActions
.SET_CANVAS_ZOOM
,
160 showJsonViewer
: ComposerAppActions
.SHOW_JSON_VIEWER
,
161 closeJsonViewer
: ComposerAppActions
.CLOSE_JSON_VIEWER
,
162 toggleCanvasPanelTray
: CanvasPanelTrayActions
.TOGGLE_OPEN_CLOSE
,
163 openCanvasPanelTray
: CanvasPanelTrayActions
.OPEN
,
164 closeCanvasPanelTray
: CanvasPanelTrayActions
.CLOSE
,
165 enterFullScreenMode
: ComposerAppActions
.ENTER_FULL_SCREEN_MODE
,
166 exitFullScreenMode
: ComposerAppActions
.EXIT_FULL_SCREEN_MODE
,
167 showAssets
: ComposerAppActions
.showAssets
,
168 showDescriptor
: ComposerAppActions
.showDescriptor
,
169 getFilelistSuccess
: FileManagerActions
.getFilelistSuccess
,
170 updateFileLocationInput
: FileManagerActions
.updateFileLocationInput
,
171 sendDownloadFileRequst
: FileManagerActions
.sendDownloadFileRequst
,
172 addFileSuccess
: FileManagerActions
.addFileSuccess
,
173 deletePackageFile
: FileManagerActions
.deletePackageFile
,
174 deleteFileSuccess
: FileManagerActions
.deleteFileSuccess
,
175 closeFileManagerSockets
: FileManagerActions
.closeFileManagerSockets
,
176 openFileManagerSockets
: FileManagerActions
.openFileManagerSockets
,
177 openDownloadMonitoringSocketSuccess
: FileManagerActions
.openDownloadMonitoringSocketSuccess
,
178 getFilelistSocketSuccess
: FileManagerActions
.getFilelistSocketSuccess
,
179 newPathNameUpdated
: FileManagerActions
.newPathNameUpdated
,
180 createDirectory
: FileManagerActions
.createDirectory
182 this.exportPublicMethods({
183 closeFileManagerSockets
: this.closeFileManagerSockets
.bind(this)
188 if (e
.type
=== 'resize-manager.resize.catalog-panel') {
189 const layout
= Object
.assign({}, this.layout
);
190 layout
.left
= Math
.max(0, layout
.left
- e
.moved
.x
);
191 if (layout
.left
!== this.layout
.left
) {
192 this.setState({layout
: layout
});
194 } else if (e
.type
=== 'resize-manager.resize.details-panel') {
195 const layout
= Object
.assign({}, this.layout
);
196 layout
.right
= Math
.max(0, layout
.right
+ e
.moved
.x
);
197 if (layout
.right
!== this.layout
.right
) {
198 this.setState({layout
: layout
});
200 } else if (/^resize-manager\.resize\.canvas-panel-tray/.test(e
.type
)) {
201 const layout
= Object
.assign({}, this.layout
);
202 layout
.bottom
= Math
.max(25, layout
.bottom
+ e
.moved
.y
);
203 if (layout
.bottom
!== this.layout
.bottom
) {
204 const zoom
= autoZoomCanvasScale(layout
.bottom
) ;
205 if (this.zoom
!== zoom
) {
206 this.setState({layout
: layout
, zoom
: zoom
});
208 this.setState({layout
: layout
});
211 } else if (e
.type
!== 'resize') {
212 console
.log('no resize handler for ', e
.type
, '. Do you need to add a handler in ComposerAppStore::onResize()?')
214 SelectionManager
.refreshOutline();
221 if(!document
.body
.classList
.contains('resizing')) {
222 containers
= [item
].reduce(DescriptorModelFactory
.buildCatalogItemFactory(CatalogDataStore
.getState().catalogs
), []);
224 containers
.filter(d
=> DescriptorModelFactory
.isConnectionPoint(d
)).forEach(d
=> {
225 d
.cpNumber
= ++cpNumber
;
226 containers
.filter(d
=> DescriptorModelFactory
.isVnfdConnectionPointRef(d
)).filter(ref
=> ref
.key
=== d
.key
).forEach(ref
=> ref
.cpNumber
= d
.cpNumber
);
228 this.setState({containers
: containers
, item
: _cloneDeep(item
)});
230 SelectionManager
.refreshOutline();
233 editCatalogItem(item
) {
235 self
.closeFileManagerSockets();
236 if (item
&& item
.uiState
) {
237 item
.uiState
.isOpenForEdit
= true;
238 if (item
.uiState
.type
!== 'nsd') {
239 this.closeCanvasPanelTray();
242 SelectionManager
.select(item
);
243 this.updateItem(item
);
245 this.openFileManagerSockets(item
);
248 catalogItemMetaDataChanged(item
) {
249 this.updateItem(item
);
252 catalogItemDescriptorChanged(itemDescriptor
) {
253 this.catalogItemMetaDataChanged(itemDescriptor
.model
);
257 this.setState({showMore
: true});
261 this.setState({showMore
: false});
265 NotificationError
.defer({msg
: data
.errorMessage
, type
: 'error'})
266 // this.setState({message: data.errorMessage, messageType: 'error'});
270 this.setState({message
: '', messageType
: ''});
273 toggleShowMoreInfo() {
274 this.setState({showMore
: !this.showMore
});
277 applyDefaultLayout() {
278 if (this.item
&& this.item
.uiState
&& this.item
.uiState
.containerPositionMap
) {
279 if (!_isEmpty(this.item
.uiState
.containerPositionMap
)) {
280 this.item
.uiState
.containerPositionMap
= {};
281 CatalogItemsActions
.catalogItemMetaDataChanged
.defer(this.item
);
286 addVirtualLinkDescriptor(dropCoordinates
= null) {
289 if (this.item
.uiState
.type
=== 'nsd') {
290 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
291 vld
= nsdc
.createVld();
292 } else if (this.item
.uiState
.type
=== 'vnfd') {
293 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
294 vld
= vnfd
.createVld();
297 vld
.uiState
.dropCoordinates
= dropCoordinates
;
298 SelectionManager
.clearSelectionAndRemoveOutline();
299 SelectionManager
.addSelection(vld
);
300 this.updateItem(vld
.getRoot().model
);
301 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vld
.getRoot());
306 addForwardingGraphDescriptor(dropCoordinates
= null) {
307 if (this.item
&& this.item
.uiState
.type
=== 'nsd') {
308 const nsdc
= DescriptorModelFactory
.newNetworkService(this.item
);
309 const fg
= nsdc
.createVnffgd();
310 fg
.uiState
.dropCoordinates
= dropCoordinates
;
311 SelectionManager
.clearSelectionAndRemoveOutline();
312 SelectionManager
.addSelection(fg
);
313 this.updateItem(nsdc
.model
);
314 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(nsdc
);
318 addVirtualDeploymentDescriptor(dropCoordinates
= null) {
319 if (this.item
.uiState
.type
=== 'vnfd') {
320 const vnfd
= DescriptorModelFactory
.newVirtualNetworkFunction(this.item
);
321 const vdu
= vnfd
.createVdu();
322 vdu
.uiState
.dropCoordinates
= dropCoordinates
;
323 SelectionManager
.clearSelectionAndRemoveOutline();
324 SelectionManager
.addSelection(vdu
);
325 this.updateItem(vdu
.getRoot().model
);
326 CatalogItemsActions
.catalogItemDescriptorChanged
.defer(vdu
.getRoot());
330 selectModel(container
) {
331 if (SelectionManager
.select(container
)) {
332 const model
= DescriptorModelFactory
.isContainer(container
) ? container
.getRoot().model
: container
;
333 this.catalogItemMetaDataChanged(model
);
338 const uid
= UID
.from(obj
);
339 requestAnimationFrame(() => {
340 SelectionManager
.outline(Array
.from(document
.querySelectorAll(`[data-uid="${uid}"]`)));
345 SelectionManager
.clearSelectionAndRemoveOutline();
346 this.catalogItemMetaDataChanged(this.item
);
349 setDragState(dragState
) {
350 this.setState({drag
: dragState
});
353 filterCatalogByType(typeValue
) {
354 this.setState({filterCatalogByTypeValue
: typeValue
})
357 setCanvasZoom(zoom
) {
358 this.setState({zoom
: zoom
});
362 this.setState({showJSONViewer
: true});
366 this.setState({showJSONViewer
: false});
369 toggleCanvasPanelTray() {
370 const layout
= this.layout
;
371 if (layout
.bottom
> 25) {
372 this.closeCanvasPanelTray();
374 this.openCanvasPanelTray();
378 openCanvasPanelTray() {
380 left
: this.layout
.left
,
381 right
: this.layout
.right
,
384 const zoom
= defaults
.defaultPanelTrayOpenZoom
;
385 if (this.zoom
!== zoom
) {
386 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: this.zoom
});
388 this.setState({layout
: layout
});
392 closeCanvasPanelTray() {
394 left
: this.layout
.left
,
395 right
: this.layout
.right
,
398 const zoom
= this.restoreZoom
|| autoZoomCanvasScale(layout
.bottom
);
399 if (this.zoom
!== zoom
) {
400 this.setState({layout
: layout
, zoom
: zoom
, restoreZoom
: null});
402 this.setState({layout
: layout
, restoreZoom
: null});
406 enterFullScreenMode() {
409 * https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
410 * This is an experimental api but works our target browsers and ignored by others
412 const eventNames
= ['fullscreenchange', 'mozfullscreenchange', 'webkitfullscreenchange', 'msfullscreenchange'];
414 const appRoot
= document
.body
;//.getElementById('RIFT_wareLaunchpadComposerAppRoot');
418 function onFullScreenChange() {
420 if (isFullScreen()) {
421 const layout
= comp
.layout
;
422 const restoreLayout
= _cloneDeep(layout
);
423 uiTransientState
.restoreLayout
= restoreLayout
;
426 comp
.setState({fullScreenMode
: true, layout
: layout
, restoreLayout
: restoreLayout
});
428 comp
.setState({fullScreenMode
: false, layout
: uiTransientState
.restoreLayout
});
433 if (this.fullScreenMode
=== false) {
435 if (appRoot
.requestFullscreen
) {
436 appRoot
.requestFullscreen();
437 } else if (appRoot
.msRequestFullscreen
) {
438 appRoot
.msRequestFullscreen();
439 } else if (appRoot
.mozRequestFullScreen
) {
440 appRoot
.mozRequestFullScreen();
441 } else if (appRoot
.webkitRequestFullscreen
) {
442 appRoot
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
445 eventNames
.map(name
=> {
446 document
.removeEventListener(name
, onFullScreenChange
);
447 document
.addEventListener(name
, onFullScreenChange
);
454 exitFullScreenMode() {
456 if (document
.exitFullscreen
) {
457 document
.exitFullscreen();
458 } else if (document
.msExitFullscreen
) {
459 document
.msExitFullscreen();
460 } else if (document
.mozCancelFullScreen
) {
461 document
.mozCancelFullScreen();
462 } else if (document
.webkitExitFullscreen
) {
463 document
.webkitExitFullscreen();
466 this.setState({fullScreenMode
: false});
471 panelTabShown
: 'assets'
476 panelTabShown
: 'descriptor'
480 //File Manager methods
481 getFilelistSuccess(data
) {
483 let filesState
= null;
484 if (self
.fileMonitoringSocketID
) {
486 if(data
.hasOwnProperty('contents')) {
487 filesState
= addInputState( _cloneDeep(this.filesState
),data
);
488 let normalizedData
= normalizeTree(data
);
491 data
: _mergeWith(normalizedData
.data
, self
.files
.data
, function(obj
, src
) {
492 return _uniqBy(obj
? obj
.concat(src
) : src
, 'name');
494 id
: normalizedData
.id
496 filesState
: filesState
503 if(!_isEqual(newState
.files
, this.files
) || ! _isEqual(newState
.fileState
, this.fileState
)) {
504 this.setState(newState
);
508 function normalizeTree(data
) {
513 data
.contents
.map(getContents
);
514 function getContents(d
) {
515 if(d
.hasOwnProperty('contents')) {
517 d
.contents
.map(function(c
,i
) {
518 if (!c
.hasOwnProperty('contents')) {
525 f
.data
[d
.name
] = contents
;
530 function addInputState(obj
, d
) {
532 if(d
.hasOwnProperty('contents')) {
533 d
.contents
.map(addInputState
.bind(null, obj
))
541 sendDownloadFileRequst(data
) {
542 let id
= data
.id
|| this.item
.id
;
543 let type
= data
.type
|| this.item
.uiState
.type
;
544 let path
= data
.path
;
546 this.getInstance().addFile(id
, type
, path
, url
, data
.refresh
);
548 updateFileLocationInput
= (data
) => {
549 let name
= data
.name
;
550 let value
= data
.value
;
551 var filesState
= _cloneDeep(this.filesState
);
552 filesState
[name
] = value
;
554 filesState
: filesState
557 addFileSuccess
= (data
) => {
559 let path
= data
.path
;
560 let fileName
= data
.fileName
;
561 let files
= _cloneDeep(this.files
);
562 let loadingIndex
= files
.data
[path
].push({
563 status
: 'DOWNLOADING',
564 name
: path
+ '/' + fileName
566 this.setState({files
: files
});
570 startWatchingJob
= () => {
571 let ws
= window
.multiplexer
.channel(this.jobSocketId
);
576 openDownloadMonitoringSocketSuccess
= (id
) => {
578 let ws
= window
.multiplexer
.channel(id
);
579 let downloadJobs
= _cloneDeep(self
.downloadJobs
);
580 let newFiles
= false;
581 ws
.onmessage
= (socket
) => {
582 if (self
.files
&& self
.files
.length
> 0) {
585 jobs
= JSON
.parse(socket
.data
);
587 newFiles
= _cloneDeep(self
.files
);
588 jobs
.map(function(j
) {
589 //check if not in completed state
590 let fullPath
= j
['package-path'];
591 let path
= fullPath
.split('/');
592 let fileName
= path
.pop();
593 path
= path
.join('/');
594 let index
= _findIndex(self
.files
.data
[path
], function(o
){
595 return fullPath
== o
.name
597 if((index
> -1) && newFiles
.data
[path
][index
]) {
598 newFiles
.data
[path
][index
].status
= j
.status
600 if(j
.status
.toUpperCase() == 'LOADING...' || j
.status
.toUpperCase() == 'IN_PROGRESS') {
601 newFiles
.data
[path
].push({
613 // console.log(JSON.parse(socket.data));
622 getFilelistSocketSuccess
= (id
) => {
624 let ws
= window
.multiplexer
.channel(id
);
625 ws
.onmessage
= (socket
) => {
626 if (self
.fileMonitoringSocketID
) {
629 data
= JSON
.parse(socket
.data
);
631 self
.getFilelistSuccess(data
)
636 fileMonitoringSocketID
: id
,
637 fileMonitoringSocket
: ws
641 closeFileManagerSockets() {
642 this.fileMonitoringSocketID
= null;
645 fileMonitoringSocketID
: null
647 // fileMonitoringSocket : null,
649 this.jobSocket
&& this.jobSocket
.close();
650 this.fileMonitoringSocket
&& this.fileMonitoringSocket
.close();
651 console
.log('closing');
653 openFileManagerSockets(i
) {
655 let item
= i
|| self
.item
;
656 // this.closeFileManagerSockets();
657 this.getInstance().openFileMonitoringSocket(item
.id
, item
.uiState
.type
).then(function() {
658 // // self.getInstance().openDownloadMonitoringSocket(item.id);
660 this.getInstance().openDownloadMonitoringSocket(item
.id
);
665 deletePackageFile(name
) {
666 let id
= this.item
.id
;
667 let type
= this.item
.uiState
.type
;
668 this.getInstance().deleteFile(id
, type
, name
);
670 deleteFileSuccess
= (data
) => {
671 let path
= data
.path
.split('/')
672 let files
= _cloneDeep(this.files
);
674 path
= path
.join('/');
675 let pathFiles
= files
.data
[path
]
676 _remove(pathFiles
, function(c
) {
677 return c
.name
== data
.path
;
684 newPathNameUpdated
= (event
) => {
685 const value
= event
.target
.value
;
690 createDirectory
= () => {
691 console
.log(this.newPathName
);
692 this.sendDownloadFileRequst({
694 type
: this.item
.uiState
.type
,
695 path
: this.item
.name
+ '/' + this.newPathName
,
696 url
: utils
.getSearchParams(window
.location
).dev_download_server
|| window
.location
.protocol
+ '//' + window
.location
.host
,
705 export default alt
.createStore(ComposerAppStore
, 'ComposerAppStore');