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.

491 lines
15 KiB

9 years ago
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. // update label Module
  62. this.updateLabelModule();
  63. let dataChanged = this.updateEdgeType();
  64. // if anything has been updates, reset the selection width and the hover width
  65. this._setInteractionWidths();
  66. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  67. this.connect();
  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. this.edgeType.connect();
  232. }
  233. /**
  234. * Disconnect an edge from its nodes
  235. */
  236. disconnect() {
  237. if (this.from) {
  238. this.from.detachEdge(this);
  239. this.from = undefined;
  240. }
  241. if (this.to) {
  242. this.to.detachEdge(this);
  243. this.to = undefined;
  244. }
  245. this.connected = false;
  246. }
  247. /**
  248. * get the title of this edge.
  249. * @return {string} title The title of the edge, or undefined when no title
  250. * has been set.
  251. */
  252. getTitle() {
  253. return this.title;
  254. }
  255. /**
  256. * check if this node is selecte
  257. * @return {boolean} selected True if node is selected, else false
  258. */
  259. isSelected() {
  260. return this.selected;
  261. }
  262. /**
  263. * Retrieve the value of the edge. Can be undefined
  264. * @return {Number} value
  265. */
  266. getValue() {
  267. return this.options.value;
  268. }
  269. /**
  270. * Adjust the value range of the edge. The edge will adjust it's width
  271. * based on its value.
  272. * @param {Number} min
  273. * @param {Number} max
  274. * @param total
  275. */
  276. setValueRange(min, max, total) {
  277. if (this.options.value !== undefined) {
  278. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  279. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  280. if (this.options.scaling.label.enabled === true) {
  281. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  282. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  283. }
  284. this.options.width = this.options.scaling.min + scale * widthDiff;
  285. }
  286. else {
  287. this.options.width = this.baseWidth;
  288. this.options.font.size = this.baseFontSize;
  289. }
  290. this._setInteractionWidths();
  291. }
  292. _setInteractionWidths() {
  293. if (typeof this.options.hoverWidth === 'function') {
  294. this.edgeType.hoverWidth = this.options.hoverWidth(this.options.width);
  295. }
  296. else {
  297. this.edgeType.hoverWidth = this.options.hoverWidth + this.options.width;
  298. }
  299. if (typeof this.options.selectionWidth === 'function') {
  300. this.edgeType.selectionWidth = this.options.selectionWidth(this.options.width);
  301. }
  302. else {
  303. this.edgeType.selectionWidth = this.options.selectionWidth + this.options.width;
  304. }
  305. }
  306. /**
  307. * Redraw a edge
  308. * Draw this edge in the given canvas
  309. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  310. * @param {CanvasRenderingContext2D} ctx
  311. */
  312. draw(ctx) {
  313. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  314. this.drawArrows(ctx, via);
  315. this.drawLabel (ctx, via);
  316. }
  317. drawArrows(ctx, viaNode) {
  318. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  319. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  320. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  321. }
  322. drawLabel(ctx, viaNode) {
  323. if (this.options.label !== undefined) {
  324. // set style
  325. var node1 = this.from;
  326. var node2 = this.to;
  327. var selected = (this.from.selected || this.to.selected || this.selected);
  328. if (node1.id != node2.id) {
  329. var point = this.edgeType.getPoint(0.5, viaNode);
  330. ctx.save();
  331. // if the label has to be rotated:
  332. if (this.options.font.align !== "horizontal") {
  333. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  334. ctx.translate(point.x, this.labelModule.size.yLine);
  335. this._rotateForLabelAlignment(ctx);
  336. }
  337. // draw the label
  338. this.labelModule.draw(ctx, point.x, point.y, selected);
  339. ctx.restore();
  340. }
  341. else {
  342. var x, y;
  343. var radius = this.options.selfReferenceSize;
  344. if (node1.shape.width > node1.shape.height) {
  345. x = node1.x + node1.shape.width * 0.5;
  346. y = node1.y - radius;
  347. }
  348. else {
  349. x = node1.x + radius;
  350. y = node1.y - node1.shape.height * 0.5;
  351. }
  352. point = this._pointOnCircle(x, y, radius, 0.125);
  353. this.labelModule.draw(ctx, point.x, point.y, selected);
  354. }
  355. }
  356. }
  357. /**
  358. * Check if this object is overlapping with the provided object
  359. * @param {Object} obj an object with parameters left, top
  360. * @return {boolean} True if location is located on the edge
  361. */
  362. isOverlappingWith(obj) {
  363. if (this.connected) {
  364. var distMax = 10;
  365. var xFrom = this.from.x;
  366. var yFrom = this.from.y;
  367. var xTo = this.to.x;
  368. var yTo = this.to.y;
  369. var xObj = obj.left;
  370. var yObj = obj.top;
  371. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  372. return (dist < distMax);
  373. }
  374. else {
  375. return false
  376. }
  377. }
  378. /**
  379. * Rotates the canvas so the text is most readable
  380. * @param {CanvasRenderingContext2D} ctx
  381. * @private
  382. */
  383. _rotateForLabelAlignment(ctx) {
  384. var dy = this.from.y - this.to.y;
  385. var dx = this.from.x - this.to.x;
  386. var angleInDegrees = Math.atan2(dy, dx);
  387. // rotate so label it is readable
  388. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  389. angleInDegrees = angleInDegrees + Math.PI;
  390. }
  391. ctx.rotate(angleInDegrees);
  392. }
  393. /**
  394. * Get a point on a circle
  395. * @param {Number} x
  396. * @param {Number} y
  397. * @param {Number} radius
  398. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  399. * @return {Object} point
  400. * @private
  401. */
  402. _pointOnCircle(x, y, radius, percentage) {
  403. var angle = percentage * 2 * Math.PI;
  404. return {
  405. x: x + radius * Math.cos(angle),
  406. y: y - radius * Math.sin(angle)
  407. }
  408. }
  409. select() {
  410. this.selected = true;
  411. }
  412. unselect() {
  413. this.selected = false;
  414. }
  415. }
  416. export default Edge;