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.

413 lines
12 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.value = undefined;
  33. this.selected = false;
  34. this.hover = false;
  35. this.labelDirty = true;
  36. this.colorDirty = true;
  37. this.from = undefined; // a node
  38. this.to = undefined; // a node
  39. this.edgeType = undefined;
  40. this.connected = false;
  41. this.labelModule = new Label(this.body, this.options);
  42. this.setOptions(options);
  43. }
  44. /**
  45. * Set or overwrite options for the edge
  46. * @param {Object} options an object with options
  47. * @param doNotEmit
  48. */
  49. setOptions(options) {
  50. if (!options) {
  51. return;
  52. }
  53. this.colorDirty = true;
  54. var fields = [
  55. 'id',
  56. 'font',
  57. 'from',
  58. 'hidden',
  59. 'hoverWidth',
  60. 'label',
  61. 'length',
  62. 'line',
  63. 'opacity',
  64. 'physics',
  65. 'scaling',
  66. 'selfReferenceSize',
  67. 'to',
  68. 'title',
  69. 'value',
  70. 'width',
  71. 'widthMin',
  72. 'widthMax',
  73. 'widthSelectionMultiplier'
  74. ];
  75. util.selectiveDeepExtend(fields, this.options, options);
  76. util.mergeOptions(this.options, options, 'smooth');
  77. util.mergeOptions(this.options, options, 'dashes');
  78. if (options.id !== undefined) {this.id = options.id;}
  79. if (options.from !== undefined) {this.fromId = options.from;}
  80. if (options.to !== undefined) {this.toId = options.to;}
  81. if (options.title !== undefined) {this.title = options.title;}
  82. if (options.value !== undefined) {this.value = options.value;}
  83. // hanlde multiple input cases for arrows
  84. if (options.arrows !== undefined) {
  85. if (typeof options.arrows === 'string') {
  86. let arrows = options.arrows.toLowerCase();
  87. if (arrows.indexOf("to") != -1) {this.options.arrows.to.enabled = true;}
  88. if (arrows.indexOf("middle") != -1) {this.options.arrows.middle.enabled = true;}
  89. if (arrows.indexOf("from") != -1) {this.options.arrows.from.enabled = true;}
  90. }
  91. else if (typeof options.arrows === 'object') {
  92. util.mergeOptions(this.options.arrows, options.arrows, 'to');
  93. util.mergeOptions(this.options.arrows, options.arrows, 'middle');
  94. util.mergeOptions(this.options.arrows, options.arrows, 'from');
  95. }
  96. else {
  97. throw new Error("The arrow options can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(options.arrows));
  98. }
  99. }
  100. // hanlde multiple input cases for color
  101. if (options.color !== undefined) {
  102. if (util.isString(options.color)) {
  103. util.assignAllKeys(this.options.color, options.color);
  104. this.options.color.inherit.enabled = false;
  105. }
  106. else {
  107. this.options.color = util.bridgeObject(options.color);
  108. if (options.color.inherit === undefined) {
  109. this.options.color.inherit.enabled = false;
  110. }
  111. }
  112. util.mergeOptions(this.options.color, options.color, 'inherit');
  113. }
  114. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  115. this.connect();
  116. // update label Module
  117. this.updateLabelModule();
  118. let dataChanged = this.updateEdgeType();
  119. return dataChanged;
  120. }
  121. updateLabelModule() {
  122. this.labelModule.setOptions(this.options);
  123. }
  124. updateEdgeType() {
  125. let dataChanged = false;
  126. let changeInType = true;
  127. if (this.edgeType !== undefined) {
  128. if (this.edgeType instanceof BezierEdgeDynamic && this.options.smooth.enabled == true && this.options.smooth.dynamic == true) {changeInType = false;}
  129. if (this.edgeType instanceof BezierEdgeStatic && this.options.smooth.enabled == true && this.options.smooth.dynamic == false){changeInType = false;}
  130. if (this.edgeType instanceof StraightEdge && this.options.smooth.enabled == false) {changeInType = false;}
  131. if (changeInType == true) {
  132. dataChanged = this.edgeType.cleanup();
  133. }
  134. }
  135. if (changeInType === true) {
  136. if (this.options.smooth.enabled === true) {
  137. if (this.options.smooth.dynamic === true) {
  138. dataChanged = true;
  139. this.edgeType = new BezierEdgeDynamic(this.options, this.body, this.labelModule);
  140. }
  141. else {
  142. this.edgeType = new BezierEdgeStatic(this.options, this.body, this.labelModule);
  143. }
  144. }
  145. else {
  146. this.edgeType = new StraightEdge(this.options, this.body, this.labelModule);
  147. }
  148. }
  149. else {
  150. // if nothing changes, we just set the options.
  151. this.edgeType.setOptions(this.options);
  152. }
  153. return dataChanged;
  154. }
  155. /**
  156. * Enable or disable the physics.
  157. * @param status
  158. */
  159. togglePhysics(status) {
  160. if (this.options.smooth.enabled == true && this.options.smooth.dynamic == true) {
  161. if (this.via === undefined) {
  162. this.via.pptions.physics = status;
  163. }
  164. }
  165. this.options.physics = status;
  166. }
  167. /**
  168. * Connect an edge to its nodes
  169. */
  170. connect() {
  171. this.disconnect();
  172. this.from = this.body.nodes[this.fromId] || undefined;
  173. this.to = this.body.nodes[this.toId] || undefined;
  174. this.connected = (this.from !== undefined && this.to !== undefined);
  175. if (this.connected === true) {
  176. this.from.attachEdge(this);
  177. this.to.attachEdge(this);
  178. }
  179. else {
  180. if (this.from) {
  181. this.from.detachEdge(this);
  182. }
  183. if (this.to) {
  184. this.to.detachEdge(this);
  185. }
  186. }
  187. }
  188. /**
  189. * Disconnect an edge from its nodes
  190. */
  191. disconnect() {
  192. if (this.from) {
  193. this.from.detachEdge(this);
  194. this.from = undefined;
  195. }
  196. if (this.to) {
  197. this.to.detachEdge(this);
  198. this.to = undefined;
  199. }
  200. this.connected = false;
  201. }
  202. /**
  203. * get the title of this edge.
  204. * @return {string} title The title of the edge, or undefined when no title
  205. * has been set.
  206. */
  207. getTitle() {
  208. return typeof this.title === "function" ? this.title() : this.title;
  209. }
  210. /**
  211. * check if this node is selecte
  212. * @return {boolean} selected True if node is selected, else false
  213. */
  214. isSelected() {
  215. return this.selected;
  216. }
  217. /**
  218. * Retrieve the value of the edge. Can be undefined
  219. * @return {Number} value
  220. */
  221. getValue() {
  222. return this.value;
  223. }
  224. /**
  225. * Adjust the value range of the edge. The edge will adjust it's width
  226. * based on its value.
  227. * @param {Number} min
  228. * @param {Number} max
  229. * @param total
  230. */
  231. setValueRange(min, max, total) {
  232. if (this.value !== undefined) {
  233. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  234. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  235. if (this.options.scaling.label.enabled == true) {
  236. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  237. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  238. }
  239. this.options.width = this.options.scaling.min + scale * widthDiff;
  240. }
  241. }
  242. /**
  243. * Redraw a edge
  244. * Draw this edge in the given canvas
  245. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  246. * @param {CanvasRenderingContext2D} ctx
  247. */
  248. draw(ctx) {
  249. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  250. this.drawArrows(ctx, via);
  251. this.drawLabel (ctx, via);
  252. }
  253. drawArrows(ctx, viaNode) {
  254. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  255. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  256. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  257. }
  258. drawLabel(ctx, viaNode) {
  259. if (this.options.label !== undefined) {
  260. // set style
  261. var node1 = this.from;
  262. var node2 = this.to;
  263. var selected = (this.from.selected || this.to.selected || this.selected);
  264. if (node1.id != node2.id) {
  265. var point = this.edgeType.getPoint(0.5, viaNode);
  266. ctx.save();
  267. // if the label has to be rotated:
  268. if (this.options.font.align !== "horizontal") {
  269. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  270. ctx.translate(point.x, this.labelModule.size.yLine);
  271. this._rotateForLabelAlignment(ctx);
  272. }
  273. // draw the label
  274. this.labelModule.draw(ctx, point.x, point.y, selected);
  275. ctx.restore();
  276. }
  277. else {
  278. var x, y;
  279. var radius = this.options.selfReferenceSize;
  280. if (node1.width > node1.height) {
  281. x = node1.x + node1.width * 0.5;
  282. y = node1.y - radius;
  283. }
  284. else {
  285. x = node1.x + radius;
  286. y = node1.y - node1.height * 0.5;
  287. }
  288. point = this._pointOnCircle(x, y, radius, 0.125);
  289. this.labelModule.draw(ctx, point.x, point.y, selected);
  290. }
  291. }
  292. }
  293. /**
  294. * Check if this object is overlapping with the provided object
  295. * @param {Object} obj an object with parameters left, top
  296. * @return {boolean} True if location is located on the edge
  297. */
  298. isOverlappingWith(obj) {
  299. if (this.connected) {
  300. var distMax = 10;
  301. var xFrom = this.from.x;
  302. var yFrom = this.from.y;
  303. var xTo = this.to.x;
  304. var yTo = this.to.y;
  305. var xObj = obj.left;
  306. var yObj = obj.top;
  307. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  308. return (dist < distMax);
  309. }
  310. else {
  311. return false
  312. }
  313. }
  314. /**
  315. * Rotates the canvas so the text is most readable
  316. * @param {CanvasRenderingContext2D} ctx
  317. * @private
  318. */
  319. _rotateForLabelAlignment(ctx) {
  320. var dy = this.from.y - this.to.y;
  321. var dx = this.from.x - this.to.x;
  322. var angleInDegrees = Math.atan2(dy, dx);
  323. // rotate so label it is readable
  324. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  325. angleInDegrees = angleInDegrees + Math.PI;
  326. }
  327. ctx.rotate(angleInDegrees);
  328. }
  329. /**
  330. * Get a point on a circle
  331. * @param {Number} x
  332. * @param {Number} y
  333. * @param {Number} radius
  334. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  335. * @return {Object} point
  336. * @private
  337. */
  338. _pointOnCircle(x, y, radius, percentage) {
  339. var angle = percentage * 2 * Math.PI;
  340. return {
  341. x: x + radius * Math.cos(angle),
  342. y: y - radius * Math.sin(angle)
  343. }
  344. }
  345. select() {
  346. this.selected = true;
  347. }
  348. unselect() {
  349. this.selected = false;
  350. }
  351. }
  352. export default Edge;