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.

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