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.

377 lines
11 KiB

9 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. 'title',
  111. 'value',
  112. 'x',
  113. 'y'
  114. ];
  115. util.selectiveDeepExtend(fields, this.options, options);
  116. // basic options
  117. if (options.id !== undefined) {this.id = options.id;}
  118. if (this.id === undefined) {
  119. throw "Node must have an id";
  120. }
  121. if (options.x !== undefined) {this.x = options.x; this.predefinedPosition = true;}
  122. if (options.y !== undefined) {this.y = options.y; this.predefinedPosition = true;}
  123. if (options.value !== undefined) {this.value = options.value;}
  124. // copy group options
  125. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  126. var groupObj = this.grouplist.get(options.group);
  127. util.deepExtend(this.options, groupObj);
  128. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  129. this.options.color = util.parseColor(this.options.color);
  130. }
  131. // individual shape options
  132. if (options.color !== undefined) {
  133. this.options.color = util.parseColor(options.color);
  134. }
  135. if (this.options.image !== undefined && this.options.image != "") {
  136. if (this.imagelist) {
  137. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  138. }
  139. else {
  140. throw "No imagelist provided";
  141. }
  142. }
  143. if (options.fixed !== undefined) {
  144. if (typeof options.fixed == 'boolean') {
  145. this.options.fixed.x = true;
  146. this.options.fixed.y = true;
  147. }
  148. else {
  149. if (options.fixed.x !== undefined && typeof options.fixed.x == 'boolean') {
  150. this.options.fixed.x = options.fixed.x;
  151. }
  152. if (options.fixed.y !== undefined && typeof options.fixed.y == 'boolean') {
  153. this.options.fixed.y = options.fixed.y;
  154. }
  155. }
  156. }
  157. this.updateShape();
  158. this.updateLabelModule();
  159. // reset the size of the node, this can be changed
  160. this._reset();
  161. }
  162. updateLabelModule() {
  163. this.labelModule.setOptions(this.options);
  164. }
  165. updateShape() {
  166. // choose draw method depending on the shape
  167. switch (this.options.shape) {
  168. case 'box':
  169. this.shape = new Box(this.options, this.body, this.labelModule);
  170. break;
  171. case 'circle':
  172. this.shape = new Circle(this.options, this.body, this.labelModule);
  173. break;
  174. case 'circularImage':
  175. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  176. break;
  177. case 'database':
  178. this.shape = new Database(this.options, this.body, this.labelModule);
  179. break;
  180. case 'diamond':
  181. this.shape = new Diamond(this.options, this.body, this.labelModule);
  182. break;
  183. case 'dot':
  184. this.shape = new Dot(this.options, this.body, this.labelModule);
  185. break;
  186. case 'ellipse':
  187. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  188. break;
  189. case 'icon':
  190. this.shape = new Icon(this.options, this.body, this.labelModule);
  191. break;
  192. case 'image':
  193. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  194. break;
  195. case 'square':
  196. this.shape = new Square(this.options, this.body, this.labelModule);
  197. break;
  198. case 'star':
  199. this.shape = new Star(this.options, this.body, this.labelModule);
  200. break;
  201. case 'text':
  202. this.shape = new Text(this.options, this.body, this.labelModule);
  203. break;
  204. case 'triangle':
  205. this.shape = new Triangle(this.options, this.body, this.labelModule);
  206. break;
  207. case 'triangleDown':
  208. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  209. break;
  210. default:
  211. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  212. break;
  213. }
  214. this._reset();
  215. }
  216. /**
  217. * select this node
  218. */
  219. select() {
  220. this.selected = true;
  221. this._reset();
  222. }
  223. /**
  224. * unselect this node
  225. */
  226. unselect() {
  227. this.selected = false;
  228. this._reset();
  229. }
  230. /**
  231. * Reset the calculated size of the node, forces it to recalculate its size
  232. * @private
  233. */
  234. _reset() {
  235. this.shape.width = undefined;
  236. this.shape.height = undefined;
  237. }
  238. /**
  239. * get the title of this node.
  240. * @return {string} title The title of the node, or undefined when no title
  241. * has been set.
  242. */
  243. getTitle() {
  244. return typeof this.options.title === "function" ? this.options.title() : this.options.title;
  245. }
  246. /**
  247. * Calculate the distance to the border of the Node
  248. * @param {CanvasRenderingContext2D} ctx
  249. * @param {Number} angle Angle in radians
  250. * @returns {number} distance Distance to the border in pixels
  251. */
  252. distanceToBorder(ctx, angle) {
  253. return this.shape.distanceToBorder(ctx,angle);
  254. }
  255. /**
  256. * Check if this node has a fixed x and y position
  257. * @return {boolean} true if fixed, false if not
  258. */
  259. isFixed() {
  260. return (this.options.fixed.x && this.options.fixed.y);
  261. }
  262. /**
  263. * check if this node is selecte
  264. * @return {boolean} selected True if node is selected, else false
  265. */
  266. isSelected() {
  267. return this.selected;
  268. }
  269. /**
  270. * Retrieve the value of the node. Can be undefined
  271. * @return {Number} value
  272. */
  273. getValue() {
  274. return this.value;
  275. }
  276. /**
  277. * Adjust the value range of the node. The node will adjust it's size
  278. * based on its value.
  279. * @param {Number} min
  280. * @param {Number} max
  281. */
  282. setValueRange(min, max, total) {
  283. if (this.value !== undefined) {
  284. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  285. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  286. if (this.options.scaling.label.enabled == true) {
  287. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  288. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  289. }
  290. this.options.size = this.options.scaling.min + scale * sizeDiff;
  291. }
  292. }
  293. /**
  294. * Draw this node in the given canvas
  295. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  296. * @param {CanvasRenderingContext2D} ctx
  297. */
  298. draw(ctx) {
  299. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  300. }
  301. /**
  302. * Recalculate the size of this node in the given canvas
  303. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  304. * @param {CanvasRenderingContext2D} ctx
  305. */
  306. resize(ctx) {
  307. this.shape.resize(ctx);
  308. }
  309. /**
  310. * Check if this object is overlapping with the provided object
  311. * @param {Object} obj an object with parameters left, top, right, bottom
  312. * @return {boolean} True if location is located on node
  313. */
  314. isOverlappingWith(obj) {
  315. return (
  316. this.shape.left < obj.right &&
  317. this.shape.left + this.shape.width > obj.left &&
  318. this.shape.top < obj.bottom &&
  319. this.shape.top + this.shape.height > obj.top
  320. );
  321. }
  322. }
  323. export default Node;