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.

367 lines
9.3 KiB

  1. var util = require("../../util");
  2. var DataSet = require('../../DataSet');
  3. var DataView = require('../../DataView');
  4. import Edge from "./components/Edge"
  5. import Label from "./components/shared/Label"
  6. class EdgesHandler {
  7. constructor(body, images, groups) {
  8. this.body = body;
  9. this.images = images;
  10. this.groups = groups;
  11. // create the edge API in the body container
  12. this.body.functions.createEdge = this.create.bind(this);
  13. this.edgesListeners = {
  14. 'add': (event, params) => {this.add(params.items);},
  15. 'update': (event, params) => {this.update(params.items);},
  16. 'remove': (event, params) => {this.remove(params.items);}
  17. };
  18. this.options = {};
  19. this.defaultOptions = {
  20. arrows: {
  21. to: {enabled: false, scaleFactor:1}, // boolean / {arrowScaleFactor:1} / {enabled: false, arrowScaleFactor:1}
  22. middle: {enabled: false, scaleFactor:1},
  23. from: {enabled: false, scaleFactor:1}
  24. },
  25. color: {
  26. color:'#848484',
  27. highlight:'#848484',
  28. hover: '#848484',
  29. inherit: 'from',
  30. opacity:1.0
  31. },
  32. dashes:{
  33. enabled: false,
  34. pattern:[5,5]
  35. },
  36. font: {
  37. color: '#343434',
  38. size: 14, // px
  39. face: 'arial',
  40. background: 'none',
  41. stroke: 1, // px
  42. strokeColor: '#ffffff',
  43. align:'horizontal'
  44. },
  45. hidden: false,
  46. hoverWidth: 1.5,
  47. label: undefined,
  48. length: undefined,
  49. physics: true,
  50. scaling:{
  51. min: 1,
  52. max: 15,
  53. label: {
  54. enabled: true,
  55. min: 14,
  56. max: 30,
  57. maxVisible: 30,
  58. drawThreshold: 3
  59. },
  60. customScalingFunction: function (min,max,total,value) {
  61. if (max === min) {
  62. return 0.5;
  63. }
  64. else {
  65. var scale = 1 / (max - min);
  66. return Math.max(0,(value - min)*scale);
  67. }
  68. }
  69. },
  70. selectionWidth: 1,
  71. selfReferenceSize:20,
  72. shadow:{
  73. enabled: false,
  74. size:10,
  75. x:5,
  76. y:5
  77. },
  78. smooth: {
  79. enabled: true,
  80. dynamic: true,
  81. type: "continuous",
  82. roundness: 0.5
  83. },
  84. title:undefined,
  85. width: 1,
  86. value: undefined
  87. };
  88. util.extend(this.options, this.defaultOptions);
  89. // this allows external modules to force all dynamic curves to turn static.
  90. this.body.emitter.on("_forceDisableDynamicCurves", (type) => {
  91. let emitChange = false;
  92. for (let edgeId in this.body.edges) {
  93. if (this.body.edges.hasOwnProperty(edgeId)) {
  94. let edge = this.body.edges[edgeId];
  95. let edgeData = this.body.data.edges._data[edgeId];
  96. // only forcilby remove the smooth curve if the data has been set of the edge has the smooth curves defined.
  97. // this is because a change in the global would not affect these curves.
  98. if (edgeData !== undefined) {
  99. let edgeOptions = edgeData.smooth;
  100. if (edgeOptions !== undefined) {
  101. if (edgeOptions.enabled === true && edgeOptions.dynamic === true) {
  102. if (type === undefined) {
  103. edge.setOptions({smooth: false});
  104. }
  105. else {
  106. edge.setOptions({smooth: {dynamic: false, type: type}});
  107. }
  108. emitChange = true;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. if (emitChange === true) {
  115. this.body.emitter.emit("_dataChanged");
  116. }
  117. });
  118. // this is called when options of EXISTING nodes or edges have changed.
  119. this.body.emitter.on("_dataUpdated", () => {
  120. this.reconnectEdges();
  121. this.markAllEdgesAsDirty();
  122. });
  123. // refresh the edges. Used when reverting from hierarchical layout
  124. this.body.emitter.on("refreshEdges", this.refresh.bind(this));
  125. this.body.emitter.on("refresh", this.refresh.bind(this));
  126. }
  127. setOptions(options) {
  128. if (options !== undefined) {
  129. // use the parser from the Edge class to fill in all shorthand notations
  130. Edge.parseOptions(this.options, options);
  131. // hanlde multiple input cases for color
  132. if (options.color !== undefined) {
  133. this.markAllEdgesAsDirty();
  134. }
  135. // update smooth settings in all edges
  136. let dataChanged = false;
  137. if (options.smooth !== undefined) {
  138. for (let edgeId in this.body.edges) {
  139. if (this.body.edges.hasOwnProperty(edgeId)) {
  140. dataChanged = this.body.edges[edgeId].updateEdgeType() || dataChanged;
  141. }
  142. }
  143. }
  144. // update fonts in all edges
  145. if (options.font !== undefined) {
  146. // use the parser from the Label class to fill in all shorthand notations
  147. Label.parseOptions(this.options,options);
  148. for (let edgeId in this.body.edges) {
  149. if (this.body.edges.hasOwnProperty(edgeId)) {
  150. this.body.edges[edgeId].updateLabelModule();
  151. }
  152. }
  153. }
  154. // update the state of the variables if needed
  155. if (options.hidden !== undefined || options.physics !== undefined || dataChanged === true) {
  156. this.body.emitter.emit('_dataChanged');
  157. }
  158. }
  159. }
  160. /**
  161. * Load edges by reading the data table
  162. * @param {Array | DataSet | DataView} edges The data containing the edges.
  163. * @private
  164. * @private
  165. */
  166. setData(edges, doNotEmit = false) {
  167. var oldEdgesData = this.body.data.edges;
  168. if (edges instanceof DataSet || edges instanceof DataView) {
  169. this.body.data.edges = edges;
  170. }
  171. else if (Array.isArray(edges)) {
  172. this.body.data.edges = new DataSet();
  173. this.body.data.edges.add(edges);
  174. }
  175. else if (!edges) {
  176. this.body.data.edges = new DataSet();
  177. }
  178. else {
  179. throw new TypeError('Array or DataSet expected');
  180. }
  181. // TODO: is this null or undefined or false?
  182. if (oldEdgesData) {
  183. // unsubscribe from old dataset
  184. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  185. }
  186. // remove drawn edges
  187. this.body.edges = {};
  188. // TODO: is this null or undefined or false?
  189. if (this.body.data.edges) {
  190. // subscribe to new dataset
  191. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  192. // draw all new nodes
  193. var ids = this.body.data.edges.getIds();
  194. this.add(ids, true);
  195. }
  196. if (doNotEmit === false) {
  197. this.body.emitter.emit("_dataChanged");
  198. }
  199. }
  200. /**
  201. * Add edges
  202. * @param {Number[] | String[]} ids
  203. * @private
  204. */
  205. add(ids, doNotEmit = false) {
  206. var edges = this.body.edges;
  207. var edgesData = this.body.data.edges;
  208. for (let i = 0; i < ids.length; i++) {
  209. var id = ids[i];
  210. var oldEdge = edges[id];
  211. if (oldEdge) {
  212. oldEdge.disconnect();
  213. }
  214. var data = edgesData.get(id, {"showInternalIds" : true});
  215. edges[id] = this.create(data);
  216. }
  217. if (doNotEmit === false) {
  218. this.body.emitter.emit("_dataChanged");
  219. }
  220. }
  221. /**
  222. * Update existing edges, or create them when not yet existing
  223. * @param {Number[] | String[]} ids
  224. * @private
  225. */
  226. update(ids) {
  227. var edges = this.body.edges;
  228. var edgesData = this.body.data.edges;
  229. var dataChanged = false;
  230. for (var i = 0; i < ids.length; i++) {
  231. var id = ids[i];
  232. var data = edgesData.get(id);
  233. var edge = edges[id];
  234. if (edge === null) {
  235. // update edge
  236. edge.disconnect();
  237. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  238. edge.connect();
  239. }
  240. else {
  241. // create edge
  242. this.body.edges[id] = this.create(data);
  243. dataChanged = true;
  244. }
  245. }
  246. if (dataChanged === true) {
  247. this.body.emitter.emit("_dataChanged");
  248. }
  249. else {
  250. this.body.emitter.emit("_dataUpdated");
  251. }
  252. }
  253. /**
  254. * Remove existing edges. Non existing ids will be ignored
  255. * @param {Number[] | String[]} ids
  256. * @private
  257. */
  258. remove(ids) {
  259. var edges = this.body.edges;
  260. for (var i = 0; i < ids.length; i++) {
  261. var id = ids[i];
  262. var edge = edges[id];
  263. if (edge !== undefined) {
  264. if (edge.via != null) {
  265. delete this.body.supportNodes[edge.via.id];
  266. }
  267. edge.disconnect();
  268. delete edges[id];
  269. }
  270. }
  271. this.body.emitter.emit("_dataChanged");
  272. }
  273. refresh() {
  274. let edges = this.body.edges;
  275. for (let edgeId in edges) {
  276. let edge = undefined;
  277. if (edges.hasOwnProperty(edgeId)) {
  278. edge = edges[edgeId];
  279. }
  280. let data = this.body.data.edges._data[edgeId];
  281. if (edge !== undefined && data !== undefined) {
  282. edge.setOptions(data);
  283. }
  284. }
  285. }
  286. create(properties) {
  287. return new Edge(properties, this.body, this.options)
  288. }
  289. markAllEdgesAsDirty() {
  290. for (var edgeId in this.body.edges) {
  291. this.body.edges[edgeId].edgeType.colorDirty = true;
  292. }
  293. }
  294. /**
  295. * Reconnect all edges
  296. * @private
  297. */
  298. reconnectEdges() {
  299. var id;
  300. var nodes = this.body.nodes;
  301. var edges = this.body.edges;
  302. for (id in nodes) {
  303. if (nodes.hasOwnProperty(id)) {
  304. nodes[id].edges = [];
  305. }
  306. }
  307. for (id in edges) {
  308. if (edges.hasOwnProperty(id)) {
  309. var edge = edges[id];
  310. edge.from = null;
  311. edge.to = null;
  312. edge.connect();
  313. }
  314. }
  315. }
  316. }
  317. export default EdgesHandler;