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.

434 lines
13 KiB

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