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.

374 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 Dot from './nodes/shapes/dot'
  8. import Ellipse from './nodes/shapes/ellipse'
  9. import Icon from './nodes/shapes/icon'
  10. import Image from './nodes/shapes/image'
  11. import Square from './nodes/shapes/square'
  12. import Star from './nodes/shapes/star'
  13. import Text from './nodes/shapes/text'
  14. import Triangle from './nodes/shapes/triangle'
  15. import TriangleDown from './nodes/shapes/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.edges = []; // all edges connected to this node
  46. // set defaults for the options
  47. this.id = undefined;
  48. this.imagelist = imagelist;
  49. this.grouplist = grouplist;
  50. // state options
  51. this.x = null;
  52. this.y = null;
  53. this.predefinedPosition = false; // used to check if initial zoomExtent should just take the range or approximate
  54. this.selected = false;
  55. this.hover = false;
  56. this.labelModule = new Label(this.body, this.options);
  57. this.setOptions(options);
  58. }
  59. /**
  60. * Attach a edge to the node
  61. * @param {Edge} edge
  62. */
  63. attachEdge(edge) {
  64. if (this.edges.indexOf(edge) == -1) {
  65. this.edges.push(edge);
  66. }
  67. }
  68. /**
  69. * Detach a edge from the node
  70. * @param {Edge} edge
  71. */
  72. detachEdge(edge) {
  73. var index = this.edges.indexOf(edge);
  74. if (index != -1) {
  75. this.edges.splice(index, 1);
  76. }
  77. }
  78. /**
  79. * Enable or disable the physics.
  80. * @param status
  81. */
  82. togglePhysics(status) {
  83. this.options.physics = status;
  84. }
  85. /**
  86. * Set or overwrite options for the node
  87. * @param {Object} options an object with options
  88. * @param {Object} constants and object with default, global options
  89. */
  90. setOptions(options) {
  91. if (!options) {
  92. return;
  93. }
  94. var fields = [
  95. 'borderWidth',
  96. 'borderWidthSelected',
  97. 'brokenImage',
  98. 'customScalingFunction',
  99. 'font',
  100. 'hidden',
  101. 'icon',
  102. 'id',
  103. 'image',
  104. 'label',
  105. 'physics',
  106. 'shape',
  107. 'size',
  108. 'value'
  109. ];
  110. util.selectiveDeepExtend(fields, this.options, options);
  111. // basic options
  112. if (options.id !== undefined) {
  113. this.id = options.id;
  114. }
  115. if (options.x !== undefined) {
  116. this.x = options.x;
  117. this.predefinedPosition = true;
  118. }
  119. if (options.y !== undefined) {
  120. this.y = options.y;
  121. this.predefinedPosition = true;
  122. }
  123. if (options.value !== undefined) {
  124. this.value = options.value;
  125. }
  126. if (options.level !== undefined) {
  127. this.level = options.level;
  128. this.preassignedLevel = true;
  129. }
  130. if (this.id === undefined) {
  131. throw "Node must have an id";
  132. }
  133. // copy group options
  134. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  135. var groupObj = this.grouplist.get(options.group);
  136. util.deepExtend(this.options, groupObj);
  137. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  138. this.options.color = util.parseColor(this.options.color);
  139. }
  140. // individual shape options
  141. if (options.color !== undefined) {
  142. this.options.color = util.parseColor(options.color);
  143. }
  144. if (this.options.image !== undefined && this.options.image != "") {
  145. if (this.imagelist) {
  146. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  147. }
  148. else {
  149. throw "No imagelist provided";
  150. }
  151. }
  152. if (options.fixed !== undefined) {
  153. if (typeof options.fixed == 'boolean') {
  154. this.options.fixed.x = true;
  155. this.options.fixed.y = true;
  156. }
  157. else {
  158. if (options.fixed.x !== undefined && typeof options.fixed.x == 'boolean') {
  159. this.options.fixed.x = options.fixed.x;
  160. }
  161. if (options.fixed.y !== undefined && typeof options.fixed.y == 'boolean') {
  162. this.options.fixed.y = options.fixed.y;
  163. }
  164. }
  165. }
  166. // choose draw method depending on the shape
  167. switch (this.options.shape) {
  168. case 'database':
  169. this.shape = new Database(this.options, this.body, this.labelModule);
  170. break;
  171. case 'box':
  172. this.shape = new Box(this.options, this.body, this.labelModule);
  173. break;
  174. case 'circle':
  175. this.shape = new Circle(this.options, this.body, this.labelModule);
  176. break;
  177. case 'ellipse':
  178. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  179. break;
  180. // TODO: add diamond shape
  181. case 'image':
  182. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  183. break;
  184. case 'circularImage':
  185. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  186. break;
  187. case 'text':
  188. this.shape = new Text(this.options, this.body, this.labelModule);
  189. break;
  190. case 'dot':
  191. this.shape = new Dot(this.options, this.body, this.labelModule);
  192. break;
  193. case 'square':
  194. this.shape = new Square(this.options, this.body, this.labelModule);
  195. break;
  196. case 'triangle':
  197. this.shape = new Triangle(this.options, this.body, this.labelModule);
  198. break;
  199. case 'triangleDown':
  200. this.shape = new TriangleDown(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 'icon':
  206. this.shape = new Icon(this.options, this.body, this.labelModule);
  207. break;
  208. default:
  209. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  210. break;
  211. }
  212. this.labelModule.setOptions(this.options, options);
  213. // reset the size of the node, this can be changed
  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.title === "function" ? this.title() : this.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;