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.

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