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.

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