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.

654 lines
18 KiB

10 years ago
  1. var util = require('../../../util');
  2. import Label from './unified/label.js'
  3. import BezierEdgeDynamic from './edges/bezierEdgeDynamic'
  4. import BezierEdgeStatic from './edges/bezierEdgeStatic'
  5. import StraightEdge from './edges/straightEdge'
  6. /**
  7. * @class Edge
  8. *
  9. * A edge connects two nodes
  10. * @param {Object} properties Object with options. Must contain
  11. * At least options from and to.
  12. * Available options: from (number),
  13. * to (number), label (string, color (string),
  14. * width (number), style (string),
  15. * length (number), title (string)
  16. * @param {Network} network A Network object, used to find and edge to
  17. * nodes.
  18. * @param {Object} constants An object with default values for
  19. * example for the color
  20. */
  21. class Edge {
  22. constructor(options, body, globalOptions) {
  23. if (body === undefined) {
  24. throw "No body provided";
  25. }
  26. this.options = util.bridgeObject(globalOptions);
  27. this.body = body;
  28. // initialize variables
  29. this.id = undefined;
  30. this.fromId = undefined;
  31. this.toId = undefined;
  32. this.title = undefined;
  33. this.value = undefined;
  34. this.selected = false;
  35. this.hover = false;
  36. this.labelDirty = true;
  37. this.colorDirty = true;
  38. this.from = undefined; // a node
  39. this.to = undefined; // a node
  40. this.edgeType = undefined;
  41. this.connected = false;
  42. this.labelModule = new Label(this.body, this.options);
  43. this.setOptions(options);
  44. this.controlNodesEnabled = false;
  45. this.controlNodes = {from: undefined, to: undefined, positions: {}};
  46. this.connectedNode = undefined;
  47. }
  48. /**
  49. * Set or overwrite options for the edge
  50. * @param {Object} options an object with options
  51. * @param doNotEmit
  52. */
  53. setOptions(options) {
  54. if (!options) {
  55. return;
  56. }
  57. this.colorDirty = true;
  58. var fields = [
  59. 'id',
  60. 'font',
  61. 'from',
  62. 'hidden',
  63. 'hoverWidth',
  64. 'label',
  65. 'length',
  66. 'line',
  67. 'opacity',
  68. 'physics',
  69. 'scaling',
  70. 'selfReferenceSize',
  71. 'to',
  72. 'value',
  73. 'width',
  74. 'widthMin',
  75. 'widthMax',
  76. 'widthSelectionMultiplier'
  77. ];
  78. util.selectiveDeepExtend(fields, this.options, options);
  79. util.mergeOptions(this.options, options, 'smooth');
  80. util.mergeOptions(this.options, options, 'dashes');
  81. if (options.arrows !== undefined) {
  82. if (typeof options.arrows === 'string') {
  83. let arrows = options.arrows.toLowerCase();
  84. if (arrows.indexOf("to") != -1) {this.options.arrows.to.enabled = true;}
  85. if (arrows.indexOf("middle") != -1) {this.options.arrows.middle.enabled = true;}
  86. if (arrows.indexOf("from") != -1) {this.options.arrows.from.enabled = true;}
  87. }
  88. else if (typeof options.arrows === 'object') {
  89. util.mergeOptions(this.options.arrows, options.arrows, 'to');
  90. util.mergeOptions(this.options.arrows, options.arrows, 'middle');
  91. util.mergeOptions(this.options.arrows, options.arrows, 'from');
  92. }
  93. else {
  94. throw new Error("The arrow options can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(options.arrows));
  95. }
  96. }
  97. if (options.id !== undefined) {this.id = options.id;}
  98. if (options.from !== undefined) {this.fromId = options.from;}
  99. if (options.to !== undefined) {this.toId = options.to;}
  100. if (options.title !== undefined) {this.title = options.title;}
  101. if (options.value !== undefined) {this.value = options.value;}
  102. if (options.color !== undefined) {
  103. if (util.isString(options.color)) {
  104. this.options.color.color = options.color;
  105. this.options.color.highlight = options.color;
  106. }
  107. else {
  108. if (options.color.color !== undefined) {
  109. this.options.color.color = options.color.color;
  110. }
  111. if (options.color.highlight !== undefined) {
  112. this.options.color.highlight = options.color.highlight;
  113. }
  114. if (options.color.hover !== undefined) {
  115. this.options.color.hover = options.color.hover;
  116. }
  117. }
  118. // inherit colors
  119. if (options.color.inherit === undefined) {
  120. this.options.color.inherit.enabled = false;
  121. }
  122. else {
  123. util.mergeOptions(this.options.color, options.color, 'inherit');
  124. }
  125. }
  126. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  127. this.connect();
  128. this.labelModule.setOptions(this.options);
  129. this.updateEdgeType();
  130. return this.edgeType.setOptions(this.options);
  131. }
  132. updateEdgeType() {
  133. let dataChanged = false;
  134. let changeInType = true;
  135. if (this.edgeType !== undefined) {
  136. if (this.edgeType instanceof BezierEdgeDynamic && this.options.smooth.enabled == true && this.options.smooth.dynamic == true) {changeInType = false;}
  137. if (this.edgeType instanceof BezierEdgeStatic && this.options.smooth.enabled == true && this.options.smooth.dynamic == false){changeInType = false;}
  138. if (this.edgeType instanceof StraightEdge && this.options.smooth.enabled == false) {changeInType = false;}
  139. if (changeInType == true) {
  140. dataChanged = this.edgeType.cleanup();
  141. }
  142. }
  143. if (changeInType === true) {
  144. if (this.options.smooth.enabled === true) {
  145. if (this.options.smooth.dynamic === true) {
  146. dataChanged = true;
  147. this.edgeType = new BezierEdgeDynamic(this.options, this.body, this.labelModule);
  148. }
  149. else {
  150. this.edgeType = new BezierEdgeStatic(this.options, this.body, this.labelModule);
  151. }
  152. }
  153. else {
  154. this.edgeType = new StraightEdge(this.options, this.body, this.labelModule);
  155. }
  156. }
  157. return dataChanged;
  158. }
  159. /**
  160. * Enable or disable the physics.
  161. * @param status
  162. */
  163. togglePhysics(status) {
  164. if (this.options.smooth.enabled == true && this.options.smooth.dynamic == true) {
  165. if (this.via === undefined) {
  166. this.via.pptions.physics = status;
  167. }
  168. }
  169. this.options.physics = status;
  170. }
  171. /**
  172. * Connect an edge to its nodes
  173. */
  174. connect() {
  175. this.disconnect();
  176. this.from = this.body.nodes[this.fromId] || undefined;
  177. this.to = this.body.nodes[this.toId] || undefined;
  178. this.connected = (this.from !== undefined && this.to !== undefined);
  179. if (this.connected === true) {
  180. this.from.attachEdge(this);
  181. this.to.attachEdge(this);
  182. }
  183. else {
  184. if (this.from) {
  185. this.from.detachEdge(this);
  186. }
  187. if (this.to) {
  188. this.to.detachEdge(this);
  189. }
  190. }
  191. }
  192. /**
  193. * Disconnect an edge from its nodes
  194. */
  195. disconnect() {
  196. if (this.from) {
  197. this.from.detachEdge(this);
  198. this.from = undefined;
  199. }
  200. if (this.to) {
  201. this.to.detachEdge(this);
  202. this.to = undefined;
  203. }
  204. this.connected = false;
  205. }
  206. /**
  207. * get the title of this edge.
  208. * @return {string} title The title of the edge, or undefined when no title
  209. * has been set.
  210. */
  211. getTitle() {
  212. return typeof this.title === "function" ? this.title() : this.title;
  213. }
  214. /**
  215. * check if this node is selecte
  216. * @return {boolean} selected True if node is selected, else false
  217. */
  218. isSelected() {
  219. return this.selected;
  220. }
  221. /**
  222. * Retrieve the value of the edge. Can be undefined
  223. * @return {Number} value
  224. */
  225. getValue() {
  226. return this.value;
  227. }
  228. /**
  229. * Adjust the value range of the edge. The edge will adjust it's width
  230. * based on its value.
  231. * @param {Number} min
  232. * @param {Number} max
  233. * @param total
  234. */
  235. setValueRange(min, max, total) {
  236. if (this.value !== undefined) {
  237. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  238. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  239. if (this.options.scaling.label.enabled == true) {
  240. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  241. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  242. }
  243. this.options.width = this.options.scaling.min + scale * widthDiff;
  244. }
  245. }
  246. /**
  247. * Redraw a edge
  248. * Draw this edge in the given canvas
  249. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  250. * @param {CanvasRenderingContext2D} ctx
  251. */
  252. draw(ctx) {
  253. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  254. this.drawArrows(ctx, via);
  255. this.drawLabel (ctx, via);
  256. }
  257. drawArrows(ctx, viaNode) {
  258. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode);}
  259. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode);}
  260. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode);}
  261. }
  262. drawLabel(ctx, viaNode) {
  263. if (this.options.label !== undefined) {
  264. // set style
  265. var node1 = this.from;
  266. var node2 = this.to;
  267. var selected = (this.from.selected || this.to.selected || this.selected);
  268. if (node1.id != node2.id) {
  269. var point = this.edgeType.getPoint(0.5, viaNode);
  270. ctx.save();
  271. // if the label has to be rotated:
  272. if (this.options.font.align !== "horizontal") {
  273. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  274. ctx.translate(point.x, this.labelModule.size.yLine);
  275. this._rotateForLabelAlignment(ctx);
  276. }
  277. // draw the label
  278. this.labelModule.draw(ctx, point.x, point.y, selected);
  279. ctx.restore();
  280. }
  281. else {
  282. var x, y;
  283. var radius = this.options.selfReferenceSize;
  284. if (node1.width > node1.height) {
  285. x = node1.x + node1.width * 0.5;
  286. y = node1.y - radius;
  287. }
  288. else {
  289. x = node1.x + radius;
  290. y = node1.y - node1.height * 0.5;
  291. }
  292. point = this._pointOnCircle(x, y, radius, 0.125);
  293. this.labelModule.draw(ctx, point.x, point.y, selected);
  294. }
  295. }
  296. }
  297. /**
  298. * Check if this object is overlapping with the provided object
  299. * @param {Object} obj an object with parameters left, top
  300. * @return {boolean} True if location is located on the edge
  301. */
  302. isOverlappingWith(obj) {
  303. if (this.connected) {
  304. var distMax = 10;
  305. var xFrom = this.from.x;
  306. var yFrom = this.from.y;
  307. var xTo = this.to.x;
  308. var yTo = this.to.y;
  309. var xObj = obj.left;
  310. var yObj = obj.top;
  311. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  312. return (dist < distMax);
  313. }
  314. else {
  315. return false
  316. }
  317. }
  318. /**
  319. * Rotates the canvas so the text is most readable
  320. * @param {CanvasRenderingContext2D} ctx
  321. * @private
  322. */
  323. _rotateForLabelAlignment(ctx) {
  324. var dy = this.from.y - this.to.y;
  325. var dx = this.from.x - this.to.x;
  326. var angleInDegrees = Math.atan2(dy, dx);
  327. // rotate so label it is readable
  328. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  329. angleInDegrees = angleInDegrees + Math.PI;
  330. }
  331. ctx.rotate(angleInDegrees);
  332. }
  333. /**
  334. * Get a point on a circle
  335. * @param {Number} x
  336. * @param {Number} y
  337. * @param {Number} radius
  338. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  339. * @return {Object} point
  340. * @private
  341. */
  342. _pointOnCircle(x, y, radius, percentage) {
  343. var angle = percentage * 2 * Math.PI;
  344. return {
  345. x: x + radius * Math.cos(angle),
  346. y: y - radius * Math.sin(angle)
  347. }
  348. }
  349. select() {
  350. this.selected = true;
  351. }
  352. unselect() {
  353. this.selected = false;
  354. }
  355. //*************************************************************************************************//
  356. //*************************************************************************************************//
  357. //*************************************************************************************************//
  358. //*************************************************************************************************//
  359. //*********************** MOVE THESE FUNCTIONS TO THE MANIPULATION SYSTEM ************************//
  360. //*************************************************************************************************//
  361. //*************************************************************************************************//
  362. //*************************************************************************************************//
  363. //*************************************************************************************************//
  364. /**
  365. * This function draws the control nodes for the manipulator.
  366. * In order to enable this, only set the this.controlNodesEnabled to true.
  367. * @param ctx
  368. */
  369. _drawControlNodes(ctx) {
  370. if (this.controlNodesEnabled == true) {
  371. if (this.controlNodes.from === undefined && this.controlNodes.to === undefined) {
  372. var nodeIdFrom = "edgeIdFrom:".concat(this.id);
  373. var nodeIdTo = "edgeIdTo:".concat(this.id);
  374. var nodeFromOptions = {
  375. id: nodeIdFrom,
  376. shape: 'dot',
  377. color: {background: '#ff0000', border: '#3c3c3c', highlight: {background: '#07f968'}},
  378. radius: 7,
  379. borderWidth: 2,
  380. borderWidthSelected: 2,
  381. hidden: false,
  382. physics: false
  383. };
  384. var nodeToOptions = util.deepExtend({},nodeFromOptions);
  385. nodeToOptions.id = nodeIdTo;
  386. this.controlNodes.from = this.body.functions.createNode(nodeFromOptions);
  387. this.controlNodes.to = this.body.functions.createNode(nodeToOptions);
  388. }
  389. this.controlNodes.positions = {};
  390. if (this.controlNodes.from.selected == false) {
  391. this.controlNodes.positions.from = this.getControlNodeFromPosition(ctx);
  392. this.controlNodes.from.x = this.controlNodes.positions.from.x;
  393. this.controlNodes.from.y = this.controlNodes.positions.from.y;
  394. }
  395. if (this.controlNodes.to.selected == false) {
  396. this.controlNodes.positions.to = this.getControlNodeToPosition(ctx);
  397. this.controlNodes.to.x = this.controlNodes.positions.to.x;
  398. this.controlNodes.to.y = this.controlNodes.positions.to.y;
  399. }
  400. this.controlNodes.from.draw(ctx);
  401. this.controlNodes.to.draw(ctx);
  402. }
  403. else {
  404. this.controlNodes = {from: undefined, to: undefined, positions: {}};
  405. }
  406. }
  407. /**
  408. * Enable control nodes.
  409. * @private
  410. */
  411. _enableControlNodes() {
  412. this.fromBackup = this.from;
  413. this.toBackup = this.to;
  414. this.controlNodesEnabled = true;
  415. }
  416. /**
  417. * disable control nodes and remove from dynamicEdges from old node
  418. * @private
  419. */
  420. _disableControlNodes() {
  421. this.fromId = this.from.id;
  422. this.toId = this.to.id;
  423. if (this.fromId != this.fromBackup.id) { // from was changed, remove edge from old 'from' node dynamic edges
  424. this.fromBackup.detachEdge(this);
  425. }
  426. else if (this.toId != this.toBackup.id) { // to was changed, remove edge from old 'to' node dynamic edges
  427. this.toBackup.detachEdge(this);
  428. }
  429. this.fromBackup = undefined;
  430. this.toBackup = undefined;
  431. this.controlNodesEnabled = false;
  432. }
  433. /**
  434. * This checks if one of the control nodes is selected and if so, returns the control node object. Else it returns undefined.
  435. * @param x
  436. * @param y
  437. * @returns {undefined}
  438. * @private
  439. */
  440. _getSelectedControlNode(x, y) {
  441. var positions = this.controlNodes.positions;
  442. var fromDistance = Math.sqrt(Math.pow(x - positions.from.x, 2) + Math.pow(y - positions.from.y, 2));
  443. var toDistance = Math.sqrt(Math.pow(x - positions.to.x, 2) + Math.pow(y - positions.to.y, 2));
  444. if (fromDistance < 15) {
  445. this.connectedNode = this.from;
  446. this.from = this.controlNodes.from;
  447. return this.controlNodes.from;
  448. }
  449. else if (toDistance < 15) {
  450. this.connectedNode = this.to;
  451. this.to = this.controlNodes.to;
  452. return this.controlNodes.to;
  453. }
  454. else {
  455. return undefined;
  456. }
  457. }
  458. /**
  459. * this resets the control nodes to their original position.
  460. * @private
  461. */
  462. _restoreControlNodes() {
  463. if (this.controlNodes.from.selected == true) {
  464. this.from = this.connectedNode;
  465. this.connectedNode = undefined;
  466. this.controlNodes.from.unselect();
  467. }
  468. else if (this.controlNodes.to.selected == true) {
  469. this.to = this.connectedNode;
  470. this.connectedNode = undefined;
  471. this.controlNodes.to.unselect();
  472. }
  473. }
  474. /**
  475. * this calculates the position of the control nodes on the edges of the parent nodes.
  476. *
  477. * @param ctx
  478. * @returns {x: *, y: *}
  479. */
  480. getControlNodeFromPosition(ctx) {
  481. // draw arrow head
  482. var controlnodeFromPos;
  483. if (this.options.smooth.enabled == true) {
  484. controlnodeFromPos = this._findBorderPositionBezier(true, ctx);
  485. }
  486. else {
  487. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  488. var dx = (this.to.x - this.from.x);
  489. var dy = (this.to.y - this.from.y);
  490. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  491. var fromBorderDist = this.from.distanceToBorder(ctx, angle + Math.PI);
  492. var fromBorderPoint = (edgeSegmentLength - fromBorderDist) / edgeSegmentLength;
  493. controlnodeFromPos = {};
  494. controlnodeFromPos.x = (fromBorderPoint) * this.from.x + (1 - fromBorderPoint) * this.to.x;
  495. controlnodeFromPos.y = (fromBorderPoint) * this.from.y + (1 - fromBorderPoint) * this.to.y;
  496. }
  497. return controlnodeFromPos;
  498. }
  499. /**
  500. * this calculates the position of the control nodes on the edges of the parent nodes.
  501. *
  502. * @param ctx
  503. * @returns {{from: {x: number, y: number}, to: {x: *, y: *}}}
  504. */
  505. getControlNodeToPosition(ctx) {
  506. // draw arrow head
  507. var controlnodeToPos;
  508. if (this.options.smooth.enabled == true) {
  509. controlnodeToPos = this._findBorderPositionBezier(false, ctx);
  510. }
  511. else {
  512. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  513. var dx = (this.to.x - this.from.x);
  514. var dy = (this.to.y - this.from.y);
  515. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  516. var toBorderDist = this.to.distanceToBorder(ctx, angle);
  517. var toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  518. controlnodeToPos = {};
  519. controlnodeToPos.x = (1 - toBorderPoint) * this.from.x + toBorderPoint * this.to.x;
  520. controlnodeToPos.y = (1 - toBorderPoint) * this.from.y + toBorderPoint * this.to.y;
  521. }
  522. return controlnodeToPos;
  523. }
  524. }
  525. export default Edge;