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.

353 lines
9.1 KiB

  1. /**
  2. * Created by Alex on 3/4/2015.
  3. */
  4. var util = require("../../util");
  5. var DataSet = require('../../DataSet');
  6. var DataView = require('../../DataView');
  7. import Edge from "./components/Edge"
  8. class EdgesHandler {
  9. constructor(body, images, groups) {
  10. this.body = body;
  11. this.images = images;
  12. this.groups = groups;
  13. // create the edge API in the body container
  14. this.body.functions.createEdge = this.create.bind(this);
  15. this.edgesListeners = {
  16. 'add': (event, params) => {this.add(params.items);},
  17. 'update': (event, params) => {this.update(params.items);},
  18. 'remove': (event, params) => {this.remove(params.items);}
  19. };
  20. this.options = {};
  21. this.defaultOptions = {
  22. arrows: {
  23. to: {enabled: false, scaleFactor:1}, // boolean / {arrowScaleFactor:1} / {enabled: false, arrowScaleFactor:1}
  24. middle: {enabled: false, scaleFactor:1},
  25. from: {enabled: false, scaleFactor:1}
  26. },
  27. color: {
  28. color:'#848484',
  29. highlight:'#848484',
  30. hover: '#848484',
  31. inherit: {
  32. enabled: true,
  33. source: 'from', // from / true
  34. useGradients: false // release in 4.0
  35. },
  36. opacity:1.0
  37. },
  38. dashes:{
  39. enabled: false,
  40. preset: 'dotted',
  41. length: 10,
  42. gap: 5,
  43. altLength: undefined
  44. },
  45. font: {
  46. color: '#343434',
  47. size: 14, // px
  48. face: 'arial',
  49. background: 'none',
  50. stroke: 1, // px
  51. strokeColor: 'white',
  52. align:'horizontal'
  53. },
  54. hidden: false,
  55. hoverWidth: 1.5,
  56. label: undefined,
  57. length: undefined,
  58. physics: true,
  59. scaling:{
  60. min: 1,
  61. max: 15,
  62. label: {
  63. enabled: true,
  64. min: 14,
  65. max: 30,
  66. maxVisible: 30,
  67. drawThreshold: 3
  68. },
  69. customScalingFunction: function (min,max,total,value) {
  70. if (max == min) {
  71. return 0.5;
  72. }
  73. else {
  74. var scale = 1 / (max - min);
  75. return Math.max(0,(value - min)*scale);
  76. }
  77. }
  78. },
  79. selfReferenceSize:20,
  80. smooth: {
  81. enabled: true,
  82. dynamic: true,
  83. type: "continuous",
  84. roundness: 0.5
  85. },
  86. title:undefined,
  87. width: 1,
  88. widthSelectionMultiplier: 2,
  89. value:1
  90. };
  91. util.extend(this.options, this.defaultOptions);
  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 edgeOptions = this.body.edges[edgeId].options.smooth;
  98. if (edgeOptions.enabled === true && edgeOptions.dynamic === true) {
  99. if (type === undefined) {
  100. edge.setOptions({smooth:false});
  101. }
  102. else {
  103. edge.setOptions({smooth:{dynamic:false, type:type}});
  104. }
  105. emitChange = true;
  106. }
  107. }
  108. }
  109. if (emitChange === true) {
  110. this.body.emitter.emit("_dataChanged");
  111. }
  112. });
  113. // this is called when options of EXISTING nodes or edges have changed.
  114. this.body.emitter.on("_dataUpdated", () => {
  115. var t0 = new Date().valueOf();
  116. // update values
  117. this.reconnectEdges();
  118. this.markAllEdgesAsDirty();
  119. // start simulation (can be called safely, even if already running)
  120. console.log("_dataUpdated took:", new Date().valueOf() - t0);
  121. });
  122. }
  123. setOptions(options) {
  124. if (options !== undefined) {
  125. util.mergeOptions(this.options, options, 'smooth');
  126. util.mergeOptions(this.options, options, 'dashes');
  127. // hanlde multiple input cases for arrows
  128. if (options.arrows !== undefined) {
  129. if (typeof options.arrows === 'string') {
  130. let arrows = options.arrows.toLowerCase();
  131. if (arrows.indexOf("to") != -1) {this.options.arrows.to.enabled = true;}
  132. if (arrows.indexOf("middle") != -1) {this.options.arrows.middle.enabled = true;}
  133. if (arrows.indexOf("from") != -1) {this.options.arrows.from.enabled = true;}
  134. }
  135. else if (typeof options.arrows === 'object') {
  136. util.mergeOptions(this.options.arrows, options.arrows, 'to');
  137. util.mergeOptions(this.options.arrows, options.arrows, 'middle');
  138. util.mergeOptions(this.options.arrows, options.arrows, 'from');
  139. }
  140. else {
  141. throw new Error("The arrow options can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(options.arrows));
  142. }
  143. }
  144. // hanlde multiple input cases for color
  145. if (options.color !== undefined) {
  146. if (util.isString(options.color)) {
  147. util.assignAllKeys(this.options.color, options.color);
  148. this.options.color.inherit.enabled = false;
  149. }
  150. else {
  151. util.extend(this.options.color, options.color);
  152. if (options.color.inherit === undefined) {
  153. this.options.color.inherit.enabled = false;
  154. }
  155. }
  156. util.mergeOptions(this.options.color, options.color, 'inherit');
  157. }
  158. // font cases are handled by the Label class
  159. }
  160. }
  161. /**
  162. * Load edges by reading the data table
  163. * @param {Array | DataSet | DataView} edges The data containing the edges.
  164. * @private
  165. * @private
  166. */
  167. setData(edges, doNotEmit = false) {
  168. var oldEdgesData = this.body.data.edges;
  169. if (edges instanceof DataSet || edges instanceof DataView) {
  170. this.body.data.edges = edges;
  171. }
  172. else if (Array.isArray(edges)) {
  173. this.body.data.edges = new DataSet();
  174. this.body.data.edges.add(edges);
  175. }
  176. else if (!edges) {
  177. this.body.data.edges = new DataSet();
  178. }
  179. else {
  180. throw new TypeError('Array or DataSet expected');
  181. }
  182. // TODO: is this null or undefined or false?
  183. if (oldEdgesData) {
  184. // unsubscribe from old dataset
  185. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  186. }
  187. // remove drawn edges
  188. this.body.edges = {};
  189. // TODO: is this null or undefined or false?
  190. if (this.body.data.edges) {
  191. // subscribe to new dataset
  192. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  193. // draw all new nodes
  194. var ids = this.body.data.edges.getIds();
  195. this.add(ids, true);
  196. }
  197. if (doNotEmit === false) {
  198. this.body.emitter.emit("_dataChanged");
  199. }
  200. }
  201. /**
  202. * Add edges
  203. * @param {Number[] | String[]} ids
  204. * @private
  205. */
  206. add(ids, doNotEmit = false) {
  207. var edges = this.body.edges;
  208. var edgesData = this.body.data.edges;
  209. for (let i = 0; i < ids.length; i++) {
  210. var id = ids[i];
  211. var oldEdge = edges[id];
  212. if (oldEdge) {
  213. oldEdge.disconnect();
  214. }
  215. var data = edgesData.get(id, {"showInternalIds" : true});
  216. edges[id] = this.create(data);
  217. }
  218. if (doNotEmit === false) {
  219. this.body.emitter.emit("_dataChanged");
  220. }
  221. }
  222. /**
  223. * Update existing edges, or create them when not yet existing
  224. * @param {Number[] | String[]} ids
  225. * @private
  226. */
  227. update(ids) {
  228. var edges = this.body.edges;
  229. var edgesData = this.body.data.edges;
  230. var dataChanged = false;
  231. for (var i = 0; i < ids.length; i++) {
  232. var id = ids[i];
  233. var data = edgesData.get(id);
  234. var edge = edges[id];
  235. if (edge === null) {
  236. // update edge
  237. edge.disconnect();
  238. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  239. edge.connect();
  240. }
  241. else {
  242. // create edge
  243. this.body.edges[id] = this.create(data);
  244. dataChanged = true;
  245. }
  246. }
  247. if (dataChanged === true) {
  248. this.body.emitter.emit("_dataChanged");
  249. }
  250. else {
  251. this.body.emitter.emit("_dataUpdated");
  252. }
  253. }
  254. /**
  255. * Remove existing edges. Non existing ids will be ignored
  256. * @param {Number[] | String[]} ids
  257. * @private
  258. */
  259. remove(ids) {
  260. var edges = this.body.edges;
  261. for (var i = 0; i < ids.length; i++) {
  262. var id = ids[i];
  263. var edge = edges[id];
  264. if (edge !== undefined) {
  265. if (edge.via != null) {
  266. delete this.body.supportNodes[edge.via.id];
  267. }
  268. edge.disconnect();
  269. delete edges[id];
  270. }
  271. }
  272. this.body.emitter.emit("_dataChanged");
  273. }
  274. create(properties) {
  275. return new Edge(properties, this.body, this.options)
  276. }
  277. markAllEdgesAsDirty() {
  278. for (var edgeId in this.body.edges) {
  279. this.body.edges[edgeId].colorDirty = true;
  280. }
  281. }
  282. /**
  283. * Reconnect all edges
  284. * @private
  285. */
  286. reconnectEdges() {
  287. var id;
  288. var nodes = this.body.nodes;
  289. var edges = this.body.edges;
  290. for (id in nodes) {
  291. if (nodes.hasOwnProperty(id)) {
  292. nodes[id].edges = [];
  293. }
  294. }
  295. for (id in edges) {
  296. if (edges.hasOwnProperty(id)) {
  297. var edge = edges[id];
  298. edge.from = null;
  299. edge.to = null;
  300. edge.connect();
  301. }
  302. }
  303. }
  304. }
  305. export default EdgesHandler;