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.

519 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. }
  305. _setInteractionWidths() {
  306. if (typeof this.options.hoverWidth === 'function') {
  307. this.edgeType.hoverWidth = this.options.hoverWidth(this.options.width);
  308. }
  309. else {
  310. this.edgeType.hoverWidth = this.options.hoverWidth + this.options.width;
  311. }
  312. if (typeof this.options.selectionWidth === 'function') {
  313. this.edgeType.selectionWidth = this.options.selectionWidth(this.options.width);
  314. }
  315. else {
  316. this.edgeType.selectionWidth = this.options.selectionWidth + this.options.width;
  317. }
  318. }
  319. /**
  320. * Redraw a edge
  321. * Draw this edge in the given canvas
  322. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  323. * @param {CanvasRenderingContext2D} ctx
  324. */
  325. draw(ctx) {
  326. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  327. this.drawArrows(ctx, via);
  328. this.drawLabel (ctx, via);
  329. }
  330. drawArrows(ctx, viaNode) {
  331. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  332. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  333. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  334. }
  335. drawLabel(ctx, viaNode) {
  336. if (this.options.label !== undefined) {
  337. // set style
  338. var node1 = this.from;
  339. var node2 = this.to;
  340. var selected = (this.from.selected || this.to.selected || this.selected);
  341. if (node1.id != node2.id) {
  342. this.labelModule.pointToSelf = false;
  343. var point = this.edgeType.getPoint(0.5, viaNode);
  344. ctx.save();
  345. // if the label has to be rotated:
  346. if (this.options.font.align !== "horizontal") {
  347. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  348. ctx.translate(point.x, this.labelModule.size.yLine);
  349. this._rotateForLabelAlignment(ctx);
  350. }
  351. // draw the label
  352. this.labelModule.draw(ctx, point.x, point.y, selected);
  353. ctx.restore();
  354. }
  355. else {
  356. // Ignore the orientations.
  357. this.labelModule.pointToSelf = true;
  358. var x, y;
  359. var radius = this.options.selfReferenceSize;
  360. if (node1.shape.width > node1.shape.height) {
  361. x = node1.x + node1.shape.width * 0.5;
  362. y = node1.y - radius;
  363. }
  364. else {
  365. x = node1.x + radius;
  366. y = node1.y - node1.shape.height * 0.5;
  367. }
  368. point = this._pointOnCircle(x, y, radius, 0.125);
  369. this.labelModule.draw(ctx, point.x, point.y, selected);
  370. }
  371. }
  372. }
  373. /**
  374. * Check if this object is overlapping with the provided object
  375. * @param {Object} obj an object with parameters left, top
  376. * @return {boolean} True if location is located on the edge
  377. */
  378. isOverlappingWith(obj) {
  379. if (this.connected) {
  380. var distMax = 10;
  381. var xFrom = this.from.x;
  382. var yFrom = this.from.y;
  383. var xTo = this.to.x;
  384. var yTo = this.to.y;
  385. var xObj = obj.left;
  386. var yObj = obj.top;
  387. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  388. return (dist < distMax);
  389. }
  390. else {
  391. return false
  392. }
  393. }
  394. /**
  395. * Rotates the canvas so the text is most readable
  396. * @param {CanvasRenderingContext2D} ctx
  397. * @private
  398. */
  399. _rotateForLabelAlignment(ctx) {
  400. var dy = this.from.y - this.to.y;
  401. var dx = this.from.x - this.to.x;
  402. var angleInDegrees = Math.atan2(dy, dx);
  403. // rotate so label it is readable
  404. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  405. angleInDegrees = angleInDegrees + Math.PI;
  406. }
  407. ctx.rotate(angleInDegrees);
  408. }
  409. /**
  410. * Get a point on a circle
  411. * @param {Number} x
  412. * @param {Number} y
  413. * @param {Number} radius
  414. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  415. * @return {Object} point
  416. * @private
  417. */
  418. _pointOnCircle(x, y, radius, percentage) {
  419. var angle = percentage * 2 * Math.PI;
  420. return {
  421. x: x + radius * Math.cos(angle),
  422. y: y - radius * Math.sin(angle)
  423. }
  424. }
  425. select() {
  426. this.selected = true;
  427. }
  428. unselect() {
  429. this.selected = false;
  430. }
  431. /**
  432. * cleans all required things on delete
  433. * @returns {*}
  434. */
  435. cleanup() {
  436. return this.edgeType.cleanup();
  437. }
  438. }
  439. export default Edge;