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.

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