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.

479 lines
14 KiB

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