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.

443 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.globalOptions = globalOptions;
  48. this.body = body;
  49. this.edges = []; // all edges connected to this node
  50. // set defaults for the options
  51. this.id = undefined;
  52. this.imagelist = imagelist;
  53. this.grouplist = grouplist;
  54. // state options
  55. this.x = undefined;
  56. this.y = undefined;
  57. this.baseSize = this.options.size;
  58. this.baseFontSize = this.options.font.size;
  59. this.predefinedPosition = false; // used to check if initial fit should just take the range or approximate
  60. this.selected = false;
  61. this.hover = false;
  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. * Set or overwrite options for the node
  86. * @param {Object} options an object with options
  87. * @param {Object} constants and object with default, global options
  88. */
  89. setOptions(options) {
  90. let currentShape = this.options.shape;
  91. if (!options) {
  92. return;
  93. }
  94. // basic options
  95. if (options.id !== undefined) {this.id = options.id;}
  96. if (this.id === undefined) {
  97. throw "Node must have an id";
  98. }
  99. // set these options locally
  100. // clear x and y positions
  101. if (options.x !== undefined) {
  102. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  103. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  104. }
  105. if (options.y !== undefined) {
  106. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  107. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  108. }
  109. if (options.size !== undefined) {this.baseSize = options.size;}
  110. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  111. // copy group options
  112. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  113. var groupObj = this.grouplist.get(options.group);
  114. util.deepExtend(this.options, groupObj);
  115. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  116. this.options.color = util.parseColor(this.options.color);
  117. }
  118. // this transforms all shorthands into fully defined options
  119. Node.parseOptions(this.options, options, true, this.globalOptions);
  120. // load the images
  121. if (this.options.image !== undefined) {
  122. if (this.imagelist) {
  123. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  124. }
  125. else {
  126. throw "No imagelist provided";
  127. }
  128. }
  129. this.updateLabelModule();
  130. this.updateShape(currentShape);
  131. if (options.hidden !== undefined || options.physics !== undefined) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  138. * Static so it can also be used by the handler.
  139. * @param parentOptions
  140. * @param newOptions
  141. */
  142. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  143. var fields = [
  144. 'color',
  145. 'font',
  146. 'fixed',
  147. 'shadow'
  148. ];
  149. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  150. // merge the shadow options into the parent.
  151. util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions);
  152. // individual shape newOptions
  153. if (newOptions.color !== undefined && newOptions.color !== null) {
  154. let parsedColor = util.parseColor(newOptions.color);
  155. util.fillIfDefined(parentOptions.color, parsedColor);
  156. }
  157. else if (allowDeletion === true && newOptions.color === null) {
  158. parentOptions.color = util.bridgeObject(globalOptions.color); // set the object back to the global options
  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 && newOptions.font !== null) {
  177. Label.parseOptions(parentOptions.font, newOptions);
  178. }
  179. else if (allowDeletion === true && newOptions.font === null) {
  180. parentOptions.font = util.bridgeObject(globalOptions.font); // set the object back to the global options
  181. }
  182. // handle the scaling options, specifically the label part
  183. if (newOptions.scaling !== undefined) {
  184. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label', allowDeletion, globalOptions.scaling);
  185. }
  186. }
  187. updateLabelModule() {
  188. if (this.options.label === undefined || this.options.label === null) {
  189. this.options.label = '';
  190. }
  191. this.labelModule.setOptions(this.options, true);
  192. if (this.labelModule.baseSize !== undefined) {
  193. this.baseFontSize = this.labelModule.baseSize;
  194. }
  195. }
  196. updateShape(currentShape) {
  197. if (currentShape === this.options.shape && this.shape) {
  198. this.shape.setOptions(this.options, this.imageObj);
  199. }
  200. else {
  201. // choose draw method depending on the shape
  202. switch (this.options.shape) {
  203. case 'box':
  204. this.shape = new Box(this.options, this.body, this.labelModule);
  205. break;
  206. case 'circle':
  207. this.shape = new Circle(this.options, this.body, this.labelModule);
  208. break;
  209. case 'circularImage':
  210. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  211. break;
  212. case 'database':
  213. this.shape = new Database(this.options, this.body, this.labelModule);
  214. break;
  215. case 'diamond':
  216. this.shape = new Diamond(this.options, this.body, this.labelModule);
  217. break;
  218. case 'dot':
  219. this.shape = new Dot(this.options, this.body, this.labelModule);
  220. break;
  221. case 'ellipse':
  222. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  223. break;
  224. case 'icon':
  225. this.shape = new Icon(this.options, this.body, this.labelModule);
  226. break;
  227. case 'image':
  228. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  229. break;
  230. case 'square':
  231. this.shape = new Square(this.options, this.body, this.labelModule);
  232. break;
  233. case 'star':
  234. this.shape = new Star(this.options, this.body, this.labelModule);
  235. break;
  236. case 'text':
  237. this.shape = new Text(this.options, this.body, this.labelModule);
  238. break;
  239. case 'triangle':
  240. this.shape = new Triangle(this.options, this.body, this.labelModule);
  241. break;
  242. case 'triangleDown':
  243. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  244. break;
  245. default:
  246. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  247. break;
  248. }
  249. }
  250. this._reset();
  251. }
  252. /**
  253. * select this node
  254. */
  255. select() {
  256. this.selected = true;
  257. this._reset();
  258. }
  259. /**
  260. * unselect this node
  261. */
  262. unselect() {
  263. this.selected = false;
  264. this._reset();
  265. }
  266. /**
  267. * Reset the calculated size of the node, forces it to recalculate its size
  268. * @private
  269. */
  270. _reset() {
  271. this.shape.width = undefined;
  272. this.shape.height = undefined;
  273. }
  274. /**
  275. * get the title of this node.
  276. * @return {string} title The title of the node, or undefined when no title
  277. * has been set.
  278. */
  279. getTitle() {
  280. return this.options.title;
  281. }
  282. /**
  283. * Calculate the distance to the border of the Node
  284. * @param {CanvasRenderingContext2D} ctx
  285. * @param {Number} angle Angle in radians
  286. * @returns {number} distance Distance to the border in pixels
  287. */
  288. distanceToBorder(ctx, angle) {
  289. return this.shape.distanceToBorder(ctx,angle);
  290. }
  291. /**
  292. * Check if this node has a fixed x and y position
  293. * @return {boolean} true if fixed, false if not
  294. */
  295. isFixed() {
  296. return (this.options.fixed.x && this.options.fixed.y);
  297. }
  298. /**
  299. * check if this node is selecte
  300. * @return {boolean} selected True if node is selected, else false
  301. */
  302. isSelected() {
  303. return this.selected;
  304. }
  305. /**
  306. * Retrieve the value of the node. Can be undefined
  307. * @return {Number} value
  308. */
  309. getValue() {
  310. return this.options.value;
  311. }
  312. /**
  313. * Adjust the value range of the node. The node will adjust it's size
  314. * based on its value.
  315. * @param {Number} min
  316. * @param {Number} max
  317. */
  318. setValueRange(min, max, total) {
  319. if (this.options.value !== undefined) {
  320. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  321. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  322. if (this.options.scaling.label.enabled === true) {
  323. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  324. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  325. }
  326. this.options.size = this.options.scaling.min + scale * sizeDiff;
  327. }
  328. else {
  329. this.options.size = this.baseSize;
  330. this.options.font.size = this.baseFontSize;
  331. }
  332. this.updateLabelModule();
  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. export default Node;