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.

349 lines
8.9 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. this.reconnectEdges();
  116. this.markAllEdgesAsDirty();
  117. });
  118. }
  119. setOptions(options) {
  120. if (options !== undefined) {
  121. util.mergeOptions(this.options, options, 'smooth');
  122. util.mergeOptions(this.options, options, 'dashes');
  123. // hanlde multiple input cases for arrows
  124. if (options.arrows !== undefined) {
  125. if (typeof options.arrows === 'string') {
  126. let arrows = options.arrows.toLowerCase();
  127. if (arrows.indexOf("to") != -1) {this.options.arrows.to.enabled = true;}
  128. if (arrows.indexOf("middle") != -1) {this.options.arrows.middle.enabled = true;}
  129. if (arrows.indexOf("from") != -1) {this.options.arrows.from.enabled = true;}
  130. }
  131. else if (typeof options.arrows === 'object') {
  132. util.mergeOptions(this.options.arrows, options.arrows, 'to');
  133. util.mergeOptions(this.options.arrows, options.arrows, 'middle');
  134. util.mergeOptions(this.options.arrows, options.arrows, 'from');
  135. }
  136. else {
  137. throw new Error("The arrow options can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(options.arrows));
  138. }
  139. }
  140. // hanlde multiple input cases for color
  141. if (options.color !== undefined) {
  142. if (util.isString(options.color)) {
  143. util.assignAllKeys(this.options.color, options.color);
  144. this.options.color.inherit.enabled = false;
  145. }
  146. else {
  147. util.extend(this.options.color, options.color);
  148. if (options.color.inherit === undefined) {
  149. this.options.color.inherit.enabled = false;
  150. }
  151. }
  152. util.mergeOptions(this.options.color, options.color, 'inherit');
  153. }
  154. // font cases are handled by the Label class
  155. }
  156. }
  157. /**
  158. * Load edges by reading the data table
  159. * @param {Array | DataSet | DataView} edges The data containing the edges.
  160. * @private
  161. * @private
  162. */
  163. setData(edges, doNotEmit = false) {
  164. var oldEdgesData = this.body.data.edges;
  165. if (edges instanceof DataSet || edges instanceof DataView) {
  166. this.body.data.edges = edges;
  167. }
  168. else if (Array.isArray(edges)) {
  169. this.body.data.edges = new DataSet();
  170. this.body.data.edges.add(edges);
  171. }
  172. else if (!edges) {
  173. this.body.data.edges = new DataSet();
  174. }
  175. else {
  176. throw new TypeError('Array or DataSet expected');
  177. }
  178. // TODO: is this null or undefined or false?
  179. if (oldEdgesData) {
  180. // unsubscribe from old dataset
  181. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  182. }
  183. // remove drawn edges
  184. this.body.edges = {};
  185. // TODO: is this null or undefined or false?
  186. if (this.body.data.edges) {
  187. // subscribe to new dataset
  188. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  189. // draw all new nodes
  190. var ids = this.body.data.edges.getIds();
  191. this.add(ids, true);
  192. }
  193. if (doNotEmit === false) {
  194. this.body.emitter.emit("_dataChanged");
  195. }
  196. }
  197. /**
  198. * Add edges
  199. * @param {Number[] | String[]} ids
  200. * @private
  201. */
  202. add(ids, doNotEmit = false) {
  203. var edges = this.body.edges;
  204. var edgesData = this.body.data.edges;
  205. for (let i = 0; i < ids.length; i++) {
  206. var id = ids[i];
  207. var oldEdge = edges[id];
  208. if (oldEdge) {
  209. oldEdge.disconnect();
  210. }
  211. var data = edgesData.get(id, {"showInternalIds" : true});
  212. edges[id] = this.create(data);
  213. }
  214. if (doNotEmit === false) {
  215. this.body.emitter.emit("_dataChanged");
  216. }
  217. }
  218. /**
  219. * Update existing edges, or create them when not yet existing
  220. * @param {Number[] | String[]} ids
  221. * @private
  222. */
  223. update(ids) {
  224. var edges = this.body.edges;
  225. var edgesData = this.body.data.edges;
  226. var dataChanged = false;
  227. for (var i = 0; i < ids.length; i++) {
  228. var id = ids[i];
  229. var data = edgesData.get(id);
  230. var edge = edges[id];
  231. if (edge === null) {
  232. // update edge
  233. edge.disconnect();
  234. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  235. edge.connect();
  236. }
  237. else {
  238. // create edge
  239. this.body.edges[id] = this.create(data);
  240. dataChanged = true;
  241. }
  242. }
  243. if (dataChanged === true) {
  244. this.body.emitter.emit("_dataChanged");
  245. }
  246. else {
  247. this.body.emitter.emit("_dataUpdated");
  248. }
  249. }
  250. /**
  251. * Remove existing edges. Non existing ids will be ignored
  252. * @param {Number[] | String[]} ids
  253. * @private
  254. */
  255. remove(ids) {
  256. var edges = this.body.edges;
  257. for (var i = 0; i < ids.length; i++) {
  258. var id = ids[i];
  259. var edge = edges[id];
  260. if (edge !== undefined) {
  261. if (edge.via != null) {
  262. delete this.body.supportNodes[edge.via.id];
  263. }
  264. edge.disconnect();
  265. delete edges[id];
  266. }
  267. }
  268. this.body.emitter.emit("_dataChanged");
  269. }
  270. create(properties) {
  271. return new Edge(properties, this.body, this.options)
  272. }
  273. markAllEdgesAsDirty() {
  274. for (var edgeId in this.body.edges) {
  275. this.body.edges[edgeId].colorDirty = true;
  276. }
  277. }
  278. /**
  279. * Reconnect all edges
  280. * @private
  281. */
  282. reconnectEdges() {
  283. var id;
  284. var nodes = this.body.nodes;
  285. var edges = this.body.edges;
  286. for (id in nodes) {
  287. if (nodes.hasOwnProperty(id)) {
  288. nodes[id].edges = [];
  289. }
  290. }
  291. for (id in edges) {
  292. if (edges.hasOwnProperty(id)) {
  293. var edge = edges[id];
  294. edge.from = null;
  295. edge.to = null;
  296. edge.connect();
  297. }
  298. }
  299. }
  300. }
  301. export default EdgesHandler;