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.

484 lines
14 KiB

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