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.

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