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.

370 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. // choose draw method depending on the shape
  158. switch (this.options.shape) {
  159. case 'box':
  160. this.shape = new Box(this.options, this.body, this.labelModule);
  161. break;
  162. case 'circle':
  163. this.shape = new Circle(this.options, this.body, this.labelModule);
  164. break;
  165. case 'circularImage':
  166. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  167. break;
  168. case 'database':
  169. this.shape = new Database(this.options, this.body, this.labelModule);
  170. break;
  171. case 'diamond':
  172. this.shape = new Diamond(this.options, this.body, this.labelModule);
  173. break;
  174. case 'dot':
  175. this.shape = new Dot(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. case 'icon':
  181. this.shape = new Icon(this.options, this.body, this.labelModule);
  182. break;
  183. case 'image':
  184. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  185. break;
  186. case 'square':
  187. this.shape = new Square(this.options, this.body, this.labelModule);
  188. break;
  189. case 'star':
  190. this.shape = new Star(this.options, this.body, this.labelModule);
  191. break;
  192. case 'text':
  193. this.shape = new Text(this.options, this.body, this.labelModule);
  194. break;
  195. case 'triangle':
  196. this.shape = new Triangle(this.options, this.body, this.labelModule);
  197. break;
  198. case 'triangleDown':
  199. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  200. break;
  201. default:
  202. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  203. break;
  204. }
  205. this.labelModule.setOptions(this.options, options);
  206. // reset the size of the node, this can be changed
  207. this._reset();
  208. }
  209. /**
  210. * select this node
  211. */
  212. select() {
  213. this.selected = true;
  214. this._reset();
  215. }
  216. /**
  217. * unselect this node
  218. */
  219. unselect() {
  220. this.selected = false;
  221. this._reset();
  222. }
  223. /**
  224. * Reset the calculated size of the node, forces it to recalculate its size
  225. * @private
  226. */
  227. _reset() {
  228. this.shape.width = undefined;
  229. this.shape.height = undefined;
  230. }
  231. /**
  232. * get the title of this node.
  233. * @return {string} title The title of the node, or undefined when no title
  234. * has been set.
  235. */
  236. getTitle() {
  237. return typeof this.options.title === "function" ? this.options.title() : this.options.title;
  238. }
  239. /**
  240. * Calculate the distance to the border of the Node
  241. * @param {CanvasRenderingContext2D} ctx
  242. * @param {Number} angle Angle in radians
  243. * @returns {number} distance Distance to the border in pixels
  244. */
  245. distanceToBorder(ctx, angle) {
  246. return this.shape.distanceToBorder(ctx,angle);
  247. }
  248. /**
  249. * Check if this node has a fixed x and y position
  250. * @return {boolean} true if fixed, false if not
  251. */
  252. isFixed() {
  253. return (this.options.fixed.x && this.options.fixed.y);
  254. }
  255. /**
  256. * check if this node is selecte
  257. * @return {boolean} selected True if node is selected, else false
  258. */
  259. isSelected() {
  260. return this.selected;
  261. }
  262. /**
  263. * Retrieve the value of the node. Can be undefined
  264. * @return {Number} value
  265. */
  266. getValue() {
  267. return this.value;
  268. }
  269. /**
  270. * Adjust the value range of the node. The node will adjust it's size
  271. * based on its value.
  272. * @param {Number} min
  273. * @param {Number} max
  274. */
  275. setValueRange(min, max, total) {
  276. if (this.value !== undefined) {
  277. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  278. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  279. if (this.options.scaling.label.enabled == true) {
  280. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  281. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  282. }
  283. this.options.size = this.options.scaling.min + scale * sizeDiff;
  284. }
  285. }
  286. /**
  287. * Draw this node in the given canvas
  288. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  289. * @param {CanvasRenderingContext2D} ctx
  290. */
  291. draw(ctx) {
  292. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  293. }
  294. /**
  295. * Recalculate the size of this node in the given canvas
  296. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  297. * @param {CanvasRenderingContext2D} ctx
  298. */
  299. resize(ctx) {
  300. this.shape.resize(ctx);
  301. }
  302. /**
  303. * Check if this object is overlapping with the provided object
  304. * @param {Object} obj an object with parameters left, top, right, bottom
  305. * @return {boolean} True if location is located on node
  306. */
  307. isOverlappingWith(obj) {
  308. return (
  309. this.shape.left < obj.right &&
  310. this.shape.left + this.shape.width > obj.left &&
  311. this.shape.top < obj.bottom &&
  312. this.shape.top + this.shape.height > obj.top
  313. );
  314. }
  315. }
  316. export default Node;