UNPKG

gojs

Version:

Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams

87 lines (82 loc) 3.21 kB
<!DOCTYPE html> <html> <head> <title>HTML Drag and Drop</title> <meta name="description" content="Drag-and-drop HTML elements into a GoJS Diagram using jQuery." /> <!-- Copyright 1998-2016 by Northwoods Software Corporation. --> <meta charset="UTF-8"> <script src="go.js"></script> <link href="../assets/css/goSamples.css" rel="stylesheet" type="text/css" /> <!-- you don't need to use this --> <script src="goSamples.js"></script> <!-- this is only for the GoJS Samples framework --> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script> <script id="code"> function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this // Note that we do not use $ here as an alias for go.GraphObject.make because we are using $ for jQuery var $$ = go.GraphObject.make; // for conciseness in defining templates myDiagram = $$(go.Diagram, "myDiagramDiv", // must name or refer to the DIV HTML element { initialPosition: new go.Point(0, 0) }); myDiagram.nodeTemplate = $$(go.Node, "Auto", { locationSpot: go.Spot.Center }, new go.Binding("location", "loc", go.Point.parse), $$(go.Shape, "Ellipse", { fill: "white" }), $$(go.TextBlock, { margin: 5 }, new go.Binding("text", "text").makeTwoWay())); $("li").draggable({ stack: "#myDiagramDiv", revert: true, revertDuration: 0 }); $("#myDiagramDiv").droppable({ activeClass: "ui-state-highlight", drop: function(event, ui) { var elt = ui.draggable.first(); var text = elt.context.textContent; var x = ui.offset.left - myDiagram.div.offsetLeft; var y = ui.offset.top - myDiagram.div.offsetTop; var p = new go.Point(x, y); var q = myDiagram.transformViewToDoc(p); var model = myDiagram.model; model.startTransaction("drop"); model.addNodeData({ text: text, loc: go.Point.stringify(q) }); model.commitTransaction("drop"); } }); } </script> </head> <body onload="init()"> <div id="sample"> <div style="width:100%; white-space:nowrap;"> <span style="display: inline-block; vertical-align: top; width:20%"> <div id="myItems" style="border: solid 1px black; height: 700px"> <ul> <li>First</li> <li>Second</li> <li>Third</li> <li>Fourth</li> <li>Fifth</li> </ul> </div> </span> <span style="display: inline-block; vertical-align: top; width:80%"> <div id="myDiagramDiv" style="border: solid 1px black; height: 700px"></div> </span> </div> <div id ="description"> <p> This demonstrates using jQuery drag-and-drop capability to allow the user to drag HTML list items into a GoJS diagram. </p> </div> </div> </body> </html>