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.

486 lines
14 KiB

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