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.

377 lines
9.6 KiB

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