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.

374 lines
9.6 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. length: undefined,
  46. physics: true,
  47. scaling:{
  48. min: 1,
  49. max: 15,
  50. label: {
  51. enabled: true,
  52. min: 14,
  53. max: 30,
  54. maxVisible: 30,
  55. drawThreshold: 3
  56. },
  57. customScalingFunction: function (min,max,total,value) {
  58. if (max === min) {
  59. return 0.5;
  60. }
  61. else {
  62. var scale = 1 / (max - min);
  63. return Math.max(0,(value - min)*scale);
  64. }
  65. }
  66. },
  67. selectionWidth: 1,
  68. selfReferenceSize:20,
  69. shadow:{
  70. enabled: false,
  71. size:10,
  72. x:5,
  73. y:5
  74. },
  75. smooth: {
  76. enabled: true,
  77. dynamic: true,
  78. type: "continuous",
  79. roundness: 0.5
  80. },
  81. title:undefined,
  82. width: 1,
  83. value: undefined
  84. };
  85. util.extend(this.options, this.defaultOptions);
  86. this.bindEventListeners();
  87. }
  88. bindEventListeners() {
  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. this.body.emitter.on("destroy", () => {
  127. delete this.body.functions.createEdge;
  128. delete this.edgesListeners.add;
  129. delete this.edgesListeners.update;
  130. delete this.edgesListeners.remove;
  131. delete this.edgesListeners;
  132. });
  133. }
  134. setOptions(options) {
  135. if (options !== undefined) {
  136. // use the parser from the Edge class to fill in all shorthand notations
  137. Edge.parseOptions(this.options, options);
  138. // hanlde multiple input cases for color
  139. if (options.color !== undefined) {
  140. this.markAllEdgesAsDirty();
  141. }
  142. // update smooth settings in all edges
  143. let dataChanged = false;
  144. if (options.smooth !== undefined) {
  145. for (let edgeId in this.body.edges) {
  146. if (this.body.edges.hasOwnProperty(edgeId)) {
  147. dataChanged = this.body.edges[edgeId].updateEdgeType() || dataChanged;
  148. }
  149. }
  150. }
  151. // update fonts in all edges
  152. if (options.font !== undefined) {
  153. // use the parser from the Label class to fill in all shorthand notations
  154. Label.parseOptions(this.options, options);
  155. for (let edgeId in this.body.edges) {
  156. if (this.body.edges.hasOwnProperty(edgeId)) {
  157. this.body.edges[edgeId].updateLabelModule();
  158. }
  159. }
  160. }
  161. // update the state of the variables if needed
  162. if (options.hidden !== undefined || options.physics !== undefined || dataChanged === true) {
  163. this.body.emitter.emit('_dataChanged');
  164. }
  165. }
  166. }
  167. /**
  168. * Load edges by reading the data table
  169. * @param {Array | DataSet | DataView} edges The data containing the edges.
  170. * @private
  171. * @private
  172. */
  173. setData(edges, doNotEmit = false) {
  174. var oldEdgesData = this.body.data.edges;
  175. if (edges instanceof DataSet || edges instanceof DataView) {
  176. this.body.data.edges = edges;
  177. }
  178. else if (Array.isArray(edges)) {
  179. this.body.data.edges = new DataSet();
  180. this.body.data.edges.add(edges);
  181. }
  182. else if (!edges) {
  183. this.body.data.edges = new DataSet();
  184. }
  185. else {
  186. throw new TypeError('Array or DataSet expected');
  187. }
  188. // TODO: is this null or undefined or false?
  189. if (oldEdgesData) {
  190. // unsubscribe from old dataset
  191. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  192. }
  193. // remove drawn edges
  194. this.body.edges = {};
  195. // TODO: is this null or undefined or false?
  196. if (this.body.data.edges) {
  197. // subscribe to new dataset
  198. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  199. // draw all new nodes
  200. var ids = this.body.data.edges.getIds();
  201. this.add(ids, true);
  202. }
  203. if (doNotEmit === false) {
  204. this.body.emitter.emit("_dataChanged");
  205. }
  206. }
  207. /**
  208. * Add edges
  209. * @param {Number[] | String[]} ids
  210. * @private
  211. */
  212. add(ids, doNotEmit = false) {
  213. var edges = this.body.edges;
  214. var edgesData = this.body.data.edges;
  215. for (let i = 0; i < ids.length; i++) {
  216. var id = ids[i];
  217. var oldEdge = edges[id];
  218. if (oldEdge) {
  219. oldEdge.disconnect();
  220. }
  221. var data = edgesData.get(id, {"showInternalIds" : true});
  222. edges[id] = this.create(data);
  223. }
  224. if (doNotEmit === false) {
  225. this.body.emitter.emit("_dataChanged");
  226. }
  227. }
  228. /**
  229. * Update existing edges, or create them when not yet existing
  230. * @param {Number[] | String[]} ids
  231. * @private
  232. */
  233. update(ids) {
  234. var edges = this.body.edges;
  235. var edgesData = this.body.data.edges;
  236. var dataChanged = false;
  237. for (var i = 0; i < ids.length; i++) {
  238. var id = ids[i];
  239. var data = edgesData.get(id);
  240. var edge = edges[id];
  241. if (edge === null) {
  242. // update edge
  243. edge.disconnect();
  244. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  245. edge.connect();
  246. }
  247. else {
  248. // create edge
  249. this.body.edges[id] = this.create(data);
  250. dataChanged = true;
  251. }
  252. }
  253. if (dataChanged === true) {
  254. this.body.emitter.emit("_dataChanged");
  255. }
  256. else {
  257. this.body.emitter.emit("_dataUpdated");
  258. }
  259. }
  260. /**
  261. * Remove existing edges. Non existing ids will be ignored
  262. * @param {Number[] | String[]} ids
  263. * @private
  264. */
  265. remove(ids) {
  266. var edges = this.body.edges;
  267. for (var i = 0; i < ids.length; i++) {
  268. var id = ids[i];
  269. var edge = edges[id];
  270. if (edge !== undefined) {
  271. if (edge.via != null) {
  272. delete this.body.supportNodes[edge.via.id];
  273. }
  274. edge.disconnect();
  275. delete edges[id];
  276. }
  277. }
  278. this.body.emitter.emit("_dataChanged");
  279. }
  280. refresh() {
  281. let edges = this.body.edges;
  282. for (let edgeId in edges) {
  283. let edge = undefined;
  284. if (edges.hasOwnProperty(edgeId)) {
  285. edge = edges[edgeId];
  286. }
  287. let data = this.body.data.edges._data[edgeId];
  288. if (edge !== undefined && data !== undefined) {
  289. edge.setOptions(data);
  290. }
  291. }
  292. }
  293. create(properties) {
  294. return new Edge(properties, this.body, this.options)
  295. }
  296. markAllEdgesAsDirty() {
  297. for (var edgeId in this.body.edges) {
  298. this.body.edges[edgeId].edgeType.colorDirty = true;
  299. }
  300. }
  301. /**
  302. * Reconnect all edges
  303. * @private
  304. */
  305. reconnectEdges() {
  306. var id;
  307. var nodes = this.body.nodes;
  308. var edges = this.body.edges;
  309. for (id in nodes) {
  310. if (nodes.hasOwnProperty(id)) {
  311. nodes[id].edges = [];
  312. }
  313. }
  314. for (id in edges) {
  315. if (edges.hasOwnProperty(id)) {
  316. var edge = edges[id];
  317. edge.from = null;
  318. edge.to = null;
  319. edge.connect();
  320. }
  321. }
  322. }
  323. }
  324. export default EdgesHandler;