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.

236 lines
5.5 KiB

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