diff --git a/webapp/app.py b/webapp/app.py index 30d933b9..5ca17fa1 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -182,6 +182,37 @@ def create_node(session_id): ), 201 +@app.route("/sessions//nodes/", methods=["PUT"]) +@synchronized +def edit_node(session_id, node_id): + session = coreemu.sessions.get(session_id) + if not session: + return jsonify(error="session does not exist"), 404 + + if node_id.isdigit(): + node_id = int(node_id) + node = session.objects.get(node_id) + if not node: + return jsonify(error="node does not exist"), 404 + + data = request.get_json() or {} + + node_options = NodeOptions() + x = data.get("x") + y = data.get("y") + node_options.set_position(x, y) + lat = data.get("lat") + lon = data.get("lon") + alt = data.get("alt") + node_options.set_location(lat, lon, alt) + + result = session.update_node(node_id, node_options) + if result: + return jsonify() + else: + return jsonify(error="error during node edit"), 404 + + @app.route("/sessions//nodes/") def get_node(session_id, node_id): session = coreemu.sessions.get(session_id) diff --git a/webapp/static/corenetwork.js b/webapp/static/corenetwork.js index 1be78120..47bda1d1 100644 --- a/webapp/static/corenetwork.js +++ b/webapp/static/corenetwork.js @@ -155,6 +155,7 @@ class CoreNetwork { }; this.network = new vis.Network(this.container, this.networkData, this.networkOptions); this.network.on('doubleClick', this.addNode.bind(this)); + this.network.on('dragEnd', this.dragEnd.bind(this)); this.edges.on('add', this.addEdge.bind(this)); this.nodesEnabled = false; } @@ -173,6 +174,25 @@ class CoreNetwork { return session; } + getCoreNode(nodeId) { + return this.nodes.get(nodeId).coreNode; + } + + async dragEnd(properties) { + console.log('drag end properties: ', properties); + if (properties.nodes.length == 1) { + const nodeId = properties.nodes[0]; + const networkNode = this.nodes.get(nodeId); + const coreNode = networkNode.coreNode; + coreNode.x = properties.pointer.canvas.x; + coreNode.y = properties.pointer.canvas.y; + if (await this.coreRest.isRunning()) { + console.log('updated core node location: ', coreNode.x, coreNode.y); + await this.coreRest.editNode(coreNode); + } + } + } + reset() { this.nodeId = 0; this.nodes.clear(); diff --git a/webapp/static/corerest.js b/webapp/static/corerest.js index b13ebf83..9e4a1b9e 100644 --- a/webapp/static/corerest.js +++ b/webapp/static/corerest.js @@ -69,6 +69,14 @@ class CoreRest { return await postJson(`/sessions/${this.currentSession}/nodes`, node); } + async editNode(node) { + return await putJson(`/sessions/${this.currentSession}/nodes/${node.id}`, { + id: node.id, + x: node.x, + y: node.y + }); + } + async createLink(link) { return await postJson(`/sessions/${this.currentSession}/links`, link); } @@ -102,4 +110,9 @@ class CoreRest { return session; } + + async isRunning() { + const session = await this.getSession(); + return session.state === SessionStates.runtime; + } }