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.

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