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.

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