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.

385 lines
9.9 KiB

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