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.

451 lines
14 KiB

9 years ago
9 years ago
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. import Validator from "../../../shared/Validator";
  18. import {printStyle} from "../../../shared/Validator";
  19. /**
  20. * @class Node
  21. * A node. A node can be connected to other nodes via one or multiple edges.
  22. * @param {object} options An object containing options for the node. All
  23. * options are optional, except for the id.
  24. * {number} id Id of the node. Required
  25. * {string} label Text label for the node
  26. * {number} x Horizontal position of the node
  27. * {number} y Vertical position of the node
  28. * {string} shape Node shape, available:
  29. * "database", "circle", "ellipse",
  30. * "box", "image", "text", "dot",
  31. * "star", "triangle", "triangleDown",
  32. * "square", "icon"
  33. * {string} image An image url
  34. * {string} title An title text, can be HTML
  35. * {anytype} group A group name or number
  36. * @param {Network.Images} imagelist A list with images. Only needed
  37. * when the node has an image
  38. * @param {Network.Groups} grouplist A list with groups. Needed for
  39. * retrieving group options
  40. * @param {Object} constants An object with default values for
  41. * example for the color
  42. *
  43. */
  44. class Node {
  45. constructor(options, body, imagelist, grouplist, globalOptions) {
  46. this.options = util.bridgeObject(globalOptions);
  47. this.body = body;
  48. this.edges = []; // all edges connected to this node
  49. // set defaults for the options
  50. this.id = undefined;
  51. this.imagelist = imagelist;
  52. this.grouplist = grouplist;
  53. // state options
  54. this.x = undefined;
  55. this.y = undefined;
  56. this.baseSize = this.options.size;
  57. this.baseFontSize = this.options.font.size;
  58. this.predefinedPosition = false; // used to check if initial fit should just take the range or approximate
  59. this.selected = false;
  60. this.hover = false;
  61. this.labelModule = new Label(this.body, this.options);
  62. this.setOptions(options);
  63. }
  64. /**
  65. * Attach a edge to the node
  66. * @param {Edge} edge
  67. */
  68. attachEdge(edge) {
  69. if (this.edges.indexOf(edge) === -1) {
  70. this.edges.push(edge);
  71. }
  72. }
  73. /**
  74. * Detach a edge from the node
  75. * @param {Edge} edge
  76. */
  77. detachEdge(edge) {
  78. var index = this.edges.indexOf(edge);
  79. if (index != -1) {
  80. this.edges.splice(index, 1);
  81. }
  82. }
  83. /**
  84. * Set or overwrite options for the node
  85. * @param {Object} options an object with options
  86. * @param {Object} constants and object with default, global options
  87. */
  88. setOptions(options) {
  89. let currentShape = this.options.shape;
  90. if (!options) {
  91. return;
  92. }
  93. // basic options
  94. if (options.id !== undefined) {this.id = options.id;}
  95. if (this.id === undefined) {
  96. throw "Node must have an id";
  97. }
  98. // set these options locally
  99. // clear x and y positions
  100. if (options.x !== undefined) {
  101. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  102. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  103. }
  104. if (options.y !== undefined) {
  105. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  106. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  107. }
  108. if (options.size !== undefined) {this.baseSize = options.size;}
  109. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  110. // copy group options
  111. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  112. var groupObj = this.grouplist.get(options.group);
  113. util.deepExtend(this.options, groupObj);
  114. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  115. this.options.color = util.parseColor(this.options.color);
  116. }
  117. // this transforms all shorthands into fully defined options
  118. Node.parseOptions(this.options, options, true);
  119. // load the images
  120. if (this.options.image !== undefined) {
  121. if (this.imagelist) {
  122. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  123. }
  124. else {
  125. throw "No imagelist provided";
  126. }
  127. }
  128. this.updateShape(currentShape);
  129. this.updateLabelModule();
  130. if (options.hidden !== undefined || options.physics !== undefined) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  137. * Static so it can also be used by the handler.
  138. * @param parentOptions
  139. * @param newOptions
  140. */
  141. static parseOptions(parentOptions, newOptions, allowDeletion = false) {
  142. var fields = [
  143. 'color',
  144. 'font',
  145. 'fixed',
  146. 'shadow'
  147. ];
  148. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  149. // merge the shadow options into the parent.
  150. util.mergeOptions(parentOptions, newOptions, 'shadow');
  151. // individual shape newOptions
  152. if (newOptions.color !== undefined && newOptions.color !== null) {
  153. let parsedColor = util.parseColor(newOptions.color);
  154. util.fillIfDefined(parentOptions.color, parsedColor);
  155. }
  156. else if (allowDeletion === true && newOptions.color === null) {
  157. parentOptions.color = undefined;
  158. delete parentOptions.color;
  159. }
  160. // handle the fixed options
  161. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  162. if (typeof newOptions.fixed === 'boolean') {
  163. parentOptions.fixed.x = newOptions.fixed;
  164. parentOptions.fixed.y = newOptions.fixed;
  165. }
  166. else {
  167. if (newOptions.fixed.x !== undefined && typeof newOptions.fixed.x === 'boolean') {
  168. parentOptions.fixed.x = newOptions.fixed.x;
  169. }
  170. if (newOptions.fixed.y !== undefined && typeof newOptions.fixed.y === 'boolean') {
  171. parentOptions.fixed.y = newOptions.fixed.y;
  172. }
  173. }
  174. }
  175. // handle the font options
  176. if (newOptions.font !== undefined) {
  177. Label.parseOptions(parentOptions.font, newOptions);
  178. }
  179. // handle the scaling options, specifically the label part
  180. if (newOptions.scaling !== undefined) {
  181. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label');
  182. }
  183. }
  184. updateLabelModule() {
  185. if (this.options.label === undefined || this.options.label === null) {
  186. this.options.label = '';
  187. }
  188. this.labelModule.setOptions(this.options, true);
  189. if (this.labelModule.baseSize !== undefined) {
  190. this.baseFontSize = this.labelModule.baseSize;
  191. }
  192. }
  193. updateShape(currentShape) {
  194. if (currentShape === this.options.shape && this.shape) {
  195. this.shape.setOptions(this.options);
  196. }
  197. else {
  198. // clean up the shape if it is already made so the new shape can start clean.
  199. if (this.shape) {
  200. this.shape.cleanup();
  201. }
  202. // choose draw method depending on the shape
  203. switch (this.options.shape) {
  204. case 'box':
  205. this.shape = new Box(this.options, this.body, this.labelModule);
  206. break;
  207. case 'circle':
  208. this.shape = new Circle(this.options, this.body, this.labelModule);
  209. break;
  210. case 'circularImage':
  211. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  212. break;
  213. case 'database':
  214. this.shape = new Database(this.options, this.body, this.labelModule);
  215. break;
  216. case 'diamond':
  217. this.shape = new Diamond(this.options, this.body, this.labelModule);
  218. break;
  219. case 'dot':
  220. this.shape = new Dot(this.options, this.body, this.labelModule);
  221. break;
  222. case 'ellipse':
  223. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  224. break;
  225. case 'icon':
  226. this.shape = new Icon(this.options, this.body, this.labelModule);
  227. break;
  228. case 'image':
  229. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  230. break;
  231. case 'square':
  232. this.shape = new Square(this.options, this.body, this.labelModule);
  233. break;
  234. case 'star':
  235. this.shape = new Star(this.options, this.body, this.labelModule);
  236. break;
  237. case 'text':
  238. this.shape = new Text(this.options, this.body, this.labelModule);
  239. break;
  240. case 'triangle':
  241. this.shape = new Triangle(this.options, this.body, this.labelModule);
  242. break;
  243. case 'triangleDown':
  244. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  245. break;
  246. default:
  247. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  248. break;
  249. }
  250. }
  251. this._reset();
  252. }
  253. /**
  254. * select this node
  255. */
  256. select() {
  257. this.selected = true;
  258. this._reset();
  259. }
  260. /**
  261. * unselect this node
  262. */
  263. unselect() {
  264. this.selected = false;
  265. this._reset();
  266. }
  267. /**
  268. * Reset the calculated size of the node, forces it to recalculate its size
  269. * @private
  270. */
  271. _reset() {
  272. this.shape.width = undefined;
  273. this.shape.height = undefined;
  274. }
  275. /**
  276. * get the title of this node.
  277. * @return {string} title The title of the node, or undefined when no title
  278. * has been set.
  279. */
  280. getTitle() {
  281. return this.options.title;
  282. }
  283. /**
  284. * Calculate the distance to the border of the Node
  285. * @param {CanvasRenderingContext2D} ctx
  286. * @param {Number} angle Angle in radians
  287. * @returns {number} distance Distance to the border in pixels
  288. */
  289. distanceToBorder(ctx, angle) {
  290. return this.shape.distanceToBorder(ctx,angle);
  291. }
  292. /**
  293. * Check if this node has a fixed x and y position
  294. * @return {boolean} true if fixed, false if not
  295. */
  296. isFixed() {
  297. return (this.options.fixed.x && this.options.fixed.y);
  298. }
  299. /**
  300. * check if this node is selecte
  301. * @return {boolean} selected True if node is selected, else false
  302. */
  303. isSelected() {
  304. return this.selected;
  305. }
  306. /**
  307. * Retrieve the value of the node. Can be undefined
  308. * @return {Number} value
  309. */
  310. getValue() {
  311. return this.options.value;
  312. }
  313. /**
  314. * Adjust the value range of the node. The node will adjust it's size
  315. * based on its value.
  316. * @param {Number} min
  317. * @param {Number} max
  318. */
  319. setValueRange(min, max, total) {
  320. if (this.options.value !== undefined) {
  321. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  322. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  323. if (this.options.scaling.label.enabled === true) {
  324. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  325. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  326. }
  327. this.options.size = this.options.scaling.min + scale * sizeDiff;
  328. }
  329. else {
  330. this.options.size = this.baseSize;
  331. this.options.font.size = this.baseFontSize;
  332. }
  333. }
  334. /**
  335. * Draw this node in the given canvas
  336. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  337. * @param {CanvasRenderingContext2D} ctx
  338. */
  339. draw(ctx) {
  340. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  341. }
  342. /**
  343. * Update the bounding box of the shape
  344. */
  345. updateBoundingBox(ctx) {
  346. this.shape.updateBoundingBox(this.x,this.y,ctx);
  347. }
  348. /**
  349. * Recalculate the size of this node in the given canvas
  350. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  351. * @param {CanvasRenderingContext2D} ctx
  352. */
  353. resize(ctx) {
  354. this.shape.resize(ctx, this.selected);
  355. }
  356. /**
  357. * Check if this object is overlapping with the provided object
  358. * @param {Object} obj an object with parameters left, top, right, bottom
  359. * @return {boolean} True if location is located on node
  360. */
  361. isOverlappingWith(obj) {
  362. return (
  363. this.shape.left < obj.right &&
  364. this.shape.left + this.shape.width > obj.left &&
  365. this.shape.top < obj.bottom &&
  366. this.shape.top + this.shape.height > obj.top
  367. );
  368. }
  369. /**
  370. * Check if this object is overlapping with the provided object
  371. * @param {Object} obj an object with parameters left, top, right, bottom
  372. * @return {boolean} True if location is located on node
  373. */
  374. isBoundingBoxOverlappingWith(obj) {
  375. return (
  376. this.shape.boundingBox.left < obj.right &&
  377. this.shape.boundingBox.right > obj.left &&
  378. this.shape.boundingBox.top < obj.bottom &&
  379. this.shape.boundingBox.bottom > obj.top
  380. );
  381. }
  382. /**
  383. * clean all required things on delete.
  384. * @returns {*}
  385. */
  386. cleanup() {
  387. return this.shape.cleanup();
  388. }
  389. }
  390. export default Node;