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.

376 lines
11 KiB

9 years ago
  1. var util = require('../../../util');
  2. import Label from './unified/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 Dot from './nodes/shapes/dot'
  8. import Ellipse from './nodes/shapes/ellipse'
  9. import Icon from './nodes/shapes/icon'
  10. import Image from './nodes/shapes/image'
  11. import Square from './nodes/shapes/square'
  12. import Star from './nodes/shapes/star'
  13. import Text from './nodes/shapes/text'
  14. import Triangle from './nodes/shapes/triangle'
  15. import TriangleDown from './nodes/shapes/triangleDown'
  16. /**
  17. * @class Node
  18. * A node. A node can be connected to other nodes via one or multiple edges.
  19. * @param {object} options An object containing options for the node. All
  20. * options are optional, except for the id.
  21. * {number} id Id of the node. Required
  22. * {string} label Text label for the node
  23. * {number} x Horizontal position of the node
  24. * {number} y Vertical position of the node
  25. * {string} shape Node shape, available:
  26. * "database", "circle", "ellipse",
  27. * "box", "image", "text", "dot",
  28. * "star", "triangle", "triangleDown",
  29. * "square", "icon"
  30. * {string} image An image url
  31. * {string} title An title text, can be HTML
  32. * {anytype} group A group name or number
  33. * @param {Network.Images} imagelist A list with images. Only needed
  34. * when the node has an image
  35. * @param {Network.Groups} grouplist A list with groups. Needed for
  36. * retrieving group options
  37. * @param {Object} constants An object with default values for
  38. * example for the color
  39. *
  40. */
  41. class Node {
  42. constructor(options, body, imagelist, grouplist, globalOptions) {
  43. this.options = util.bridgeObject(globalOptions);
  44. this.body = body;
  45. this.edges = []; // all edges connected to this node
  46. // set defaults for the options
  47. this.id = undefined;
  48. this.boundingBox = {top: 0, left: 0, right: 0, bottom: 0};
  49. this.imagelist = imagelist;
  50. this.grouplist = grouplist;
  51. // state options
  52. this.x = null;
  53. this.y = null;
  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. var fields = [
  96. 'borderWidth',
  97. 'borderWidthSelected',
  98. 'brokenImage',
  99. 'customScalingFunction',
  100. 'font',
  101. 'hidden',
  102. 'icon',
  103. 'id',
  104. 'image',
  105. 'label',
  106. 'physics',
  107. 'shape',
  108. 'size',
  109. 'value'
  110. ];
  111. util.selectiveDeepExtend(fields, this.options, options);
  112. // basic options
  113. if (options.id !== undefined) {
  114. this.id = options.id;
  115. }
  116. if (options.x !== undefined) {
  117. this.x = options.x;
  118. this.predefinedPosition = true;
  119. }
  120. if (options.y !== undefined) {
  121. this.y = options.y;
  122. this.predefinedPosition = true;
  123. }
  124. if (options.value !== undefined) {
  125. this.value = options.value;
  126. }
  127. if (options.level !== undefined) {
  128. this.level = options.level;
  129. this.preassignedLevel = true;
  130. }
  131. if (this.id === undefined) {
  132. throw "Node must have an id";
  133. }
  134. // copy group options
  135. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  136. var groupObj = this.grouplist.get(options.group);
  137. util.deepExtend(this.options, groupObj);
  138. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  139. this.options.color = util.parseColor(this.options.color);
  140. }
  141. // individual shape options
  142. if (options.color !== undefined) {
  143. this.options.color = util.parseColor(options.color);
  144. }
  145. if (this.options.image !== undefined && this.options.image != "") {
  146. if (this.imagelist) {
  147. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  148. }
  149. else {
  150. throw "No imagelist provided";
  151. }
  152. }
  153. if (options.fixed !== undefined) {
  154. if (typeof options.fixed == 'boolean') {
  155. this.options.fixed.x = true;
  156. this.options.fixed.y = true;
  157. }
  158. else {
  159. if (options.fixed.x !== undefined && typeof options.fixed.x == 'boolean') {
  160. this.options.fixed.x = options.fixed.x;
  161. }
  162. if (options.fixed.y !== undefined && typeof options.fixed.y == 'boolean') {
  163. this.options.fixed.y = options.fixed.y;
  164. }
  165. }
  166. }
  167. // choose draw method depending on the shape
  168. switch (this.options.shape) {
  169. case 'database':
  170. this.shape = new Database(this.options, this.body, this.labelModule);
  171. break;
  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 'ellipse':
  179. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  180. break;
  181. // TODO: add diamond shape
  182. case 'image':
  183. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  184. break;
  185. case 'circularImage':
  186. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  187. break;
  188. case 'text':
  189. this.shape = new Text(this.options, this.body, this.labelModule);
  190. break;
  191. case 'dot':
  192. this.shape = new Dot(this.options, this.body, this.labelModule);
  193. break;
  194. case 'square':
  195. this.shape = new Square(this.options, this.body, this.labelModule);
  196. break;
  197. case 'triangle':
  198. this.shape = new Triangle(this.options, this.body, this.labelModule);
  199. break;
  200. case 'triangleDown':
  201. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  202. break;
  203. case 'star':
  204. this.shape = new Star(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. default:
  210. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  211. break;
  212. }
  213. this.labelModule.setOptions(this.options, options);
  214. // reset the size of the node, this can be changed
  215. this._reset();
  216. }
  217. /**
  218. * select this node
  219. */
  220. select() {
  221. this.selected = true;
  222. this._reset();
  223. }
  224. /**
  225. * unselect this node
  226. */
  227. unselect() {
  228. this.selected = false;
  229. this._reset();
  230. }
  231. /**
  232. * Reset the calculated size of the node, forces it to recalculate its size
  233. * @private
  234. */
  235. _reset() {
  236. this.shape.width = undefined;
  237. this.shape.height = undefined;
  238. }
  239. /**
  240. * get the title of this node.
  241. * @return {string} title The title of the node, or undefined when no title
  242. * has been set.
  243. */
  244. getTitle() {
  245. return typeof this.title === "function" ? this.title() : this.title;
  246. }
  247. /**
  248. * Calculate the distance to the border of the Node
  249. * @param {CanvasRenderingContext2D} ctx
  250. * @param {Number} angle Angle in radians
  251. * @returns {number} distance Distance to the border in pixels
  252. */
  253. distanceToBorder(ctx, angle) {
  254. return this.shape.distanceToBorder(ctx,angle);
  255. }
  256. /**
  257. * Check if this node has a fixed x and y position
  258. * @return {boolean} true if fixed, false if not
  259. */
  260. isFixed() {
  261. return (this.options.fixed.x && this.options.fixed.y);
  262. }
  263. /**
  264. * check if this node is selecte
  265. * @return {boolean} selected True if node is selected, else false
  266. */
  267. isSelected() {
  268. return this.selected;
  269. }
  270. /**
  271. * Retrieve the value of the node. Can be undefined
  272. * @return {Number} value
  273. */
  274. getValue() {
  275. return this.value;
  276. }
  277. /**
  278. * Adjust the value range of the node. The node will adjust it's size
  279. * based on its value.
  280. * @param {Number} min
  281. * @param {Number} max
  282. */
  283. setValueRange(min, max, total) {
  284. if (this.value !== undefined) {
  285. var scale = this.options.scaling.customScalingFunction(min, max, total, this.value);
  286. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  287. if (this.options.scaling.label.enabled == true) {
  288. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  289. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  290. }
  291. this.options.size = this.options.scaling.min + scale * sizeDiff;
  292. }
  293. }
  294. /**
  295. * Draw this node in the given canvas
  296. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  297. * @param {CanvasRenderingContext2D} ctx
  298. */
  299. draw(ctx) {
  300. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  301. }
  302. /**
  303. * Recalculate the size of this node in the given canvas
  304. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  305. * @param {CanvasRenderingContext2D} ctx
  306. */
  307. resize(ctx) {
  308. this.shape.resize(ctx);
  309. }
  310. /**
  311. * Check if this object is overlapping with the provided object
  312. * @param {Object} obj an object with parameters left, top, right, bottom
  313. * @return {boolean} True if location is located on node
  314. */
  315. isOverlappingWith(obj) {
  316. return (
  317. this.shape.left < obj.right &&
  318. this.shape.left + this.shape.width > obj.left &&
  319. this.shape.top < obj.bottom &&
  320. this.shape.top + this.shape.height > obj.top
  321. );
  322. }
  323. }
  324. export default Node;