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.

572 lines
18 KiB

9 years ago
9 years ago
9 years ago
  1. var util = require('../../../util');
  2. var Label = require('./shared/Label').default;
  3. var ComponentUtil = require('./shared/ComponentUtil').default;
  4. var Box = require('./nodes/shapes/Box').default;
  5. var Circle = require('./nodes/shapes/Circle').default;
  6. var CircularImage = require('./nodes/shapes/CircularImage').default;
  7. var Database = require('./nodes/shapes/Database').default;
  8. var Diamond = require('./nodes/shapes/Diamond').default;
  9. var Dot = require('./nodes/shapes/Dot').default;
  10. var Ellipse = require('./nodes/shapes/Ellipse').default;
  11. var Icon = require('./nodes/shapes/Icon').default;
  12. var Image = require('./nodes/shapes/Image').default;
  13. var Square = require('./nodes/shapes/Square').default;
  14. var Star = require('./nodes/shapes/Star').default;
  15. var Text = require('./nodes/shapes/Text').default;
  16. var Triangle = require('./nodes/shapes/Triangle').default;
  17. var TriangleDown = require('./nodes/shapes/TriangleDown').default;
  18. var { printStyle } = require("../../../shared/Validator");
  19. /**
  20. * A node. A node can be connected to other nodes via one or multiple edges.
  21. */
  22. class Node {
  23. /**
  24. *
  25. * @param {object} options An object containing options for the node. All
  26. * options are optional, except for the id.
  27. * {number} id Id of the node. Required
  28. * {string} label Text label for the node
  29. * {number} x Horizontal position of the node
  30. * {number} y Vertical position of the node
  31. * {string} shape Node shape, available:
  32. * "database", "circle", "ellipse",
  33. * "box", "image", "text", "dot",
  34. * "star", "triangle", "triangleDown",
  35. * "square", "icon"
  36. * {string} image An image url
  37. * {string} title An title text, can be HTML
  38. * {anytype} group A group name or number
  39. * @param {Object} body
  40. * @param {Network.Images} imagelist A list with images. Only needed
  41. * when the node has an image
  42. * @param {Groups} grouplist A list with groups. Needed for
  43. * retrieving group options
  44. * @param {Object} globalOptions An object with default values for
  45. * example for the color
  46. * @param {Object} defaultOptions
  47. * @param {Object} nodeOptions
  48. */
  49. constructor(options, body, imagelist, grouplist, globalOptions, defaultOptions, nodeOptions) {
  50. this.options = util.bridgeObject(globalOptions);
  51. this.globalOptions = globalOptions;
  52. this.defaultOptions = defaultOptions;
  53. this.nodeOptions = nodeOptions;
  54. this.body = body;
  55. this.edges = []; // all edges connected to this node
  56. // set defaults for the options
  57. this.id = undefined;
  58. this.imagelist = imagelist;
  59. this.grouplist = grouplist;
  60. // state options
  61. this.x = undefined;
  62. this.y = undefined;
  63. this.baseSize = this.options.size;
  64. this.baseFontSize = this.options.font.size;
  65. this.predefinedPosition = false; // used to check if initial fit should just take the range or approximate
  66. this.selected = false;
  67. this.hover = false;
  68. this.labelModule = new Label(this.body, this.options, false /* Not edge label */);
  69. this.setOptions(options);
  70. }
  71. /**
  72. * Attach a edge to the node
  73. * @param {Edge} edge
  74. */
  75. attachEdge(edge) {
  76. if (this.edges.indexOf(edge) === -1) {
  77. this.edges.push(edge);
  78. }
  79. }
  80. /**
  81. * Detach a edge from the node
  82. *
  83. * @param {Edge} edge
  84. */
  85. detachEdge(edge) {
  86. var index = this.edges.indexOf(edge);
  87. if (index != -1) {
  88. this.edges.splice(index, 1);
  89. }
  90. }
  91. /**
  92. * Set or overwrite options for the node
  93. *
  94. * @param {Object} options an object with options
  95. * @returns {null|boolean}
  96. */
  97. setOptions(options) {
  98. let currentShape = this.options.shape;
  99. if (!options) {
  100. return; // Note that the return value will be 'undefined'! This is OK.
  101. }
  102. // basic options
  103. if (options.id !== undefined) {this.id = options.id;}
  104. if (this.id === undefined) {
  105. throw "Node must have an id";
  106. }
  107. Node.checkMass(options, this.id);
  108. // set these options locally
  109. // clear x and y positions
  110. if (options.x !== undefined) {
  111. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  112. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  113. }
  114. if (options.y !== undefined) {
  115. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  116. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  117. }
  118. if (options.size !== undefined) {this.baseSize = options.size;}
  119. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  120. // copy group options
  121. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  122. var groupObj = this.grouplist.get(options.group);
  123. util.deepExtend(this.options, groupObj);
  124. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  125. this.options.color = util.parseColor(this.options.color);
  126. }
  127. // this transforms all shorthands into fully defined options
  128. Node.parseOptions(this.options, options, true, this.globalOptions);
  129. let pile = [options, this.options, this.defaultOptions];
  130. this.chooser = ComponentUtil.choosify('node', pile);
  131. this._load_images();
  132. this.updateLabelModule(options);
  133. this.updateShape(currentShape);
  134. this.labelModule.propagateFonts(this.nodeOptions, options, this.defaultOptions);
  135. return (options.hidden !== undefined || options.physics !== undefined);
  136. }
  137. /**
  138. * Load the images from the options, for the nodes that need them.
  139. *
  140. * TODO: The imageObj members should be moved to CircularImageBase.
  141. * It's the only place where they are required.
  142. *
  143. * @private
  144. */
  145. _load_images() {
  146. // Don't bother loading for nodes without images
  147. if (this.options.shape !== 'circularImage' && this.options.shape !== 'image') {
  148. return;
  149. }
  150. if (this.options.image === undefined) {
  151. throw "Option image must be defined for node type '" + this.options.shape + "'";
  152. }
  153. if (this.imagelist === undefined) {
  154. throw "Internal Error: No images provided";
  155. }
  156. if (typeof this.options.image === 'string') {
  157. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  158. } else {
  159. if (this.options.image.unselected === undefined) {
  160. throw "No unselected image provided";
  161. }
  162. this.imageObj = this.imagelist.load(this.options.image.unselected, this.options.brokenImage, this.id);
  163. if (this.options.image.selected !== undefined) {
  164. this.imageObjAlt = this.imagelist.load(this.options.image.selected, this.options.brokenImage, this.id);
  165. } else {
  166. this.imageObjAlt = undefined;
  167. }
  168. }
  169. }
  170. /**
  171. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  172. * Static so it can also be used by the handler.
  173. *
  174. * @param {Object} parentOptions
  175. * @param {Object} newOptions
  176. * @param {boolean} [allowDeletion=false]
  177. * @param {Object} [globalOptions={}]
  178. */
  179. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  180. var fields = [
  181. 'color',
  182. 'font',
  183. 'fixed',
  184. 'shadow'
  185. ];
  186. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  187. Node.checkMass(newOptions);
  188. // merge the shadow options into the parent.
  189. util.mergeOptions(parentOptions, newOptions, 'shadow', globalOptions);
  190. // individual shape newOptions
  191. if (newOptions.color !== undefined && newOptions.color !== null) {
  192. let parsedColor = util.parseColor(newOptions.color);
  193. util.fillIfDefined(parentOptions.color, parsedColor);
  194. }
  195. else if (allowDeletion === true && newOptions.color === null) {
  196. parentOptions.color = util.bridgeObject(globalOptions.color); // set the object back to the global options
  197. }
  198. // handle the fixed options
  199. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  200. if (typeof newOptions.fixed === 'boolean') {
  201. parentOptions.fixed.x = newOptions.fixed;
  202. parentOptions.fixed.y = newOptions.fixed;
  203. }
  204. else {
  205. if (newOptions.fixed.x !== undefined && typeof newOptions.fixed.x === 'boolean') {
  206. parentOptions.fixed.x = newOptions.fixed.x;
  207. }
  208. if (newOptions.fixed.y !== undefined && typeof newOptions.fixed.y === 'boolean') {
  209. parentOptions.fixed.y = newOptions.fixed.y;
  210. }
  211. }
  212. }
  213. // handle the font options
  214. if (newOptions.font !== undefined && newOptions.font !== null) {
  215. Label.parseOptions(parentOptions.font, newOptions);
  216. }
  217. else if (allowDeletion === true && newOptions.font === null) {
  218. parentOptions.font = util.bridgeObject(globalOptions.font); // set the object back to the global options
  219. }
  220. // handle the scaling options, specifically the label part
  221. if (newOptions.scaling !== undefined) {
  222. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label', globalOptions.scaling);
  223. }
  224. }
  225. /**
  226. *
  227. * @returns {{color: *, borderWidth: *, borderColor: *, size: *, borderDashes: (boolean|Array|allOptions.nodes.shapeProperties.borderDashes|{boolean, array}), borderRadius: (number|allOptions.nodes.shapeProperties.borderRadius|{number}|Array), shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *}}
  228. */
  229. getFormattingValues() {
  230. let values = {
  231. color: this.options.color.background,
  232. borderWidth: this.options.borderWidth,
  233. borderColor: this.options.color.border,
  234. size: this.options.size,
  235. borderDashes: this.options.shapeProperties.borderDashes,
  236. borderRadius: this.options.shapeProperties.borderRadius,
  237. shadow: this.options.shadow.enabled,
  238. shadowColor: this.options.shadow.color,
  239. shadowSize: this.options.shadow.size,
  240. shadowX: this.options.shadow.x,
  241. shadowY: this.options.shadow.y
  242. };
  243. if (this.selected || this.hover) {
  244. if (this.chooser === true) {
  245. if (this.selected) {
  246. values.borderWidth *= 2;
  247. values.color = this.options.color.highlight.background;
  248. values.borderColor = this.options.color.highlight.border;
  249. values.shadow = this.options.shadow.enabled;
  250. } else if (this.hover) {
  251. values.color = this.options.color.hover.background;
  252. values.borderColor = this.options.color.hover.border;
  253. values.shadow = this.options.shadow.enabled;
  254. }
  255. } else if (typeof this.chooser === 'function') {
  256. this.chooser(values, this.options.id, this.selected, this.hover);
  257. if (values.shadow === false) {
  258. if ((values.shadowColor !== this.options.shadow.color) ||
  259. (values.shadowSize !== this.options.shadow.size) ||
  260. (values.shadowX !== this.options.shadow.x) ||
  261. (values.shadowY !== this.options.shadow.y)) {
  262. values.shadow = true;
  263. }
  264. }
  265. }
  266. } else {
  267. values.shadow = this.options.shadow.enabled;
  268. }
  269. return values;
  270. }
  271. /**
  272. *
  273. * @param {Object} options
  274. */
  275. updateLabelModule(options) {
  276. if (this.options.label === undefined || this.options.label === null) {
  277. this.options.label = '';
  278. }
  279. let pile = [options, this.nodeOptions, this.defaultOptions];
  280. this.labelModule.update(this.options, pile);
  281. if (this.labelModule.baseSize !== undefined) {
  282. this.baseFontSize = this.labelModule.baseSize;
  283. }
  284. }
  285. /**
  286. *
  287. * @param {string} currentShape
  288. */
  289. updateShape(currentShape) {
  290. if (currentShape === this.options.shape && this.shape) {
  291. this.shape.setOptions(this.options, this.imageObj, this.imageObjAlt);
  292. }
  293. else {
  294. // choose draw method depending on the shape
  295. switch (this.options.shape) {
  296. case 'box':
  297. this.shape = new Box(this.options, this.body, this.labelModule);
  298. break;
  299. case 'circle':
  300. this.shape = new Circle(this.options, this.body, this.labelModule);
  301. break;
  302. case 'circularImage':
  303. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
  304. break;
  305. case 'database':
  306. this.shape = new Database(this.options, this.body, this.labelModule);
  307. break;
  308. case 'diamond':
  309. this.shape = new Diamond(this.options, this.body, this.labelModule);
  310. break;
  311. case 'dot':
  312. this.shape = new Dot(this.options, this.body, this.labelModule);
  313. break;
  314. case 'ellipse':
  315. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  316. break;
  317. case 'icon':
  318. this.shape = new Icon(this.options, this.body, this.labelModule);
  319. break;
  320. case 'image':
  321. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
  322. break;
  323. case 'square':
  324. this.shape = new Square(this.options, this.body, this.labelModule);
  325. break;
  326. case 'star':
  327. this.shape = new Star(this.options, this.body, this.labelModule);
  328. break;
  329. case 'text':
  330. this.shape = new Text(this.options, this.body, this.labelModule);
  331. break;
  332. case 'triangle':
  333. this.shape = new Triangle(this.options, this.body, this.labelModule);
  334. break;
  335. case 'triangleDown':
  336. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  337. break;
  338. default:
  339. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  340. break;
  341. }
  342. }
  343. this.needsRefresh();
  344. }
  345. /**
  346. * select this node
  347. */
  348. select() {
  349. this.selected = true;
  350. this.needsRefresh();
  351. }
  352. /**
  353. * unselect this node
  354. */
  355. unselect() {
  356. this.selected = false;
  357. this.needsRefresh();
  358. }
  359. /**
  360. * Reset the calculated size of the node, forces it to recalculate its size
  361. */
  362. needsRefresh() {
  363. this.shape.refreshNeeded = true;
  364. }
  365. /**
  366. * get the title of this node.
  367. * @return {string} title The title of the node, or undefined when no title
  368. * has been set.
  369. */
  370. getTitle() {
  371. return this.options.title;
  372. }
  373. /**
  374. * Calculate the distance to the border of the Node
  375. * @param {CanvasRenderingContext2D} ctx
  376. * @param {Number} angle Angle in radians
  377. * @returns {number} distance Distance to the border in pixels
  378. */
  379. distanceToBorder(ctx, angle) {
  380. return this.shape.distanceToBorder(ctx,angle);
  381. }
  382. /**
  383. * Check if this node has a fixed x and y position
  384. * @return {boolean} true if fixed, false if not
  385. */
  386. isFixed() {
  387. return (this.options.fixed.x && this.options.fixed.y);
  388. }
  389. /**
  390. * check if this node is selecte
  391. * @return {boolean} selected True if node is selected, else false
  392. */
  393. isSelected() {
  394. return this.selected;
  395. }
  396. /**
  397. * Retrieve the value of the node. Can be undefined
  398. * @return {Number} value
  399. */
  400. getValue() {
  401. return this.options.value;
  402. }
  403. /**
  404. * Adjust the value range of the node. The node will adjust it's size
  405. * based on its value.
  406. * @param {Number} min
  407. * @param {Number} max
  408. * @param {Number} total
  409. */
  410. setValueRange(min, max, total) {
  411. if (this.options.value !== undefined) {
  412. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  413. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  414. if (this.options.scaling.label.enabled === true) {
  415. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  416. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  417. }
  418. this.options.size = this.options.scaling.min + scale * sizeDiff;
  419. }
  420. else {
  421. this.options.size = this.baseSize;
  422. this.options.font.size = this.baseFontSize;
  423. }
  424. this.updateLabelModule();
  425. }
  426. /**
  427. * Draw this node in the given canvas
  428. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  429. * @param {CanvasRenderingContext2D} ctx
  430. */
  431. draw(ctx) {
  432. let values = this.getFormattingValues();
  433. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover, values);
  434. }
  435. /**
  436. * Update the bounding box of the shape
  437. * @param {CanvasRenderingContext2D} ctx
  438. */
  439. updateBoundingBox(ctx) {
  440. this.shape.updateBoundingBox(this.x,this.y,ctx);
  441. }
  442. /**
  443. * Recalculate the size of this node in the given canvas
  444. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  445. * @param {CanvasRenderingContext2D} ctx
  446. */
  447. resize(ctx) {
  448. let values = this.getFormattingValues();
  449. this.shape.resize(ctx, this.selected, this.hover, values);
  450. }
  451. /**
  452. * Check if this object is overlapping with the provided object
  453. * @param {Object} obj an object with parameters left, top, right, bottom
  454. * @return {boolean} True if location is located on node
  455. */
  456. isOverlappingWith(obj) {
  457. return (
  458. this.shape.left < obj.right &&
  459. this.shape.left + this.shape.width > obj.left &&
  460. this.shape.top < obj.bottom &&
  461. this.shape.top + this.shape.height > obj.top
  462. );
  463. }
  464. /**
  465. * Check if this object is overlapping with the provided object
  466. * @param {Object} obj an object with parameters left, top, right, bottom
  467. * @return {boolean} True if location is located on node
  468. */
  469. isBoundingBoxOverlappingWith(obj) {
  470. return (
  471. this.shape.boundingBox.left < obj.right &&
  472. this.shape.boundingBox.right > obj.left &&
  473. this.shape.boundingBox.top < obj.bottom &&
  474. this.shape.boundingBox.bottom > obj.top
  475. );
  476. }
  477. /**
  478. * Check valid values for mass
  479. *
  480. * The mass may not be negative or zero. If it is, reset to 1
  481. *
  482. * @param {object} options
  483. * @param {vis.Node.id} id
  484. */
  485. static checkMass(options, id) {
  486. if (options.mass !== undefined && options.mass <= 0) {
  487. let strId = '';
  488. if (id !== undefined) {
  489. strId = ' in node id: ' + id;
  490. }
  491. console.log('%cNegative or zero mass disallowed' + strId +
  492. ', setting mass to 1.' , printStyle);
  493. options.mass = 1;
  494. }
  495. }
  496. }
  497. export default Node;