Advertisement
Guest User

Untitled

a guest
May 28th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <link rel="stylesheet" href="joint.css" />
  6. <script src="joint.js"></script>
  7. <script src="joint.shapes.devs.js"></script>
  8.  
  9.  
  10. </head>
  11. <body>
  12.  
  13. <title>Test</title>
  14. <!-- Form for creating a new node -->
  15. <div>
  16. <form id="NewNode" action="form_action.asp">
  17. Question: <input type="text" id = "quest" name="quest"><br>
  18. <input type="button" onclick="addNode()" value="Submit">
  19. <input type="button" onclick="addSubNode()" value="New Answer">
  20. </form>
  21. </div>
  22. <br>
  23. <!-- Zoom Options are set here -->
  24. <div>
  25. <button onclick="zoomIn()">Zoom in</button>
  26. <button onclick="zoomOut()">Zoom Out</button>
  27. <button onclick="zoomReset()">Reset</button>
  28. </div>
  29. <br>
  30. <div id="paper"> </div>
  31. </body>
  32.  
  33.  
  34. <script>
  35.  
  36. // Make the graph
  37. //------------------------------------------------------
  38. var graph = new joint.dia.Graph;
  39. var selected;
  40. var paper = new joint.dia.Paper({ el: $('#paper'), width: 650, height: 500, gridSize: 1, model: graph });
  41.  
  42. var el1 = new joint.shapes.basic.Rect({
  43. position: { x: 50, y: 50 }, size: { width: 100, height: 40 },
  44. attrs: {
  45. rect: { 'stroke-width': '5', 'stroke-opacity': .7, stroke: 'black', rx: 3, ry: 3, fill: 'lightgray', 'fill-opacity': .5 },
  46. text: { text: 'Drop me over B', 'font-size': 10, style: { 'text-shadow': '1px 1px 1px lightgray' } }
  47. }
  48. });
  49. graph.addCells([el1, el1.clone().translate(200, 50).attr('text/text', 'B')]);
  50.  
  51. // Here is the real deal. Listen on cell:pointerup and link to an element found below.
  52. paper.on('cell:pointerup', function(cellView, evt, x, y) {
  53.  
  54. // Find the first element below that is not a link nor the dragged element itself.
  55. var elementBelow = graph.get('cells').find(function(cell) {
  56. if (cell instanceof joint.dia.Link) return false; // Not interested in links.
  57. if (cell.id === cellView.model.id) return false; // The same element as the dropped one.
  58. if (cell.getBBox().containsPoint(g.point(x, y))) {
  59. return true;
  60. }
  61. return false;
  62. });
  63.  
  64. // If the two elements are connected already, don't
  65. // connect them again (this is application specific though).
  66. if (elementBelow && !_.contains(graph.getNeighbors(elementBelow), cellView.model)) {
  67.  
  68. graph.addCell(new joint.dia.Link({
  69. source: { id: cellView.model.id }, target: { id: elementBelow.id },
  70. attrs: { '.marker-source': { d: 'M 10 0 L 0 5 L 10 10 z' } }
  71. }));
  72. // Move the element a bit to the side.
  73. cellView.model.translate(-200, 0);
  74. }
  75. selected = cellView.model.id;
  76. });
  77.  
  78. paper.on('cell:pointerclick', function(cellView, evt, x, y) {
  79. selected = cellView.model.id;
  80. });
  81.  
  82. // form to add another node
  83. //------------------------------------------------------
  84. function makeQuestion(){
  85. var question = document.getElementById("quest").value;
  86. var ans = document.getElementById("ans").value;
  87. var rect3 = new joint.shapes.basic.Rect({
  88. position: { x: 100, y: 20 },
  89. size: { width: 100, height: 30 },
  90. attrs: { rect: { fill: 'blue' }, text: { text: "go", fill: 'white' } }
  91. });
  92. graph.addCell(rect3);
  93.  
  94. }
  95. function addNode(){
  96. var question = document.getElementById("quest").value;
  97. var rect3 = new joint.shapes.basic.Rect({
  98. position: { x: 200, y: 50 },
  99. size: { width: 200, height: 100 },
  100. attrs: { rect: { fill: '#E74C3C' }, text: { text: question , fill: 'white' } }
  101. });
  102. graph.addCell(rect3);
  103. }
  104.  
  105. // Zoom options
  106. //------------------------------------------------------
  107. var graphScale = 1;
  108.  
  109. var paperScale = function(sx, sy) {
  110. paper.scale(sx, sy);
  111. };
  112.  
  113. function zoomIn(){
  114. graphScale += 0.1;
  115. paperScale(graphScale, graphScale);
  116. }
  117.  
  118. function zoomOut(){
  119. graphScale -= 0.1;
  120. paperScale(graphScale, graphScale);
  121. }
  122.  
  123.  
  124. var zoomReset = function() {
  125. graphScale = 1;
  126. paperScale(graphScale, graphScale);
  127. };
  128.  
  129.  
  130. paper.$el.on('mousewheel DOMMouseScroll', onMouseWheel);
  131.  
  132. function onMouseWheel(e) {
  133.  
  134. e.preventDefault();
  135. e = e.originalEvent;
  136.  
  137. var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))) / 50;
  138. var offsetX = (e.offsetX || e.clientX - $(this).offset().left); // offsetX is not defined in FF
  139. var offsetY = (e.offsetY || e.clientY - $(this).offset().top); // offsetY is not defined in FF
  140. var p = offsetToLocalPoint(offsetX, offsetY);
  141. var newScale = V(paper.viewport).scale().sx + delta; // the current paper scale changed by delta
  142.  
  143. if (newScale > 0.4 && newScale < 2) {
  144. paper.setOrigin(0, 0); // reset the previous viewport translation
  145. paper.scale(newScale, newScale, p.x, p.y);
  146. }
  147. }
  148.  
  149. function offsetToLocalPoint(x, y) {
  150. var svgPoint = paper.svg.createSVGPoint();
  151. svgPoint.x = x;
  152. svgPoint.y = y;
  153. // Transform point into the viewport coordinate system.
  154. var pointTransformed = svgPoint.matrixTransform(paper.viewport.getCTM().inverse());
  155. return pointTransformed;
  156. }
  157.  
  158. function addSubNode(){
  159. if(selected){
  160. var rect4 = new joint.shapes.basic.Rect({
  161. position: { x: 300, y: 50 },
  162. size: { width: 200, height: 25 },
  163. attrs: { rect: { fill: 'blue' }, text: { text: "go" , fill: 'white' } }
  164. });
  165.  
  166.  
  167. var returnedCell = graph.getCell(selected);
  168. alert(returnedCell);
  169. returnedCell.embed(rect4)
  170. graph.addCell([rect4, returnedCell])
  171. }
  172.  
  173. var ClickableView = joint.dia.ElementView.extend({
  174. pointerdown: function () {
  175. this._click = true;
  176. joint.dia.ElementView.prototype.pointerdown.apply(this, arguments);
  177. },
  178. pointermove: function () {
  179. this._click = false;
  180. joint.dia.ElementView.prototype.pointermove.apply(this, arguments);
  181. },
  182. pointerup: function (evt, x, y) {
  183. if (this._click) {
  184. // triggers an event on the paper and the element itself
  185. this.notify('cell:click', evt, x, y);
  186. } else {
  187. joint.dia.ElementView.prototype.pointerup.apply(this, arguments);
  188. }
  189. }
  190. });
  191.  
  192.  
  193. </script>
  194. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement