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.

409 lines
11 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. this.markAllEdgesAsDirty();
  145. });
  146. // refresh the edges. Used when reverting from hierarchical layout
  147. this.body.emitter.on("refreshEdges", this.refresh.bind(this));
  148. this.body.emitter.on("refresh", this.refresh.bind(this));
  149. this.body.emitter.on("destroy", () => {
  150. util.forEach(this.edgesListeners, (callback, event) => {
  151. if (this.body.data.edges)
  152. this.body.data.edges.off(event, callback);
  153. });
  154. delete this.body.functions.createEdge;
  155. delete this.edgesListeners.add;
  156. delete this.edgesListeners.update;
  157. delete this.edgesListeners.remove;
  158. delete this.edgesListeners;
  159. });
  160. }
  161. setOptions(options) {
  162. this.edgeOptions = options;
  163. if (options !== undefined) {
  164. // use the parser from the Edge class to fill in all shorthand notations
  165. Edge.parseOptions(this.options, options);
  166. // handle multiple input cases for color
  167. if (options.color !== undefined) {
  168. this.markAllEdgesAsDirty();
  169. }
  170. // update smooth settings in all edges
  171. let dataChanged = false;
  172. if (options.smooth !== undefined) {
  173. for (let edgeId in this.body.edges) {
  174. if (this.body.edges.hasOwnProperty(edgeId)) {
  175. dataChanged = this.body.edges[edgeId].updateEdgeType() || dataChanged;
  176. }
  177. }
  178. }
  179. // update fonts in all edges
  180. if (options.font !== undefined) {
  181. // use the parser from the Label class to fill in all shorthand notations
  182. Label.parseOptions(this.options.font, options);
  183. for (let edgeId in this.body.edges) {
  184. if (this.body.edges.hasOwnProperty(edgeId)) {
  185. this.body.edges[edgeId].updateLabelModule();
  186. }
  187. }
  188. }
  189. // update the state of the variables if needed
  190. if (options.hidden !== undefined || options.physics !== undefined || dataChanged === true) {
  191. this.body.emitter.emit('_dataChanged');
  192. }
  193. }
  194. }
  195. /**
  196. * Load edges by reading the data table
  197. * @param {Array | DataSet | DataView} edges The data containing the edges.
  198. * @private
  199. * @private
  200. */
  201. setData(edges, doNotEmit = false) {
  202. var oldEdgesData = this.body.data.edges;
  203. if (edges instanceof DataSet || edges instanceof DataView) {
  204. this.body.data.edges = edges;
  205. }
  206. else if (Array.isArray(edges)) {
  207. this.body.data.edges = new DataSet();
  208. this.body.data.edges.add(edges);
  209. }
  210. else if (!edges) {
  211. this.body.data.edges = new DataSet();
  212. }
  213. else {
  214. throw new TypeError('Array or DataSet expected');
  215. }
  216. // TODO: is this null or undefined or false?
  217. if (oldEdgesData) {
  218. // unsubscribe from old dataset
  219. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  220. }
  221. // remove drawn edges
  222. this.body.edges = {};
  223. // TODO: is this null or undefined or false?
  224. if (this.body.data.edges) {
  225. // subscribe to new dataset
  226. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  227. // draw all new nodes
  228. var ids = this.body.data.edges.getIds();
  229. this.add(ids, true);
  230. }
  231. if (doNotEmit === false) {
  232. this.body.emitter.emit("_dataChanged");
  233. }
  234. }
  235. /**
  236. * Add edges
  237. * @param {Number[] | String[]} ids
  238. * @private
  239. */
  240. add(ids, doNotEmit = false) {
  241. var edges = this.body.edges;
  242. var edgesData = this.body.data.edges;
  243. for (let i = 0; i < ids.length; i++) {
  244. var id = ids[i];
  245. var oldEdge = edges[id];
  246. if (oldEdge) {
  247. oldEdge.disconnect();
  248. }
  249. var data = edgesData.get(id, {"showInternalIds" : true});
  250. edges[id] = this.create(data);
  251. }
  252. if (doNotEmit === false) {
  253. this.body.emitter.emit("_dataChanged");
  254. }
  255. }
  256. /**
  257. * Update existing edges, or create them when not yet existing
  258. * @param {Number[] | String[]} ids
  259. * @private
  260. */
  261. update(ids) {
  262. var edges = this.body.edges;
  263. var edgesData = this.body.data.edges;
  264. var dataChanged = false;
  265. for (var i = 0; i < ids.length; i++) {
  266. var id = ids[i];
  267. var data = edgesData.get(id);
  268. var edge = edges[id];
  269. if (edge !== undefined) {
  270. // update edge
  271. edge.disconnect();
  272. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  273. edge.connect();
  274. }
  275. else {
  276. // create edge
  277. this.body.edges[id] = this.create(data);
  278. dataChanged = true;
  279. }
  280. }
  281. if (dataChanged === true) {
  282. this.body.emitter.emit("_dataChanged");
  283. }
  284. else {
  285. this.body.emitter.emit("_dataUpdated");
  286. }
  287. }
  288. /**
  289. * Remove existing edges. Non existing ids will be ignored
  290. * @param {Number[] | String[]} ids
  291. * @private
  292. */
  293. remove(ids) {
  294. var edges = this.body.edges;
  295. for (var i = 0; i < ids.length; i++) {
  296. var id = ids[i];
  297. var edge = edges[id];
  298. if (edge !== undefined) {
  299. edge.cleanup();
  300. edge.disconnect();
  301. delete edges[id];
  302. }
  303. }
  304. this.body.emitter.emit("_dataChanged");
  305. }
  306. refresh() {
  307. let edges = this.body.edges;
  308. for (let edgeId in edges) {
  309. let edge = undefined;
  310. if (edges.hasOwnProperty(edgeId)) {
  311. edge = edges[edgeId];
  312. }
  313. let data = this.body.data.edges._data[edgeId];
  314. if (edge !== undefined && data !== undefined) {
  315. edge.setOptions(data);
  316. }
  317. }
  318. }
  319. create(properties) {
  320. return new Edge(properties, this.body, this.options, this.defaultOptions, this.edgeOptions)
  321. }
  322. markAllEdgesAsDirty() {
  323. for (var edgeId in this.body.edges) {
  324. this.body.edges[edgeId].edgeType.colorDirty = true;
  325. }
  326. }
  327. /**
  328. * Reconnect all edges
  329. * @private
  330. */
  331. reconnectEdges() {
  332. var id;
  333. var nodes = this.body.nodes;
  334. var edges = this.body.edges;
  335. for (id in nodes) {
  336. if (nodes.hasOwnProperty(id)) {
  337. nodes[id].edges = [];
  338. }
  339. }
  340. for (id in edges) {
  341. if (edges.hasOwnProperty(id)) {
  342. var edge = edges[id];
  343. edge.from = null;
  344. edge.to = null;
  345. edge.connect();
  346. }
  347. }
  348. }
  349. getConnectedNodes(edgeId) {
  350. let nodeList = [];
  351. if (this.body.edges[edgeId] !== undefined) {
  352. let edge = this.body.edges[edgeId];
  353. if (edge.fromId) {nodeList.push(edge.fromId);}
  354. if (edge.toId) {nodeList.push(edge.toId);}
  355. }
  356. return nodeList;
  357. }
  358. }
  359. export default EdgesHandler;