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.

255 lines
7.2 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, clearNodePopUp, 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, cancelNodeEdit, 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, cancelAction, 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 = cancelAction.bind(this, callback);
  141. document.getElementById('node-popUp').style.display = 'block';
  142. }
  143. // Callback passed as parameter is ignored
  144. function clearNodePopUp() {
  145. document.getElementById('node-saveButton').onclick = null;
  146. document.getElementById('node-cancelButton').onclick = null;
  147. document.getElementById('node-popUp').style.display = 'none';
  148. }
  149. function cancelNodeEdit(callback) {
  150. clearNodePopUp();
  151. callback(null);
  152. }
  153. function saveNodeData(data, callback) {
  154. data.label = document.getElementById('node-label').value;
  155. clearNodePopUp();
  156. callback(data);
  157. }
  158. function editEdgeWithoutDrag(data, callback) {
  159. // filling in the popup DOM elements
  160. document.getElementById('edge-label').value = data.label;
  161. document.getElementById('edge-saveButton').onclick = saveEdgeData.bind(this, data, callback);
  162. document.getElementById('edge-cancelButton').onclick = cancelEdgeEdit.bind(this,callback);
  163. document.getElementById('edge-popUp').style.display = 'block';
  164. }
  165. function clearEdgePopUp() {
  166. document.getElementById('edge-saveButton').onclick = null;
  167. document.getElementById('edge-cancelButton').onclick = null;
  168. document.getElementById('edge-popUp').style.display = 'none';
  169. }
  170. function cancelEdgeEdit(callback) {
  171. clearEdgePopUp();
  172. callback(null);
  173. }
  174. function saveEdgeData(data, callback) {
  175. if (typeof data.to === 'object')
  176. data.to = data.to.id
  177. if (typeof data.from === 'object')
  178. data.from = data.from.id
  179. data.label = document.getElementById('edge-label').value;
  180. clearEdgePopUp();
  181. callback(data);
  182. }
  183. function init() {
  184. setDefaultLocale();
  185. draw();
  186. }
  187. </script>
  188. </head>
  189. <body onload="init();">
  190. <h2>Editing the nodes and edges-without-drag (localized)</h2>
  191. <p style="width: 700px; font-size:14px; text-align: justify;">
  192. The localization is only relevant to the manipulation buttons.
  193. </p>
  194. <p>
  195. <label for="locale">Select a locale:</label>
  196. <select id="locale" onchange="draw();">
  197. <option value="en">en</option>
  198. <option value="de">de</option>
  199. <option value="es">es</option>
  200. <option value="it">it</option>
  201. <option value="nl">nl</option>
  202. <option value="pt-br">pt</option>
  203. <option value="ru">ru</option>
  204. </select>
  205. </p>
  206. <div id="node-popUp">
  207. <span id="node-operation">node</span> <br>
  208. <table style="margin:auto;">
  209. <tr>
  210. <td>id</td><td><input id="node-id" value="new value" /></td>
  211. </tr>
  212. <tr>
  213. <td>label</td><td><input id="node-label" value="new value" /></td>
  214. </tr>
  215. </table>
  216. <input type="button" value="save" id="node-saveButton" />
  217. <input type="button" value="cancel" id="node-cancelButton" />
  218. </div>
  219. <div id="edge-popUp">
  220. <span id="edge-operation">edge</span> <br>
  221. <table style="margin:auto;">
  222. <tr>
  223. <td>label</td><td><input id="edge-label" value="new value" /></td>
  224. </tr></table>
  225. <input type="button" value="save" id="edge-saveButton" />
  226. <input type="button" value="cancel" id="edge-cancelButton" />
  227. </div>
  228. <br />
  229. <div id="mynetwork"></div>
  230. </body>
  231. </html>