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.

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