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.

386 lines
11 KiB

  1. var util = require('../../../util');
  2. import Label from './unified/label'
  3. import Box from './nodes/box'
  4. import Circle from './nodes/circle'
  5. import CircularImage from './nodes/circularImage'
  6. import Database from './nodes/database'
  7. import Dot from './nodes/dot'
  8. import Ellipse from './nodes/ellipse'
  9. import Icon from './nodes/icon'
  10. import Image from './nodes/image'
  11. import Square from './nodes/square'
  12. import Star from './nodes/star'
  13. import Text from './nodes/text'
  14. import Triangle from './nodes/triangle'
  15. import TriangleDown from './nodes/triangleDown'
  16. /**
  17. * @class Node
  18. * A node. A node can be connected to other nodes via one or multiple edges.
  19. * @param {object} options An object containing options for the node. All
  20. * options are optional, except for the id.
  21. * {number} id Id of the node. Required
  22. * {string} label Text label for the node
  23. * {number} x Horizontal position of the node
  24. * {number} y Vertical position of the node
  25. * {string} shape Node shape, available:
  26. * "database", "circle", "ellipse",
  27. * "box", "image", "text", "dot",
  28. * "star", "triangle", "triangleDown",
  29. * "square", "icon"
  30. * {string} image An image url
  31. * {string} title An title text, can be HTML
  32. * {anytype} group A group name or number
  33. * @param {Network.Images} imagelist A list with images. Only needed
  34. * when the node has an image
  35. * @param {Network.Groups} grouplist A list with groups. Needed for
  36. * retrieving group options
  37. * @param {Object} constants An object with default values for
  38. * example for the color
  39. *
  40. */
  41. class Node {
  42. constructor(options, body, imagelist, grouplist, globalOptions) {
  43. this.options = util.bridgeObject(globalOptions);
  44. this.body = body;
  45. this.selected = false;
  46. this.hover = false;
  47. this.edges = []; // all edges connected to this node
  48. // set defaults for the options
  49. this.id = undefined;
  50. this.allowedToMoveX = false;
  51. this.allowedToMoveY = false;
  52. this.xFixed = false;
  53. this.yFixed = false;
  54. this.boundingBox = {top: 0, left: 0, right: 0, bottom: 0};
  55. this.imagelist = imagelist;
  56. this.grouplist = grouplist;
  57. // physics options
  58. this.x = null;
  59. this.y = null;
  60. this.predefinedPosition = false; // used to check if initial zoomExtent should just take the range or approximate
  61. this.fixedData = {x: null, y: null};
  62. this.labelModule = new Label(this.body, this.options);
  63. this.setOptions(options);
  64. }
  65. /**
  66. * Attach a edge to the node
  67. * @param {Edge} edge
  68. */
  69. attachEdge(edge) {
  70. if (this.edges.indexOf(edge) == -1) {
  71. this.edges.push(edge);
  72. }
  73. }
  74. /**
  75. * Detach a edge from the node
  76. * @param {Edge} edge
  77. */
  78. detachEdge(edge) {
  79. var index = this.edges.indexOf(edge);
  80. if (index != -1) {
  81. this.edges.splice(index, 1);
  82. }
  83. }
  84. /**
  85. * Set or overwrite options for the node
  86. * @param {Object} options an object with options
  87. * @param {Object} constants and object with default, global options
  88. */
  89. setOptions(options) {
  90. if (!options) {
  91. return;
  92. }
  93. var fields = [
  94. 'id',
  95. 'borderWidth',
  96. 'borderWidthSelected',
  97. 'shape',
  98. 'image',
  99. 'brokenImage',
  100. 'size',
  101. 'label',
  102. 'customScalingFunction',
  103. 'icon',
  104. 'value',
  105. 'hidden',
  106. 'physics'
  107. ];
  108. util.selectiveDeepExtend(fields, this.options, options);
  109. // basic options
  110. if (options.id !== undefined) {
  111. this.id = options.id;
  112. }
  113. if (options.title !== undefined) {
  114. this.title = options.title;
  115. }
  116. if (options.x !== undefined) {
  117. this.x = options.x;
  118. this.predefinedPosition = true;
  119. }
  120. if (options.y !== undefined) {
  121. this.y = options.y;
  122. this.predefinedPosition = true;
  123. }
  124. if (options.value !== undefined) {
  125. this.value = options.value;
  126. }
  127. if (options.level !== undefined) {
  128. this.level = options.level;
  129. this.preassignedLevel = true;
  130. }
  131. if (options.triggerFunction !== undefined) {
  132. this.triggerFunction = options.triggerFunction;
  133. }
  134. if (this.id === undefined) {
  135. throw "Node must have an id";
  136. }
  137. // copy group options
  138. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  139. var groupObj = this.grouplist.get(options.group);
  140. util.deepExtend(this.options, groupObj);
  141. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  142. this.options.color = util.parseColor(this.options.color);
  143. }
  144. // individual shape options
  145. if (options.color !== undefined) {
  146. this.options.color = util.parseColor(options.color);
  147. }
  148. if (this.options.image !== undefined && this.options.image != "") {
  149. if (this.imagelist) {
  150. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  151. }
  152. else {
  153. throw "No imagelist provided";
  154. }
  155. }
  156. if (options.allowedToMoveX !== undefined) {
  157. this.xFixed = !options.allowedToMoveX;
  158. this.allowedToMoveX = options.allowedToMoveX;
  159. }
  160. else if (options.x !== undefined && this.allowedToMoveX == false) {
  161. this.xFixed = true;
  162. }
  163. if (options.allowedToMoveY !== undefined) {
  164. this.yFixed = !options.allowedToMoveY;
  165. this.allowedToMoveY = options.allowedToMoveY;
  166. }
  167. else if (options.y !== undefined && this.allowedToMoveY == false) {
  168. this.yFixed = true;
  169. }
  170. // choose draw method depending on the shape
  171. switch (this.options.shape) {
  172. case 'database':
  173. this.shape = new Database(this.options, this.body, this.labelModule);
  174. break;
  175. case 'box':
  176. this.shape = new Box(this.options, this.body, this.labelModule);
  177. break;
  178. case 'circle':
  179. this.shape = new Circle(this.options, this.body, this.labelModule);
  180. break;
  181. case 'ellipse':
  182. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  183. break;
  184. // TODO: add diamond shape
  185. case 'image':
  186. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  187. break;
  188. case 'circularImage':
  189. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  190. break;
  191. case 'text':
  192. this.shape = new Text(this.options, this.body, this.labelModule);
  193. break;
  194. case 'dot':
  195. this.shape = new Dot(this.options, this.body, this.labelModule);
  196. break;
  197. case 'square':
  198. this.shape = new Square(this.options, this.body, this.labelModule);
  199. break;
  200. case 'triangle':
  201. this.shape = new Triangle(this.options, this.body, this.labelModule);
  202. break;
  203. case 'triangleDown':
  204. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  205. break;
  206. case 'star':
  207. this.shape = new Star(this.options, this.body, this.labelModule);
  208. break;
  209. case 'icon':
  210. this.shape = new Icon(this.options, this.body, this.labelModule);
  211. break;
  212. default:
  213. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  214. break;
  215. }
  216. this.labelModule.setOptions(this.options);
  217. // reset the size of the node, this can be changed
  218. this._reset();
  219. }
  220. /**
  221. * select this node
  222. */
  223. select() {
  224. this.selected = true;
  225. this._reset();
  226. }
  227. /**
  228. * unselect this node
  229. */
  230. unselect() {
  231. this.selected = false;
  232. this._reset();
  233. }
  234. /**
  235. * Reset the calculated size of the node, forces it to recalculate its size
  236. * @private
  237. */
  238. _reset() {
  239. this.width = undefined;
  240. this.height = undefined;
  241. }
  242. /**
  243. * get the title of this node.
  244. * @return {string} title The title of the node, or undefined when no title
  245. * has been set.
  246. */
  247. getTitle() {
  248. return typeof this.title === "function" ? this.title() : this.title;
  249. }
  250. /**
  251. * Calculate the distance to the border of the Node
  252. * @param {CanvasRenderingContext2D} ctx
  253. * @param {Number} angle Angle in radians
  254. * @returns {number} distance Distance to the border in pixels
  255. */
  256. distanceToBorder(ctx, angle) {
  257. this.shape.distanceToBorder(ctx,angle);
  258. }
  259. /**
  260. * Check if this node has a fixed x and y position
  261. * @return {boolean} true if fixed, false if not
  262. */
  263. isFixed() {
  264. return (this.xFixed && this.yFixed);
  265. }
  266. /**
  267. * check if this node is selecte
  268. * @return {boolean} selected True if node is selected, else false
  269. */
  270. isSelected() {
  271. return this.selected;
  272. }
  273. /**
  274. * Retrieve the value of the node. Can be undefined
  275. * @return {Number} value
  276. */
  277. getValue() {
  278. return this.value;
  279. }
  280. /**
  281. * Adjust the value range of the node. The node will adjust it's size
  282. * based on its value.
  283. * @param {Number} min
  284. * @param {Number} max
  285. */
  286. setValueRange(min, max, total) {
  287. if (this.value !== undefined) {
  288. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  289. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  290. if (this.options.scaling.label.enabled == true) {
  291. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  292. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  293. }
  294. this.options.size = this.options.scaling.min + scale * sizeDiff;
  295. }
  296. }
  297. /**
  298. * Draw this node in the given canvas
  299. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  300. * @param {CanvasRenderingContext2D} ctx
  301. */
  302. draw(ctx) {
  303. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  304. }
  305. /**
  306. * Recalculate the size of this node in the given canvas
  307. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  308. * @param {CanvasRenderingContext2D} ctx
  309. */
  310. resize(ctx) {
  311. this.shape.resize(ctx);
  312. }
  313. /**
  314. * Check if this object is overlapping with the provided object
  315. * @param {Object} obj an object with parameters left, top, right, bottom
  316. * @return {boolean} True if location is located on node
  317. */
  318. isOverlappingWith(obj) {
  319. return (
  320. this.shape.left < obj.right &&
  321. this.shape.left + this.shape.width > obj.left &&
  322. this.shape.top < obj.bottom &&
  323. this.shape.top + this.shape.height > obj.top
  324. );
  325. }
  326. }
  327. export default Node;