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.

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