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.

467 lines
14 KiB

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