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.

396 lines
10 KiB

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