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.

489 lines
15 KiB

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