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.

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