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.

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