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.

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