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.

371 lines
9.6 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: '#ffffff',
  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. // update smooth settings
  155. let dataChanged = false;
  156. if (options.smooth !== undefined) {
  157. for (let nodeId in this.body.edges) {
  158. if (this.body.edges.hasOwnProperty(nodeId)) {
  159. dataChanged = this.body.edges[nodeId].updateEdgeType() || dataChanged;
  160. }
  161. }
  162. }
  163. // update fonts
  164. if (options.font) {
  165. for (let nodeId in this.body.edges) {
  166. if (this.body.edges.hasOwnProperty(nodeId)) {
  167. this.body.edges[nodeId].updateLabelModule();
  168. }
  169. }
  170. }
  171. // update the state of the variables if needed
  172. if (options.hidden !== undefined || options.physics !== undefined || dataChanged === true) {
  173. this.body.emitter.emit('_dataChanged');
  174. }
  175. }
  176. }
  177. /**
  178. * Load edges by reading the data table
  179. * @param {Array | DataSet | DataView} edges The data containing the edges.
  180. * @private
  181. * @private
  182. */
  183. setData(edges, doNotEmit = false) {
  184. var oldEdgesData = this.body.data.edges;
  185. if (edges instanceof DataSet || edges instanceof DataView) {
  186. this.body.data.edges = edges;
  187. }
  188. else if (Array.isArray(edges)) {
  189. this.body.data.edges = new DataSet();
  190. this.body.data.edges.add(edges);
  191. }
  192. else if (!edges) {
  193. this.body.data.edges = new DataSet();
  194. }
  195. else {
  196. throw new TypeError('Array or DataSet expected');
  197. }
  198. // TODO: is this null or undefined or false?
  199. if (oldEdgesData) {
  200. // unsubscribe from old dataset
  201. util.forEach(this.edgesListeners, (callback, event) => {oldEdgesData.off(event, callback);});
  202. }
  203. // remove drawn edges
  204. this.body.edges = {};
  205. // TODO: is this null or undefined or false?
  206. if (this.body.data.edges) {
  207. // subscribe to new dataset
  208. util.forEach(this.edgesListeners, (callback, event) => {this.body.data.edges.on(event, callback);});
  209. // draw all new nodes
  210. var ids = this.body.data.edges.getIds();
  211. this.add(ids, true);
  212. }
  213. if (doNotEmit === false) {
  214. this.body.emitter.emit("_dataChanged");
  215. }
  216. }
  217. /**
  218. * Add edges
  219. * @param {Number[] | String[]} ids
  220. * @private
  221. */
  222. add(ids, doNotEmit = false) {
  223. var edges = this.body.edges;
  224. var edgesData = this.body.data.edges;
  225. for (let i = 0; i < ids.length; i++) {
  226. var id = ids[i];
  227. var oldEdge = edges[id];
  228. if (oldEdge) {
  229. oldEdge.disconnect();
  230. }
  231. var data = edgesData.get(id, {"showInternalIds" : true});
  232. edges[id] = this.create(data);
  233. }
  234. if (doNotEmit === false) {
  235. this.body.emitter.emit("_dataChanged");
  236. }
  237. }
  238. /**
  239. * Update existing edges, or create them when not yet existing
  240. * @param {Number[] | String[]} ids
  241. * @private
  242. */
  243. update(ids) {
  244. var edges = this.body.edges;
  245. var edgesData = this.body.data.edges;
  246. var dataChanged = false;
  247. for (var i = 0; i < ids.length; i++) {
  248. var id = ids[i];
  249. var data = edgesData.get(id);
  250. var edge = edges[id];
  251. if (edge === null) {
  252. // update edge
  253. edge.disconnect();
  254. dataChanged = edge.setOptions(data) || dataChanged; // if a support node is added, data can be changed.
  255. edge.connect();
  256. }
  257. else {
  258. // create edge
  259. this.body.edges[id] = this.create(data);
  260. dataChanged = true;
  261. }
  262. }
  263. if (dataChanged === true) {
  264. this.body.emitter.emit("_dataChanged");
  265. }
  266. else {
  267. this.body.emitter.emit("_dataUpdated");
  268. }
  269. }
  270. /**
  271. * Remove existing edges. Non existing ids will be ignored
  272. * @param {Number[] | String[]} ids
  273. * @private
  274. */
  275. remove(ids) {
  276. var edges = this.body.edges;
  277. for (var i = 0; i < ids.length; i++) {
  278. var id = ids[i];
  279. var edge = edges[id];
  280. if (edge !== undefined) {
  281. if (edge.via != null) {
  282. delete this.body.supportNodes[edge.via.id];
  283. }
  284. edge.disconnect();
  285. delete edges[id];
  286. }
  287. }
  288. this.body.emitter.emit("_dataChanged");
  289. }
  290. create(properties) {
  291. return new Edge(properties, this.body, this.options)
  292. }
  293. markAllEdgesAsDirty() {
  294. for (var edgeId in this.body.edges) {
  295. this.body.edges[edgeId].colorDirty = true;
  296. }
  297. }
  298. /**
  299. * Reconnect all edges
  300. * @private
  301. */
  302. reconnectEdges() {
  303. var id;
  304. var nodes = this.body.nodes;
  305. var edges = this.body.edges;
  306. for (id in nodes) {
  307. if (nodes.hasOwnProperty(id)) {
  308. nodes[id].edges = [];
  309. }
  310. }
  311. for (id in edges) {
  312. if (edges.hasOwnProperty(id)) {
  313. var edge = edges[id];
  314. edge.from = null;
  315. edge.to = null;
  316. edge.connect();
  317. }
  318. }
  319. }
  320. }
  321. export default EdgesHandler;