Advertisement
Guest User

Untitled

a guest
May 29th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. //Wait for jsPLumb library to load
  2. jsPlumb.ready(function() {
  3.  
  4.  
  5. // Double cliclk to add a node.
  6. //---------------------------------------------------------
  7. var i = 0;
  8. var a = 0;
  9.  
  10. $('#container').dblclick(function(e) {
  11.  
  12. //Create divs to be inserted into the container
  13. var newState = $('<div>').attr('id', 'state' + i).addClass('item');
  14. var title = $('<div>').addClass('title');
  15. var stateName = $('<input>').attr('type', 'text'); // input for name
  16. title.append(stateName);
  17.  
  18.  
  19.  
  20. var addChoice = $('<button>').text('Add Choice').attr('id', a);
  21.  
  22. var connect = $('<div>').addClass('connect');
  23.  
  24. // ensure that the div is placed where the user double clicks
  25. newState.css({
  26. 'top': e.pageY,
  27. 'left': e.pageX
  28. });
  29.  
  30. // Makes the new div a target
  31. jsPlumb.makeTarget(newState, {
  32. anchor: 'TopLeft'
  33. });
  34.  
  35. // Makes the new div Draggable
  36. jsPlumb.draggable(newState, {
  37. containment: 'parent'
  38. });
  39.  
  40. // Makes the connect div a source
  41. jsPlumb.makeSource(connect, {
  42. parent: newState,
  43. anchor: 'Continuous',
  44. endpoint: 'Rectangle'
  45. });
  46.  
  47. // When you double click on the div, it disappears
  48. /*newState.dblclick(function(e) {
  49. jsPlumb.detachAllConnections($(this));
  50. // alert(this.id);
  51. $(this).remove();
  52. e.stopPropagation();
  53. });*/
  54.  
  55. // Function that links to button to add new div
  56. addChoice.click(function(e) {
  57. var pDoc = document.getElementById(this.id);
  58. var parent = (pDoc.parentNode.id);
  59.  
  60. var choice = $('<div>').addClass('title'); // make class "choice" and add .addClass(choice);
  61. var choiceInput = $('<input>').attr('type', 'text'); // input for name
  62. choiceInput.keyup(function(e) {
  63. if (e.keyCode === 13) {
  64. $(this).parent().text(this.value);
  65. }
  66. });
  67. choice.append(choiceInput);
  68. newState.append(choice);
  69. choiceInput.focus();
  70.  
  71. });
  72.  
  73.  
  74.  
  75. stateName.keyup(function(e) {
  76. if (e.keyCode === 13) {
  77. // THe following is used to reset the id of the div, But use caution because there can be no two identiacal id's
  78. // var state = $(this).closest('.item'); // Find the div that represents the state.
  79. // state.children('.title').text(this.value); // Change the title text.
  80. // state.attr('id', this.value); // Alter the id of the div.
  81.  
  82. $(this).parent().text(this.value); // Sets the value of the text equal to the input
  83. }
  84. });
  85. stateName.focus();
  86.  
  87. // At the connect divs to the main newState div
  88. newState.append(title);
  89. newState.append(addChoice);
  90. newState.append(connect);
  91.  
  92. // Add the div to the container
  93. $('#container').append(newState);
  94.  
  95.  
  96. i++;
  97.  
  98. });
  99.  
  100. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement