From 7b540068c03a75f41c4fa5b9d4b50b276e43b8c7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 11 May 2020 20:21:55 +0200 Subject: [PATCH] Saving and Loading Workspace from Backend --- Canvas.js | 12 ++---- .../FunctionLibraries/UiObjectsFromNodes.js | 2 +- DesignSpace/Workspace/Workspace.js | 41 +++++++++++++------ Spaces/ChartingSpace.js | 6 ++- Spaces/CockpitSpace.js | 22 ++++++---- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/Canvas.js b/Canvas.js index b13ac359c..51ad14431 100644 --- a/Canvas.js +++ b/Canvas.js @@ -109,12 +109,12 @@ function newCanvas () { thisObject.topSpace = newTopSpace() thisObject.topSpace.initialize() - thisObject.designSpace = newDesignSpace() - thisObject.designSpace.initialize() - thisObject.cockpitSpace = newCockpitSpace() thisObject.cockpitSpace.initialize() + thisObject.designSpace = newDesignSpace() + thisObject.designSpace.initialize() + thisObject.panelsSpace = newPanelsSpace() thisObject.panelsSpace.initialize() @@ -295,11 +295,7 @@ function newCanvas () { } if (event.shiftKey === true && (event.ctrlKey === true || event.metaKey === true) && (event.key === 'S' || event.key === 's')) { - let saved = canvas.designSpace.workspace.save() - if (saved === true) { - canvas.cockpitSpace.setStatus('Workspace Saved.', 50, canvas.cockpitSpace.statusTypes.ALL_GOOD) - } - + canvas.designSpace.workspace.save() event.preventDefault() return } diff --git a/DesignSpace/Workspace/FunctionLibraries/UiObjectsFromNodes.js b/DesignSpace/Workspace/FunctionLibraries/UiObjectsFromNodes.js index d6253f558..992a37b8f 100644 --- a/DesignSpace/Workspace/FunctionLibraries/UiObjectsFromNodes.js +++ b/DesignSpace/Workspace/FunctionLibraries/UiObjectsFromNodes.js @@ -22,7 +22,7 @@ function newUiObjectsFromNodes () { addIncludedNodes() function addIncludedNodes () { - blobService = newFileStorage() + let blobService = newFileStorage() // if (node.code === undefined) { node.code = '{ \n"includeDataMines": ["Masters", "Sparta", "TradingEngines"],\n"includeTradingSystems": ["Sparta-WHB-BTC-USDT", "Example-ETH-USDT", "Sparta-BRR-BTC-USDT"],\n"includeSuperScripts": ["Masters"]\n }' diff --git a/DesignSpace/Workspace/Workspace.js b/DesignSpace/Workspace/Workspace.js index fc23acf05..f50e50f4d 100644 --- a/DesignSpace/Workspace/Workspace.js +++ b/DesignSpace/Workspace/Workspace.js @@ -85,19 +85,34 @@ function newWorkspace () { function initialize () { try { - let reset = window.localStorage.getItem('Reset') - let savedWorkspace = window.localStorage.getItem(CANVAS_APP_NAME + '.' + 'Workspace') + let lastUsedWorkspace = window.localStorage.getItem('Last Used Workspace') - if (savedWorkspace === null || reset !== null) { - thisObject.workspaceNode = getWorkspace() + if (lastUsedWorkspace !== 'undefined' && lastUsedWorkspace !== null && lastUsedWorkspace !== undefined) { + let blobService = newFileStorage() + blobService.getFileFromHost('LoadWorkspace' + '/' + lastUsedWorkspace, onFileReceived, true) + function onFileReceived (err, text, response) { + if (err && err.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) { + canvas.cockpitSpace.setStatus('Could not load the last Workspace used, called "' + lastUsedWorkspace + '". Will switch to the default Workspace instead.', 500, canvas.cockpitSpace.statusTypes.WARNING) + thisObject.workspaceNode = getWorkspace() // This is the default workspace that comes with the system. + finishInitialization() + return + } + thisObject.workspaceNode = JSON.parse(text) + finishInitialization() + } } else { - thisObject.workspaceNode = JSON.parse(savedWorkspace) + thisObject.workspaceNode = getWorkspace() // This is the default workspace that comes with the system. + finishInitialization() } - functionLibraryUiObjectsFromNodes.recreateWorkspace(thisObject.workspaceNode) - setupEventsServerClients() - thisObject.enabled = true - setInterval(saveWorkspace, 60000) + function finishInitialization () { + functionLibraryUiObjectsFromNodes.recreateWorkspace(thisObject.workspaceNode) + setupEventsServerClients() + thisObject.enabled = true + canvas.cockpitSpace.initializePosition() + + setInterval(saveWorkspace, 60000) + } } catch (err) { if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) } } @@ -171,14 +186,16 @@ function newWorkspace () { window.localStorage.setItem(CANVAS_APP_NAME + '.' + 'Workspace', textToSave) window.localStorage.setItem('Session Timestamp', sessionTimestamp) - let url = 'SaveWorkspace/' + workspace.name - callServer(textToSave, url, onResponse) + if (workspace.name !== undefined) { + let url = 'SaveWorkspace/' + workspace.name + callServer(textToSave, url, onResponse) + } function onResponse (err) { if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { window.localStorage.setItem('Last Used Workspace', workspace.name) window.localStorage.setItem('Session Timestamp', sessionTimestamp) - canvas.cockpitSpace.setStatus('Workspace Auto-Saved.', 50, canvas.cockpitSpace.statusTypes.ALL_GOOD) + canvas.cockpitSpace.setStatus(workspace.name + ' Saved.', 50, canvas.cockpitSpace.statusTypes.ALL_GOOD) } else { canvas.cockpitSpace.setStatus('Could not save the Workspace at the Backend. Please check the Backend Console for more information.', 150, canvas.cockpitSpace.statusTypes.WARNING) } diff --git a/Spaces/ChartingSpace.js b/Spaces/ChartingSpace.js index ae4d6fee0..91568cacf 100644 --- a/Spaces/ChartingSpace.js +++ b/Spaces/ChartingSpace.js @@ -96,8 +96,10 @@ function newChartingSpace () { let rootNodes = canvas.designSpace.workspace.workspaceNode.rootNodes for (let i = 0; i < rootNodes.length; i++) { let rootNode = rootNodes[i] - if (rootNode.type === 'Charting Space') { - thisObject.payload = rootNode.payload + if (rootNode !== null) { + if (rootNode.type === 'Charting Space') { + thisObject.payload = rootNode.payload + } } } diff --git a/Spaces/CockpitSpace.js b/Spaces/CockpitSpace.js index 248e07c39..ed8cfb932 100644 --- a/Spaces/CockpitSpace.js +++ b/Spaces/CockpitSpace.js @@ -20,7 +20,8 @@ physics: physics, getContainer: getContainer, finalize: finalize, - initialize: initialize + initialize: initialize, + initializePosition: initializePosition } thisObject.container = newContainer() @@ -39,6 +40,7 @@ let statusCounter = 0 let statusText let statusType + let isInitilized = false return thisObject @@ -52,6 +54,15 @@ } function initialize () { + canvasBrowserResizedEventSubscriptionId = canvas.eventHandler.listenToEvent('Browser Resized', resize) + selfMouseClickEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseClick', onMouseClick) + + thisObject.fullscreen = newFullScreen() + thisObject.fullscreen.container.connectToParent(thisObject.container) + thisObject.fullscreen.initialize() + } + + function initializePosition () { thisObject.container.frame.position.x = 0 let INITIAL_POSITION @@ -68,13 +79,7 @@ INITIAL_POSITION = 100 thisObject.container.frame.position.y = browserCanvas.height * INITIAL_POSITION / 100 - COCKPIT_SPACE_HEIGHT } - - canvasBrowserResizedEventSubscriptionId = canvas.eventHandler.listenToEvent('Browser Resized', resize) - selfMouseClickEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseClick', onMouseClick) - - thisObject.fullscreen = newFullScreen() - thisObject.fullscreen.container.connectToParent(thisObject.container) - thisObject.fullscreen.initialize() + isInitilized = true } function onMouseClick (event) { @@ -148,6 +153,7 @@ thisObject.fullscreen.physics() } function thisObjectPhysics () { + if (isInitilized === false) { return } /* Check the limits */ thisObject.status = 'MIDDLE'