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.

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