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.

391 lines
11 KiB

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