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.

691 lines
25 KiB

  1. var util = require('../../util');
  2. var Node = require('../Node');
  3. var Edge = require('../Edge');
  4. /**
  5. * clears the toolbar div element of children
  6. *
  7. * @private
  8. */
  9. exports._clearManipulatorBar = function() {
  10. this._recursiveDOMDelete(this.manipulationDiv);
  11. this.manipulationDOM = {};
  12. this._manipulationReleaseOverload = function () {};
  13. delete this.sectors['support']['nodes']['targetNode'];
  14. delete this.sectors['support']['nodes']['targetViaNode'];
  15. this.controlNodesActive = false;
  16. this.freezeSimulation = false;
  17. };
  18. /**
  19. * Manipulation UI temporarily overloads certain functions to extend or replace them. To be able to restore
  20. * these functions to their original functionality, we saved them in this.cachedFunctions.
  21. * This function restores these functions to their original function.
  22. *
  23. * @private
  24. */
  25. exports._restoreOverloadedFunctions = function() {
  26. for (var functionName in this.cachedFunctions) {
  27. if (this.cachedFunctions.hasOwnProperty(functionName)) {
  28. this[functionName] = this.cachedFunctions[functionName];
  29. delete this.cachedFunctions[functionName];
  30. }
  31. }
  32. };
  33. /**
  34. * Enable or disable edit-mode.
  35. *
  36. * @private
  37. */
  38. exports._toggleEditMode = function() {
  39. this.editMode = !this.editMode;
  40. var toolbar = this.manipulationDiv;
  41. var closeDiv = this.closeDiv;
  42. var editModeDiv = this.editModeDiv;
  43. if (this.editMode == true) {
  44. toolbar.style.display="block";
  45. closeDiv.style.display="block";
  46. editModeDiv.style.display="none";
  47. closeDiv.onclick = this._toggleEditMode.bind(this);
  48. }
  49. else {
  50. toolbar.style.display="none";
  51. closeDiv.style.display="none";
  52. editModeDiv.style.display="block";
  53. closeDiv.onclick = null;
  54. }
  55. this._createManipulatorBar()
  56. };
  57. /**
  58. * main function, creates the main toolbar. Removes functions bound to the select event. Binds all the buttons of the toolbar.
  59. *
  60. * @private
  61. */
  62. exports._createManipulatorBar = function() {
  63. // remove bound functions
  64. if (this.boundFunction) {
  65. this.off('select', this.boundFunction);
  66. }
  67. var locale = this.constants.locales[this.constants.locale];
  68. if (this.edgeBeingEdited !== undefined) {
  69. this.edgeBeingEdited._disableControlNodes();
  70. this.edgeBeingEdited = undefined;
  71. this.selectedControlNode = null;
  72. this.controlNodesActive = false;
  73. this._redraw();
  74. }
  75. // restore overloaded functions
  76. this._restoreOverloadedFunctions();
  77. // resume calculation
  78. this.freezeSimulation = false;
  79. // reset global variables
  80. this.blockConnectingEdgeSelection = false;
  81. this.forceAppendSelection = false;
  82. this.manipulationDOM = {};
  83. if (this.editMode == true) {
  84. while (this.manipulationDiv.hasChildNodes()) {
  85. this.manipulationDiv.removeChild(this.manipulationDiv.firstChild);
  86. }
  87. this.manipulationDOM['addNodeSpan'] = document.createElement('span');
  88. this.manipulationDOM['addNodeSpan'].className = 'network-manipulationUI add';
  89. this.manipulationDOM['addNodeLabelSpan'] = document.createElement('span');
  90. this.manipulationDOM['addNodeLabelSpan'].className = 'network-manipulationLabel';
  91. this.manipulationDOM['addNodeLabelSpan'].innerHTML = locale['addNode'];
  92. this.manipulationDOM['addNodeSpan'].appendChild(this.manipulationDOM['addNodeLabelSpan']);
  93. this.manipulationDOM['seperatorLineDiv1'] = document.createElement('div');
  94. this.manipulationDOM['seperatorLineDiv1'].className = 'network-seperatorLine';
  95. this.manipulationDOM['addEdgeSpan'] = document.createElement('span');
  96. this.manipulationDOM['addEdgeSpan'].className = 'network-manipulationUI connect';
  97. this.manipulationDOM['addEdgeLabelSpan'] = document.createElement('span');
  98. this.manipulationDOM['addEdgeLabelSpan'].className = 'network-manipulationLabel';
  99. this.manipulationDOM['addEdgeLabelSpan'].innerHTML = locale['addEdge'];
  100. this.manipulationDOM['addEdgeSpan'].appendChild(this.manipulationDOM['addEdgeLabelSpan']);
  101. this.manipulationDiv.appendChild(this.manipulationDOM['addNodeSpan']);
  102. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv1']);
  103. this.manipulationDiv.appendChild(this.manipulationDOM['addEdgeSpan']);
  104. if (this._getSelectedNodeCount() == 1 && this.triggerFunctions.edit) {
  105. this.manipulationDOM['seperatorLineDiv2'] = document.createElement('div');
  106. this.manipulationDOM['seperatorLineDiv2'].className = 'network-seperatorLine';
  107. this.manipulationDOM['editNodeSpan'] = document.createElement('span');
  108. this.manipulationDOM['editNodeSpan'].className = 'network-manipulationUI edit';
  109. this.manipulationDOM['editNodeLabelSpan'] = document.createElement('span');
  110. this.manipulationDOM['editNodeLabelSpan'].className = 'network-manipulationLabel';
  111. this.manipulationDOM['editNodeLabelSpan'].innerHTML = locale['editNode'];
  112. this.manipulationDOM['editNodeSpan'].appendChild(this.manipulationDOM['editNodeLabelSpan']);
  113. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv2']);
  114. this.manipulationDiv.appendChild(this.manipulationDOM['editNodeSpan']);
  115. }
  116. else if (this._getSelectedEdgeCount() == 1 && this._getSelectedNodeCount() == 0) {
  117. this.manipulationDOM['seperatorLineDiv3'] = document.createElement('div');
  118. this.manipulationDOM['seperatorLineDiv3'].className = 'network-seperatorLine';
  119. this.manipulationDOM['editEdgeSpan'] = document.createElement('span');
  120. this.manipulationDOM['editEdgeSpan'].className = 'network-manipulationUI edit';
  121. this.manipulationDOM['editEdgeLabelSpan'] = document.createElement('span');
  122. this.manipulationDOM['editEdgeLabelSpan'].className = 'network-manipulationLabel';
  123. this.manipulationDOM['editEdgeLabelSpan'].innerHTML = locale['editEdge'];
  124. this.manipulationDOM['editEdgeSpan'].appendChild(this.manipulationDOM['editEdgeLabelSpan']);
  125. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv3']);
  126. this.manipulationDiv.appendChild(this.manipulationDOM['editEdgeSpan']);
  127. }
  128. if (this._selectionIsEmpty() == false) {
  129. this.manipulationDOM['seperatorLineDiv4'] = document.createElement('div');
  130. this.manipulationDOM['seperatorLineDiv4'].className = 'network-seperatorLine';
  131. this.manipulationDOM['deleteSpan'] = document.createElement('span');
  132. this.manipulationDOM['deleteSpan'].className = 'network-manipulationUI delete';
  133. this.manipulationDOM['deleteLabelSpan'] = document.createElement('span');
  134. this.manipulationDOM['deleteLabelSpan'].className = 'network-manipulationLabel';
  135. this.manipulationDOM['deleteLabelSpan'].innerHTML = locale['del'];
  136. this.manipulationDOM['deleteSpan'].appendChild(this.manipulationDOM['deleteLabelSpan']);
  137. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv4']);
  138. this.manipulationDiv.appendChild(this.manipulationDOM['deleteSpan']);
  139. }
  140. // bind the icons
  141. this.manipulationDOM['addNodeSpan'].onclick = this._createAddNodeToolbar.bind(this);
  142. this.manipulationDOM['addEdgeSpan'].onclick = this._createAddEdgeToolbar.bind(this);
  143. if (this._getSelectedNodeCount() == 1 && this.triggerFunctions.edit) {
  144. this.manipulationDOM['editNodeSpan'].onclick = this._editNode.bind(this);
  145. }
  146. else if (this._getSelectedEdgeCount() == 1 && this._getSelectedNodeCount() == 0) {
  147. this.manipulationDOM['editEdgeSpan'].onclick = this._createEditEdgeToolbar.bind(this);
  148. }
  149. if (this._selectionIsEmpty() == false) {
  150. this.manipulationDOM['deleteSpan'].onclick = this._deleteSelected.bind(this);
  151. }
  152. this.closeDiv.onclick = this._toggleEditMode.bind(this);
  153. var me = this;
  154. this.boundFunction = me._createManipulatorBar;
  155. this.on('select', this.boundFunction);
  156. }
  157. else {
  158. while (this.editModeDiv.hasChildNodes()) {
  159. this.editModeDiv.removeChild(this.editModeDiv.firstChild);
  160. }
  161. this.manipulationDOM['editModeSpan'] = document.createElement('span');
  162. this.manipulationDOM['editModeSpan'].className = 'network-manipulationUI edit editmode';
  163. this.manipulationDOM['editModeLabelSpan'] = document.createElement('span');
  164. this.manipulationDOM['editModeLabelSpan'].className = 'network-manipulationLabel';
  165. this.manipulationDOM['editModeLabelSpan'].innerHTML = locale['edit'];
  166. this.manipulationDOM['editModeSpan'].appendChild(this.manipulationDOM['editModeLabelSpan']);
  167. this.editModeDiv.appendChild(this.manipulationDOM['editModeSpan']);
  168. this.manipulationDOM['editModeSpan'].onclick = this._toggleEditMode.bind(this);
  169. }
  170. };
  171. /**
  172. * Create the toolbar for adding Nodes
  173. *
  174. * @private
  175. */
  176. exports._createAddNodeToolbar = function() {
  177. // clear the toolbar
  178. this._clearManipulatorBar();
  179. if (this.boundFunction) {
  180. this.off('select', this.boundFunction);
  181. }
  182. var locale = this.constants.locales[this.constants.locale];
  183. this.manipulationDOM = {};
  184. this.manipulationDOM['backSpan'] = document.createElement('span');
  185. this.manipulationDOM['backSpan'].className = 'network-manipulationUI back';
  186. this.manipulationDOM['backLabelSpan'] = document.createElement('span');
  187. this.manipulationDOM['backLabelSpan'].className = 'network-manipulationLabel';
  188. this.manipulationDOM['backLabelSpan'].innerHTML = locale['back'];
  189. this.manipulationDOM['backSpan'].appendChild(this.manipulationDOM['backLabelSpan']);
  190. this.manipulationDOM['seperatorLineDiv1'] = document.createElement('div');
  191. this.manipulationDOM['seperatorLineDiv1'].className = 'network-seperatorLine';
  192. this.manipulationDOM['descriptionSpan'] = document.createElement('span');
  193. this.manipulationDOM['descriptionSpan'].className = 'network-manipulationUI none';
  194. this.manipulationDOM['descriptionLabelSpan'] = document.createElement('span');
  195. this.manipulationDOM['descriptionLabelSpan'].className = 'network-manipulationLabel';
  196. this.manipulationDOM['descriptionLabelSpan'].innerHTML = locale['addDescription'];
  197. this.manipulationDOM['descriptionSpan'].appendChild(this.manipulationDOM['descriptionLabelSpan']);
  198. this.manipulationDiv.appendChild(this.manipulationDOM['backSpan']);
  199. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv1']);
  200. this.manipulationDiv.appendChild(this.manipulationDOM['descriptionSpan']);
  201. // bind the icon
  202. this.manipulationDOM['backSpan'].onclick = this._createManipulatorBar.bind(this);
  203. // we use the boundFunction so we can reference it when we unbind it from the "select" event.
  204. var me = this;
  205. this.boundFunction = me._addNode;
  206. this.on('select', this.boundFunction);
  207. };
  208. /**
  209. * create the toolbar to connect nodes
  210. *
  211. * @private
  212. */
  213. exports._createAddEdgeToolbar = function() {
  214. // clear the toolbar
  215. this._clearManipulatorBar();
  216. this._unselectAll(true);
  217. this.freezeSimulation = true;
  218. if (this.boundFunction) {
  219. this.off('select', this.boundFunction);
  220. }
  221. var locale = this.constants.locales[this.constants.locale];
  222. this._unselectAll();
  223. this.forceAppendSelection = false;
  224. this.blockConnectingEdgeSelection = true;
  225. this.manipulationDOM = {};
  226. this.manipulationDOM['backSpan'] = document.createElement('span');
  227. this.manipulationDOM['backSpan'].className = 'network-manipulationUI back';
  228. this.manipulationDOM['backLabelSpan'] = document.createElement('span');
  229. this.manipulationDOM['backLabelSpan'].className = 'network-manipulationLabel';
  230. this.manipulationDOM['backLabelSpan'].innerHTML = locale['back'];
  231. this.manipulationDOM['backSpan'].appendChild(this.manipulationDOM['backLabelSpan']);
  232. this.manipulationDOM['seperatorLineDiv1'] = document.createElement('div');
  233. this.manipulationDOM['seperatorLineDiv1'].className = 'network-seperatorLine';
  234. this.manipulationDOM['descriptionSpan'] = document.createElement('span');
  235. this.manipulationDOM['descriptionSpan'].className = 'network-manipulationUI none';
  236. this.manipulationDOM['descriptionLabelSpan'] = document.createElement('span');
  237. this.manipulationDOM['descriptionLabelSpan'].className = 'network-manipulationLabel';
  238. this.manipulationDOM['descriptionLabelSpan'].innerHTML = locale['edgeDescription'];
  239. this.manipulationDOM['descriptionSpan'].appendChild(this.manipulationDOM['descriptionLabelSpan']);
  240. this.manipulationDiv.appendChild(this.manipulationDOM['backSpan']);
  241. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv1']);
  242. this.manipulationDiv.appendChild(this.manipulationDOM['descriptionSpan']);
  243. // bind the icon
  244. this.manipulationDOM['backSpan'].onclick = this._createManipulatorBar.bind(this);
  245. // we use the boundFunction so we can reference it when we unbind it from the "select" event.
  246. var me = this;
  247. this.boundFunction = me._handleConnect;
  248. this.on('select', this.boundFunction);
  249. // temporarily overload functions
  250. this.cachedFunctions["_handleTouch"] = this._handleTouch;
  251. this.cachedFunctions["_manipulationReleaseOverload"] = this._manipulationReleaseOverload;
  252. this.cachedFunctions["_handleDragStart"] = this._handleDragStart;
  253. this.cachedFunctions["_handleDragEnd"] = this._handleDragEnd;
  254. this._handleTouch = this._handleConnect;
  255. this._manipulationReleaseOverload = function () {};
  256. this._handleDragStart = function () {};
  257. this._handleDragEnd = this._finishConnect;
  258. // redraw to show the unselect
  259. this._redraw();
  260. };
  261. /**
  262. * create the toolbar to edit edges
  263. *
  264. * @private
  265. */
  266. exports._createEditEdgeToolbar = function() {
  267. // clear the toolbar
  268. this._clearManipulatorBar();
  269. this.controlNodesActive = true;
  270. if (this.boundFunction) {
  271. this.off('select', this.boundFunction);
  272. }
  273. this.edgeBeingEdited = this._getSelectedEdge();
  274. this.edgeBeingEdited._enableControlNodes();
  275. var locale = this.constants.locales[this.constants.locale];
  276. this.manipulationDOM = {};
  277. this.manipulationDOM['backSpan'] = document.createElement('span');
  278. this.manipulationDOM['backSpan'].className = 'network-manipulationUI back';
  279. this.manipulationDOM['backLabelSpan'] = document.createElement('span');
  280. this.manipulationDOM['backLabelSpan'].className = 'network-manipulationLabel';
  281. this.manipulationDOM['backLabelSpan'].innerHTML = locale['back'];
  282. this.manipulationDOM['backSpan'].appendChild(this.manipulationDOM['backLabelSpan']);
  283. this.manipulationDOM['seperatorLineDiv1'] = document.createElement('div');
  284. this.manipulationDOM['seperatorLineDiv1'].className = 'network-seperatorLine';
  285. this.manipulationDOM['descriptionSpan'] = document.createElement('span');
  286. this.manipulationDOM['descriptionSpan'].className = 'network-manipulationUI none';
  287. this.manipulationDOM['descriptionLabelSpan'] = document.createElement('span');
  288. this.manipulationDOM['descriptionLabelSpan'].className = 'network-manipulationLabel';
  289. this.manipulationDOM['descriptionLabelSpan'].innerHTML = locale['editEdgeDescription'];
  290. this.manipulationDOM['descriptionSpan'].appendChild(this.manipulationDOM['descriptionLabelSpan']);
  291. this.manipulationDiv.appendChild(this.manipulationDOM['backSpan']);
  292. this.manipulationDiv.appendChild(this.manipulationDOM['seperatorLineDiv1']);
  293. this.manipulationDiv.appendChild(this.manipulationDOM['descriptionSpan']);
  294. // bind the icon
  295. this.manipulationDOM['backSpan'].onclick = this._createManipulatorBar.bind(this);
  296. // temporarily overload functions
  297. this.cachedFunctions["_handleTouch"] = this._handleTouch;
  298. this.cachedFunctions["_manipulationReleaseOverload"] = this._manipulationReleaseOverload;
  299. this.cachedFunctions["_handleTap"] = this._handleTap;
  300. this.cachedFunctions["_handleDragStart"] = this._handleDragStart;
  301. this.cachedFunctions["_handleOnDrag"] = this._handleOnDrag;
  302. this._handleTouch = this._selectControlNode;
  303. this._handleTap = function () {};
  304. this._handleOnDrag = this._controlNodeDrag;
  305. this._handleDragStart = function () {}
  306. this._manipulationReleaseOverload = this._releaseControlNode;
  307. // redraw to show the unselect
  308. this._redraw();
  309. };
  310. /**
  311. * the function bound to the selection event. It checks if you want to connect a cluster and changes the description
  312. * to walk the user through the process.
  313. *
  314. * @private
  315. */
  316. exports._selectControlNode = function(pointer) {
  317. this.edgeBeingEdited.controlNodes.from.unselect();
  318. this.edgeBeingEdited.controlNodes.to.unselect();
  319. this.selectedControlNode = this.edgeBeingEdited._getSelectedControlNode(this._XconvertDOMtoCanvas(pointer.x),this._YconvertDOMtoCanvas(pointer.y));
  320. if (this.selectedControlNode !== null) {
  321. this.selectedControlNode.select();
  322. this.freezeSimulation = true;
  323. }
  324. this._redraw();
  325. };
  326. /**
  327. * the function bound to the selection event. It checks if you want to connect a cluster and changes the description
  328. * to walk the user through the process.
  329. *
  330. * @private
  331. */
  332. exports._controlNodeDrag = function(event) {
  333. var pointer = this._getPointer(event.gesture.center);
  334. if (this.selectedControlNode !== null && this.selectedControlNode !== undefined) {
  335. this.selectedControlNode.x = this._XconvertDOMtoCanvas(pointer.x);
  336. this.selectedControlNode.y = this._YconvertDOMtoCanvas(pointer.y);
  337. }
  338. this._redraw();
  339. };
  340. /**
  341. *
  342. * @param pointer
  343. * @private
  344. */
  345. exports._releaseControlNode = function(pointer) {
  346. var newNode = this._getNodeAt(pointer);
  347. if (newNode !== null) {
  348. if (this.edgeBeingEdited.controlNodes.from.selected == true) {
  349. this.edgeBeingEdited._restoreControlNodes();
  350. this._editEdge(newNode.id, this.edgeBeingEdited.to.id);
  351. this.edgeBeingEdited.controlNodes.from.unselect();
  352. }
  353. if (this.edgeBeingEdited.controlNodes.to.selected == true) {
  354. this.edgeBeingEdited._restoreControlNodes();
  355. this._editEdge(this.edgeBeingEdited.from.id, newNode.id);
  356. this.edgeBeingEdited.controlNodes.to.unselect();
  357. }
  358. }
  359. else {
  360. this.edgeBeingEdited._restoreControlNodes();
  361. }
  362. this.freezeSimulation = false;
  363. this._redraw();
  364. };
  365. /**
  366. * the function bound to the selection event. It checks if you want to connect a cluster and changes the description
  367. * to walk the user through the process.
  368. *
  369. * @private
  370. */
  371. exports._handleConnect = function(pointer) {
  372. if (this._getSelectedNodeCount() == 0) {
  373. var node = this._getNodeAt(pointer);
  374. if (node != null) {
  375. if (node.clusterSize > 1) {
  376. alert(this.constants.locales[this.constants.locale]['createEdgeError'])
  377. }
  378. else {
  379. this._selectObject(node,false);
  380. var supportNodes = this.sectors['support']['nodes'];
  381. // create a node the temporary line can look at
  382. supportNodes['targetNode'] = new Node({id:'targetNode'},{},{},this.constants);
  383. var targetNode = supportNodes['targetNode'];
  384. targetNode.x = node.x;
  385. targetNode.y = node.y;
  386. // create a temporary edge
  387. this.edges['connectionEdge'] = new Edge({id:"connectionEdge",from:node.id,to:targetNode.id}, this, this.constants);
  388. var connectionEdge = this.edges['connectionEdge'];
  389. connectionEdge.from = node;
  390. connectionEdge.connected = true;
  391. connectionEdge.options.smoothCurves = {enabled: true,
  392. dynamic: false,
  393. type: "continuous",
  394. roundness: 0.5
  395. };
  396. connectionEdge.selected = true;
  397. connectionEdge.to = targetNode;
  398. this.cachedFunctions["_handleOnDrag"] = this._handleOnDrag;
  399. this._handleOnDrag = function(event) {
  400. var pointer = this._getPointer(event.gesture.center);
  401. var connectionEdge = this.edges['connectionEdge'];
  402. connectionEdge.to.x = this._XconvertDOMtoCanvas(pointer.x);
  403. connectionEdge.to.y = this._YconvertDOMtoCanvas(pointer.y);
  404. };
  405. this.moving = true;
  406. this.start();
  407. }
  408. }
  409. }
  410. };
  411. exports._finishConnect = function(event) {
  412. if (this._getSelectedNodeCount() == 1) {
  413. var pointer = this._getPointer(event.gesture.center);
  414. // restore the drag function
  415. this._handleOnDrag = this.cachedFunctions["_handleOnDrag"];
  416. delete this.cachedFunctions["_handleOnDrag"];
  417. // remember the edge id
  418. var connectFromId = this.edges['connectionEdge'].fromId;
  419. // remove the temporary nodes and edge
  420. delete this.edges['connectionEdge'];
  421. delete this.sectors['support']['nodes']['targetNode'];
  422. delete this.sectors['support']['nodes']['targetViaNode'];
  423. var node = this._getNodeAt(pointer);
  424. if (node != null) {
  425. if (node.clusterSize > 1) {
  426. alert(this.constants.locales[this.constants.locale]["createEdgeError"])
  427. }
  428. else {
  429. this._createEdge(connectFromId,node.id);
  430. this._createManipulatorBar();
  431. }
  432. }
  433. this._unselectAll();
  434. }
  435. };
  436. /**
  437. * Adds a node on the specified location
  438. */
  439. exports._addNode = function() {
  440. if (this._selectionIsEmpty() && this.editMode == true) {
  441. var positionObject = this._pointerToPositionObject(this.pointerPosition);
  442. var defaultData = {id:util.randomUUID(),x:positionObject.left,y:positionObject.top,label:"new",allowedToMoveX:true,allowedToMoveY:true};
  443. if (this.triggerFunctions.add) {
  444. if (this.triggerFunctions.add.length == 2) {
  445. var me = this;
  446. this.triggerFunctions.add(defaultData, function(finalizedData) {
  447. me.nodesData.add(finalizedData);
  448. me._createManipulatorBar();
  449. me.moving = true;
  450. me.start();
  451. });
  452. }
  453. else {
  454. throw new Error('The function for add does not support two arguments (data,callback)');
  455. this._createManipulatorBar();
  456. this.moving = true;
  457. this.start();
  458. }
  459. }
  460. else {
  461. this.nodesData.add(defaultData);
  462. this._createManipulatorBar();
  463. this.moving = true;
  464. this.start();
  465. }
  466. }
  467. };
  468. /**
  469. * connect two nodes with a new edge.
  470. *
  471. * @private
  472. */
  473. exports._createEdge = function(sourceNodeId,targetNodeId) {
  474. if (this.editMode == true) {
  475. var defaultData = {from:sourceNodeId, to:targetNodeId};
  476. if (this.triggerFunctions.connect) {
  477. if (this.triggerFunctions.connect.length == 2) {
  478. var me = this;
  479. this.triggerFunctions.connect(defaultData, function(finalizedData) {
  480. me.edgesData.add(finalizedData);
  481. me.moving = true;
  482. me.start();
  483. });
  484. }
  485. else {
  486. throw new Error('The function for connect does not support two arguments (data,callback)');
  487. this.moving = true;
  488. this.start();
  489. }
  490. }
  491. else {
  492. this.edgesData.add(defaultData);
  493. this.moving = true;
  494. this.start();
  495. }
  496. }
  497. };
  498. /**
  499. * connect two nodes with a new edge.
  500. *
  501. * @private
  502. */
  503. exports._editEdge = function(sourceNodeId,targetNodeId) {
  504. if (this.editMode == true) {
  505. var defaultData = {id: this.edgeBeingEdited.id, from:sourceNodeId, to:targetNodeId};
  506. if (this.triggerFunctions.editEdge) {
  507. if (this.triggerFunctions.editEdge.length == 2) {
  508. var me = this;
  509. this.triggerFunctions.editEdge(defaultData, function(finalizedData) {
  510. me.edgesData.update(finalizedData);
  511. me.moving = true;
  512. me.start();
  513. });
  514. }
  515. else {
  516. throw new Error('The function for edit does not support two arguments (data, callback)');
  517. this.moving = true;
  518. this.start();
  519. }
  520. }
  521. else {
  522. this.edgesData.update(defaultData);
  523. this.moving = true;
  524. this.start();
  525. }
  526. }
  527. };
  528. /**
  529. * Create the toolbar to edit the selected node. The label and the color can be changed. Other colors are derived from the chosen color.
  530. *
  531. * @private
  532. */
  533. exports._editNode = function() {
  534. if (this.triggerFunctions.edit && this.editMode == true) {
  535. var node = this._getSelectedNode();
  536. var data = {id:node.id,
  537. label: node.label,
  538. group: node.options.group,
  539. shape: node.options.shape,
  540. color: {
  541. background:node.options.color.background,
  542. border:node.options.color.border,
  543. highlight: {
  544. background:node.options.color.highlight.background,
  545. border:node.options.color.highlight.border
  546. }
  547. }};
  548. if (this.triggerFunctions.edit.length == 2) {
  549. var me = this;
  550. this.triggerFunctions.edit(data, function (finalizedData) {
  551. me.nodesData.update(finalizedData);
  552. me._createManipulatorBar();
  553. me.moving = true;
  554. me.start();
  555. });
  556. }
  557. else {
  558. throw new Error('The function for edit does not support two arguments (data, callback)');
  559. }
  560. }
  561. else {
  562. throw new Error('No edit function has been bound to this button');
  563. }
  564. };
  565. /**
  566. * delete everything in the selection
  567. *
  568. * @private
  569. */
  570. exports._deleteSelected = function() {
  571. if (!this._selectionIsEmpty() && this.editMode == true) {
  572. if (!this._clusterInSelection()) {
  573. var selectedNodes = this.getSelectedNodes();
  574. var selectedEdges = this.getSelectedEdges();
  575. if (this.triggerFunctions.del) {
  576. var me = this;
  577. var data = {nodes: selectedNodes, edges: selectedEdges};
  578. if (this.triggerFunctions.del.length == 2) {
  579. this.triggerFunctions.del(data, function (finalizedData) {
  580. me.edgesData.remove(finalizedData.edges);
  581. me.nodesData.remove(finalizedData.nodes);
  582. me._unselectAll();
  583. me.moving = true;
  584. me.start();
  585. });
  586. }
  587. else {
  588. throw new Error('The function for delete does not support two arguments (data, callback)')
  589. }
  590. }
  591. else {
  592. this.edgesData.remove(selectedEdges);
  593. this.nodesData.remove(selectedNodes);
  594. this._unselectAll();
  595. this.moving = true;
  596. this.start();
  597. }
  598. }
  599. else {
  600. alert(this.constants.locales[this.constants.locale]["deleteClusterError"]);
  601. }
  602. }
  603. };