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.

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