From 1c09156478d1f515a9d43d73f4e697f7e4fb85f3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 28 Aug 2020 20:30:06 +0200 Subject: [PATCH] node path --- .../Workspace/FunctionLibraries/NodePath.js | 56 +++++++++++++++++++ DesignSpace/Workspace/Workspace.js | 14 ++++- Utilities/Clipboard.js | 35 ++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 DesignSpace/Workspace/FunctionLibraries/NodePath.js create mode 100644 Utilities/Clipboard.js diff --git a/DesignSpace/Workspace/FunctionLibraries/NodePath.js b/DesignSpace/Workspace/FunctionLibraries/NodePath.js new file mode 100644 index 000000000..02dc6362c --- /dev/null +++ b/DesignSpace/Workspace/FunctionLibraries/NodePath.js @@ -0,0 +1,56 @@ +function newNodePath() { + thisObject = { + getNodePath: getNodePath + } + + return thisObject + + function getNodePath(node) { + + let nodePath = 'value' + + recursiveGet(node) + + return 'tradingEngine.' + nodePath + + function recursiveGet(node) { + + if (node.payload === undefined) { return } + if (node.payload.parentNode === undefined) { return } + + let nodeDefinition = getNodeDefinition(node.payload.parentNode) + if (nodeDefinition === undefined) { return } + if (nodeDefinition.properties === undefined) { return } + + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + let parentProperty = node.payload.parentNode[property.name] + if (parentProperty === undefined) { continue } + + switch (property.type) { + case 'node': { + if (parentProperty.id === node.id) { + /* We found it */ + nodePath = property.name + '.' + nodePath + recursiveGet(node.payload.parentNode) + } + } + break + case 'array': { + for (let j = 0; j < parentProperty.length; j++) { + let arrayProperty = parentProperty[j] + if (arrayProperty === undefined) {continue} + if (arrayProperty.id === node.id) { + /* We found it */ + nodePath = property.name + '[' + j + ']' + '.' + nodePath + recursiveGet(node.payload.parentNode) + } + } + } + break + } + } + } + } +} diff --git a/DesignSpace/Workspace/Workspace.js b/DesignSpace/Workspace/Workspace.js index 1fc06b306..db4879ff6 100644 --- a/DesignSpace/Workspace/Workspace.js +++ b/DesignSpace/Workspace/Workspace.js @@ -64,6 +64,7 @@ function newWorkspace() { let functionLibraryCCXTFunctions = newCCXTFunctions() let functionLibraryWebhookFunctions = newWebhookFunctions() let functionLibraryDependenciesFilter = newDependenciesFilter() + let functionLibraryNodePath = newNodePath() thisObject.nodeChildren = newNodeChildren() @@ -323,7 +324,7 @@ function newWorkspace() { if (stringifyReadyNodes.length > 0) { return JSON.stringify(workspace) } else { - return + return } } @@ -454,6 +455,17 @@ function newWorkspace() { async function onMenuItemClick(payload, action, relatedUiObject, callBackFunction) { switch (action) { + case 'Copy Node Path': + { + let nodePath = functionLibraryNodePath.getNodePath(payload.node) + + copyTextToClipboard(nodePath) + + canvas.cockpitSpace.setStatus( + nodePath + ' copied to the Clipboard.' + , 50, canvas.cockpitSpace.statusTypes.ALL_GOOD) + } + break case 'Add UI Object': { functionLibraryUiObjectsFromNodes.addUIObject(payload.node, relatedUiObject) diff --git a/Utilities/Clipboard.js b/Utilities/Clipboard.js new file mode 100644 index 000000000..d6af25c88 --- /dev/null +++ b/Utilities/Clipboard.js @@ -0,0 +1,35 @@ +function fallbackCopyTextToClipboard(text) { + var textArea = document.createElement("textarea"); + textArea.value = text; + + // Avoid scrolling to bottom + textArea.style.top = "0"; + textArea.style.left = "0"; + textArea.style.position = "fixed"; + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + var successful = document.execCommand('copy'); + var msg = successful ? 'successful' : 'unsuccessful'; + //console.log('Fallback: Copying text command was ' + msg); + } catch (err) { + console.error('Fallback: Oops, unable to copy', err); + } + + document.body.removeChild(textArea); +} + +function copyTextToClipboard(text) { + if (!navigator.clipboard) { + fallbackCopyTextToClipboard(text); + return; + } + navigator.clipboard.writeText(text).then(function() { + //console.log('Async: Copying to clipboard was successful!'); + }, function(err) { + console.error('Async: Could not copy text: ', err); + }); +} \ No newline at end of file