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.

446 lines
14 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. // clear x and y positions
  106. if (options.x !== undefined) {
  107. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  108. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  109. }
  110. if (options.y !== undefined) {
  111. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  112. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  113. }
  114. if (options.size !== undefined) {this.baseSize = options.size;}
  115. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  116. // copy group options
  117. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  118. var groupObj = this.grouplist.get(options.group);
  119. util.deepExtend(this.options, groupObj);
  120. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  121. this.options.color = util.parseColor(this.options.color);
  122. }
  123. // this transforms all shorthands into fully defined options
  124. Node.parseOptions(this.options, options, true);
  125. // load the images
  126. if (this.options.image !== undefined) {
  127. if (this.imagelist) {
  128. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  129. }
  130. else {
  131. throw "No imagelist provided";
  132. }
  133. }
  134. this.updateShape();
  135. this.updateLabelModule();
  136. // reset the size of the node, this can be changed
  137. this._reset();
  138. if (options.hidden !== undefined || options.physics !== undefined) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  145. * Static so it can also be used by the handler.
  146. * @param parentOptions
  147. * @param newOptions
  148. */
  149. static parseOptions(parentOptions, newOptions, allowDeletion = false) {
  150. var fields = [
  151. 'color',
  152. 'font',
  153. 'fixed',
  154. 'shadow'
  155. ];
  156. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  157. // merge the shadow options into the parent.
  158. util.mergeOptions(parentOptions, newOptions, 'shadow');
  159. // individual shape newOptions
  160. if (newOptions.color !== undefined && newOptions.color !== null) {
  161. let parsedColor = util.parseColor(newOptions.color);
  162. util.fillIfDefined(parentOptions.color, parsedColor);
  163. }
  164. else if (allowDeletion === true && newOptions.color === null) {
  165. parentOptions.color = undefined;
  166. delete parentOptions.color;
  167. }
  168. // handle the fixed options
  169. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  170. if (typeof newOptions.fixed === 'boolean') {
  171. parentOptions.fixed.x = newOptions.fixed;
  172. parentOptions.fixed.y = newOptions.fixed;
  173. }
  174. else {
  175. if (newOptions.fixed.x !== undefined && typeof newOptions.fixed.x === 'boolean') {
  176. parentOptions.fixed.x = newOptions.fixed.x;
  177. }
  178. if (newOptions.fixed.y !== undefined && typeof newOptions.fixed.y === 'boolean') {
  179. parentOptions.fixed.y = newOptions.fixed.y;
  180. }
  181. }
  182. }
  183. // handle the font options
  184. if (newOptions.font !== undefined) {
  185. Label.parseOptions(parentOptions.font, newOptions);
  186. }
  187. // handle the scaling options, specifically the label part
  188. if (newOptions.scaling !== undefined) {
  189. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label');
  190. }
  191. }
  192. updateLabelModule() {
  193. if (this.options.label === undefined || this.options.label === null) {
  194. this.options.label = '';
  195. }
  196. this.labelModule.setOptions(this.options, true);
  197. if (this.labelModule.baseSize !== undefined) {
  198. this.baseFontSize = this.labelModule.baseSize;
  199. }
  200. }
  201. updateShape() {
  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. 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. }
  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;