diff --git a/src/public/javascripts/services/drag_and_drop.js b/src/public/javascripts/services/drag_and_drop.js index 072e552ce..7e7bac84f 100644 --- a/src/public/javascripts/services/drag_and_drop.js +++ b/src/public/javascripts/services/drag_and_drop.js @@ -8,11 +8,17 @@ const dragAndDropSetup = { return false; } + node.setSelected(true); + + const selectedNodes = treeService.getSelectedNodes().map(node => { + return { + noteId: node.data.noteId, + title: node.data.title + } + }); + // this is for dragging notes into relation map - data.dataTransfer.setData("text", JSON.stringify({ - noteId: node.data.noteId, - title: node.data.title - })); + data.dataTransfer.setData("text", JSON.stringify(selectedNodes)); // This function MUST be defined to enable dragging for the tree. // Return false to cancel dragging of node. @@ -27,9 +33,6 @@ const dragAndDropSetup = { // This function MUST be defined to enable dropping of items on the tree. // data.hitMode is 'before', 'after', or 'over'. - const nodeToMove = data.otherNode; - nodeToMove.setSelected(true); - const selectedNodes = treeService.getSelectedNodes(); if (data.hitMode === "before") { diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index 2ac6af958..be723f429 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -383,6 +383,13 @@ function getFreePosition() { return [100, maxY + 200]; } +async function refresh() { + // delete all endpoints and connections + jsPlumbInstance.deleteEveryEndpoint(); + + await loadNotesAndRelations(); +} + $addChildNotesButton.click(async () => { const children = await server.get("notes/" + noteDetailService.getCurrentNoteId() + "/children"); @@ -411,10 +418,7 @@ $addChildNotesButton.click(async () => { saveData(); - // delete all endpoints and connections - jsPlumbInstance.deleteEveryEndpoint(); - - await loadNotesAndRelations(); + await refresh(); }); let clipboard = null; @@ -448,27 +452,46 @@ function getZoom() { return matches[1]; } - -function dragover_handler(ev) { - ev.preventDefault(); - // Set the dropEffect to move - //ev.dataTransfer.dropEffect = "move"; - - console.log("DRAGOVER"); -} - -function drop_handler(ev) { +async function dropNoteOntoRelationMapHandler(ev) { ev.preventDefault(); - const note = JSON.parse(ev.originalEvent.dataTransfer.getData("text")); + const notes = JSON.parse(ev.originalEvent.dataTransfer.getData("text")); let {x, y} = getMousePosition(ev); // modifying position so that cursor is on the top-center of the box - x -= 80; + const startX = x -= 80; y -= 15; - createNoteBox(note.noteId, note.title, x, y); + const currentNoteId = treeService.getCurrentNode().data.noteId; + + for (const note of notes) { + if (note.noteId === currentNoteId) { + // we don't allow placing current (relation map) into itself + // the reason is that when dragging notes from the tree, the relation map is always selected + // since it's focused. + continue; + } + + const exists = mapData.notes.some(n => n.noteId === note.noteId); + + if (exists) { + alert(`Note "${note.title}" is already placed into the diagram`); + continue; + } + + mapData.notes.push({id: note.noteId, x, y}); + + if (x - startX > 1000) { + x = startX; + y += 200; + } + else { + x += 200; + } + } + + await refresh(); } function getMousePosition(evt) { @@ -482,8 +505,8 @@ function getMousePosition(evt) { }; } -$component.on("drop", drop_handler); -$component.on("dragover", dragover_handler); +$component.on("drop", dropNoteOntoRelationMapHandler); +$component.on("dragover", ev => ev.preventDefault()); export default { show, diff --git a/src/views/details/relation_map.ejs b/src/views/details/relation_map.ejs index a06f62d55..550973fc5 100644 --- a/src/views/details/relation_map.ejs +++ b/src/views/details/relation_map.ejs @@ -1,20 +1,19 @@