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.

381 lines
11 KiB

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