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.

268 lines
6.6 KiB

9 years ago
  1. var util = require("../../util");
  2. var DataSet = require('../../DataSet');
  3. var DataView = require('../../DataView');
  4. import Node from "./components/Node";
  5. import Label from "./components/unified/Label";
  6. class NodesHandler {
  7. constructor(body, images, groups, layoutEngine) {
  8. this.body = body;
  9. this.images = images;
  10. this.groups = groups;
  11. this.layoutEngine = layoutEngine;
  12. // create the node API in the body container
  13. this.body.functions.createNode = this.create.bind(this);
  14. this.nodesListeners = {
  15. 'add': (event, params) => {this.add(params.items);},
  16. 'update': (event, params) => {this.update(params.items, params.data);},
  17. 'remove': (event, params) => {this.remove(params.items);}
  18. };
  19. this.options = {};
  20. this.defaultOptions = {
  21. borderWidth: 1,
  22. borderWidthSelected: undefined,
  23. brokenImage:undefined,
  24. color: {
  25. border: '#2B7CE9',
  26. background: '#97C2FC',
  27. highlight: {
  28. border: '#2B7CE9',
  29. background: '#D2E5FF'
  30. },
  31. hover: {
  32. border: '#2B7CE9',
  33. background: '#D2E5FF'
  34. }
  35. },
  36. fixed: {
  37. x:false,
  38. y:false
  39. },
  40. font: {
  41. color: '#343434',
  42. size: 14, // px
  43. face: 'arial',
  44. background: 'none',
  45. stroke: 0, // px
  46. strokeColor: '#ffffff',
  47. align: 'horizontal'
  48. },
  49. group: undefined,
  50. hidden: false,
  51. icon: {
  52. face: 'FontAwesome', //'FontAwesome',
  53. code: undefined, //'\uf007',
  54. size: 50, //50,
  55. color:'#2B7CE9' //'#aa00ff'
  56. },
  57. image: undefined, // --> URL
  58. label: undefined,
  59. level: undefined,
  60. mass: 1,
  61. physics: true,
  62. scaling: {
  63. min: 10,
  64. max: 30,
  65. label: {
  66. enabled: false,
  67. min: 14,
  68. max: 30,
  69. maxVisible: 30,
  70. drawThreshold: 3
  71. },
  72. customScalingFunction: function (min,max,total,value) {
  73. if (max === min) {
  74. return 0.5;
  75. }
  76. else {
  77. var scale = 1 / (max - min);
  78. return Math.max(0,(value - min)*scale);
  79. }
  80. }
  81. },
  82. shape: 'ellipse',
  83. size: 25,
  84. title: undefined,
  85. value: undefined,
  86. x: undefined,
  87. y: undefined
  88. };
  89. util.extend(this.options, this.defaultOptions);
  90. }
  91. setOptions(options) {
  92. if (options !== undefined) {
  93. Node.parseOptions(this.options, options);
  94. // update the shape in all nodes
  95. if (options.shape !== undefined) {
  96. for (let nodeId in this.body.nodes) {
  97. if (this.body.nodes.hasOwnProperty(nodeId)) {
  98. this.body.nodes[nodeId].updateShape();
  99. }
  100. }
  101. }
  102. // update the shape size in all nodes
  103. if (options.font !== undefined) {
  104. Label.parseOptions(this.options.font,options);
  105. for (let nodeId in this.body.nodes) {
  106. if (this.body.nodes.hasOwnProperty(nodeId)) {
  107. this.body.nodes[nodeId].updateLabelModule();
  108. this.body.nodes[nodeId]._reset();
  109. }
  110. }
  111. }
  112. // update the shape size in all nodes
  113. if (options.size !== undefined) {
  114. for (let nodeId in this.body.nodes) {
  115. if (this.body.nodes.hasOwnProperty(nodeId)) {
  116. this.body.nodes[nodeId]._reset();
  117. }
  118. }
  119. }
  120. // update the state of the variables if needed
  121. if (options.hidden !== undefined || options.physics !== undefined) {
  122. this.body.emitter.emit('_dataChanged');
  123. }
  124. }
  125. }
  126. /**
  127. * Set a data set with nodes for the network
  128. * @param {Array | DataSet | DataView} nodes The data containing the nodes.
  129. * @private
  130. */
  131. setData(nodes, doNotEmit = false) {
  132. var oldNodesData = this.body.data.nodes;
  133. if (nodes instanceof DataSet || nodes instanceof DataView) {
  134. this.body.data.nodes = nodes;
  135. }
  136. else if (Array.isArray(nodes)) {
  137. this.body.data.nodes = new DataSet();
  138. this.body.data.nodes.add(nodes);
  139. }
  140. else if (!nodes) {
  141. this.body.data.nodes = new DataSet();
  142. }
  143. else {
  144. throw new TypeError('Array or DataSet expected');
  145. }
  146. if (oldNodesData) {
  147. // unsubscribe from old dataset
  148. util.forEach(this.nodesListeners, function (callback, event) {
  149. oldNodesData.off(event, callback);
  150. });
  151. }
  152. // remove drawn nodes
  153. this.body.nodes = {};
  154. if (this.body.data.nodes) {
  155. // subscribe to new dataset
  156. var me = this;
  157. util.forEach(this.nodesListeners, function (callback, event) {
  158. me.body.data.nodes.on(event, callback);
  159. });
  160. // draw all new nodes
  161. var ids = this.body.data.nodes.getIds();
  162. this.add(ids, true);
  163. }
  164. if (doNotEmit === false) {
  165. this.body.emitter.emit("_dataChanged");
  166. }
  167. }
  168. /**
  169. * Add nodes
  170. * @param {Number[] | String[]} ids
  171. * @private
  172. */
  173. add(ids, doNotEmit = false) {
  174. var id;
  175. var newNodes = [];
  176. for (var i = 0; i < ids.length; i++) {
  177. id = ids[i];
  178. var properties = this.body.data.nodes.get(id);
  179. var node = this.create(properties);;
  180. newNodes.push(node);
  181. this.body.nodes[id] = node; // note: this may replace an existing node
  182. }
  183. this.layoutEngine.positionInitially(newNodes);
  184. if (doNotEmit === false) {
  185. this.body.emitter.emit("_dataChanged");
  186. }
  187. }
  188. /**
  189. * Update existing nodes, or create them when not yet existing
  190. * @param {Number[] | String[]} ids
  191. * @private
  192. */
  193. update(ids, changedData) {
  194. var nodes = this.body.nodes;
  195. var dataChanged = false;
  196. for (var i = 0; i < ids.length; i++) {
  197. var id = ids[i];
  198. var node = nodes[id];
  199. var data = changedData[i];
  200. if (node !== undefined) {
  201. // update node
  202. node.setOptions(data, this.constants);
  203. }
  204. else {
  205. dataChanged = true;
  206. // create node
  207. node = this.create(properties);
  208. nodes[id] = node;
  209. }
  210. }
  211. if (dataChanged === true) {
  212. this.body.emitter.emit("_dataChanged");
  213. }
  214. else {
  215. this.body.emitter.emit("_dataUpdated");
  216. }
  217. }
  218. /**
  219. * Remove existing nodes. If nodes do not exist, the method will just ignore it.
  220. * @param {Number[] | String[]} ids
  221. * @private
  222. */
  223. remove(ids) {
  224. var nodes = this.body.nodes;
  225. for (let i = 0; i < ids.length; i++) {
  226. var id = ids[i];
  227. delete nodes[id];
  228. }
  229. this.body.emitter.emit("_dataChanged");
  230. }
  231. create(properties, constructorClass = Node) {
  232. return new constructorClass(properties, this.body, this.images, this.groups, this.options)
  233. }
  234. }
  235. export default NodesHandler;