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.

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