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.

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