vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.1 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>Network | Manipulation | Edit Edge Without Drag</title>
  6. <style type="text/css">
  7. body, select {
  8. font: 10pt sans;
  9. }
  10. #mynetwork {
  11. position:relative;
  12. width: 800px;
  13. height: 600px;
  14. border: 1px solid lightgray;
  15. }
  16. table.legend_table {
  17. font-size: 11px;
  18. border-width:1px;
  19. border-color:#d3d3d3;
  20. border-style:solid;
  21. }
  22. table.legend_table,td {
  23. border-width:1px;
  24. border-color:#d3d3d3;
  25. border-style:solid;
  26. padding: 2px;
  27. }
  28. div.table_content {
  29. width:80px;
  30. text-align:center;
  31. }
  32. div.table_description {
  33. width:100px;
  34. }
  35. #operation {
  36. font-size:28px;
  37. }
  38. #node-popUp {
  39. display:none;
  40. position:absolute;
  41. top:350px;
  42. left:170px;
  43. z-index:299;
  44. width:250px;
  45. height:120px;
  46. background-color: #f9f9f9;
  47. border-style:solid;
  48. border-width:3px;
  49. border-color: #5394ed;
  50. padding:10px;
  51. text-align: center;
  52. }
  53. #edge-popUp {
  54. display:none;
  55. position:absolute;
  56. top:350px;
  57. left:170px;
  58. z-index:299;
  59. width:250px;
  60. height:90px;
  61. background-color: #f9f9f9;
  62. border-style:solid;
  63. border-width:3px;
  64. border-color: #5394ed;
  65. padding:10px;
  66. text-align: center;
  67. }
  68. </style>
  69. <script type="text/javascript" src="../exampleUtil.js"></script>
  70. <script type="text/javascript" src="../../../dist/vis.js"></script>
  71. <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />
  72. <script type="text/javascript">
  73. var nodes = null;
  74. var edges = null;
  75. var network = null;
  76. // randomly create some nodes and edges
  77. var data = getScaleFreeNetwork(25);
  78. var seed = 2;
  79. function setDefaultLocale() {
  80. var defaultLocal = navigator.language;
  81. var select = document.getElementById('locale');
  82. select.selectedIndex = 0; // set fallback value
  83. for (var i = 0, j = select.options.length; i < j; ++i) {
  84. if (select.options[i].getAttribute('value') === defaultLocal) {
  85. select.selectedIndex = i;
  86. break;
  87. }
  88. }
  89. }
  90. function destroy() {
  91. if (network !== null) {
  92. network.destroy();
  93. network = null;
  94. }
  95. }
  96. function draw() {
  97. destroy();
  98. nodes = [];
  99. edges = [];
  100. // create a network
  101. var container = document.getElementById('mynetwork');
  102. var options = {
  103. layout: {randomSeed:seed}, // just to make sure the layout is the same when the locale is changed
  104. locale: document.getElementById('locale').value,
  105. manipulation: {
  106. addNode: function (data, callback) {
  107. // filling in the popup DOM elements
  108. document.getElementById('node-operation').innerHTML = "Add Node";
  109. editNode(data, callback);
  110. },
  111. editNode: function (data, callback) {
  112. // filling in the popup DOM elements
  113. document.getElementById('node-operation').innerHTML = "Edit Node";
  114. editNode(data, callback);
  115. },
  116. addEdge: function (data, callback) {
  117. if (data.from == data.to) {
  118. var r = confirm("Do you want to connect the node to itself?");
  119. if (r != true) {
  120. callback(null);
  121. return;
  122. }
  123. }
  124. document.getElementById('edge-operation').innerHTML = "Add Edge";
  125. editEdgeWithoutDrag(data, callback);
  126. },
  127. editEdge: {
  128. editWithoutDrag: function(data, callback) {
  129. document.getElementById('edge-operation').innerHTML = "Edit Edge";
  130. editEdgeWithoutDrag(data,callback);
  131. }
  132. }
  133. }
  134. };
  135. network = new vis.Network(container, data, options);
  136. }
  137. function editNode(data, callback) {
  138. document.getElementById('node-label').value = data.label;
  139. document.getElementById('node-saveButton').onclick = saveNodeData.bind(this, data, callback);
  140. document.getElementById('node-cancelButton').onclick = clearNodePopUp.bind();
  141. document.getElementById('node-popUp').style.display = 'block';
  142. }
  143. function clearNodePopUp() {
  144. document.getElementById('node-saveButton').onclick = null;
  145. document.getElementById('node-cancelButton').onclick = null;
  146. document.getElementById('node-popUp').style.display = 'none';
  147. }
  148. function cancelNodeEdit(callback) {
  149. clearNodePopUp();
  150. callback(null);
  151. }
  152. function saveNodeData(data, callback) {
  153. data.label = document.getElementById('node-label').value;
  154. clearNodePopUp();
  155. callback(data);
  156. }
  157. function editEdgeWithoutDrag(data, callback) {
  158. // filling in the popup DOM elements
  159. document.getElementById('edge-label').value = data.label;
  160. document.getElementById('edge-saveButton').onclick = saveEdgeData.bind(this, data, callback);
  161. document.getElementById('edge-cancelButton').onclick = cancelEdgeEdit.bind(this,callback);
  162. document.getElementById('edge-popUp').style.display = 'block';
  163. }
  164. function clearEdgePopUp() {
  165. document.getElementById('edge-saveButton').onclick = null;
  166. document.getElementById('edge-cancelButton').onclick = null;
  167. document.getElementById('edge-popUp').style.display = 'none';
  168. }
  169. function cancelEdgeEdit(callback) {
  170. clearEdgePopUp();
  171. callback(null);
  172. }
  173. function saveEdgeData(data, callback) {
  174. if (typeof data.to === 'object')
  175. data.to = data.to.id
  176. if (typeof data.from === 'object')
  177. data.from = data.from.id
  178. data.label = document.getElementById('edge-label').value;
  179. clearEdgePopUp();
  180. callback(data);
  181. }
  182. function init() {
  183. setDefaultLocale();
  184. draw();
  185. }
  186. </script>
  187. </head>
  188. <body onload="init();">
  189. <h2>Editing the nodes and edges-without-drag (localized)</h2>
  190. <p style="width: 700px; font-size:14px; text-align: justify;">
  191. The localization is only relevant to the manipulation buttons.
  192. </p>
  193. <p>
  194. <label for="locale">Select a locale:</label>
  195. <select id="locale" onchange="draw();">
  196. <option value="en">en</option>
  197. <option value="de">de</option>
  198. <option value="es">es</option>
  199. <option value="it">it</option>
  200. <option value="nl">nl</option>
  201. <option value="pt-br">pt</option>
  202. <option value="ru">ru</option>
  203. </select>
  204. </p>
  205. <div id="node-popUp">
  206. <span id="node-operation">node</span> <br>
  207. <table style="margin:auto;">
  208. <tr>
  209. <td>id</td><td><input id="node-id" value="new value" /></td>
  210. </tr>
  211. <tr>
  212. <td>label</td><td><input id="node-label" value="new value" /></td>
  213. </tr>
  214. </table>
  215. <input type="button" value="save" id="node-saveButton" />
  216. <input type="button" value="cancel" id="node-cancelButton" />
  217. </div>
  218. <div id="edge-popUp">
  219. <span id="edge-operation">edge</span> <br>
  220. <table style="margin:auto;">
  221. <tr>
  222. <td>label</td><td><input id="edge-label" value="new value" /></td>
  223. </tr></table>
  224. <input type="button" value="save" id="edge-saveButton" />
  225. <input type="button" value="cancel" id="edge-cancelButton" />
  226. </div>
  227. <br />
  228. <div id="mynetwork"></div>
  229. </body>
  230. </html>