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.

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